parsing - In MegaParsec is there a way of anding 2 parsers? -
if have parser like:
notzeroorone :: parser char notzeroorone = noneof ['0' , '1']
is there way can combine rules of parser digitchar can parser pass if both parsers pass?
something like
biggerthanone :: parser char biggerthanone = digitchar && notzeroorone
as user2407038 suggests in comments, it's achievable using lookahead
function.
biggerthanone :: parser char biggerthanone = lookahead digitchar *> notzeroorone
however, parsers sequential nature, it's both more efficient , comprehendable apply sequential logic. e.g., using monad
instance of parser
:
biggerthanone :: parser char biggerthanone = c <- digitchar if c /= '0' && c /= '1' return c else unexpected "not bigger one"
or monadplus
:
biggerthanone :: parser char biggerthanone = mfilter (\c -> c /= '0' && c /= '1') digitchar
which can refactored use ord
instance of char
, pretty express intent:
biggerthanone :: parser char biggerthanone = mfilter (> '1') digitchar
Comments
Post a Comment