Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Page
Library
Module
Module type
Parameter
Class
Class type
Source
PratterSourceParse expressions with infix, prefix or postfix operators.
To parse expressions from type 'i to type 'o, you need to tell the parser
'o -> 'o -> 'o;'i should be considered an operator;'i that aren't operators.The algorithm implemented is an extension of the Pratt parser. The Shunting Yard algorithm could also be used.
Associativity of an operator.
type fixity = | Infix of associativityInfix operator like + in x + y.
| PrefixPrefix operator like ! in ! x.
| PostfixPostfix operator like ^ in x ^.
The fixity of an operator
type 't error = [ | `Op_conflict of 'tPriority or associativiy conflict between two operators. In `OpConflict o, o is an operator which generates a conflict.
| `Too_few_argumentsMore arguments are expected. It is raised for instance on partial application of operators, such as x +; or when an empty input is given to the parser.
]Errors that can be encountered while parsing a stream of terms.
Parsers from sequences of 'tok to values of type 'out.
val expression :
appl:('b -> 'b -> 'b) ->
token:('a -> 'b) ->
ops:('a -> (fixity * float * 'b) list) ->
('a, 'b) parserexpression appl token ops is a parser from sequences of 'a to values of type 'b. The parser is driven by the operator table ops which determines which tokens are operators. A token t can be used as an operator if ops t isn't empty. Each value (f, p, s) of ops t means that token t can be parsed as token s with fixity f and priority p.
The function appl is used to combine two expressions.
For instance, assuming that + is declared infix and we're working with numbers, it can transform 3 + 5 × 2 encoded as the stream of terms 3, +, 5, ×, 2 into the binary tree @(@(×,@(@(+,3),5)),2) where @ denotes application nodes. Such a parsing could be produced by the parameters ops and appl such that (types of tokens are schematised for simplicity)
ops + = [Infix Left, 1.0, +] andappl x y = @(x, y).