Domanda

Let's say I am defining the following parser:

let identifier = many1Satisfy isLetter //match an identifier
let parser = identifier //our parser is only to match identifiers
test parser " abc" //the text to parse contains a leading space which should yield us an error

When parsing, an error ocurrs, as one would expect:

Failure: Error in Ln: 1 Col: 1
 abc
^
Unknown Error(s)

I am curious on why can't it decide the problem is that he's expecting a letter and can't find one. Am I expected to add that info somehow to the parser by myself?

È stato utile?

Soluzione

as to why it can't tell you whats wrong: I guess this is due to the "many1Satisfy" - you see this combinator wraps another parser and I guess it just don't know in which state of "many1" an error occured and not what error - so it says "Unknown Error(s)"

this should work:

let ws = spaces
let identifier = ws >>. (many1Satisfy isLetter) //match an identifier, ignore whitespaces infront
let parser = identifier //our parser is only to match identifiers
test parser " abc"

Altri suggerimenti

White space is not a regular character. In your case you need to ignore the white spaces and for that you need to compose your parser with the parser which ignores white spaces and use this new composed parser to parse the identifier.

Check 4.6 Handling whitespace

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top