| 1 | # Grammar extraction tool for documentation |
| 2 | |
| 3 | `doc_grammar` extracts Coq's grammar from `.mlg` files, edits it and inserts it |
| 4 | into `.rst` files. The tool inserts `prodn` directives for grammar productions. |
| 5 | It also updates `tacn` and `cmd` directives when they can be unambiguously matched to |
| 6 | productions of the grammar (in practice, that's probably almost always). |
| 7 | `tacv` and `cmdv` directives are not updated because matching them appears to require |
| 8 | human judgement. `doc_grammar` generates a few files that may be useful to |
| 9 | developers and documenters. |
| 10 | |
| 11 | The mlg grammars present several challenges to generating an accurate grammar |
| 12 | for documentation purposes: |
| 13 | |
| 14 | * The 30+ mlg files don't define an overall order in which nonterminals should |
| 15 | appear in a complete grammar. |
| 16 | |
| 17 | * Even within a single mlg file, nonterminals and productions are often given |
| 18 | in an order that's much different from what a reader of the documentation would |
| 19 | expect. In a small number of cases, changing the order in the mlg would change |
| 20 | how some inputs are parsed, in particular when the order determines how to distinguish |
| 21 | otherwise ambiguous inputs. |
| 22 | |
| 23 | Strictly speaking, that means our grammar is not a context free grammar even though |
| 24 | we gloss over that distinction in the documentation. |
| 25 | |
| 26 | * For a few nonterminals, some productions are only available if certain plugins |
| 27 | are activated (e.g. SSR). Readers should be informed about these. |
| 28 | |
| 29 | * Some limited parts of the grammar are defined in OCaml, including lookahead symbols |
| 30 | like `test_bracket_ident` and references to nonterminals in other files using |
| 31 | qualified names such as `Prim.ident`. A few symbols are defined multiple times, |
| 32 | such as `scope` and `orient`. |
| 33 | |
| 34 | ## What the tool does |
| 35 | |
| 36 | 1. The tool reads all the `mlg` files and generates `fullGrammar`, which includes |
| 37 | all the grammar without the actions for each production or the OCaml code. This |
| 38 | file is provided as a convenience to make it easier to examine the (mostly) |
| 39 | unprocessed grammar of the mlg files with less clutter. This step includes two |
| 40 | transformations that rename some nonterminal symbols: |
| 41 | |
| 42 | First, nonterminals that use levels (`"5" RIGHTA` below) are modified, for example: |
| 43 | |
| 44 | ``` |
| 45 | ltac_expr: |
| 46 | [ "5" RIGHTA |
| 47 | [ ... ] |
| 48 | [ "4" ... |
| 49 | ``` |
| 50 | |
| 51 | becomes |
| 52 | |
| 53 | ``` |
| 54 | tactic_expr5: [ |
| 55 | | ... |
| 56 | | tactic_expr4 |
| 57 | ] |
| 58 | ``` |
| 59 | |
| 60 | Second, nonterminals that are local to an .mlg will be renamed, if necessary, to |
| 61 | make them unique. For example, `strategy_level` is defined as a local nonterminal |
| 62 | in both `g_prim.mlg` and in `extraargs.mlg`. The nonterminal defined in the former |
| 63 | remains `strategy_level` because it happens to be processed before the latter, |
| 64 | in which the nonterminal is renamed to `EXTRAARGS_strategy_level` to make the local |
| 65 | symbol unique. |
| 66 | |
| 67 | Nonterminals listed after `GLOBAL:` are global; otherwise they are local. |
| 68 | |
| 69 | References to renamed symbols are updated with the modified names. |
| 70 | |
| 71 | Note: the 4 SSR mlgs and ssreflect-proof-language.rst are currently excluded |
| 72 | from processing (hard coded). |
| 73 | |
| 74 | 2. The tool applies grammar editing operations specified by `common.edit_mlg` to |
| 75 | generate `editedGrammar`. |
| 76 | |
| 77 | 3. `orderedGrammar` gives the desired order for nonterminals and individual productions |
| 78 | in the documented grammar. Developers should edit this file only to reorder lines. |
| 79 | `doc_grammar` updates `orderedGrammar` so it has the same set of nonterminals and productions |
| 80 | as `editedGrammar` while retaining the previous ordering. Since the position of |
| 81 | new or renamed nonterminals is unspecified, they tend to show up in the wrong |
| 82 | place in `orderedGrammar`, therefore users should review the output and make |
| 83 | appropriate adjustments to the order. |
| 84 | |
| 85 | The update process removes manually-added comments from `orderedGrammar` while |
| 86 | automatically-generated comments will be regenerated. |
| 87 | |
| 88 | 4. The tool updates the `.rst` files. Comments in the form |
| 89 | `.. insertprodn <first nt> <last nt>` indicate inserting the productions for a |
| 90 | range of nonterminals (in `orderedGrammar` order). `.. cmd::` and `.. tacn::` |
| 91 | directives are updated using |
| 92 | prefixes in the form `[a-zA-Z0-9_ ]+` from the directive and the |
| 93 | grammar. If there is unique match in the grammar, the directive is updated, if needed. |
| 94 | Multiple matches or no match gives an error message. |
| 95 | |
| 96 | 5. For reference, the tool generates `prodnGrammar`, which has the entire grammar in the form of `prodns`. |
| 97 | |
| 98 | 6. If requested by command-line arguments `-check-cmds` or `-check-tacs`, |
| 99 | the tool generates `prodnCommands` (for commands) and `prodnTactics` (for tactics). |
| 100 | The former lists all commands that are under `command` in `orderedGrammar` and |
| 101 | compares it to the `:cmd:` and `:cmdv:` given in the rst files. The latter |
| 102 | lists all tactics that are under `simple_tactic` in the grammar and compares it |
| 103 | to the `:tacn:` and `:tacv:`. The tags at the beginning of each line mean: |
| 104 | |
| 105 | - (no tag) - the grammar and the rst match exactly and uniquely |
| 106 | - `-` - a grammar production that can't be matched to an rst file entry |
| 107 | - `+` - an rst entry that doesn't match a grammar production |
| 108 | - `v` - the rst entry is a `:cmdv:` or `:tacv:` |
| 109 | - `?` - the match between the grammar and the rst files is not unique |
| 110 | |
| 111 | These command line arguments also generate error messages for commands and |
| 112 | tactics that are in the grammar but not the documentation and vice versa. |
| 113 | |
| 114 | ## How to use the tool |
| 115 | |
| 116 | * `make doc_gram` updates `fullGrammar`. |
| 117 | |
| 118 | * `make doc_gram_verify` verifies that `fullGrammar`, `orderedGrammar` and `*.rst` |
| 119 | are consistent with the `.mlg` files. This is for use by CI. |
| 120 | |
| 121 | * `make doc_gram_rsts` updates the `*Grammar` and `.rst` files. |
| 122 | |
| 123 | * `make doc_gram_rsts DOCGRAMWARN=1` will additionally print warnings. |
| 124 | |
| 125 | Changes to `fullGrammar`, `orderedGrammar` and `*.rst` should be checked in to git. |
| 126 | The `prodn*` and other `*Grammar` files should not. |
| 127 | |
| 128 | ### Command line arguments |
| 129 | |
| 130 | The executable takes a list of `.mlg` and `.rst` files as arguments. The tool |
| 131 | inserts the grammar into the `*.rst` as specified by comments in those files. |
| 132 | The order of the `.mlg` files affects the order of nonterminals and productions in |
| 133 | `fullGrammar`. The order doesn't matter for the `.rst` files. |
| 134 | |
| 135 | Specifying the `-verify` command line argument avoids updating any of the files, |
| 136 | but verifies that the current files are consistent. This setting is meant for |
| 137 | use in CI; it will be up to each developer to include the changes to `*Grammar` and |
| 138 | the `.rst` files in their PRs when they've changed the grammar. |
| 139 | |
| 140 | Other command line arguments: |
| 141 | |
| 142 | * `-check-tacs` causes generation of `prodnTactics` |
| 143 | |
| 144 | * `-check-cmds` causes generation of `prodnCommands` |
| 145 | |
| 146 | * `-no-warn` suppresses printing of some warning messages |
| 147 | |
| 148 | * `-no-update` puts updates to `fullGrammar` and `orderedGrammar` into new files named |
| 149 | `*.new`, leaving the originals unmodified. For use in Dune. |
| 150 | |
| 151 | * `-short` limits processing to updating/verifying only the `fullGrammar` file |
| 152 | |
| 153 | * `-verbose` prints more messages about the grammar |
| 154 | |
| 155 | * `-verify` described above |
| 156 | |
| 157 | ### Grammar editing scripts |
| 158 | |
| 159 | The grammar editing script `common.edit_mlg` is similar in format to `.mlg` files but stripped |
| 160 | of all OCaml features. This is an easy way to include productions to match or add without |
| 161 | writing another parser. The `DOC_GRAMMAR` token at the beginning of each file |
| 162 | signals the use of the streamlined syntax. |
| 163 | |
| 164 | The edit file has a series of items in the form of productions. Items are applied |
| 165 | in the order they appear. There are two types of editing operations: |
| 166 | |
| 167 | * Global edits - edit rules that apply to the entire grammar in a single operation. |
| 168 | These are identified by using specific reserved names as the non-terminal name. |
| 169 | |
| 170 | * Local edits - edit rules that apply to the productions of a single non-terminal. |
| 171 | The rule is a local edit if the non-terminal name isn't reserved. Individual |
| 172 | productions within a local edit that begin with a different set of reserved names |
| 173 | edit existing productions. For example `binders: [ | DELETE Procq.Constr.binders ]` |
| 174 | deletes the production `binders: [ | Procq.Constr.binders]` |
| 175 | |
| 176 | Productions that don't begin with a reserved name are added to the grammar, |
| 177 | such as `empty: [ | ]`, which adds a new non-terminal `empty` with an |
| 178 | empty production on the right-hand side. |
| 179 | |
| 180 | Another example: `LEFTQMARK: [ | "?" ]` is a local edit that treats `LEFTQMARK` as |
| 181 | the name of a non-terminal and adds a production for it. (We know that LEFTQMARK |
| 182 | is a token but doc_grammar does not.) `SPLICE: [ | LEFTQMARK ]` requests replacing all |
| 183 | uses of `LEFTQMARK` anywhere in the grammar with its productions and removing the |
| 184 | non-terminal. The combined effect of these two is to replace all uses of |
| 185 | `LEFTQMARK` with `"?"`. |
| 186 | |
| 187 | |
| 188 | |
| 189 | Here are the current operations: |
| 190 | |
| 191 | ### Global edits |
| 192 | |
| 193 | `DELETE` - deletes the specified non-terminals anywhere in the grammar. Each |
| 194 | should appear as a separate production. Useful for removing non-terminals that |
| 195 | only do lookahead that shouldn't be in the documentation. |
| 196 | |
| 197 | `RENAME` - each production specifies an (old name, new name) pair of |
| 198 | non-terminals to rename. |
| 199 | |
| 200 | `SPLICE` - requests replacing all uses of the nonterminals anywhere in the |
| 201 | grammar with its productions and removing the non-terminal. Each should appear |
| 202 | as a separate production. (Doesn't work recursively; splicing for both |
| 203 | `A: [ | B ]` and `B: [ | C ]` must be done in separate SPLICE operations.) |
| 204 | |
| 205 | `OPTINREF` - applies the local `OPTINREF` edit to every nonterminal |
| 206 | |
| 207 | `REACHABLE` - suppresses the "Unreachable symbol" warning for the listed nonterminals |
| 208 | and any symbols reachable from them. |
| 209 | |
| 210 | `NOTINRSTS` - suppresses the "Nonterminal <NT> not included in .rst files" messages for |
| 211 | the listed nonterminals. |
| 212 | |
| 213 | ### Local edits |
| 214 | |
| 215 | `DELETE <production>` - removes the specified production from the grammar |
| 216 | |
| 217 | `EDIT <production>` - modifies the specified production using the following tags |
| 218 | that appear in the specified production: |
| 219 | |
| 220 | * `USE_NT <name>` LIST* - extracts LIST* as a new nonterminal with the specified |
| 221 | new non-terminal name |
| 222 | |
| 223 | * `ADD_OPT <grammar symbol>` - looks for a production that matches the specified |
| 224 | production **without** `<grammar_symbol>`. If found, both productions are |
| 225 | replaced with single production with `OPT <grammar_symbol>` |
| 226 | |
| 227 | The current version handles a single USE_NT or ADD_OPT per EDIT. These symbols |
| 228 | may appear in the middle of the production given in the EDIT. |
| 229 | |
| 230 | `APPENDALL <symbols>` - inserts <symbols> at the end of every production in |
| 231 | <edited_nt>. |
| 232 | |
| 233 | `INSERTALL <symbols>` - inserts <symbols> at the beginning of every production in |
| 234 | <edited_nt>. |
| 235 | |
| 236 | `REPLACE` - (2 sequential productions) - removes `<oldprod>` and |
| 237 | inserts `<newprod>` in its place. |
| 238 | |
| 239 | ``` |
| 240 | | REPLACE <oldprod> |
| 241 | | WITH <newprod> |
| 242 | ``` |
| 243 | |
| 244 | `COPYALL <destination>` - creates a new nonterminal `<destination>` and copies |
| 245 | all the productions in the nonterminal to `<destination>`. |
| 246 | |
| 247 | `MOVETO <destination> <production>` - moves the production to `<destination>` and, |
| 248 | if needed, creates a new production <edited_nt> -> \<destination>. |
| 249 | |
| 250 | `MOVEALLBUT <destination>` - moves all the productions in the nonterminal to `<destination>` |
| 251 | *except* for the productions following the `MOVEALLBUT` production in the edit script |
| 252 | (terminated only by the closing `]`). |
| 253 | |
| 254 | `OPTINREF` - verifies that <edited_nt> has an empty production. If so, it removes |
| 255 | the empty production and replaces all references to <edited_nt> throughout the |
| 256 | grammar with `OPT <edited_nt>` |
| 257 | |
| 258 | `PRINT` <nonterminal> - prints the nonterminal definition at that point in |
| 259 | applying the edits. Most useful when the edits get a bit complicated to follow. |
| 260 | |
| 261 | `(any other nonterminal name)` - adds a new production (and possibly a new nonterminal) |
| 262 | to the grammar. |
| 263 | |
| 264 | ### `.rst` file updates |
| 265 | |
| 266 | `doc_grammar` updates `.rst` files where it sees the following 3 lines |
| 267 | |
| 268 | ``` |
| 269 | .. insertprodn <start> <end> |
| 270 | |
| 271 | .. prodn:: |
| 272 | ``` |
| 273 | |
| 274 | The end of the existing `prodn` is recognized by a blank line. |
| 275 | |
| 276 | ### Tagging productions |
| 277 | |
| 278 | `doc_grammar` tags the origin of productions from plugins that aren't automatically |
| 279 | loaded. In grammar files, they appear as `(* XXX plugin *)`. In rsts, productions |
| 280 | generated by `.. insertprodn` will include where relevant three spaces as (a delimiter) |
| 281 | and a tag name after each production, which Sphinx will show on the far right-hand side |
| 282 | of the production. |
| 283 | |
| 284 | The origin of a production can be specified explicitly in `common.edit_mlg` with the |
| 285 | `TAG name` appearing at the end of a production. `name` must be in quotes if it |
| 286 | contains whitespace characters. Some edit operations preserve the |
| 287 | tags, but others, such as `REPLACE ... WITH ...` do not. |
| 288 | |
| 289 | A mapping from filenames to tags (e.g. "g_ltac2.mlg" is "Ltac2") is hard-coded as is |
| 290 | filtering to avoid showing tags for, say, Ltac2 productions from appearing on every |
| 291 | production in that chapter. |
| 292 | |
| 293 | If desired, this mechanism could be extended to tag certain productions as deprecated, |
| 294 | perhaps in conjunction with a coqpp change. |