module Cf_parser: sig
.. end
Functional LL(x) parsing with monadic combinators.
This module implements function left-shift/left-reduce parser combinators
using a state-exception monad over the input stream. To evaluate a parser
monad is to parse an input stream. The state monad is lifted into the
exception monad to facilitate backtracking. Parsers should signal errors
in the input stream with ordinary Objective Caml exceptions.
type ('i, 'o)
t = 'i Cf_seq.t -> ('o * 'i Cf_seq.t) option
The parser monad. A function that parses a sequence of input tokens.
Returns None
if the parser does not recognize any symbols. Otherwise
returns the reduced output and the remainder of the input tokens.
exception Error
Generic parser error with no parameters.
val nil : ('i, 'o) t
A parser that never recognizes any input, i.e. it always returns None
.
val err : ?f:('i Cf_seq.t -> exn) -> unit -> ('i, 'x) t
Use err ?f ()
to compose parser that applies the input token stream to
the optional function f
to obtain an Objective Caml exception, then
raises the exception. The default function simply raises Error
.
val req : ?f:('i Cf_seq.t -> exn) -> ('i, 'o) t -> ('i, 'o) t
Use req f p
to create a parser that requires the input stream to match
the parser p
or it will be passed to the parser err f
instead.
val fin : ('i, unit) t
A parser that produces the unit value when it recognizes the end of the
input token sequence.
val alt : ('i, 'o) t list -> ('i, 'o) t
Use alt plist
to create a parser that produces the output from the first
parser in the list plist
that recognizes a pattern in the input. If no
parser in the list recognizes a pattern, then the parser constructed by
this function returns None
.
val altz : ('i, 'o) t Cf_seq.t -> ('i, 'o) t
Use altz pseq
to create a parser that produces the output from the first
parser in the lazy sequence pseq
that recognizes a pattern in the input.
If no parser in the sequence recognizes a pattern, then the parser
constructed by this function returns None
.
val sat : ('i -> bool) -> ('i, 'i) t
Use sat f
to create a parser that recognizes, shifts and reduces input
tokens for which the satisfier function f
returns true
.
val tok : ('i -> 'o option) -> ('i, 'o) t
Use tok f
to recognize and shift input tokens for which the tokenizer
function f
reduces an output value.
val lit : string -> 'o -> (char, 'o) t
Use lit s obj
to obtain a parser on character input sequences that
produces the output obj
when it recognizes the literal s
in the input.
val unfold : ('i, 'o) t -> 'i Cf_seq.t -> 'o Cf_seq.t
Use unfold p i
to create a sequence of output values recognized by
applying the input token sequence i
to the parser p
until no more
input is recognized.
class ['i]
cursor : int ->
object
.. end
A class useful for tracking the position in the input token stream that
corresponds to the head of the sequence passed to a parser.
module X: sig
.. end
A module of parser extensions for working with input sequences that require
position information in the parse function.
module Op: sig
.. end
Open this module to take the parser operators into the current scope.
val filter : ('o -> bool) -> ('i, 'o) t -> ('i, 'o) t
Use filter f p
to produce a parser that applies f
to each output symbol
of p
and ignores all those for which the result is false
.
val map : ('x -> 'y) -> ('i, 'x) t -> ('i, 'y) t
Use map f p
to produce a parser that transforms each output symbol of p
by applying f
to its value.
val optmap : ('x -> 'y option) -> ('i, 'x) t -> ('i, 'y) t
Use optmap f p
to produce a parser that transforms each output symbol of
p
by applying f
to its value and ignoring all those for which the
result is None
.
val to_extended : ('i, 'o) t -> ('a #cursor, 'i, 'o) X.t
Use to_extended p
to convert the parser p
into an extended parser that
ignores the position information woven into the input stream.
val of_extended : ('i #cursor as 'c) ->
('c, 'i, 'o) X.t -> ('i, 'o) t
Use of_extended c p
to convert the parser p
that requires position
information in the input stream into a parser that assumes the input begins
at the position of the cursor c
.