Javascript 如何解析pegjs中的嵌套注释?

Javascript 如何解析pegjs中的嵌套注释?,javascript,parsing,grammar,peg,pegjs,Javascript,Parsing,Grammar,Peg,Pegjs,我想知道如何在pegjs中解析注释(比如,lahaskell) 目标是: {- This is a comment and should parse. Comments start with {- and end with -}. If you've noticed, I still included {- and -} in the comment. This means that comments should also nest {- even {-

我想知道如何在pegjs中解析注释(比如,lahaskell)

目标是:

{-
    This is a comment and should parse.
    Comments start with {- and end with -}.
    If you've noticed, I still included {- and -} in the comment.
    This means that comments should also nest
    {- even {- to -} arbitrary -} levels
    But they should be balanced
-}
例如,不应分析以下内容:

{- I am an unbalanced -} comment -}
但你也应该有一个逃生机制:

{- I can escape comment \{- characters like this \-} -}
这一分类看起来像是解析s表达式,但使用s表达式很容易:

sExpression = "(" [^)]* ")"
因为亲密的帕伦斯只是一个角色,而我不能用胡萝卜来形容它。顺便说一句,我想知道为什么一个人不能在pegjs中“不”一个比一个字符长的东西


感谢您的帮助。

这不能处理您的转义机制,但应该可以让您开始(这里有一个链接,可以实时查看它:;只需单击屏幕顶部的
buildparser
Parse

start = comment

comment = COMSTART (not_com/comment)* COMSTOP

not_com = (!COMSTOP !COMSTART.)

COMSTART = '{-'

COMSTOP = '-}'
要回答您的一般问题:

顺便说一句,我想知道为什么一个人不能在pegjs中“不”一个比一个字符长的东西


最简单的方法是
(!rulename.)
其中
rulename
是语法中定义的另一条规则。
!rulename
部分只是确保下一步扫描的内容与
rulename
不匹配,但您仍然必须为规则定义要匹配的内容,这就是为什么我将
包含在内的原因。这很有帮助。我已经看过了!运算符,但我认为它不起作用。我知道我理解我实际上必须包含一些东西才能对其进行解析。