the-sett / parser-recoverable / Parser.Recoverable

Parser.Recoverable encodes parsers that can have Partial result when there is an error, but the parsing is able to recover and continue and still produce a result.

This Parser.Recoverable module does not do any automatic error recovery, its functions behave the same way as those in Parser.Advanced.

Parsers


type alias Parser context problem value =
Parser.Advanced.Parser context problem (Outcome context problem value)

The type of recoverable parsers.

run : Parser c x a -> String -> Outcome c x a

Runs the parser. This differs from Parser.run in that it produces an Outcome instead of a Result. This allows for a Partial result when the parser recovers from an error. In this case the parser will still produce a result, but will also list the errors it encountered and was able to recover from.

Basics


type alias DeadEnd context problem =
Parser.Advanced.DeadEnd context problem

The same as Parser.Advanced.DeadEnd.

inContext : c -> Parser c x a -> Parser c x a

Just like Parser.inContext.

Building Blocks

int : x -> x -> Parser c x Basics.Int

Just like Parser.int where you have to handle negation yourself. The only difference is that you provide a two potential problems:

int : x -> x -> Parser c x Int
int expecting invalid =
    number
        { int = Ok identity
        , hex = Err invalid
        , octal = Err invalid
        , binary = Err invalid
        , float = Err invalid
        , invalid = invalid
        , expecting = expecting
        }

You can use problems like ExpectingInt and InvalidNumber.

float : x -> x -> Parser c x Basics.Float

Just like Parser.float where you have to handle negation yourself. The only difference is that you provide a two potential problems:

float : x -> x -> Parser c x Float
float expecting invalid =
    number
        { int = Ok toFloat
        , hex = Err invalid
        , octal = Err invalid
        , binary = Err invalid
        , float = Ok identity
        , invalid = invalid
        , expecting = expecting
        }

You can use problems like ExpectingFloat and InvalidNumber.

number : { int : Result x (Basics.Int -> a), hex : Result x (Basics.Int -> a), octal : Result x (Basics.Int -> a), binary : Result x (Basics.Int -> a), float : Result x (Basics.Float -> a), invalid : x, expecting : x } -> Parser c x a

Just like Parser.number where you have to handle negation yourself. The only difference is that you provide all the potential problems.

symbol : String -> x -> Parser c x ()

Just like Parser.symbol except you also provide a custom problem.

comma : Parser Context Problem ()
comma =
    symbol "," ExpectingComma

keyword : String -> x -> Parser c x ()

Just like Parser.keyword except you provide a custom problem.

let_ : Parser Context Problem ()
let_ =
    symbol "let" ExpectingLet

Note that this would fail to chomp letter because of the subsequent characters. Use token if you do not want that last letter check.

variable : { start : Char -> Basics.Bool, inner : Char -> Basics.Bool, reserved : Set String, expecting : x } -> Parser c x String

Just like Parser.variable except you specify a custom problem.

token : String -> x -> Parser c x ()

Just like Parser.token except you provide a custom problem.

end : x -> Parser c x ()

Just like Parser.endexcept you provide the problem that arises when the parser is not at the end of the input.

Pipelines

ignore : Parser c x ignore -> Parser c x keep -> Parser c x keep

This is a flipped version of |.

If either parser fails, the result of this will fail. If either parser produces a Partial result but not a Failure, the parsing will continue and carry forward any errors and produce a Partial result.

keep : Parser c x a -> Parser c x (a -> b) -> Parser c x b

This is a flipped version of |=

If either parser fails, the result of this will fail. If either parser produces a Partial result but not a Failure, the parsing will continue and carry forward any errors and produce a Partial result.

succeed : a -> Parser c x a

Just like Parser.succeed.

lazy : (() -> Parser c x a) -> Parser c x a

Just like Parser.lazy.

andThen : (a -> Parser c x b) -> Parser c x a -> Parser c x b

Just like Parser.andThen, except that parsing will continue if the parser produces a Partial result. Any errors attached to a Partial result will always be carried forward by the parser, so the overall result at the end will be Partial.

problem : x -> Parser c x a

Just like Parser.problem except you provide a custom problem.

Branches

oneOf : List (Parser c x a) -> Parser c x a

Just like Parser.oneOf.

map : (a -> b) -> Parser c x a -> Parser c x b

Just like Parser.map, except that parsing will continue if the parser produces a Partial result. Any errors attached to a Partial result will always be carried forward by the parser, so the overall result at the end will be Partial.

backtrackable : Parser c x a -> Parser c x a

Just like Parser.backtrackable.

commit : a -> Parser c x a

Just like Parser.commit.

Loops

sequence : { start : String, startProb : x, separator : String, separatorProb : x, end : String, endProb : x, spaces : Parser c x (), item : Parser c x a, trailing : Trailing } -> Parser c x (List a)

Just like Parser.sequence.


type Trailing
    = Forbidden
    | Optional
    | Mandatory

Just like Parser.Trailing.

loop : state -> (state -> Parser c x (Step state a)) -> Parser c x a

Just like Parser.loop.


type Step state a
    = Loop state
    | Done a

Just like Parser.Step.

Whitespace

spaces : Parser c x ()

Just like Parser.x.

lineComment : String -> x -> Parser c x ()

Just like Parser.x.

multiComment : String -> x -> String -> x -> Nestable -> Parser c x ()

Just like Parser.x.


type Nestable
    = NotNestable
    | Nestable

Just like Parser.x.

Chompers

getChompedString : Parser c x a -> Parser c x String

Just like Parser.getChompedString.

chompIf : (Char -> Basics.Bool) -> x -> Parser c x ()

Just like Parser.chompIf.

chompWhile : (Char -> Basics.Bool) -> Parser c x ()

Just like Parser.chompWhile.

chompUntil : String -> x -> Parser c x ()

Just like Parser.chompUntil.

chompUntilEndOr : String -> Parser c x ()

Just like Parser.chompUntilEndOr.

mapChompedString : (String -> a -> b) -> Parser c x a -> Parser c x b

Just like Parser.mapChompedString.

Indentation

withIndent : Basics.Int -> Parser c x a -> Parser c x a

Just like Parser.withIndent.

getIndent : Parser c x Basics.Int

Just like Parser.getIndent.

Positions

getPosition : Parser c x ( Basics.Int, Basics.Int )

Just like Parser.getPosition.

getRow : Parser c x Basics.Int

Just like Parser.getRow.

getCol : Parser c x Basics.Int

Just like Parser.getCol.

getOffset : Parser c x Basics.Int

Just like Parser.getOffset.

getSource : Parser c x String

Just like Parser.getSource.