package fmlib
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=0558665285e4d7691e5a80c90ab05a7acb86c09f03ceef6589f150f6d3574573
md5=fb61f4d6e7233cf8d1d71758e6110c1e
doc/fmlib.fmlib_parse/Fmlib_parse/Character/Make/index.html
Module Character.Make
Source
State
: User state.Final
: Final result type of the parser.Semantic
: Semantic error message (triggered byfail error
)
Parameters
module State : Fmlib_std.Interfaces.ANY
module Final : Fmlib_std.Interfaces.ANY
module Semantic : Fmlib_std.Interfaces.ANY
Signature
Final Parser
Generic Combinators
include Interfaces.COMBINATOR
with type state = State.t
and type expect = string
and type semantic = Semantic.t
Basic Combinators
'a t
Type of a parse combinator returning an 'a
.
p >>= f
Parse first the input according to the combinator p
. In case of success, feed the returned value of p
into the function f
to get the combinator to parse next.
let* x = p in f x
is equivalent to p >>= f
The let*
combinator let us express parsing sequences conveniently. Example:
let* x = p in (* parse [p], result [x] in case of success. *)
let* y = q x in (* parse [q x], result [y] ... *)
let* z = r x y in (* ... *)
...
return f x y z ...
The wildcard let* _ = ...
can be used to ignore results of intermediate parsing steps.
map f p
Try combinator p
. In case of success, map the returned value x
to f x
. In case of failure, do nothing.
map f p
is equivalent to let* x = p in return (f x)
.
succeed a
Succeed immediately without consuming token. Return object a
as result.
unexpected expect
triggers a syntax error signalling the expectation expect
.
clear_last_expectation p
Clear last failed expectation.
This is useful e.g. after stripping whitespace. Since stripping whitespace means skip_one_or_more ws
or skip_zero_or_more ws
, after skipping whitespace the parser can still expect more whitespace. Therefore there is a failed expectation *whitespace* on the stack. However you rarely want this expectation to be reported.
p </> q
Try first combinator p
. In case of success or failure with consumed token, p </> q
is equivalent to p
.
If p
fails without consuming token, then p </> q
is equivalent to q
.
choices p [q r t ...]
is equivalent to p </> q </> r </> t </> ...
.
p <?> expect
Try combinator p
. In case of success or failure with consumed token, p <?> expect
is equivalent to p
.
If p
fails without consuming token, then the failed expectations are replaced with the failed expectation expect
.
Usually p
is a combinator implementing a choice between various alternatives of a grammar construct. The <?>
combinator allows to replace the set of failed grammar alternatives with a higher abstraction of the failed expectation. E.g. instead of getting the failed expectations identifier
, '('
, -
, ... we can get the failed expectation expression
.
State Combinators
get_and_update f
Get the current user state and then update the user state. The returned value is the old state.
Convenience Combinators
optional p
Try combinator p
.
- Success: Return
Some a
wherea
is the returned value. - Failure without consuming token: Return
None
- Failure with consuming token: Remain in the error state.
zero_or_more start f p
Try the combinator p
as often as possible. Return start
if p
fails without consuming token. As long as p
succeeds use f
to accumulate the results.
The first time p
fails without consuming token, return the accumulated result.
If p
fails by consuming token, then zero_or_more f p
fails with the same error.
one_or_more first next p
one_or_more first next p
is equivalent to
let* x = p in
zero_or_more (first x) next p
list_zero_or_more p
Parse zero or more occurrences of p
and returned the collected result in a list.
list_zero_or_more p
Parse one or more occurrences of p
and returned the collected results as a pair of the first value and a list of the remaining values.
skip_zero_or_more p
Parse zero or more occurrences of p
, ignore the result and return the number of occurrences.
skip_one_or_more p
Parse one or more occurrences of p
, ignore the result and return the number of occurrences.
val one_or_more_separated :
('item -> 'r) ->
('r -> 'sep -> 'item -> 'r) ->
'item t ->
'sep t ->
'r t
one_or_more_separated first next p sep
Parse one or more occurrences of p
separated by sep
. Use first
to convert the first occurrence of p
into the result and use next
to accumulate the results.
Backtracking
backtrack p expect
Try the combinator p
. In case of failure with consuming token, push the consumed token back to the lookahead and let it fail without consuming token. Use expect
to record the failed expectation.
Backtracking reduces the performance, because the token pushed back to the lookahead have to be parsed again. Try to avoid backtracking whenever possible.
followed_by p expect
Parses p
and backtracks (i.e. all tokens of p
will be pushed back to the lookahead). In case p
succeeds, the followed_by
parser succeeds without consuming token. Otherwise it fails without consuming tokens.
not_followed_by p expect
Parses p
and backtracks (i.e. all tokens of p
will be pushed back to the lookahead). In case p
succeeds, the not_followed_by
parser fails without consuming token. Otherwise it succeeds without consuming tokens.
followed_by
and not_followed_by
can be used to peek into the token stream without consuming token.
Location Combinator
located p
Parse p
and return its result with its start and end position.
Note: If p
parses strips whitespace at the end, the returned end position is at the end of the whitespace. This is not what you usually want. Therefore first parse the essential part located and then strip the whitespace.
Indentation Combinators
indent i p
Indent p
by i
columns relative to its parent.
Precondition: 0 <= i
align p
Set the indentation set of p
to {col}
where col
is the column position of its first character. Fail, if col
is not in the indentation set.
left_align p
Set the indentation set of p
to {col}
where col
is the column position of its first character. Fail, if col
is not the lower bound of the indentation set. I.e. p
is left aligned in its indentation set.
zero_or_more_aligned start next p
Parse an indented block of zero or more aligned constructs p
.
Equivalent to
zero_or_more start next (align p) |> align |> indent 1
zero_or_more_aligned first next p
Parse an indented block of one or more aligned constructs p
.
Equivalent to
one_or_more first next (align p) |> align |> indent 1