Sean Kearon
Sean Kearon

Reputation: 11427

Detect when FParsec has not parsed all the input

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"]

Upvotes: 8

Views: 99

Answers (1)

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.

Upvotes: 7

Related Questions