Last active
January 15, 2019 03:24
-
-
Save bstro/2436e7d3214e4f22544f872a56ad0512 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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" | |
) | |
] |
Author
bstro
commented
Jan 15, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment