3

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?
ulidtko
  • 14,740
  • 10
  • 56
  • 88

1 Answers1

1

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.

phadej
  • 11,947
  • 41
  • 78