I would write
integer :: Parser Integer integer = read <$ many1 space <*> many1 digit
There is a bunch of legal associative operators (for example, applications) for parsers <$> , <*> , <$ , <* . The thing in the far left corner should be a pure function that collects the value of the result from the values โโof the component. The thing to the right of each operator must be a parser that collectively provides grammar components from left to right. Which operator to use depends on two options:
the thing to the right is signal / noise _________________________ the thing to the left is \ +------------------- pure / | <$> <$ a parser | <*> <*
So, by choosing read :: String -> Integer as a pure function that will deliver analyzer semantics, we can classify the leading space as โnoiseโ and the group of digits as โsignalโ, therefore
read <$ many1 space <*> many1 digit (..) (.........) (.........) pure noise parser | (.................) | parser signal parser (.................................) parser
You can combine several features with
p1 <|> ... <|> pn
and express impossibility with
empty
It is rarely necessary to name components in parsers, and the resulting code is more like a grammar with added semantics.
pigworker Feb 27 '13 at 23:05 2013-02-27 23:05
source share