How to parse comments using FParsec

I am trying to parse lisp comments from s-expression using FParsec. It helped me a little to parse the single-line comments in this previous thread - How to convert the FParsec parser to parse spaces

While this was allowed, I still need to parse multi-line comments. Here the current code is

/// Read whitespace character as a string. let spaceAsStr = anyOf whitespaceChars |>> fun chr -> string chr /// Read a line comment. let lineComment = pchar lineCommentChar >>. restOfLine true /// Read a multiline comment. /// TODO: make multiline comments nest. let multilineComment = between (pstring openMultilineCommentStr) (pstring closeMultilineCommentStr) (charsTillString closeMultilineCommentStr true System.Int32.MaxValue) /// Read whitespace text. let whitespace = lineComment <|> multilineComment <|> spaceAsStr /// Skip any white space characters. let skipWhitespace = skipMany whitespace /// Skip at least one white space character. let skipWhitespace1 = skipMany1 whitespace 

Unfortunately, multi-user analysis never succeeds. Since this is a combinator, I cannot set breakpoints or analyze why it will not work.

Any ideas why this won't work?

+6
source share
1 answer

Try changing the bool argument for closeMultilineCommentStr to false

 (charsTillString closeMultilineCommentStr false System.Int32.MaxValue) 

Otherwise, it will skip the closeMultilineCommentStr line.

So that it works with nested comments

 let rec multilineComment o= let ign x = charsTillString x false System.Int32.MaxValue between (pstring openMultilineCommentStr) (pstring closeMultilineCommentStr) (attempt (ign openMultilineCommentStr >>. multilineComment >>. ign closeMultilineCommentStr) <|> ign closeMultilineCommentStr) <|o 
+4
source

Source: https://habr.com/ru/post/903221/


All Articles