syntactical-0.1: Distfix expression parsing library

Text.Syntactical.Data

Description

The data structures (and associated functions) used in the parser. For a normal usage, it should be enough to import only Text.Syntactical, not directly this module.

Synopsis

Documentation

data SExpr a Source

The s-expression data type used as input and output of the parser. The type is parametrized by the type of the token.

Constructors

List [SExpr a] 
Atom a 

Instances

Eq a => Eq (SExpr a) 
Show a => Show (SExpr a) 

data Tree a Source

The s-expression data type augmented to represent parts (used in the operator stack).

Constructors

Branch [Tree a] 
Leaf a 
Part (Part a) 

Instances

Eq a => Eq (Tree a) 
Show a => Show (Tree a) 

data Op a Source

The operator representation, parametrized by the token type. It allows infix, prefix, postfix, and closed operators, with possibly multiple internal holes. Different holes are possible, to drive the parse in specific ways.

Constructors

Op1 Bool a [(Hole, a)] Opening Precedence 
Op2 Bool a [(Hole, a)] Hole a 

Instances

Eq a => Eq (Op a) 
Show a => Show (Op a) 

data Associativity Source

Specify the associativity of an infix operator.

data Hole Source

The Hole is used to give various behaviours when dealing with internal holes.

Constructors

SExpression

SExpression means the content of the hole should be parsed as an s-expression. The resulting value is a List. This means the hole can be empty or contain one or more sub-expression(s).

Distfix

Distfix means the content of the hole should be parsed as a distfix expression. In this case feeding an empty hole will generate a parse error.

Instances

data Part a Source

A Part represent a single symbol of an operator.

Constructors

First (Maybe (Associativity, Precedence)) a [a] Hole 
Last (Op a) 
Lone Bool a Opening Precedence 
Middle [a] a [a] Hole 

Instances

Eq a => Eq (Part a) 
Show a => Show (Part a) 

data Table a Source

The type of the operator table.

data Priority Source

Constructors

Lower 
Higher 
NoPriority 

infx :: Associativity -> a -> Op aSource

Build a infix operator. The precedence is set to 0.

prefx :: a -> Op aSource

Build a prefix operator. The precedence is set to 0.

postfx :: a -> Op aSource

Build a postfix operator. The precedence is set to 0.

closed :: a -> Hole -> a -> Op aSource

Build a closed operator. The precedence is set to 0.

infx_ :: Associativity -> a -> Op aSource

Build a infix operator with the keep property set to False. The precedence is set to 0.

prefx_ :: a -> Op aSource

Build a prefix operator with the keep property set to False. The precedence is set to 0.

postfx_ :: a -> Op aSource

Build a postfix operator with the keep property set to False. The precedence is set to 0.

closed_ :: a -> Hole -> a -> Op aSource

Build a closed operator with the keep property set to False. The precedence is set to 0.

sexpr :: Op a -> a -> Op aSource

Add a new part separated by an SExpression hole to the right of an operator.

distfix :: Op a -> a -> Op aSource

Add a new part separated by a Distfix hole to the right of an operator.

buildTable :: [[Op a]] -> Table aSource

buildTable constructs an operator table that can be used with the shunt function. Operators are given in decreasing precedence order.

cut :: Op a -> [Part a]Source

Separate an operator in its different parts.

setPrecedence :: Precedence -> Op a -> Op aSource

Set the precedence of a given operator.

continue :: Token a => Part a -> Part a -> BoolSource

arity :: Part a -> IntSource

Return the arity of a complete Part. It is an error to call this function on a First or Middle part.

symbol :: Part a -> aSource

Return the token of a given Part.

symbols :: Op a -> [a]Source

Return all the tokens of a given operator.

next :: Part a -> [a]Source

Return the possible tokens continuing the given part.

previous :: Part a -> [a]Source

Return the tokens preceding the given part.

current :: Part a -> [a]Source

Return the tokens of the given part.

findBoth :: Token a => Table a -> a -> [Tree a] -> FindBoth aSource

findBegin :: Token a => Table a -> a -> FindBegin aSource

class Token a whereSource

The class of the types that can be parsed.

Methods

toString :: a -> StringSource

convert to a string (for showing purpose)

operator :: Op a -> [SExpr a] -> SExpr aSource

create an output node from an operator and its arguments

consider :: a -> a -> BoolSource

test if two tokens are the same (used to find match from the operator table). A default definition that compares the result of toString is provided.

showSExpr :: Token a => SExpr a -> StringSource

Show an s-expression using nested angle brackets.