Skip to content

Instantly share code, notes, and snippets.

@bstro
Last active January 15, 2019 03:24
Show Gist options
  • Save bstro/2436e7d3214e4f22544f872a56ad0512 to your computer and use it in GitHub Desktop.
Save bstro/2436e7d3214e4f22544f872a56ad0512 to your computer and use it in GitHub Desktop.
-- My goal here is to be able to parse a string like "678767".
-- However, this same parser needs to be able to handle a string like "18.1.2.4"
-- My strategy is this: I parse the input as an integer.
-- If the parser has reached the end of the string, tag the chomped value as XXXXXX (see Index type below)
-- If the parser instead reaches a `.`, continue and recursively capture every integer following a `.`
-- Tag the resulting Int and List Int with XX. (see Index type below)
-- Also, I'm not sure, but I think line 16 needs this type:
-- dotDigit : List String -> Parser.Parser (Step (List Int) (List Int))
type Index
= XXXXXX (Parser.Parser Int)
| XX (Parser.Parser ( Int, List Int ))
| Unknown String
dotDigit : List String -> Parser.Parser (Step (List String) (List String))
dotDigit nums =
let
checkNum numsSoFar num =
if String.length num > 0 then
Loop (num :: numsSoFar)
else
Done (List.reverse numsSoFar)
in
succeed (checkNum nums)
|. symbol "."
|= (getChompedString <| chompWhile Char.isDigit)
parseIndex =
succeed identity
|= Parser.int
|. Parser.oneOf
[ succeed XXXXXX
|. Parser.end
, succeed XX
|. Parser.andThen
(\value ->
if value >= 1 && value <= 64 then
Parser.loop [] dotDigit
|> Parser.map (\digits -> ( value, digits ))
else
Parser.problem "out of range"
)
]
@bstro
Copy link
Author

bstro commented Jan 15, 2019

screen shot 2019-01-14 at 10 18 34 pm

screen shot 2019-01-14 at 10 18 40 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment