1

假设我正在定义以下解析器:

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

解析时,会发生错误,正如人们所期望的那样:

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

我很好奇为什么它不能确定问题是他在期待一封信却找不到。我是否希望自己以某种方式将该信息添加到解析器中?

4

2 回答 2

2

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"
于 2011-08-27T07:57:22.997 回答
0

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

于 2011-08-27T07:51:04.360 回答