davidcavazos / parser / Parser.Error

Error reporting utilities.

Error reporting

message : Parser.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 -> Parser.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 : Parser.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|  )  "
-->     , " +~~^"
-->     ]