Module Make.Parser

A parser is a consumer of tokens. At the end of consumption there is a result which is either a successfully parsed structure or a syntax or semantic error.

A parser p is a sink of token. As long as it signals needs_more p more token can be pushed into the parser via put token p or the input stream can be ended via put_end p.

has_ended p is equivalent to not (needs_more p). has_ended p signals that the parser has either succeeded or failed.

If it has succeeded the final value is available via final p.

type t

Type of the parser.

Feeding Tokens

type token = char

Type of the tokens.

type item = token

In order to conform to the interface Fmlib_std.Interfaces.SINK.

val needs_more : t -> bool

needs_more p Does the parser p need more tokens?

val put : token -> t -> t

put tok p Push token tok into the parser p.

Even if the parser has ended, more tokens can be pushed into the parser. The parser stores the token as lookahead token.

If the parser has already received the end of the token stream via put_end, then all subsequent tokens are ignored.

val put_end : t -> t

put_end p Push and end token into the parser p.

Success

type final = Final.t

Type of the final result.

val has_succeeded : t -> bool

has_succeeded p Has the parser p succeeded?

val has_ended : t -> bool

has_ended p Has the parser p ended parsing and either succeeded or failed?

has_ended p is the same as not (needs_more p)

val final : t -> final

final p The final object constructed by the parser p in case of success.

Precondition: has_succeeded p

Syntax Errors

type expect = string * Indent.expectation option

Type of expectations.

val has_failed_syntax : t -> bool

has_failed_syntax p Has the parser p failed with a syntax error?

val failed_expectations : t -> expect list

failed_expectations p The failed expectations due to a syntax error.

Precondition: has_failed_syntax p

Semantic Errors

type semantic = Semantic.t

Type of semantic errors.

val has_failed_semantic : t -> bool

Has the parser failed because of a semantic error?

val failed_semantic : t -> semantic

The semantic error encountered.

Precondition: A semantic error has occurred.

State

type state = State.t

Type of the state of the parser (in many cases unit)

val state : t -> state

The state of the parser.

Lookaheads

val has_lookahead : t -> bool

has_lookahead p Are there any unconsumed lookahead tokens in the buffer or has the end token not yet been consumed?

val first_lookahead_token : t -> token option

The first lookahead token (or None in case there is none).

val has_received_end : t -> bool

has_received_end p Has the parser p already received the end of token stream via put_end?

val has_consumed_end : t -> bool

has_consumed_end p Has the parser p already received the end of token stream via put_end and consumed it?

val fold_lookahead : 'a -> (token -> 'a -> 'a) -> ('a -> 'a) -> t -> 'a

fold_lookahead a ftok fend p

Fold the lookahead tokens with the start value a and the folding function ftok. At the end of the lookahead tokens, call fend if there is an unconsumed end.

val transfer_lookahead : t -> t -> t

transfer_lookahead p_old p_new

Transfer the lookahead tokens from p_old to p_new

val lookaheads : t -> token array * bool

lookaheads p The lookahead token and and end flag of the parser p.

The end flag indicates that the end token has already been received via put_end p.

Position Information

val position : t -> Position.t

position p The current position in the input stream.

Can be called at any time.

val line : t -> int

line p The current line in the input stream.

Can be called at any time.

val column : t -> int

column p The current column in the input stream.

Can be called at any time.

val byte_column : t -> int

byte_column p The current byte_column in the input stream.

Can be called at any time.

Run the Parser on Streams

val run_on_string : string -> t -> t

run_on_string str p Run the parser p on the string str.

val run_on_string_at : int -> string -> t -> int * t

run_on_string str start p Run the parser p on the string str starting at index start Return the parser and the index next to be pushed in.

val run_on_channel : Stdlib.in_channel -> t -> t

run_on_channel ic p Run the parser p on input channel ic.