package batteries

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module BatParserCoSource

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

Sourcetype 'a state =
  1. | Eof
    (*

    The end of the source has been reached.

    *)
  2. | State of 'a

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).

Sourcetype 'a report =
  1. | Report of ('a state * string * 'a report) list
    (*

    The final result of parsing

    *)
Sourcemodule Source : sig ... end

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

Sourcetype ('a, 'b, 'c) t

A parser for elements of type 'a, producing elements of type 'b, with user-defined states of type 'c.

Sourceval eof : (_, unit, _) t

Accept the end of an enumeration.

Sourceval either : ('a, 'b, 'c) t list -> ('a, 'b, 'c) t

Accept one of several parsers.

Sourceval (<|>) : ('a, 'b, 'c) t -> ('a, 'b, 'c) t -> ('a, 'b, 'c) t

Accept one of two parsers

Sourceval maybe : ('a, 'b, 'c) t -> ('a, 'b option, 'c) t

Accept an optional argument.

Sourceval (~?) : ('a, 'b, 'c) t -> ('a, 'b option, 'c) t

As maybe

Sourceval bind : ('a, 'b, 'c) t -> ('b -> ('a, 'd, 'c) t) -> ('a, 'd, 'c) t

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.

Sourceval (>>=) : ('a, 'b, 'c) t -> ('b -> ('a, 'd, 'c) t) -> ('a, 'd, 'c) t

As bind

Sourceval (>>>) : ('a, _, 'c) t -> ('a, 'd, 'c) t -> ('a, 'd, 'c) t

As bind, but ignoring the result

Sourceval cons : ('a, 'b, 'c) t -> ('a, 'b list, 'c) t -> ('a, 'b list, 'c) t

cons p q applies parser p then parser q and conses the results into a list.

Sourceval (>::) : ('a, 'b, 'c) t -> ('a, 'b list, 'c) t -> ('a, 'b list, 'c) t

As cons

Sourceval label : string -> ('a, 'b, 'c) t -> ('a, 'b, 'c) t

Give a name to a parser, for debugging purposes.

Sourceval state : (_, 'b state, 'b) t

Succeed and return the state of the parser

Sourceval any : ('a, 'a, _) t

Accept any singleton value.

Sourceval return : 'b -> (_, 'b, _) t

A parser which always succeeds

Sourceval satisfy : ('a -> bool) -> ('a, 'a, _) t

satisfy p accepts one value p x such that p x = true

Sourceval filter : ('b -> bool) -> ('a, 'b, 'c) t -> ('a, 'b, 'c) t

filter f p is only accepts values x such that p accepts x and f (p x) is true

Sourceval suspend : ('a, 'b, 'c) t -> ('a, unit -> ('b, 'c report) BatPervasives.result, 'c) t

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

Sourceval run : ('a, 'b, 'c) t -> ('a, 'c) Source.t -> ('b, 'c report) BatPervasives.result

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.

Sourceval fail : (_, _, _) t

Always fail, without consuming anything.

Sourceval fatal : (_, _, _) t
Sourceval lookahead : ('a, 'b, 'c) t -> ('a, 'b option, 'c) t

lookahead p behaves as maybe p but without consuming anything

Utilities

Singletons

Sourceval exactly : 'a -> ('a, 'a, 'c) t

Accept exactly one singleton.

Sourceval one_of : 'a list -> ('a, 'a, 'c) t

Accept one of several values. Faster and more convenient than combining satisfy and either.

Sourceval none_of : 'a list -> ('a, 'a, 'c) t

Accept any value not in a list Faster and more convenient than combining satisfy and either.

Sourceval range : 'a -> 'a -> ('a, 'a, 'c) t

Accept any element from a given range.

Repetitions

Sourceval zero_plus : ?sep:('a, _, 'c) t -> ('a, 'b, 'c) t -> ('a, 'b list, 'c) t

Accept a (possibly empty) list of expressions.

Sourceval ignore_zero_plus : ?sep:('a, _, 'c) t -> ('a, _, 'c) t -> ('a, unit, 'c) t

Ignore a (possibly empty) list of expressions. Optimized version of zero_plus, for use when the list of expressions is unimportant.

Sourceval (~*) : ('a, 'b, 'c) t -> ('a, 'b list, 'c) t

As zero_plus without arguments.

Sourceval one_plus : ?sep:('a, _, 'c) t -> ('a, 'b, 'c) t -> ('a, 'b list, 'c) t

Accept a (non-empty) list of expressions

Sourceval ignore_one_plus : ?sep:('a, _, 'c) t -> ('a, _, 'c) t -> ('a, unit, 'c) t

Ignore a (non-empty) list of expressions. Optimized version of one_plus, for use when the list of expressions is unimportant.

Sourceval (~+) : ('a, 'b, 'c) t -> ('a, 'b list, 'c) t

As one_plus

Sourceval times : int -> ('a, 'b, 'c) t -> ('a, 'b list, 'c) t

times n p accepts a list of n expressions accepted by p

Sourceval (^^) : ('a, 'b, 'c) t -> int -> ('a, 'b list, 'c) t

p ^^ n is the same thing as times n p

Sourceval must : ('a, 'b, 'c) t -> ('a, 'b, 'c) t

Prevent backtracking.

Sourceval should : ('a, 'b, 'c) t -> ('a, 'b, 'c) t

Prevent backtracking.

Maps

Sourceval post_map : ('b -> 'c) -> ('a, 'b, 'd) t -> ('a, 'c, 'd) t

Pass the (successful) result of some parser through a map.

Sourceval source_map : ('a, 'b, 'c) t -> ('a, 'c) Source.t -> ('b, 'c) Source.t
Sourceval scan : ('a, _, 'c) t -> ('a, 'a list, 'c) t

Use a parser to extract list of tokens, but return that list of tokens instead of whatever the original parser returned.

Others

Sourceval sat : ('a -> bool) -> ('a, unit, _) t

satisfy p accepts one value p x such that p x = true

Sourceval debug_mode : bool ref

If set to true, debugging information will be printed to the standard error.

Sourcemodule Infix : sig ... end
OCaml

Innovation. Community. Security.