| 1 | # Parsing |
| 2 | |
| 3 | Rocq's parser is based on Camlp5 using an extensible grammar. Somewhat helpful |
| 4 | Camlp5 documentation is available [here](http://camlp5.github.io/doc/htmlc/grammars.html). |
| 5 | However, the Camlp5 code has been copied into the Rocq source tree and may differ |
| 6 | from the Camlp5 release. |
| 7 | |
| 8 | Notable attributes of the parser include: |
| 9 | |
| 10 | * The grammar is extensible at run time. This is essential for supporting notations |
| 11 | and optionally-loaded plugins that extend the grammar. |
| 12 | |
| 13 | * The grammar is split into multiple source files. Nonterminals can be local to a file |
| 14 | or global. |
| 15 | |
| 16 | * While 95% of the nonterminals and almost all the productions are defined in the grammar, |
| 17 | a few are defined directly in OCaml code. Since many developers have worked on the parser |
| 18 | over the years, this code can be idiosyncratic, reflecting various coding styles. |
| 19 | |
| 20 | * The parser is a recursive descent parser that, by default, only looks at the next token |
| 21 | to make a parsing decision. It's possible to hand-code additional lookahead where |
| 22 | necessary by writing OCaml code. |
| 23 | |
| 24 | * There's no code that checks whether a grammar is ambiguous or whether every production |
| 25 | can be recognized. Developers who modify the grammar may, in some cases, need to structure their |
| 26 | added productions in specific ways to ensure that their additions are parsable and that they |
| 27 | don't break existing productions. |
| 28 | |
| 29 | ## Contents ## |
| 30 | |
| 31 | - [Vocabulary](#vocabulary) |
| 32 | - [Grammars: `*.mlg` File Structure](#grammars-mlg-file-structure) |
| 33 | - [Grammars: Nonterminals and Productions](#grammars-nonterminals-and-productions) |
| 34 | - [Alternate production syntax](#alternate-production-syntax) |
| 35 | - [Usage notes](#usage-notes) |
| 36 | - [Other components](#other-components) |
| 37 | - [Parsing productions](#parsing-productions) |
| 38 | - [Lookahead](#lookahead) |
| 39 | |
| 40 | ## Vocabulary |
| 41 | |
| 42 | - *entry*: a nonterminal. Entries are values of type |
| 43 | `'a Procq.Entry.t` (`Entry.t` in the rest of this document), |
| 44 | where `'a` is the type of the parsed value. |
| 45 | For instance `Procq.Prim.qualid : qualid Entry.t` parses qualified identifiers ("qualid"). |
| 46 | |
| 47 | - *argument*: a nonterminal with associated functions (e.g. how to |
| 48 | print its values). Arguments are values of type |
| 49 | `('raw,'glb,'top) Genarg.genarg_type`, typically named `wit_foo`, |
| 50 | with an associated entry of type `'raw Entry.t` typically named `foo`. |
| 51 | |
| 52 | ## Grammars: `*.mlg` File Structure ## |
| 53 | |
| 54 | Grammars are defined in `*.mlg` files, which `rocq preprocess-mlg` compiles into `*.ml` files at build time. |
| 55 | `preprocess-mlg` code is in the `coqpp` directory. `coqpp` uses yacc and lex to parse the grammar files. |
| 56 | You can examine its yacc and lex input files in `coqpp_lex.mll` and `coqpp_parse.mly` for |
| 57 | details not fully covered here. |
| 58 | |
| 59 | In addition, there is a `doc_grammar` build utility that uses the `coqpp` parser to extract the |
| 60 | grammar, then edits and inserts it into the documentation. This is described in |
| 61 | [`doc/tools/docgram/README.md`](../../doc/tools/docgram/README.md). |
| 62 | `doc_grammar` generates |
| 63 | [`doc/tools/docgram/fullGrammar`](../../doc/tools/docgram/fullGrammar), |
| 64 | which has the full grammar for Rocq |
| 65 | (not including some optionally-loaded plugins). This may be easier to read since everything is |
| 66 | in one file and the parser action routines and other OCaml code are omitted. |
| 67 | |
| 68 | `*.mlg` files contain the following types of nodes (See `node` in the yacc grammar). This part is |
| 69 | very specific to Rocq (not so similar to Camlp5). |
| 70 | |
| 71 | Note that you can reverse engineer many details by comparing the `.mlg` input file with |
| 72 | the `.ml` generated by `coqpp`. |
| 73 | |
| 74 | ### OCaml code |
| 75 | |
| 76 | OCaml code enclosed in curly braces, which is copied verbatim to the generated `*.ml` file |
| 77 | |
| 78 | ### Comments |
| 79 | |
| 80 | Comments in the `*.mlg` file are in the form `(* … *)`. They are not copied |
| 81 | to the generated `*.ml` file. Comments in OCaml code are preserved. |
| 82 | |
| 83 | ### DECLARE PLUGIN |
| 84 | |
| 85 | `DECLARE PLUGIN "plugin"` - associates the file with a specific |
| 86 | plugin, for example "rocq-runtime.plugins.ltac". The string is the |
| 87 | plugin's ocamlfind name (also used to load it in the `.v` file with |
| 88 | `Declare ML Module`). |
| 89 | |
| 90 | ### GRAMMAR EXTEND |
| 91 | |
| 92 | `GRAMMAR EXTEND` - adds additional entries and productions to the grammar and declares global |
| 93 | entries referenced in the `GRAMMAR EXTEND`: |
| 94 | |
| 95 | ``` |
| 96 | GRAMMAR EXTEND Gram |
| 97 | GLOBAL: |
| 98 | bignat bigint …; |
| 99 | <nonterminal definitions> |
| 100 | END |
| 101 | ``` |
| 102 | |
| 103 | Global entries should be available in the current OCaml scope, |
| 104 | eg `open Procq.Prim` makes `qualid` available. (qualified entry names are not supported) |
| 105 | |
| 106 | `GRAMMAR EXTEND` should be used only for large syntax additions. To add new commands |
| 107 | and tactics, use these instead: |
| 108 | |
| 109 | - `VERNAC COMMAND EXTEND` (and variant `VERNAC { entry } EXTEND`) to add new commands |
| 110 | - `TACTIC EXTEND` to add new ltac1 tactics |
| 111 | - `ARGUMENT EXTEND` to add new arguments for `TACTIC EXTEND` and `VERNAC EXTEND` |
| 112 | - `VERNAC ARGUMENT EXTEND` to add new arguments for `VERNAC EXTEND` |
| 113 | |
| 114 | These constructs provide essential semantic information that's provided in a more complex, |
| 115 | less readable way with `GRAMMAR EXTEND`. |
| 116 | |
| 117 | ### VERNAC EXTEND |
| 118 | |
| 119 | `VERNAC COMMAND EXTEND` - adds new command syntax by adding productions to the |
| 120 | `command` entry. |
| 121 | |
| 122 | `VERNAC { entry } EXTEND` - adds new command syntax to the `entry` entry. |
| 123 | This is typically used for proof modes (see also `Pvernac.register_proof_mode` and the user doc for proof modes). |
| 124 | |
| 125 | Simple example: |
| 126 | |
| 127 | ``` |
| 128 | VERNAC COMMAND EXTEND CmdName CLASSIFIED AS SIDEFF |
| 129 | | [ "Cmd" arg(a) ] -> { do_interp a } |
| 130 | END |
| 131 | ``` |
| 132 | defines a command composed of the string `Cmd` followed by argument `wit_arg`. |
| 133 | Running the command calls `do_interp a` where `a` is the result of parsing `arg`. |
| 134 | |
| 135 | Example with all optional elements: |
| 136 | ``` |
| 137 | VERNAC COMMAND EXTEND CmdName CLASSIFIED AS SIDEFF |
| 138 | | #[ attr ] ![ state ] [ "Cmd" arg(a) ] |
| 139 | => { rule_classif a } |
| 140 | SYNTERP AS synv { do_synterp attr a } |
| 141 | -> { do_interp attr a synv } |
| 142 | END |
| 143 | ``` |
| 144 | |
| 145 | Productions here are represented with [alternate syntax](#alternate-production-syntax), described later. |
| 146 | |
| 147 | Nonterminals in `VERNAC EXTEND` parsing rules (e.g. `arg` in the example) |
| 148 | are arguments, possibly with [nonterminal modifiers](#nonterminal-modifiers). |
| 149 | The name prefixed by `wit_` must be available in the current OCaml scope. |
| 150 | |
| 151 | New commands should be added using this construct rather than `GRAMMAR EXTEND` so |
| 152 | they are correctly registered, such as having the correct command classifier. |
| 153 | |
| 154 | #### Extend name |
| 155 | |
| 156 | Each `VERNAC EXTEND` must have a unique name ("CmdName" in the example) |
| 157 | (unique for the plugin, you will get an anomaly at runtime if it's |
| 158 | not). |
| 159 | |
| 160 | #### Classification |
| 161 | |
| 162 | Commands must be "classified" for the STM. |
| 163 | Classifying a command is generating a |
| 164 | `Vernacextend.vernac_classification` (see comments on its |
| 165 | declaration for the meaning of the different values). |
| 166 | This is done by appending one of the following annotations to the `VERNAC COMMAND EXTEND CmdName` grammar extension: |
| 167 | - `CLASSIFIED AS QUERY` for `VtQuery` |
| 168 | - `CLASSIFIED AS SIDEFF` for `VtSideff ([], VtLater)` (most commands are in this case) |
| 169 | - `CLASSIFIED BY { code }` where code is the classification, when it is static |
| 170 | - adding `=> { code }` after the parsing rule (before its |
| 171 | interpretation), where `code` is the classification and has access |
| 172 | to argument values. |
| 173 | |
| 174 | Used when the classification is not the same for the whole block |
| 175 | or depends on the parsed values |
| 176 | (eg `| [ "foo" arg(x) ] => { classify x } -> { interpret x }`). |
| 177 | |
| 178 | Per-rule classification overrides block level classification. |
| 179 | Block classification should be omitted (it will be ignored) |
| 180 | if all rules have their own classification. |
| 181 | |
| 182 | #### State access |
| 183 | |
| 184 | Commands must explicitly declare when they use some of the global |
| 185 | state: the current proof state, program obligations state, and |
| 186 | bodies of `Qed` proofs. This is done by adding a `STATE state` |
| 187 | specifier for the whole block, or overriding it per-rule with `![ state ]` |
| 188 | before the parsing rule (after attributes if any). |
| 189 | |
| 190 | `preprocess-mlg` associates the state specifier with combinators in |
| 191 | `Vernactypes`, and the interpretation must have the type expected by |
| 192 | the combinator (except when it expects a thunk, in which case thunking is implicit). |
| 193 | |
| 194 | The following state specifiers |
| 195 | are understood (see `Coqpp_main.understand_state` if this isn't up to date): |
| 196 | - `close_proof` for `vtcloseproof`, note that closing proofs from plugin commands is buggy. |
| 197 | - `open_proof` for `vtopenproof`. |
| 198 | - `proof` for `vtmodifyproof` |
| 199 | - `proof_opt_query` for `vtreadproofopt` |
| 200 | - `proof_query` for `vtreadproof` |
| 201 | - `read_program` for `vtreadprogram` |
| 202 | - `program` for `vtmodifyprogram` |
| 203 | - `declare_program` for `vtdeclareprogram` |
| 204 | - `program_interactive` for `vtopenproofprogram` |
| 205 | - `opaque_access` for `vtopaqueaccess` |
| 206 | |
| 207 | So for instance `open_proof` requires the interpretation to have type `Declare.Proof.t` |
| 208 | (implicitly thunked), |
| 209 | `proof` requires it to have type `pstate:Declare.Proof.t -> Declare.Proof.t`. |
| 210 | |
| 211 | #### Attributes |
| 212 | |
| 213 | By default commands support no attributes (except for the general |
| 214 | attributes like `#[warnings]`). Attributes may be supported by |
| 215 | adding the list of supported attributes `#[ x = a; y = b ]` before |
| 216 | the parsing rule. `x` and `y` will be bound to the parsed attribute |
| 217 | in the interpretation, `a` and `b` are qualified identifiers of type |
| 218 | `'att Attributes.attribute`. Punning `#[ x ]` for `#[ x = x ]` is supported. |
| 219 | You can delay attribute interpretation (e.g. if it depends on the command arguments) |
| 220 | by using `Attributes.raw_attributes`. |
| 221 | |
| 222 | #### Synterp |
| 223 | |
| 224 | If the command modifies the parser or modifies state which may be |
| 225 | used to change the parser in a later command, these operations must |
| 226 | be separated from the main interpretation and done in the "synterp" |
| 227 | phase. The "synterp" phase does not have access to the "interp" |
| 228 | state (which notably includes the global env). |
| 229 | |
| 230 | This is done with `SYNTERP AS synv { synterp }`: `synterp` will be |
| 231 | evaluated in the synterp phase with the command arguments and attributes bound, |
| 232 | and its result will be bound to `synv` for the interp phase. |
| 233 | |
| 234 | ### TACTIC EXTEND |
| 235 | |
| 236 | `TACTIC EXTEND` - adds new tactic (Ltac1) syntax by adding productions to `simple_tactic`. |
| 237 | For example: |
| 238 | |
| 239 | ``` |
| 240 | TACTIC EXTEND field_lookup |
| 241 | | [ "field_lookup" tactic(f) "[" constr_list(lH) "]" ne_constr_list(lt) ] -> |
| 242 | { let (t,l) = List.sep_last lt in field_lookup f lH l t } |
| 243 | END |
| 244 | ``` |
| 245 | |
| 246 | adds a new tactic `field_lookup`. |
| 247 | |
| 248 | New tactics should be added using this construct rather than `GRAMMAR EXTEND`. |
| 249 | |
| 250 | Nonterminals in `VERNAC EXTEND` parsing rules |
| 251 | (eg `tactic`, `constr_list`, `ne_constr_list` in the example) |
| 252 | are arguments, possibly with [nonterminal modifiers](#nonterminal-modifiers). |
| 253 | The name prefixed by `wit_` must be available in the current OCaml scope. |
| 254 | |
| 255 | TODO: explain DEPRECATED, LEVEL (not shown) |
| 256 | |
| 257 | ### ARGUMENT EXTEND |
| 258 | |
| 259 | `ARGUMENT EXTEND` - defines a new argument which can be used in `TACTIC EXTEND` and `VERNAC EXTEND` |
| 260 | |
| 261 | ``` |
| 262 | ARGUMENT EXTEND ast_closure_term |
| 263 | TYPED AS type_info |
| 264 | PRINTED BY { pp_ast_closure_term } |
| 265 | INTERPRETED BY { interp_ast_closure_term } |
| 266 | GLOBALIZED BY { glob_ast_closure_term } |
| 267 | SUBSTITUTED BY { subst_ast_closure_term } |
| 268 | RAW_PRINTED BY { pp_ast_closure_term } |
| 269 | GLOB_PRINTED BY { pp_ast_closure_term } |
| 270 | | [ term_annotation(a) constr(c) ] -> { mk_ast_closure_term a c } |
| 271 | END |
| 272 | ``` |
| 273 | |
| 274 | See comments in `tacentries.mli` for partial information on the various |
| 275 | arguments. |
| 276 | |
| 277 | Nonterminals in `ARGUMENT EXTEND` parsing rules (eg `term_annotation` and `constr` in the example) |
| 278 | are entry names, possibly with [nonterminal modifiers](#nonterminal-modifiers). |
| 279 | |
| 280 | ### VERNAC ARGUMENT EXTEND |
| 281 | |
| 282 | `VERNAC ARGUMENT EXTEND` - (part of `argument_extend` in the yacc grammar) defines |
| 283 | productions for a single nonterminal which can be used in `VERNAC EXTEND`. For example: |
| 284 | |
| 285 | ``` |
| 286 | VERNAC ARGUMENT EXTEND ring_mod |
| 287 | PRINTED BY { pr_ring_mod env sigma } |
| 288 | | [ "decidable" constr(eq_test) ] -> { Ring_kind(Computational eq_test) } |
| 289 | | [ "abstract" ] -> { Ring_kind Abstract } |
| 290 | | [ "morphism" constr(morph) ] -> { Ring_kind(Morphism morph) } |
| 291 | | [ "constants" "[" tactic(cst_tac) "]" ] -> { Const_tac(CstTac cst_tac) } |
| 292 | | [ "closed" "[" ne_global_list(l) "]" ] -> { Const_tac(Closed l) } |
| 293 | | [ "preprocess" "[" tactic(pre) "]" ] -> { Pre_tac pre } |
| 294 | | [ "postprocess" "[" tactic(post) "]" ] -> { Post_tac post } |
| 295 | | [ "setoid" constr(sth) constr(ext) ] -> { Setoid(sth,ext) } |
| 296 | | [ "sign" constr(sign_spec) ] -> { Sign_spec sign_spec } |
| 297 | | [ "power" constr(pow_spec) "[" ne_global_list(l) "]" ] -> |
| 298 | { Pow_spec (Closed l, pow_spec) } |
| 299 | | [ "power_tac" constr(pow_spec) "[" tactic(cst_tac) "]" ] -> |
| 300 | { Pow_spec (CstTac cst_tac, pow_spec) } |
| 301 | | [ "div" constr(div_spec) ] -> { Div_spec div_spec } |
| 302 | END |
| 303 | ``` |
| 304 | |
| 305 | `PRINTED BY` specifies how the argument should be printed (used by |
| 306 | `-beautify`, `-time`, and various debug printers). |
| 307 | The `PRINTED BY` code has variables `env : Environ.env` and `sigma : Evd.evar_map` bound. |
| 308 | |
| 309 | Nonterminals in `VERNAC ARGUMENT EXTEND` (e.g. `constr`, `tactic`, `ne_global_list` in the example) |
| 310 | are entry names, possibly with [nonterminal modifiers](#nonterminal-modifiers). |
| 311 | |
| 312 | ### DOC_GRAMMAR |
| 313 | |
| 314 | `DOC_GRAMMAR` - Used in `doc_grammar`-generated files to permit simplified syntax |
| 315 | |
| 316 | ## Grammars: Nonterminals and Productions |
| 317 | |
| 318 | Here's a simple nonterminal definition in the Camlp5 format: |
| 319 | |
| 320 | ``` |
| 321 | universe: |
| 322 | [ [ IDENT "max"; "("; ids = LIST1 universe_expr SEP ","; ")" -> { ids } |
| 323 | | u = universe_expr -> { [u] } ] ] |
| 324 | ; |
| 325 | ``` |
| 326 | |
| 327 | In which: |
| 328 | * `universe` is the nonterminal being defined |
| 329 | * productions are separated by `|` and, as a group, are enclosed in `[ [ … ] ];` |
| 330 | * `u = universe_expr` refers to the `universe_expr` nonterminal. `u` is bound to |
| 331 | the value returned by that nonterminal's action routine, which can be |
| 332 | referred to in the action routine. For `ids = LIST1 universe_expr SEP ","`, |
| 333 | `ids` is bound to the list of values returned by `universe_expr`. |
| 334 | * `-> { … }` contains the OCaml action routine, which is executed when the production is recognized |
| 335 | and returns a value |
| 336 | * Semicolons separate adjacent grammatical elements (nonterminals, strings or other constructs) |
| 337 | |
| 338 | Grammatical elements that appear in productions are: |
| 339 | |
| 340 | - nonterminal names - identifiers in the form `[a-zA-Z0-9_]*`. These correspond to variables in |
| 341 | the generated `.ml` code. In some cases a qualified name, such as `Prim.name`, is used. |
| 342 | - `"…"` - a literal string that becomes a keyword and cannot be used as an `ident`. |
| 343 | The string doesn't have to be a valid identifier; frequently the string will contain only |
| 344 | punctuation characters. Generally we try to avoid adding new keywords that are also valid |
| 345 | identifiers--though there is an unresolved debate among the developers about whether having more |
| 346 | such keywords in general is good (e.g. it makes it easier to highlight keywords in GUIs) |
| 347 | or bad (more keywords for the user to avoid and new keywords may require changes to existing |
| 348 | proof files). |
| 349 | - `IDENT "…"` - a literal string that has the form of an `ident` that doesn't become |
| 350 | a keyword |
| 351 | - `OPT element` - optionally include `element` (e.g. a nonterminal, IDENT "…" or "…"). |
| 352 | The value is of type `'a option`. |
| 353 | - `LIST1 element` - a list of one or more `element`s. The value is of type `'a list`. |
| 354 | - `LIST0 element` - an optional list of `element`s |
| 355 | - `LIST1 element SEP sep` - a list of `element`s separated by `sep` |
| 356 | - `LIST0 element SEP sep` - an optional list of `element`s separated by `sep` |
| 357 | - `( elements )` - grouping to represent a series of elements as a unit, |
| 358 | useful within `OPT` and `LIST*`. |
| 359 | - `[ elements1 | elements2 | … ]` - alternatives (either `elements1` or `elements2` or …), |
| 360 | actually nested productions, each of which can have its own action routines |
| 361 | |
| 362 | Nonterminals can also be defined with multiple levels to specify precedence and associativity |
| 363 | of its productions. This is described in the Rocq documentation under the `Print Grammar` |
| 364 | command. The first square bracket around a nonterminal definition is for grouping |
| 365 | level definitions, which are separated with `|`, for example: |
| 366 | |
| 367 | ``` |
| 368 | ltac_expr: |
| 369 | [ "5" RIGHTA |
| 370 | [ te = binder_tactic -> { te } ] |
| 371 | | "4" LEFTA |
| 372 | : |
| 373 | ``` |
| 374 | |
| 375 | Grammar extensions can specify what level they are modifying, for example: |
| 376 | |
| 377 | ``` |
| 378 | ltac_expr: LEVEL "1" [ RIGHTA |
| 379 | [ tac = ltac_expr; intros = ssrintros_ne -> { tclintros_expr ~loc tac intros } |
| 380 | ] ]; |
| 381 | ``` |
| 382 | |
| 383 | ### Alternate production syntax ### |
| 384 | |
| 385 | Except for `GRAMMAR EXTEND`, the `EXTEND` nodes in the `*.mlg`s use simplified syntax in |
| 386 | productions that's similar to what's used in the `Tactic Notation` and |
| 387 | `Ltac2 Notation` commands. For example: |
| 388 | |
| 389 | ``` |
| 390 | TACTIC EXTEND cc |
| 391 | | [ "congruence" ] -> { congruence_tac 1000 [] } |
| 392 | | [ "congruence" integer(n) ] -> { congruence_tac n [] } |
| 393 | | [ "congruence" "with" ne_constr_list(l) ] -> { congruence_tac 1000 l } |
| 394 | | [ "congruence" integer(n) "with" ne_constr_list(l) ] -> |
| 395 | { congruence_tac n l } |
| 396 | END |
| 397 | ``` |
| 398 | |
| 399 | For `VERNAC EXTEND` and `TACTIC EXTEND`, nonterminals appearing in the |
| 400 | alternate production syntax are "arguments" (`Genarg.genarg_type`) accessed by prefixing the name with `wit_`. Some commonly used arguments are defined in |
| 401 | `stdarg.ml`. Others are defined in the code generated by `ARGUMENT EXTEND` |
| 402 | and `VERNAC ARGUMENT EXTEND` constructs. References to |
| 403 | nonterminals that don't have `wit_*` symbols cause compilation errors. |
| 404 | |
| 405 | The differences are: |
| 406 | * The outer `: [ … ];` is omitted. Each production is enclosed in `| [ … ]`. |
| 407 | * The action routine is outside the square brackets |
| 408 | * Literal strings that are valid identifiers don't become reserved keywords |
| 409 | * No semicolons separating elements of the production |
| 410 | * `integer(n)` is used to bind a nonterminal value to a variable instead of `n = integer` |
| 411 | * [nonterminal modifiers](#nonterminal-modifiers) are used |
| 412 | * There's no way to define `LEVEL`s |
| 413 | * There's no equivalent to `( elements )` or `[ elements1 | elements2 | … ]`, which may |
| 414 | require repeating similar syntax several times. For example, this single production |
| 415 | is equivalent to 8 productions in `TACTIC EXTEND` representing all possible expansions of |
| 416 | three `OPT`s: |
| 417 | |
| 418 | ``` |
| 419 | | IDENT "Add"; IDENT "Parametric"; IDENT "Relation"; LIST0 binder; ":"; constr; constr; |
| 420 | OPT [ IDENT "reflexivity"; IDENT "proved"; IDENT "by"; constr -> { … } ]; |
| 421 | OPT [ IDENT "symmetry"; IDENT "proved"; IDENT "by"; constr -> { … } ]; |
| 422 | OPT [ IDENT "transitivity"; IDENT "proved"; IDENT "by"; constr -> { … } ]; |
| 423 | IDENT "as"; ident -> { … } |
| 424 | ``` |
| 425 | |
| 426 | ### Nonterminal modifiers |
| 427 | |
| 428 | In the alternate syntax nonterminal names may be mangled by adding |
| 429 | prefixes and suffixes to parse lists and options: |
| 430 | |
| 431 | * `ne_entry_list` for `LIST1 entry` |
| 432 | * `entry_list` for `LIST0 entry` |
| 433 | * `ne_entry_list_sep(var, sep)` for `LIST1 entry SEP sep` where the list is bound to `var` |
| 434 | * `entry_list_sep(var, sep)` for `LIST0 entry SEP sep` where the list is bound to `var` |
| 435 | * `entry_opt` for `OPT entry` |
| 436 | |
| 437 | This works regardless of whether the nonterminal is an entry or an |
| 438 | argument. The demangled entry or argument name must be available in |
| 439 | the current OCaml scope (with `wit_` prefix for arguments). |
| 440 | |
| 441 | ## Usage notes |
| 442 | |
| 443 | ### Other components |
| 444 | |
| 445 | Rocq's lexer is in `clexer.ml`. Its 10 token types are defined in `tok.ml`. |
| 446 | |
| 447 | The parser is in `grammar.ml`. The extensive use of GADT (generalized algebraic datatypes) |
| 448 | makes it harder for the uninitiated to understand it. |
| 449 | |
| 450 | When the parser is invoked, the call tells the parser which nonterminal to parse. `vernac_control` |
| 451 | is the start symbol for commands. `tactic_mode` is the start symbol for tactics. |
| 452 | Tactics give syntax errors if Rocq is not in proof mode. There are additional details |
| 453 | not mentioned here. |
| 454 | |
| 455 | ### Parsing productions |
| 456 | |
| 457 | Some thoughts, not to be taken as identifying all the issues: |
| 458 | |
| 459 | Since the parser examines only the next token to make a parsing decision (and perhaps |
| 460 | because of other potentially fixable limitations), some productions have to be ordered |
| 461 | or structured in a particular way to parse correctly in all cases. |
| 462 | |
| 463 | For example, consider these productions: |
| 464 | |
| 465 | ``` |
| 466 | command: [ [ |
| 467 | | IDENT "Print"; p = printable -> { VernacPrint p } |
| 468 | | IDENT "Print"; qid = smart_global; l = OPT univ_name_list -> { VernacPrint (PrintName (qid,l)) } |
| 469 | | IDENT "Print"; IDENT "Module"; "Type"; qid = global -> |
| 470 | { VernacPrint (PrintModuleType qid) } |
| 471 | | IDENT "Print"; IDENT "Module"; qid = global -> |
| 472 | { VernacPrint (PrintModule qid) } |
| 473 | | IDENT "Print"; IDENT "Namespace" ; ns = dirpath -> |
| 474 | { VernacPrint (PrintNamespace ns) } |
| 475 | : |
| 476 | |
| 477 | printable: |
| 478 | [ [ IDENT "Term"; qid = smart_global; l = OPT univ_name_list -> { PrintName (qid,l) } |
| 479 | | IDENT "All" -> { PrintFullContext } |
| 480 | | IDENT "Section"; s = global -> { PrintSectionContext s } |
| 481 | : |
| 482 | ``` |
| 483 | |
| 484 | Reversing the order of the first two productions in `command` causes the `All` in `Print All` to |
| 485 | be parsed incorrectly as a `smart_global`, making that command unavailable. `Print Namespace nat.` |
| 486 | still works correctly, though. |
| 487 | |
| 488 | Similarly, the production for `Print Module Type` has to appear before `Print Module <global>` |
| 489 | in order to be reachable. |
| 490 | |
| 491 | Internally, the parser generates a tree that represents the possible prefixes for the |
| 492 | productions of a nonterminal as described in |
| 493 | [the Camlp5 documentation](http://camlp5.github.io/doc/htmlc/grammars.html#b:Rules-insertion). |
| 494 | |
| 495 | Here's another example in which the way the productions are written matters. `OPT` at |
| 496 | the beginning of a production doesn't always work well: |
| 497 | |
| 498 | ``` |
| 499 | command: [ [ |
| 500 | | IDENT "Foo"; n = natural -> { VernacBack 1 } |
| 501 | | OPT (IDENT "ZZ"); IDENT "Foo" -> { VernacBack 1 } |
| 502 | : |
| 503 | ``` |
| 504 | |
| 505 | `Foo.` looks like it should be accepted, but it gives a parse error: |
| 506 | |
| 507 | ``` |
| 508 | Unnamed_thm < Foo. |
| 509 | Toplevel input, characters 3-4: |
| 510 | > Foo. |
| 511 | > ^ |
| 512 | Error: |
| 513 | Syntax error: [prim:natural] expected after 'Foo' (in [vernac:command]). |
| 514 | ``` |
| 515 | |
| 516 | Reversing the order of the productions doesn't help, but splitting |
| 517 | the 'OPT' production into 2 productions works: |
| 518 | |
| 519 | ``` |
| 520 | | IDENT "Foo" -> { VernacBack 1 } |
| 521 | | IDENT "ZZ"; IDENT "Foo" -> { VernacBack 1 } |
| 522 | | IDENT "Foo"; n = natural -> { VernacBack 1 } |
| 523 | |
| 524 | ``` |
| 525 | |
| 526 | On the other hand, `OPT` works just fine when the parser has already found the |
| 527 | right production. For example `Back` and `Back <natural>` can be combined using |
| 528 | an `OPT`: |
| 529 | |
| 530 | ``` |
| 531 | | IDENT "Back"; n = OPT natural -> { VernacBack (Option.default 1 n) } |
| 532 | ``` |
| 533 | |
| 534 | ### Lookahead |
| 535 | |
| 536 | It's possible to look ahead more than one symbol using OCaml code. Generally we |
| 537 | avoid doing this unless there's a strong reason to do so. For example, this |
| 538 | code defines a new nonterminal `local_test_lpar_id_colon` that checks that |
| 539 | the next 3 tokens are `"("` `ident` and `":"` without consuming any input: |
| 540 | |
| 541 | ``` |
| 542 | let local_test_lpar_id_colon = |
| 543 | let open Procq.Lookahead in |
| 544 | to_entry "lpar_id_colon" begin |
| 545 | lk_kw "(" >> lk_ident >> lk_kw ":" |
| 546 | end |
| 547 | ``` |
| 548 | |
| 549 | This one checks that the next 2 tokens are `"["` and `"|"` with no space between. |
| 550 | This is a special case: intropatterns can have sequences like `"[|]"` that are |
| 551 | 3 different tokens with empty nonterminals between them. Making `"[|"` a keyword |
| 552 | would break existing code with "[|]": |
| 553 | |
| 554 | ``` |
| 555 | let test_array_opening = |
| 556 | let open Procq.Lookahead in |
| 557 | to_entry "test_array_opening" begin |
| 558 | lk_kw "[" >> lk_kw "|" >> check_no_space |
| 559 | end |
| 560 | ``` |
| 561 | |
| 562 | TODO: how to add a tactic or command |