Parse a String with the Grammar

MainSpec.elm test case code [⇐ Test Elm Applications] post

suite : Test suite = describe "Parsing Expression Grammar (PEG)" [ test "should parse 'abc' and convert it to all upper case 'ABC' with an action." <| \() -> let expected = Ok "ABC" actual = result in Expect.equal actual expected ]

We focus on `result`. code

{-| Parse a string with the grammar -} result : Result Error String result = let actions _ found _ = Ok (String.toUpper found) predicate _ _ state = ( True, state ) in grammarString |> fromString |> Result.andThen (\grammar -> parse grammar "" actions predicate "abc")

and the `main` function code evaluates the result of parsing:

{-| Check if the parse succeeded -} main : Html.Html msg main = case result of Ok value -> Html.text ("Parsed value: " ++ value) Err error -> Html.text ("Parse error: " ++ error.message ++ " at position " ++ String.fromInt error.position)

~

Parse a String with the Grammar post