Elm PEG Parser is a library for parsing text using Parsing Expression Grammar (PEG). It is implemented in Elm, a functional programming language for building web applications.
Add the package to your Elm project using the following command:
elm install microelm/elm-pegTo use Elm PEG Parser, start by defining a grammar using the Peg.Grammar type. Once you've defined your grammar, you can parse input text using the parse function.
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)The API documentation is available on the package website.
ConditionalPredicate rule is not fully implemented and may not behave as expected in certain cases.This library is based on the work of Ian Piumarta, who first introduced the PEG (Parsing Expression Grammar) formalism. We would like to acknowledge his contributions to the field of programming language design and parsing.
Our library extends Ian's work by providing an Elm implementation of PEG parsing that is easy to use and customizable. We have also added several new features, such as support for action functions and conditional predicates.
To learn more about Ian Piumarta's work, please visit his website.
Contributions are welcome! Please read the contributing guidelines before submitting a pull request.
This project is licensed under the Public Domain - see the LICENSE file for details.