开发者

FParsec default error messages

Let's say I am defining the following parser:

let identifier = many1Satisfy isLetter //match 开发者_如何学Goan 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?


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"


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜