pgf2json 0.1.1

This crate is an Application Programming Interface to load and interpret grammars compiled in Portable Grammar Format (PGF). The PGF format is produced as a final output from the GF compiler. The API is meant to be used for embedding GF grammars in Rust programs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
{
    "pgf_format": {
      "version": "1.0",
      "produced_by": "GF 3.2",
      "output_format_command": "gf -make -output-format=pgf_pretty MyGrammar.pgf",
      "description": "The Portable Grammar Format (PGF) is a binary format where grammar structures are serialized as a sequence of bytes. Each structure is a list of sequentially serialized fields, which can be another structure or a basic type.",
      "basic_types": [
        {
          "type": "Int8",
          "description": "8 bits integer, with sign, represented as a single byte."
        },
        {
          "type": "Int16",
          "description": "16 bits integer, with sign, represented as a sequence of two bytes (most significant byte first)."
        },
        {
          "type": "Int",
          "description": "32 bits integer with sign, encoded as a sequence of bytes with variable length. The last bit of each byte indicates if more bytes follow (1 = more bytes, 0 = last byte). The other 7 bits are parts of the integer, stored from least to most significant."
        },
        {
          "type": "String",
          "description": "A string in UTF-8 encoding. First stores the length as an Int (variable length integer) representing the number of Unicode characters, followed by the UTF-8 encoded string."
        },
        {
          "type": "Float",
          "description": "A double precision floating point number serialized in big-endian format following the IEEE754 standard."
        },
        {
          "type": "List",
          "description": "A list of objects, serialized as a variable length integer indicating the number of objects, followed by the serialization of each element."
        }
      ],
      "structures": [
        {
          "name": "PGF",
          "description": "The entire PGF file contains one structure corresponding to the abstract structure G (Definition 1, Section 2.1).",
          "fields": [
            {
              "type": "Int16",
              "description": "Major PGF version, should be 1."
            },
            {
              "type": "Int16",
              "description": "Minor PGF version, should be 0."
            },
            {
              "type": "[Flag]",
              "description": "Global flags."
            },
            {
              "type": "Abstract",
              "description": "Abstract syntax."
            },
            {
              "type": "[Concrete]",
              "description": "List of concrete syntaxes."
            }
          ],
          "notes": "Version fields are updated if PGF changes to maintain backward compatibility."
        },
        {
          "name": "Flag",
          "description": "Pairs of a name and a literal, storing configuration parameters accessible only internally by the interpreter.",
          "fields": [
            {
              "type": "String",
              "description": "Flag name."
            },
            {
              "type": "Literal",
              "description": "Flag value."
            }
          ]
        },
        {
          "name": "Abstract",
          "description": "Represents the abstract syntax A (Definition 2, Section 2.1). The name is the top-level abstract module name, and the start category is specified with the 'startcat' flag.",
          "fields": [
            {
              "type": "String",
              "description": "Name of the abstract syntax."
            },
            {
              "type": "[Flag]",
              "description": "List of flags."
            },
            {
              "type": "[AbsFun]",
              "description": "List of abstract functions."
            },
            {
              "type": "[AbsCat]",
              "description": "List of abstract categories."
            }
          ],
          "notes": "All lists are sorted by name for efficient binary search."
        },
        {
          "name": "AbsFun",
          "description": "Represents an abstract function.",
          "fields": [
            {
              "type": "String",
              "description": "Name of the function."
            },
            {
              "type": "Type",
              "description": "Function's type signature."
            },
            {
              "type": "Int",
              "description": "Function's arity."
            },
            {
              "type": "Int8",
              "description": "Constructor tag: 0 for constructor, 1 for function."
            },
            {
              "type": "[Equation]",
              "description": "Definitional equations for the function if it is not a constructor."
            },
            {
              "type": "Float",
              "description": "Probability of the function."
            }
          ],
          "notes": "Distinguishes between constructors (data f: T) and computable functions (fun f: T). Arity is the number of arguments for pattern matching in functions with equations; zero for constructors or axioms."
        },
        {
          "name": "AbsCat",
          "description": "Represents an abstract category, including its name, type information, and functions returning this category.",
          "fields": [
            {
              "type": "String",
              "description": "Name of the category."
            },
            {
              "type": "[Hypo]",
              "description": "List of hypotheses."
            },
            {
              "type": "[CatFun]",
              "description": "List of functions in source-code order."
            }
          ]
        },
        {
          "name": "CatFun",
          "description": "Used internally to store abstract functions with their probabilities.",
          "fields": [
            {
              "type": "String",
              "description": "Name of the function."
            },
            {
              "type": "Float",
              "description": "Probability of the function."
            }
          ]
        },
        {
          "name": "Type",
          "description": "Represents an abstract syntax type in the form (X1:T1) → (X2:T2) → ... → (Xn:Tn) → C e1...en.",
          "fields": [
            {
              "type": "[Hypo]",
              "description": "List of hypotheses."
            },
            {
              "type": "String",
              "description": "Name of the category in the return type."
            },
            {
              "type": "[Expression]",
              "description": "Indices in the return type."
            }
          ]
        },
        {
          "name": "Hypo",
          "description": "Represents an argument in a function type, specifying whether it is explicit (x:T) or implicit ({x}:T).",
          "fields": [
            {
              "type": "BindType",
              "description": "Binding type (implicit/explicit argument)."
            },
            {
              "type": "String",
              "description": "Variable name or '_' if no variable is bound."
            },
            {
              "type": "Type",
              "description": "Type of the variable."
            }
          ]
        },
        {
          "name": "Equation",
          "description": "Represents a computable function's equation as a pair of patterns and an expression.",
          "fields": [
            {
              "type": "[Pattern]",
              "description": "Sequence of patterns."
            },
            {
              "type": "Expression",
              "description": "Expression."
            }
          ],
          "notes": "All equations have the same number of patterns, equal to the function's arity."
        },
        {
          "name": "Pattern",
          "description": "Represents a single pattern in a definitional equation, starting with a tag encoding the pattern type.",
          "fields": [
            {
              "type": "Int8",
              "description": "Tag encoding the kind of pattern."
            }
          ],
          "pattern_types": [
            {
              "tag": 0,
              "description": "Pattern matching on constructor application (c p1 p2 ... pn).",
              "fields": [
                {
                  "type": "String",
                  "description": "Name of the constructor."
                },
                {
                  "type": "[Pattern]",
                  "description": "List of nested patterns for the arguments."
                }
              ]
            },
            {
              "tag": 1,
              "description": "Variable type.",
              "fields": [
                {
                  "type": "String",
                  "description": "Variable name."
                }
              ]
            },
            {
              "tag": 2,
              "description": "Pattern binding a variable with nested pattern matching (x@p).",
              "fields": [
                {
                  "type": "String",
                  "description": "Variable name."
                },
                {
                  "type": "Pattern",
                  "description": "Nested pattern."
                }
              ]
            },
            {
              "tag": 3,
              "description": "Wildcard (_)."
            },
            {
              "tag": 4,
              "description": "Matching a literal (string, integer, or float).",
              "fields": [
                {
                  "type": "Literal",
                  "description": "Value of the literal."
                }
              ]
            },
            {
              "tag": 5,
              "description": "Pattern matching on an implicit argument ({P}).",
              "fields": [
                {
                  "type": "[Pattern]",
                  "description": "Nested pattern."
                }
              ]
            },
            {
              "tag": 6,
              "description": "Inaccessible pattern (~p).",
              "fields": [
                {
                  "type": "Expr",
                  "description": "Nested pattern."
                }
              ]
            }
          ]
        },
        {
          "name": "Expression",
          "description": "Encodes an abstract syntax expression (tree), starting with a tag.",
          "fields": [
            {
              "type": "Int8",
              "description": "Tag encoding the expression type."
            }
          ],
          "expression_types": [
            {
              "tag": 0,
              "description": "Lambda abstraction (\\x → ...).",
              "fields": [
                {
                  "type": "BindType",
                  "description": "Tag for implicit/explicit argument."
                },
                {
                  "type": "String",
                  "description": "Variable name."
                },
                {
                  "type": "Expression",
                  "description": "Body of the lambda abstraction."
                }
              ]
            },
            {
              "tag": 1,
              "description": "Application (f x).",
              "fields": [
                {
                  "type": "Expression",
                  "description": "Left-hand expression."
                },
                {
                  "type": "Expression",
                  "description": "Right-hand expression."
                }
              ]
            },
            {
              "tag": 2,
              "description": "Literal value (string, integer, or float).",
              "fields": [
                {
                  "type": "Literal",
                  "description": "Value of the literal."
                }
              ]
            },
            {
              "tag": 3,
              "description": "Metavariable (?0, ?1, ...).",
              "fields": [
                {
                  "type": "Int",
                  "description": "ID of the metavariable."
                }
              ]
            },
            {
              "tag": 4,
              "description": "Abstract syntax function.",
              "fields": [
                {
                  "type": "String",
                  "description": "Function name."
                }
              ]
            },
            {
              "tag": 5,
              "description": "Variable.",
              "fields": [
                {
                  "type": "Int",
                  "description": "de Bruijn index of the variable."
                }
              ]
            },
            {
              "tag": 6,
              "description": "Expression with a type annotation (e:t).",
              "fields": [
                {
                  "type": "Expression",
                  "description": "Annotated expression."
                },
                {
                  "type": "Type",
                  "description": "Type of the expression."
                }
              ]
            },
            {
              "tag": 7,
              "description": "Implicit argument ({e}).",
              "fields": [
                {
                  "type": "Expression",
                  "description": "Expression for the argument."
                }
              ]
            }
          ]
        },
        {
          "name": "Literal",
          "description": "Represents built-in literal constants, starting with a tag encoding the type.",
          "fields": [
            {
              "type": "Int8",
              "description": "Literal type."
            }
          ],
          "literal_types": [
            {
              "tag": 0,
              "description": "String type.",
              "fields": [
                {
                  "type": "String",
                  "description": "Value."
                }
              ]
            },
            {
              "tag": 1,
              "description": "Integer.",
              "fields": [
                {
                  "type": "Int",
                  "description": "Value."
                }
              ]
            },
            {
              "tag": 2,
              "description": "Float type.",
              "fields": [
                {
                  "type": "Float",
                  "description": "Value."
                }
              ]
            }
          ]
        },
        {
          "name": "BindType",
          "description": "Encodes whether an argument is explicit or implicit.",
          "fields": [
            {
              "type": "Int8",
              "description": "Tag."
            }
          ]
        },
        {
          "name": "Concrete",
          "description": "Represents a concrete syntax C (Definition 3, Section 2.1), with the name of the top-level concrete module.",
          "fields": [
            {
              "type": "String",
              "description": "Name of the concrete syntax."
            },
            {
              "type": "[Flag]",
              "description": "List of flags."
            },
            {
              "type": "[PrintName]",
              "description": "List of print names."
            },
            {
              "type": "[Sequence]",
              "description": "Table with sequences (Section 2.8.1)."
            },
            {
              "type": "[CncFun]",
              "description": "List of concrete functions."
            },
            {
              "type": "[LinDef]",
              "description": "List of functions for default linearization."
            },
            {
              "type": "[ProductionSet]",
              "description": "List of production sets."
            },
            {
              "type": "[CncCat]",
              "description": "List of concrete categories."
            },
            {
              "type": "Int",
              "description": "Total number of concrete categories allocated for the grammar."
            }
          ],
          "notes": [
            "Lists Flag, PrintName, and CncCat are sorted by name for efficient binary search.",
            "The total number of concrete categories is used by the parser to determine if a category is part of the grammar or created during parsing, aiding in metavariable placement (Section 2.3.7)."
          ]
        },
        {
          "name": "PrintName",
          "description": "Stores user-friendly names for functions or categories for display in the UI, aiding localization.",
          "fields": [
            {
              "type": "String",
              "description": "Name of the function or category."
            },
            {
              "type": "String",
              "description": "Printable name."
            }
          ]
        },
        {
          "name": "Sequence",
          "description": "Represents a single sequence in PMCFG, produced during common subexpression optimization (Section 2.8.1).",
          "fields": [
            {
              "type": "[Symbol]",
              "description": "List of symbols."
            }
          ]
        },
        {
          "name": "Symbol",
          "description": "Represents a terminal or function argument in a sequence, starting with a tag encoding the symbol type.",
          "fields": [
            {
              "type": "Int8",
              "description": "Expression tag."
            }
          ],
          "symbol_types": [
            {
              "tag": 0,
              "description": "Argument representation (<k;l>) where k is the argument index and l is the constituent index.",
              "fields": [
                {
                  "type": "Int",
                  "description": "Argument index."
                },
                {
                  "type": "Int",
                  "description": "Constituent index."
                }
              ]
            },
            {
              "tag": 1,
              "description": "Argument targeting a literal category ({d;r}) if not a fresh category (Section 2.6).",
              "fields": [
                {
                  "type": "Int",
                  "description": "Argument index."
                },
                {
                  "type": "Int",
                  "description": "Constituent index."
                }
              ]
            },
            {
              "tag": 2,
              "description": "High-order argument (<d;$r>) (Section 2.7).",
              "fields": [
                {
                  "type": "Int",
                  "description": "Argument index."
                },
                {
                  "type": "Int",
                  "description": "Variable number."
                }
              ]
            },
            {
              "tag": 3,
              "description": "Terminal symbol representing a list of tokens.",
              "fields": [
                {
                  "type": "[String]",
                  "description": "Sequence of tokens."
                }
              ]
            },
            {
              "tag": 4,
              "description": "Alternative terminal symbol for phrases dependent on the next token's prefix (e.g., a/an in English).",
              "fields": [
                {
                  "type": "[String]",
                  "description": "Default form."
                },
                {
                  "type": "[Alternative]",
                  "description": "Sequence of alternatives."
                }
              ]
            }
          ]
        },
        {
          "name": "Alternative",
          "description": "Represents a phrase form dependent on the prefix of the next token (e.g., beau/bel for ami).",
          "fields": [
            {
              "type": "[String]",
              "description": "Tokens to use if the prefix matches."
            },
            {
              "type": "[String]",
              "description": "Prefix matched with the following tokens."
            }
          ]
        },
        {
          "name": "CncFun",
          "description": "Defines a concrete function (Definition 4, Section 2.1), mapping to an abstract function with a list of sequence indices.",
          "fields": [
            {
              "type": "String",
              "description": "Name of the corresponding abstract function."
            },
            {
              "type": "[Int]",
              "description": "List of indices into the sequences array."
            }
          ]
        },
        {
          "name": "LinDef",
          "description": "Stores concrete functions for default linearization of a concrete category (Section 2.5).",
          "fields": [
            {
              "type": "Int",
              "description": "Concrete category."
            },
            {
              "type": "[Int]",
              "description": "List of concrete functions."
            }
          ]
        },
        {
          "name": "ProductionSet",
          "description": "Groups productions with the same result category for efficient parser prediction.",
          "fields": [
            {
              "type": "Int",
              "description": "Result category."
            },
            {
              "type": "[Production]",
              "description": "List of productions."
            }
          ]
        },
        {
          "name": "Production",
          "description": "Represents either an application or a coercion.",
          "fields": [
            {
              "type": "Int8",
              "description": "Tag."
            }
          ],
          "production_types": [
            {
              "tag": 0,
              "description": "Application (Definition 4, Section 2.1).",
              "fields": [
                {
                  "type": "Int",
                  "description": "Concrete function."
                },
                {
                  "type": "[PArg]",
                  "description": "List of arguments."
                }
              ]
            },
            {
              "tag": 1,
              "description": "Coercion (Section 2.8.1).",
              "fields": [
                {
                  "type": "Int8",
                  "description": "Concrete category."
                }
              ]
            }
          ]
        },
        {
          "name": "PArg",
          "description": "Represents an argument in a production.",
          "fields": [
            {
              "type": "[Int]",
              "description": "Categories of high-order arguments (Section 2.7)."
            },
            {
              "type": "Int",
              "description": "Concrete category."
            }
          ]
        },
        {
          "name": "CncCat",
          "description": "Represents a set of concrete categories mapping to the same abstract category, storing the first and last category and constituent names.",
          "fields": [
            {
              "type": "String",
              "description": "Name of the corresponding abstract category (by ψ_N)."
            },
            {
              "type": "Int",
              "description": "First concrete category."
            },
            {
              "type": "Int",
              "description": "Last concrete category."
            },
            {
              "type": "[String]",
              "description": "List of constituent names."
            }
          ],
          "notes": "The length of the constituent names list equals the dimension of the category."
        }
      ]
    }
  }