Parser recovery tactics that need to be manually added to parsing code.
For these recovery tactics, a default value for a
must be given, so that the
parser can return something in the event of an error.
The aim is not to error correct parsed values (like an integer), but to get the
parser back to a state where it can continue running, whilst skipping out some
part of the input. For this reason, a value type for something being parsed out
of the text, such as an Int
or String
, is not such a useful choice here.
If skipping over some keyword or symbol or chomping characters, the parser will be
of the form Parser c x ()
. In this case, it is easy to give ()
as the default
value. The symbol or keyword will be counted as being there silently, with a warning,
or will act as a sentinal token that parsing will forward to, then try to continue.
Another common way to recover is when parsing a sequence of things, to skip any
things which are not syntactically correct. In this situation you might use
Nothing
as the default value. You can use Maybe.Extra.value
to convert a
List (Maybe a)
to a List a
, in this case.
Some other useful default values might be []
, or Dict.empty
or Set.empty
and so on.
optional : a -> Parser.Recoverable.Parser c x a -> Parser.Recoverable.Parser c x a
Silently ignore any failure.
A default value for a
must be given, so that the parser can return something
in the event of an error and succesful recovery.
skip : a -> x -> Parser.Recoverable.Parser c x a -> Parser.Recoverable.Parser c x a
Skip over a failure, but when one happens add an error to a Partial
result.
A default value for a
must be given, so that the parser can return something
in the event of an error and succesful recovery.
forward : a -> List String -> x -> (String -> String -> x) -> Parser.Recoverable.Parser c x a -> Parser.Recoverable.Parser c x a
When parsing fails, attempt to fast-forward to one of a set of sentinal tokens.
When this happens an error is added to a Partial
result.
When called with these arguments:
forward val matches noMatchProb chompedProb parser
Note that the chompedProb
argument has the type (String -> String -> x)
.
This is called with the string being skipped over, and the matched token being
consumed. This information will be combined with its position in the input text,
in the error added to the Partial
result.
A default value for a
must be given, so that the parser can return something
in the event of an error and succesful recovery.
forwardOrSkip : a -> List String -> x -> (String -> String -> x) -> Parser.Recoverable.Parser c x a -> Parser.Recoverable.Parser c x a
When parsing fails, attempt to fast-forward to one of a set of sentinal tokens,
or if none can be found, skip over the failure. When this happens an error is
added to a Partial
result.
Note that the chompedProb
argument has the type (String -> String -> x)
.
This is called with the string being skipped over, and the matched token being
consumed. If no match is found, then ""
will be given as the token consumed.
This information will be combined with its position in the input text, in the
error added to thePartial
result.
A default value for a
must be given, so that the parser can return something
in the event of an error and succesful recovery.