An UU parser recognizing just the empty string input?

93 Views Asked by At

I need a value of type Parser () which would succeed (and return ()) on empty (lenght 0) input, and fail in all other cases.

pSatisfy (const False) doesn't quite do what's required. pEnd doesn't even seem appropriate for this purpose.


pExact 0 pAscii might be the exact "by-definition" solution. Still doesn't seem to work:

ghci> runParser "<input>" (pSymbol "aaa" <|> pSymbol "bbb" <|> pExact 0 pAscii) ""
*** Exception: ambiguous parser?
1

There are 1 best solutions below

0
On

It seems that the idea of uu-parsinglib it to be more declarative than e.g. parsec, so you just have pure ():

λ> runParser "<input>" (pSymbol "aaa" <|> pSymbol "bbb" <|> pure "") "aaa"
"aaa"
λ> runParser "<input>" (pSymbol "aaa" <|> pSymbol "bbb" <|> pure "") "bbb"
"bbb"
λ> runParser "<input>" (pSymbol "aaa" <|> pSymbol "bbb" <|> pure "") ""
""
λ> runParser "<input>" (pSymbol "aaa" <|> pSymbol "bbb" <|> pure "") "ccc"
"*** Exception: Failed parsing '<input>' :
Unexpected ''c'' at end.

And you need to structure your grammar in the way it doesn't need magical EOF symbol.