Parses a PEG grammar represented as a string and returns a Result
containing the parsed Grammar.
input (String) : The PEG grammar represented as a string.Result Error Grammar : A Result containing the parsed Grammar, or an error message.import Peg exposing (Error, fromString, parse)
grammarString : String
grammarString =
"""
start <- digit+
digit <- [0-9]
"""
-- Parse the grammar string
result : Result Error Grammar
result =
fromString grammarString
-- Check if the parse succeeded
case result of
Ok grammar ->
Debug.log "Parsed grammar" grammar
Err error ->
Debug.log "Parse error" errorRepresents a PEG grammar as a list of rules, each consisting of a name and a
corresponding Rule value.
The Rule value should be a valid PEG grammar rule that can be parsed by the
fromString function.
See fromString for an example of how to create a Grammar from a
string representation of a PEG grammar.
The Actions type alias represents a function that is responsible for
performing an action on collected data. An Action is associated with a string,
which is the name of the action.
Actions take the following arguments:
actionName (String): the name of the actionmatchedText (String): the matched textcurrentState (state): the current stateResult (Result String state): a Result that contains either an error
message or the updated state.
If the Result contains an error message, parsing will be halted and the
error message will be returned.
If the Result contains an updated state, parsing will continue with the new
state.
See parse for an example of how to use parsing actions with the parser.
Represents a predicate that accepts the name of the predicate, the matched string, and the current state, and returns a Boolean value and an updated state.
predicateName (String) : The name of the predicate.matchedText (String) : The matched text.state (state) : The current state.A tuple containing a Boolean value indicating whether the predicate succeeded or failed, and an updated state.
myPredicate : Predicate String
myPredicate predicateName matchedText state =
( String.contains "hello" matchedText, state )Represents an error that occurred during parsing.
The position field is the 0-based index in the input string where the error
occurred, and the message field provides a description of the error.
Parses the given input string using the specified PEG grammar. Returns a
Resultthat is eitherOkwith the parsed result orErrwith an error message.Arguments
grammar(Grammar) : A valid PEG grammar represented as a list of(String, Rule)pairs.state(a) : The initial state that will be passed to the actions.actions(Actions a) : The actions that will be performed on the matched input.predicate(Predicate a) : The predicates that will be evaluated on the matched input.input(String) : The input string to parse.Returns
Result Error a: The result of the parse, which is eitherOkwith the parsed result orErrwith an error message.Example