Detect when FParsec has not parsed all the input
Asked Answered
G

1

8

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"]
Gloxinia answered 27/5, 2018 at 14:34 Comment(0)
H
7

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.

Holbein answered 27/5, 2018 at 15:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.