Skip to content

Instantly share code, notes, and snippets.

@bstro
Last active January 15, 2019 03:24
Show Gist options
  • Select an option

  • Save bstro/2436e7d3214e4f22544f872a56ad0512 to your computer and use it in GitHub Desktop.

Select an option

Save bstro/2436e7d3214e4f22544f872a56ad0512 to your computer and use it in GitHub Desktop.

Revisions

  1. Brendan Stromberger revised this gist Jan 15, 2019. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions parse.elm
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,9 @@
    -- 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 ))
  2. Brendan Stromberger revised this gist Jan 15, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion parse.elm
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    -- 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 value with XX. (see Index type below)
    -- Tag the resulting Int and List Int with XX. (see Index type below)

    type Index
    = XXXXXX (Parser.Parser Int)
  3. Brendan Stromberger created this gist Jan 15, 2019.
    45 changes: 45 additions & 0 deletions parse.elm
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    -- 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 value with XX. (see Index type below)

    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"
    )
    ]