package batteries
Install
Dune Dependency
Authors
Maintainers
Sources
md5=ea26b5c72e6731e59d856626049cca4d
sha512=55975b62c26f6db77433a3ac31f97af609fc6789bb62ac38b267249c78fd44ff37fe81901f1cf560857b9493a6046dd37b0d1c0234c66bd59e52843aac3ce6cb
doc/batteries.unthreaded/BatParserCo/index.html
Module BatParserCo
Source
A simple parser combinator library.
This module permits the simple definition of highly modular, dynamic parsers with unlimited backtracking. It may be used to parse any form of enumeration, including regular text, latin-1 text, bits, etc.
This library is vastly more powerful than Lexing
, Str
, Parsing
or Scanf
. It is also considerably slower.
Module CharParser
contains pre-defined parsers to deal specifically with latin-1 text. Module Genlex
contains a number of pre-defined parsers to deal specifically with programming languages.
Note This library is still very rough and needs much testing.
Base definitions
The current state of the parser.
The actual set of states is defined by the user. States are typically used to convey information, such as position in the file (i.e. line number and character).
A source for parsing. Unless you are parsing from exotic sources, you will probably not need to use this module directly. Rather, use CharParser.source_of_string
or CharParser.source_of_enum
.
Primitives
A parser for elements of type 'a
, producing elements of type 'b
, with user-defined states of type 'c
.
Monadic-style combination:
bind p f
results in a new parser which behaves as p
then, in case of success, applies f
to the result.
cons p q
applies parser p
then parser q
and conses the results into a list.
Give a name to a parser, for debugging purposes.
satisfy p
accepts one value p x
such that p x = true
filter f p
is only accepts values x
such that p
accepts x
and f (p x)
is true
suspend s
returns the state of the parser in a form that can be resumed by calling the returned function. evaluation will resume from parser s
run p s
executes parser p
on source s
. In case of success, returns Ok v
, where v
is the return value of p
. In case of failure, returns Error f
, with f
containing details on the parsing error.
lookahead p
behaves as maybe p
but without consuming anything
Utilities
Singletons
Accept one of several values. Faster and more convenient than combining satisfy
and either
.
Accept any value not in a list Faster and more convenient than combining satisfy
and either
.
Repetitions
Accept a (possibly empty) list of expressions.
Ignore a (possibly empty) list of expressions. Optimized version of zero_plus
, for use when the list of expressions is unimportant.
Accept a (non-empty) list of expressions
Ignore a (non-empty) list of expressions. Optimized version of one_plus
, for use when the list of expressions is unimportant.
times n p
accepts a list of n
expressions accepted by p
Maps
Pass the (successful) result of some parser through a map.
Use a parser to extract list of tokens, but return that list of tokens instead of whatever the original parser returned.
Others
If set to true
, debugging information will be printed to the standard error.