davidcavazos/parser/6.1.1

NOTE: the latest version is 7.0.0

Renamed to lambda-phi/parser. It is published under that name, and new versions come out there.

Versions published as davidcavazos/parser still install under that name. 2 versions of 12 come from the new location (see their notes). 10 versions could not be recovered and cannot be installed.

Rerouted (cache, verified). The original is gone. This copy comes from a Lamdera package cache made before it went. The Lamdera package server serves a copy recovered from a Lamdera package cache from 2021-02-13.

What we checked: Rebuilt from the unpacked sources in a Lamdera package cache (5 copies agree), which Elm only writes after the published SHA-1 checked out; elm.json is byte-identical to package.elm-lang.org's; the exposed modules match docs.json; the zip is our own, so its SHA-1 is not the published one.

The Lamdera compiler uses this copy and prints a one-line notice saying so. elm itself fails to download this version.

Details · package.elm-lang.org's zip: https://github.com/davidcavazos/parser/zipball/6.1.1/

Parser.Error

Error reporting utilities.

Error reporting

message : Error -> String

Creates an error message from an Error data.

import Parser exposing (parse)
import Parser.Char exposing (letter)


-- Getting a digit instead of a letter.
parse "123" letter
    |> Result.mapError message
--> Err "1:1: I was expecting a letter [a-zA-Z]. I got stuck when I got the character '1'."


-- Running out of input characters.
parse "" letter
    |> Result.mapError message
--> Err "1:0: I was expecting a letter [a-zA-Z]. I reached the end of the input text."
dump : String -> Error -> List String

Dumps the error into a human-readable format.

import Parser exposing (Parser, andThenKeep, drop, into, parse, succeed, take)
import Parser.Char exposing (char)
import Parser.Common exposing (number, spaces)


spaces
    |> andThenKeep number
    |> parse "  abc  "
    |> Result.mapError (dump "filename.txt")
--> Err
-->     [ "[ERROR] filename.txt:1:3: I was expecting a digit [0-9]. I got stuck when I got the character 'a'."
-->     , ""
-->     , "1|  abc  "
-->     , "    ^"
-->     ]




type alias Point =
    { x : Float
    , y : Float
    }


point : Parser Point
point =
    into "Point"
        (succeed Point
            |> drop (char '(')
            |> take number
            |> drop (char ',')
            |> take number
            |> drop (char ')')
        )


spaces
    |> andThenKeep point
    |> parse "  (12,)  "
    |> Result.mapError (dump "filename.txt")
--> Err
-->     [ "[ERROR] filename.txt:1:7: I was expecting a digit [0-9]. I got stuck when I got the character ')'."
-->     , "  in Point at line 1:3"
-->     , ""
-->     , "1|  (12,)  "
-->     , "    ~~~~^"
-->     ]


type alias Line =
    { p1 : Point
    , p2 : Point
    }


line : Parser Line
line =
    into "Line"
        (succeed Line
            |> drop (char '[')
            |> take point
            |> drop (char ',')
            |> take point
            |> drop (char ']')
        )


spaces
    |> andThenKeep line
    |> parse "  [(12,34),(56,)]  "
    |> Result.mapError (dump "filename.txt")
--> Err
-->     [ "[ERROR] filename.txt:1:16: I was expecting a digit [0-9]. I got stuck when I got the character ')'."
-->     , "  in Point at line 1:12"
-->     , "  in Line at line 1:3"
-->     , ""
-->     , "1|  [(12,34),(56,)]  "
-->     , "             ~~~~^"
-->     ]
dumpCodeSnippet : Error -> List String

Dumps a snippet of the input text that caused the parser to fail.

import Parser exposing (Parser, andThenKeep, drop, into, parse, succeed, take)
import Parser.Char exposing (char)
import Parser.Common exposing (number, spaces)


type alias Point =
    { x : Float
    , y : Float
    }


point : Parser Point
point =
    into "Point"
        (succeed Point
            |> drop (char '(')
            |> drop spaces
            |> take number
            |> drop spaces
            |> drop (char ',')
            |> drop spaces
            |> take number
            |> drop spaces
            |> drop (char ')')
        )


spaces
    |> andThenKeep point
    |> parse "  (12,)  "
    |> Result.mapError dumpCodeSnippet
--> Err
-->     [ "1|  (12,)  "
-->     , "    ~~~~^"
-->     ]


spaces
    |> andThenKeep point
    |> parse
        (String.join "\n"
            [ "  "
            , "  (  "
            , "  12  "
            , "  ,  "
            , "  )  "
            , "  "
            ]
        )
    |> Result.mapError dumpCodeSnippet
--> Err
-->     [ "2|  (  "
-->     , "3|  12  "
-->     , "4|  ,  "
-->     , "5|  )  "
-->     , " +~~^"
-->     ]