Module Procq

The parser of Rocq

include Gramlib.Grammar.S with type keyword_state := CLexer.keyword_state and type te := Tok.t and type 'a pattern := 'a Tok.p and type 'a with_gstate := 'a and type 'a with_kwstate := 'a and type 'a with_estate := 'a and type 'a mod_estate := 'a
type ty_pattern =
| TPattern : 'a Tok.p -> ty_pattern
type peek_error = unit
type 'a parser_v = ('apeek_error) Stdlib.result

Recoverable parsing errors are signaled use Error. To be correctly recovered we must not have consumed any tokens since the last choice point, ie we only peeked at the stream.

Other errors are signaled using the ParseError exception or even arbitrary exceptions.

Type combinators to factor the module type between explicit state passing in Grammar and global state in Procq

module Parsable : sig ... end
module Entry : sig ... end
module Symbol : sig ... end
module Rule : sig ... end
module Rules : sig ... end
module Production : sig ... end
type 'a single_extend_statement = string option * Gramlib.Gramext.g_assoc option * 'a Production.t list
type 'a extend_statement =
| Reuse of string option * 'a Production.t list(*

Extend an existing level by its optional given name. If None, picks the topmost level.

*)
| Fresh of Gramlib.Gramext.position * 'a single_extend_statement list(*

Create a level at the given position.

*)
val generalize_symbol : ('a'tr'c) Symbol.t -> ('bGramlib.Grammar.norec'c) Symbol.t option
val level_of_nonterm : ('aGramlib.Grammar.norec'c) Symbol.t -> string option
module Lookahead : sig ... end
val terminal : string -> string Tok.p

When string is not an ident, returns a keyword.

The parser of Rocq is built from three kinds of rule declarations:

Note that parsing a Rocq document is in essence stateful: the parser needs to recognize commands that start proofs and use a different parsing entry point for them.

We thus provide two different interfaces: the "raw" parsing interface, in the style of camlp5, which provides more flexibility, and a more specialize "parse_vernac" one, which will indeed adjust the state as needed.

Dynamic extension of rules

For constr notations, dynamic addition of new rules is done in several steps:

For user level tactic notations, dynamic addition of new rules is also done in several steps:

For TACTIC/VERNAC/ARGUMENT EXTEND, addition of new rules is done as follows:

Parse a string

val parse_string : 'a Entry.t -> ?loc:Loc.t -> string -> 'a
val eoi_entry : 'a Entry.t -> 'a Entry.t
val create_generic_entry2 : string -> ('aGenarg.rlevel) Genarg.abstract_argument_type -> 'a Entry.t
val register_grammar : ('raw'glb'top) Genarg.genarg_type -> 'raw Entry.t -> unit
val genarg_grammar : ('raw'glb'top) Genarg.genarg_type -> 'raw Entry.t
module Prim : sig ... end
module Constr : sig ... end
module Module : sig ... end
Type-safe grammar extension
val epsilon_value : ('a -> 'self) -> ('self_'a) Symbol.t -> 'self option
Extending the parser without synchronization
val grammar_extend : 'a Entry.t -> 'a extend_statement -> unit

Extend the grammar of Rocq, without synchronizing it with the backtracking mechanism. This means that grammar extensions defined this way will survive an undo.

Extending the parser with summary-synchronized commands

Auxiliary state of the grammar. Not marshallable.

val gramstate : unit -> GramState.t
Extension with parsing rules
type 'a grammar_command

Type of synchronized parsing extensions. The 'a type should be marshallable.

type extend_rule =
| ExtendRule : 'a Entry.t * 'a extend_statement -> extend_rule
type 'a grammar_extension = {
gext_fun : 'a -> GramState.t -> extend_rule list * GramState.t;
gext_eq : 'a -> 'a -> bool;
}

Grammar extension entry point. Given some 'a and a current grammar state, such a function must produce the list of grammar extensions that will be applied in the same order and kept synchronized w.r.t. the summary, together with a new state. It should be pure.

val create_grammar_command : string -> 'a grammar_extension -> 'a grammar_command

Create a new grammar-modifying command with the given name. The extension function is called to generate the rules for a given data.

val extend_grammar_command : 'a grammar_command -> 'a -> unit

Extend the grammar of Rocq with the given data.

Extension with parsing entries
type ('a, 'b) entry_extension = {
eext_fun : 'a -> 'b Entry.t -> GramState.t -> GramState.t;
eext_name : 'a -> string;
eext_eq : 'a -> 'a -> bool;
}

Used to generate a 'b Entry.t from a 'a. 'a must be marshallable.

type ('a, 'b) entry_command

Type of synchronized entry creation.

val create_entry_command : string -> ('a'b) entry_extension -> ('a'b) entry_command

Create a new entry-creating command with the given name.

val extend_entry_command : ('a'b) entry_command -> 'a -> 'b Entry.t

Create a new synchronized entry.

Registering grammars by name

val register_grammars_by_name : string -> Entry.any_t list -> unit
val find_grammars_by_name : string -> Entry.any_t list
Protection w.r.t. backtrack
val with_grammar_rule_protection : ('a -> 'b) -> 'a -> 'b
type frozen_t
val parser_summary_tag : frozen_t Summary.Dyn.tag
val freeze : unit -> frozen_t

Parsing state handling

val unfreeze : frozen_t -> unit
val unfreeze_only_keywords : frozen_t -> unit

for ssr hack

val get_keyword_state : unit -> CLexer.keyword_state
val modify_keyword_state : (CLexer.keyword_state -> CLexer.keyword_state) -> unit