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

How do you parse nested comments in pegjs?

我想知道您如何在 pegjs 中解析评论(例如,la Haskell)。

目标:

{-
    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 = "(" [^)]* ")"

因为闭括号只是一个字符,我可以 "not" 它与胡萝卜。顺便说一句,我想知道如何 "not" 比 pegjs 中的单个字符长的东西。

感谢您的帮助。

这不会处理您的转义机制,但它应该让您开始(这里是 link 实时查看:pegedit;只需单击 Build ParserParse 在屏幕顶部。

start = comment

comment = COMSTART (not_com/comment)* COMSTOP

not_com = (!COMSTOP !COMSTART.)

COMSTART = '{-'

COMSTOP = '-}'

回答您的一般问题:

As an aside, I'm wondering how one can "not" something that is longer than a single character in pegjs.

简单的方法是 (!rulename .),其中 rulename 是语法中定义的另一个规则。 ! rulename 部分只是确保接下来扫描的任何内容都不匹配 rulename,但您仍然必须定义一些内容以使规则匹配,这就是为什么我包含了 ..