microelm / elm-peg / Peg

Functions

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.

Arguments

Returns

Example

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.

Arguments

Returns

Example

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

Types


type alias Grammar =
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.


type alias Actions state =
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:

Arguments

Returns

See parse for an example of how to use parsing actions with the parser.


type alias Predicate state =
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.

Arguments

Returns

A tuple containing a Boolean value indicating whether the predicate succeeded or failed, and an updated state.

Example

myPredicate : Predicate String
myPredicate predicateName matchedText state =
    ( String.contains "hello" matchedText, state )


type alias Error =
{ 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.