| module Thing exposing (Thing(..), thing) | |
| import List | |
| import Parser as P exposing ((|.), (|=), Parser) | |
| {- | |
| lookAhead : Parser a -> Parser a | |
| lookAhead (Parser parse) = | |
| Parser <| | |
| \state -> | |
| case parse state of | |
| Bad x _ -> | |
| Bad x state | |
| Good a _ -> | |
| Good a state | |
| -} | |
| {- | |
| Some sample inputs: | |
| (12 (34)) | |
| (()) | |
| (()44) | |
| (()()123 (456)) | |
| ( 11 22 33 ) | |
| ( 11 ( 22 )( 33) ) | |
| (1()2) | |
| -} | |
| type Thing | |
| = Number Int | |
| | Things (List Thing) | |
| thing : Parser Thing | |
| thing = | |
| P.oneOf | |
| [ number | |
| , P.lazy (\_ -> things) | |
| ] | |
| number : Parser Thing | |
| number = | |
| P.succeed Number | |
| |= P.int | |
| |. sep | |
| things : Parser Thing | |
| things = | |
| let | |
| spaceOrThing = | |
| P.oneOf | |
| [ P.succeed Nothing |. space | |
| , P.succeed Just |= P.lazy (\_ -> thing) | |
| ] | |
| in | |
| P.succeed (Things << List.filterMap identity) | |
| |. P.symbol "(" | |
| |= P.repeat P.zeroOrMore spaceOrThing | |
| |. P.symbol ")" | |
| sep : Parser () | |
| sep = | |
| P.oneOf | |
| [ space | |
| , P.end | |
| , P.lookAhead <| | |
| P.oneOf | |
| [ P.symbol "(" | |
| , P.symbol ")" | |
| ] | |
| ] | |
| space : Parser () | |
| space = | |
| P.ignore P.oneOrMore (\c -> c == ' ') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment