How can you detect when an FParsec parser has stopped without parsing all the input?
For example, the following parser p
stops when it finds the unexpected character d
and does not continue to parse the remainder of the input.
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"]
There is a special parser eof
that corresponds to $
in regex.
Try this:
let p = many (a <|> b) .>> eof
This ensures that the parser only succeeds if the input was fully consumed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With