检测 FParsec 何时未解析所有输入
Detect when FParsec has not parsed all the input
您如何检测 FParsec 解析器何时停止而不解析所有输入?
例如,下面的解析器p
在发现意外字符d
时停止,不再继续解析输入的剩余部分。
let test p str =
match run p str with
| Success(result, _, _) -> printfn "Success: %A" result
| Failure(errorMsg, s, _) -> printfn "Failure: %s %A" errorMsg s
let str s = pstring s
let a = str "a" .>> spaces
let b = str "b" .>> spaces
let p = many (a <|> b)
test p "ab a d bba " // Success: ["a"; "b"; "a"]
有一个特殊的解析器eof
对应正则表达式中的$
。
试试这个:
let p = many (a <|> b) .>> eof
这确保解析器仅在输入完全消耗时才会成功。
您如何检测 FParsec 解析器何时停止而不解析所有输入?
例如,下面的解析器p
在发现意外字符d
时停止,不再继续解析输入的剩余部分。
let test p str =
match run p str with
| Success(result, _, _) -> printfn "Success: %A" result
| Failure(errorMsg, s, _) -> printfn "Failure: %s %A" errorMsg s
let str s = pstring s
let a = str "a" .>> spaces
let b = str "b" .>> spaces
let p = many (a <|> b)
test p "ab a d bba " // Success: ["a"; "b"; "a"]
有一个特殊的解析器eof
对应正则表达式中的$
。
试试这个:
let p = many (a <|> b) .>> eof
这确保解析器仅在输入完全消耗时才会成功。