Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when FParsec has not parsed all the input

Tags:

f#

fparsec

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"]
like image 564
Sean Kearon Avatar asked May 27 '18 14:05

Sean Kearon


1 Answers

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.

like image 126
Just another metaprogrammer Avatar answered Oct 16 '22 16:10

Just another metaprogrammer