Module Grammar


module Grammar: sig .. end
Extensible grammars.

This module implements the Camlp5 extensible grammars system. Grammars entries can be extended using the EXTEND statement, added by loading the Camlp5 pa_extend.cmo file.


type g 
The type for grammars, holding entries.
type token = string * string 
val gcreate : token Plexing.lexer -> g
Create a new grammar, without keywords, using the lexer given as parameter.
val tokens : g -> string -> (string * int) list
Given a grammar and a token pattern constructor, returns the list of the corresponding values currently used in all entries of this grammar. The integer is the number of times this pattern value is used.

Examples:


val glexer : g -> token Plexing.lexer
Return the lexer used by the grammar
type parsable 
val parsable : g -> char Stream.t -> parsable
Type and value allowing to keep the same token stream between several calls of entries of the same grammar, to prevent possible loss of tokens. To be used with Entry.parse_parsable below
module Entry: sig .. end
Module to handle entries.
val of_entry : 'a Entry.e -> g
Return the grammar associated with an entry.

Clearing grammars and entries

module Unsafe: sig .. end
Module for clearing grammars and entries.

Parsing algorithm

type parse_algorithm = Gramext.parse_algorithm = 
| Predictive
| Backtracking
| DefaultAlgorithm (*Type of algorithm used in grammar entries. Predictive: use imperative streams with predictive parsing Backtracking: use functional streams with full backtracking DefaultAlgorithm: found in the variable backtrack_parse below. The default, when a grammar is created, is DefaultAlgorithm.*)
val set_algorithm : g -> parse_algorithm -> unit
Set the parsing algorithm for all entries of a given grammar.
val backtrack_parse : bool Pervasives.ref
If True, the default parsing uses full backtracking. If False, it uses parsing with normal streams. If the environment variable CAMLP5PARAM contains "b", the default is True; otherwise, the default is False.
val backtrack_stalling_limit : int Pervasives.ref
Limitation of backtracking to prevent stalling in case of syntax error. In backtracking algorithm, when there is a syntax error, the parsing continues trying to find another solution. It some grammars, it can be very long before checking all possibilities. This number limits the number of tokens tests after a backtrack. (The number of tokens tests is reset to zero when the token stream overtakes the last reached token.) The default is 10000. If set to 0, there is no limit. Can be set by the environment variable CAMLP5PARAM by "l=value".

Functorial interface


Alternative for grammars use. Grammars are no more Ocaml values: there is no type for them. Modules generated preserve the rule "an entry cannot call an entry of another grammar" by normal OCaml typing.
module type GLexerType = sig .. end
The input signature for the functor Grammar.GMake: te is the type of the tokens.
module type S = sig .. end
Signature type of the functor Grammar.GMake.
module GMake: 
functor (L : GLexerType) -> S with type te = L.te

Miscellaneous

val skip_item : 'a -> 'a
Grammar.skip_item x can be called in a semantic action of a grammar rule to ask the grammar to skip that item if it is called in a list (LIST0 or LIST1). The function returns the item itself (for typing reasons) but its value is ignored. This function is used to allow IFDEF and IFNDEF for cases of constructor declarations and pattern matchings.
val error_verbose : bool Pervasives.ref
Flag for displaying more information in case of parsing error; default = False
val warning_verbose : bool Pervasives.ref
Flag for displaying warnings while extension; default = True
val strict_parsing : bool Pervasives.ref
Flag to apply strict parsing, without trying to recover errors; default = False
val print_entry : Format.formatter -> 'a Gramext.g_entry -> unit
General printer for all kinds of entries (obj entries)
val iter_entry : ('a Gramext.g_entry -> unit) -> 'a Gramext.g_entry -> unit
Grammar.iter_entry f e applies f to the entry e and transitively all entries called by e. The order in which the entries are passed to f is the order they appear in each entry. Each entry is passed only once.
val fold_entry : ('a Gramext.g_entry -> 'b -> 'b) -> 'a Gramext.g_entry -> 'b -> 'b
Grammar.fold_entry f e init computes (f eN .. (f e2 (f e1 init))), where e1 .. eN are e and transitively all entries called by e. The order in which the entries are passed to f is the order they appear in each entry. Each entry is passed only once.
val reinit_entry_functions : 'a Gramext.g_entry -> unit
val loc_of_token_interval : int -> int -> Ploc.t
val extend : ('a Gramext.g_entry * Gramext.position option *
(string option * Gramext.g_assoc option *
('a Gramext.g_symbol list * Gramext.g_action) list)
list)
list -> unit
val delete_rule : 'a Entry.e -> token Gramext.g_symbol list -> unit
val parse_top_symb : 'a Gramext.g_entry -> 'a Gramext.g_symbol -> 'a Stream.t -> Obj.t
val symb_failed_txt : 'a Gramext.g_entry -> 'a Gramext.g_symbol -> 'a Gramext.g_symbol -> string
val create_local_entry : g -> string -> 'a Entry.e