parse : Grammar -> state -> Actions state -> Predicate state -> String -> Result Error state
Parses the given input string using the specified PEG grammar.
Returns a Result
that is either Ok
with the parsed result or Err
with an error message.
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.Result Error a
: The result of the parse, which is either Ok
with the parsed result or Err
with an error message.import Html
import Peg exposing (Error)
grammarString : String
grammarString =
"""
start <- <digit+> {action}
digit <- [0-9]
"""
{-| Parse a string with the grammar
-}
result : Result Error String
result =
let
actions _ found _ =
Ok found
predicate _ _ state =
( True, state )
in
grammarString
|> Peg.fromString
|> Result.andThen (\grammar -> Peg.parse grammar "" actions predicate "123")
{-| Check if the parse succeeded
-}
main =
case result of
Ok value ->
Html.text ("Parsed value: " ++ value)
Err error ->
Html.text ("Parse error: " ++ error.message)
fromString : String -> Result Error Grammar
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" error
List ( String
, Rule
}
Represents 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.
String -> String -> state -> Result String state
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.
String -> String -> state -> ( Basics.Bool
, state
}
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 )
{ position : Basics.Int
, message : String
}
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.