Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An UU parser recognizing just the empty string input?

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?
like image 617
ulidtko Avatar asked Feb 28 '26 15:02

ulidtko


1 Answers

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.

like image 85
phadej Avatar answered Mar 02 '26 04:03

phadej



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!