Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to resolve FParsec error "The combinator 'many' was applied to a parser that succeeds without consuming..."
    primarykey
    data
    text
    <p>I have a parser that seems straight-forward enough. I added this sub-parser to the end to give info about general parsing errors since all the other sub-parsers failed -</p> <pre><code>/// Read the rest of a line as an error. let readError = parse { let! restOfLineStr = restOfLine true return makeViolation ("Read error on: " + restOfLineStr + ".") } /// Read an expression. do readExprRef := choice [attempt readBoolean attempt readCharacter attempt readString attempt readInt attempt readError] // just now added this sub-parser, and get the issue </code></pre> <p>However, once I add readError as a choice, I get the dreaded FParsec error about stream consumption at run-time - <code>The combinator 'many' was applied to a parser that succeeds without consuming input and without changing the parser state in any other way.</code> I don't understand why I get this since I do use the parsed rest of the line to create a used error (here 'violation') structure.</p> <p>Can someone help me understand this? Am I going about signaling parser errors to the user in the wrong way? If not, how could I fix this?</p> <p>Thank you kindly for your help!</p> <p><strong>* More detail *</strong></p> <p>Here's some more code that may be relevant -</p> <pre><code>/// The expression structure. type Expr = | Violation of Expr | Boolean of bool | Character of char | String of string | Int of int /// Make a violation from a string. let makeViolation str = Violation (String str) /// Read whitespace character as a string. let spaceAsStr = anyOf whitespaceChars |&gt;&gt; fun chr -&gt; string chr /// Read a line comment. let lineComment = pchar lineCommentChar &gt;&gt;. restOfLine true /// Read a multiline comment. /// TODO: make multiline comments nest. let multilineComment = between (pstring openMultilineCommentStr) (pstring closeMultilineCommentStr) (charsTillString closeMultilineCommentStr false System.Int32.MaxValue) /// Read whitespace text. let whitespace = lineComment &lt;|&gt; multilineComment &lt;|&gt; spaceAsStr /// Skip any white space characters. let skipWhitespace = skipMany whitespace /// Skip at least one white space character. let skipWhitespace1 = skipMany1 whitespace /// Read a boolean. let readBoolean = parse { do! skipWhitespace let! booleanValue = readStr trueStr &lt;|&gt; readStr falseStr return Boolean (booleanValue = trueStr) } /// Read a character. let readCharacter = parse { // TODO: enable reading of escaped chars do! skipWhitespace let! chr = between skipSingleQuote skipSingleQuote (manyChars (noneOf "\'")) return Character chr.[0] } /// Read a string. let readString = parse { // TODO: enable reading of escaped chars do! skipWhitespace let! str = between skipDoubleQuote skipDoubleQuote (manyChars (noneOf "\"")) return String str } /// Read an int. let readInt = parse { do! skipWhitespace let! value = pint32 let! _ = opt (skipString intSuffixStr) do! notFollowedByLetterOrNameChar do! notFollowedByDot return Int value } </code></pre> <p>I dunno. Maybe the issue is that it's already at the end of the stream once it tries to run the readError parser. Would that make restOfLine consume no input, not even whitespace?</p> <p><strong>* Conclusion *</strong></p> <p>It turns out that the approach to error reporting with a readError parser is wrong. The correct approach is to use a 'till end' parser like so -</p> <pre><code>/// Read the end of input. let readEndOfInput = skipWhitespace &gt;&gt;. eof // Read multiple exprs. let readExprs = many readExpr // Read exprs until the end of the input. let readExprsTillEnd = readExprs .&gt;&gt; readEndOfInput </code></pre> <p>Now I just run readExprsTillEnd when I need to get all the exprs in an input stream.</p> <p>Thanks again, Gustavo!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload