我想知道您如何在 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 = "(" [^)]* ")"
因为闭括号只是一个字符,我不能用胡萝卜“不”它。顺便说一句,我想知道一个人怎么能“不”比 pegjs 中的单个字符长的东西。
感谢您的帮助。
最佳答案
这不会处理你的转义机制,但它应该让你开始(这里有一个链接可以看到它:pegedit ;只需点击 Build Parser
和 Parse
在屏幕顶部。
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
,但您仍然必须定义一些内容以使规则匹配,这就是我包含 的原因。
.
关于javascript - 你如何解析pegjs中的嵌套注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28419972/