pegjs でどのようにコメントを解析するのか (たとえば、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 = "(" [^)]* ")"
閉じ括弧は1文字に過ぎず、ニンジンと一緒に「できない」からです。余談ですが、pegjsで1文字よりも長いものを「できない」のはどうしてだろうと思っています。
ご協力いただきありがとうございます。