davidcavazos/parser/6.1.0

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.

Unavailable. Nobody can download this version any more, with elm or with the Lamdera compiler: github.com/lambda-phi/parser exists but has no tag 6.1.0; Software Heritage has not archived the repo; no fork with tag 6.1.0 found (60 same-name repos checked); no cached copy. Use another version.

Details

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|  )  "
-->     , " +~~^"
-->     ]