1 2[include whatis_json.inc] 3[para] 4 5It is formally specified by the rules below: 6 7[list_begin enumerated][comment {-- json points --}] 8[enum] 9The JSON of any PEG is a JSON object. 10 11[enum] 12This object holds a single key, [const pt::grammar::peg], and its 13value. This value holds the contents of the grammar. 14 15[enum] 16The contents of the grammar are a JSON object holding the set of 17nonterminal symbols and the starting expression. The relevant keys and 18their values are 19 20[list_begin definitions][comment {-- grammar keywords --}] 21[def [const rules]] 22 23The value is a JSON object whose keys are the names of the nonterminal 24symbols known to the grammar. 25 26[list_begin enumerated][comment {-- nonterminals --}] 27[enum] 28Each nonterminal symbol may occur only once. 29 30[enum] 31The empty string is not a legal nonterminal symbol. 32 33[enum] 34The value for each symbol is a JSON object itself. The relevant 35keys and their values in this dictionary are 36 37[list_begin definitions][comment {-- nonterminal keywords --}] 38[def [const is]] 39 40The value is a JSON string holding the Tcl serialization of the 41parsing expression describing the symbols sentennial structure, as 42specified in the section [sectref {PE serialization format}]. 43 44[comment { 45 This part we could try to expand further into a json data structure 46(array of (objects of) arrays ?) 47}] 48 49[def [const mode]] 50 51The value is a JSON holding holding one of three values specifying how 52a parser should handle the semantic value produced by the symbol. 53 54[include ../modes.inc] 55[list_end][comment {-- nonterminal keywords --}] 56[list_end][comment {-- nonterminals --}] 57 58[def [const start]] 59 60The value is a JSON string holding the Tcl serialization of the start 61parsing expression of the grammar, as specified in the section 62[sectref {PE serialization format}]. 63 64[list_end][comment {-- grammar keywords --}] 65 66[enum] 67The terminal symbols of the grammar are specified implicitly as the 68set of all terminal symbols used in the start expression and on the 69RHS of the grammar rules. 70 71[list_end][comment {-- json points --}] 72 73[para] 74 75As an aside to the advanced reader, this is pretty much the same as 76the Tcl serialization of PE grammars, as specified in section 77[sectref {PEG serialization format}], except that the Tcl dictionaries 78and lists of that format are mapped to JSON objects and arrays. Only 79the parsing expressions themselves are not translated further, but 80kept as JSON strings containing a nested Tcl list, and there is no 81concept of canonicity for the JSON either. 82 83[subsection Example] 84 85Assuming the following PEG for simple mathematical expressions 86 87[para] 88[include ../example/expr_peg.inc] 89[para] 90 91a JSON serialization for it is 92 93[para] 94[include ../example/expr_json.inc] 95[para] 96 97and a Tcl serialization of the same is 98 99[para] 100[include ../example/expr_serial.inc] 101[para] 102 103The similarity of the latter to the JSON should be quite obvious. 104