pete-murphy/elm-nlds/3.0.1

Nld.Grammar

type Grammar
= AnyToken
| Literal String
| MinimalTokenGrammar (List ( Float, String ))
| RepeatGrammar Grammar
| Seq (List Grammar)
| Choice (List Grammar)
| Token TokenType

A first-class representation of an NLD-based grammar.

seq : List Grammar -> Grammar

Combine a list of Grammar values into a single Seq, flattening any nested Seq nodes.

import Nld.TokenType exposing (TokenType(..))


seq [ Literal "hello", Literal "world" ]
--> Seq [ Literal "hello", Literal "world" ]


seq [ Literal "a", Seq [ Literal "b", Literal "c" ] ]
--> Seq [ Literal "a", Literal "b", Literal "c" ]

Combine a list of Grammar values into a single Choice, flattening any nested Choice nodes.

import Nld.TokenType exposing (TokenType(..))


choice [ Literal "yes", Literal "no" ]
--> Choice [ Literal "yes", Literal "no" ]


choice [ Literal "a", Choice [ Literal "b", Literal "c" ] ]
--> Choice [ Literal "a", Literal "b", Literal "c" ]
encode : Grammar -> Value

Encode a Grammar as a JSON value.

import Json.Encode as Encode
import Nld.TokenType exposing (TokenType(..))


encode (Token NatToken)
--> Encode.object [ ( "tag", Encode.string "token" ), ( "value", Encode.object [ ( "tag", Encode.string "nat" ) ] ) ]
decoder : Decoder Grammar

Decode a Grammar from JSON.

import Json.Decode as Decode


Decode.decodeString decoder """{"tag":"anyToken"}"""
--> Ok AnyToken