Search code examples
parsingf#fparsec

Parsing string literals with FParsec?


I would like to parse string literals using FParsec. By "string literals" I mean something between opening and closing quote (in my case -- single quote):

'Please, switch off your mobile phone'

What I am currently doing is the following:

let string = between (pstring "'") (pstring "'") (manySatisfy isLetter)

But this stops after the first letter consumed. Is there any way to make it greedy?


Solution

  • It's already greedy; manySatisfy isLetter parses a sequence of letters from the input stream.

    The problem is that the parser fails with , or spaces since they are not letters. It could be fixed by using:

    manyChars (noneOf "'")
    

    or more explicitly using:

    manySatisfy ((<>) '\'')
    

    instead.