package fmlib_parse

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

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_result p is equivalent to not (needs_more p). has_result p signals that the parser has either succeeded or failed.

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

There are two types of failure:

  • Syntax error: In that case failed_expectations p returns the list of failed expectations.
  • Semantic error: In that case failed_semantic p returns the encountered semantic error.

The function state returns the user state.

The function lookaheads returns a pair. The first part of the pair is an array of unprocessed lookahead token and the second part is a flag indicating if the endtoken has been received via put_end.

type token = char

Token type.

type item = token

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

type state = State.t

User state.

type final = Final.t

Type of the final object constructed in case of success.

type expect = string * Indent.expectation option

Type of a failed expectation.

type semantic = Semantic.t

Type a semantic error.

type t

Type of the final parser.

val needs_more : t -> bool

needs_more p Does the parser p need more token?

val has_result : t -> bool

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

has_result p is the same as not (needs_more p)

val has_ended : t -> bool
  • deprecated

    Use has_result.

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 put : token -> t -> t

put token p Push token into the parser p.

Even if the parser has ended, more token 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.

val has_succeeded : t -> bool

has_succeeded p Has the parser p succeeded?

val has_failed_syntax : t -> bool

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

val has_failed_semantic : t -> bool

has_failed_semantic p Has the parser p failed with a semantic error?

val final : t -> final

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

Precondition: has_succeeded p

val failed_expectations : t -> expect list

failed_expectations p The failed expectations due to a syntax error.

Precondition: has_failed_syntax p

val failed_semantic : t -> semantic

failed_semantic p The failed semantic error.

Precondition: has_failed_semantic p

val state : t -> state

state p The user state of the parser p.

Can be called at any time.

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.

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

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 run_on_string : string -> t -> t

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

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

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

OCaml

Innovation. Community. Security.