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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
//! The declaration types a feature fills in to say what can be written in a
//! document: elements and their syntax, fallbacks, scoped packages and
//! overrides.
//!
//! A declaration is plain data. [`mint_packages`](super::mint_packages) builds
//! the techy packages and specifications that a parse uses from the
//! declarations (see the [module documentation](super) for this work, called
//! minting). A feature constructs its declarations itself and hands them to
//! its feature builder, which records the feature as their owner:
//!
//! - [`ElementDef`], an element and the syntax it is written in
//! ([`SyntaxDef`]), passed to
//! [`FeatureBuilder::element`](crate::core::registry::FeatureBuilder::element);
//! a declaration is available in text mode unless it lists its parsing
//! modes ([`ElementDef::modes`]);
//! - [`FallbackDef`], what a macro or an environment that no declaration
//! defines means in given parsing modes, passed to
//! [`FeatureBuilder::fallback`](crate::core::registry::FeatureBuilder::fallback);
//! - [`ScopedPackageDef`], a set of element declarations that is on no scope
//! stack until the declaration of an environment body or an argument, or a
//! program, adds it, passed to
//! [`FeatureBuilder::scoped_package`](crate::core::registry::FeatureBuilder::scoped_package);
//! - [`OverrideDef`], a feature's statement that its definition of a name
//! replaces another feature's, made with
//! [`FeatureBuilder::override_callable`](crate::core::registry::FeatureBuilder::override_callable).
//!
//! The two types to read first are [`SyntaxDef`], which says how an element is
//! written, and [`ArgDef`], which declares one argument: the parser that reads
//! it ([`FlmArgumentParser`], most often one of the standard kinds of
//! [`ArgStdKind`]) and what the declaration says around the parser (name,
//! parsing mode, layout, opaque wrapper, scoped packages, state events,
//! documentation).
//! [`BodyDef`] does the
//! same for an environment's body. Settings given as data write an argument
//! declaration as a map, whose syntax is a standard kind written as data or a
//! short text standing for one, an argument code; see [`super::codes`].
use fmt;
use Arc;
use Mode;
use crateElementSpec;
use crate;
use crate;
use crate;
use crate;
use crateFlmVerbatimBehavior;
/// A callable kind: one of the three forms in which an element can be written
/// in a document, a macro, an environment or a special character sequence.
///
/// [`SyntaxDef::kind`] returns it for a syntax; [`OverrideDef`] and
/// [`Symbol`](super::Symbol) use it to say which form a name belongs to. Its
/// `Display` text (`macro`, `environment`, `specials`) appears in error
/// messages and in the [registry digest](crate::model::digest) (the hash of
/// everything about a registry that can change what processing produces).
///
/// It is written to data with `Serialize`; see [serialized
/// forms](crate::core::registry#serialized-forms).
/// The declaration of one **tack-on field**: a macro that a construct absorbs
/// as data after its own arguments, as `\section{…}` absorbs the `\label{…}`
/// written after it.
///
/// A construct accepts its tack-on fields through its tack-on argument, an
/// argument declared as [`ArgStdKind::TackOn`] with the list of fields,
/// usually its last. A field is written like a macro, but its name is not
/// defined as a command anywhere: only the construct that declares the field
/// accepts it, and only where that construct's tack-on argument stands.
///
/// A field is **data that the declaring construct reads**, not content: its
/// node is a macro node named as the field is written, carrying the language's
/// field element ([`FIELD`](crate::lang::structural::FIELD)), the same element
/// for every field, and the construct that declared the field is its only
/// reader. The declaration is data only: a name, arguments, whether the field
/// repeats, and documentation. See the [`field`](super::field) module and
/// [fields](crate::guide::concepts_overview#fields-data-a-construct-reads).
///
/// Settings given as data declare a field in the structured syntax `tack-on`
/// of its tack-on argument ([the kind as data](ArgStdKind#the-kind-as-data)).
/// How the [block-structure pass](crate::lang::rewrites::blocks) lays out an
/// argument's content: as inline
/// content, as a region of blocks, or as whichever of the two the content
/// calls for.
///
/// Inline content is running text (words, formulas, commands such as
/// `\emph{…}`) with no paragraph inside it. A region of blocks holds
/// paragraphs and block-level constructs such as lists. The layout belongs to
/// the command's declaration ([`ArgDef::layout`]); an author cannot choose it
/// where the command is written.
///
/// An argument declared with an opaque wrapper ([`ArgDef::opaque_wrapper`])
/// takes only [`Inline`](ArgLayout::Inline): the block-structure pass carries
/// its content over as written, so no other layout could take effect. An
/// argument read in mathematics mode is an [opaque
/// region](crate::guide::concepts_overview#opaque-regions), which the
/// block-structure pass carries over untouched, so it may not be declared
/// [`Blocks`](ArgLayout::Blocks). Minting reports a declaration that breaks
/// either rule
/// ([`MintError::LayoutOnOpaqueWrappedArgument`](super::MintError::LayoutOnOpaqueWrappedArgument),
/// [`MintError::BlocksOnMathArgument`](super::MintError::BlocksOnMathArgument)).
/// A layout declared on an argument whose reader yields raw text or places
/// its content under an opaque wrapper by construction (a verbatim argument, a
/// characters-only group) is not reported; it has no effect, since the
/// argument's content is an opaque region.
///
/// [`arg_layout_of`](super::arg_layout_of) returns the declared layout of an
/// argument of a parsed node;
/// [`argument_holds_blocks`](crate::lang::rewrites::blocks::argument_holds_blocks)
/// returns whether an argument holds blocks, before and after the
/// block-structure pass alike.
/// The declaration of one argument of a macro, an environment or a special
/// character sequence: the parser that reads it, the name it can be read by,
/// the parsing mode it is read in, how its content is laid out, whether its
/// content is kept as written under an opaque wrapper, the scoped packages
/// available inside it, the state events it is read under, documentation, and
/// what it holds (its role).
///
/// The parser is one field among the others ([`ArgDef::kind`]): one of the
/// standard argument kinds ([`ArgStdKind`]) for the forms FLM provides, or a
/// parser of a feature's own ([`FlmArgumentParser`]) for a syntax they cannot
/// express. Either way, minting applies the name, mode, layout, opaque
/// wrapper, scoped packages and events declared here around the parser.
///
/// Give an argument a name whenever anything reads it: reading by name keeps
/// working when the argument list changes later, reading by position does not.
///
/// An argument may also state its **role**: what the text written for it
/// holds (a label, a citation key, the path of a file), as a dotted name
/// that a feature registers in the registry's namespace of argument roles.
/// The role describes the argument for a reader of the registry; parsing and
/// processing do not read it. See [`ArgDef::role`].
///
/// [`ArgDef::new`], [`ArgDef::named`], [`ArgDef::mandatory`] and
/// [`ArgDef::optional`] create a declaration; the builder methods
/// [`mode`](ArgDef::mode), [`layout`](ArgDef::layout),
/// [`blocks`](ArgDef::blocks), [`with_opaque_wrapper`](ArgDef::with_opaque_wrapper),
/// [`scoped`](ArgDef::scoped), [`event`](ArgDef::event),
/// [`docs`](ArgDef::docs) and [`role`](ArgDef::role) each change one thing.
/// Settings given as data write an argument as its argument code, or as a map
/// of its syntax (a structured syntax, the standard kind written as data, or a
/// code) with the argument's name, parsing mode, layout, opaque wrapper,
/// scoped packages, state events, documentation and role ([`ArgData`], which
/// converts into an `ArgDef` through `TryFrom`; see [`super::codes`]).
///
/// Two declarations are equal (`PartialEq`) when every field is equal, the
/// code they were read from included ([`ArgDef::code`]). Their parsers are
/// compared by the bytes that identify them
/// ([`FlmArgumentParser::identity`]), which differ for any two parsers that
/// read differently.
/// How the [block-structure pass](crate::lang::rewrites::blocks) lays out an
/// environment's body: as inline content, as a region of blocks, or according
/// to the element's block role.
///
/// [`Inline`](BodyLayout::Inline) and [`Blocks`](BodyLayout::Blocks) take
/// precedence over the element's block role; [`FromRole`](BodyLayout::FromRole)
/// follows it. A verbatim body ([`BodyDef::verbatim`]), a body declared with
/// an opaque wrapper ([`BodyDef::opaque_wrapper`]) and a body read in
/// mathematics mode are [opaque
/// regions](crate::guide::concepts_overview#opaque-regions): the
/// block-structure pass carries them over untouched, regardless of their
/// layout. A body declared with an opaque wrapper may not declare a layout
/// other than [`FromRole`](BodyLayout::FromRole)
/// ([`MintError::LayoutOnOpaqueWrappedBody`](super::MintError::LayoutOnOpaqueWrappedBody)).
///
/// It is written to data with `Serialize`; see [serialized
/// forms](crate::core::registry#serialized-forms).
/// The declaration of an environment's body: how it is laid out, which parser
/// reads it, which parsing mode it is read in, whether its content is kept as
/// written under an opaque wrapper, which scoped packages are available
/// inside it, and which state events it is read under.
///
/// [`BodyDef::new`] (the same as `BodyDef::default()`) declares a body laid
/// out by the element's block role, read the ordinary way, read in the mode
/// the environment is written in, with no opaque wrapper, with no scoped
/// package and with no event; each builder method changes one of these.
///
/// The body parser is one field among the others ([`BodyDef::parser`]): a body
/// that is not read the ordinary way (raw text, or content a feature checks as
/// it reads it) holds the object that reads it, and minting applies the
/// layout, mode, opaque wrapper, scoped packages and events declared here
/// around that object.
/// How an element is written in the source: as a macro, an environment, a
/// file-inclusion macro, a special character sequence, or not at all.
///
/// Each form holds its list of argument declarations ([`ArgDef`]), and an
/// environment also holds a body declaration ([`BodyDef`]). A syntax the
/// standard argument kinds cannot express is still one of these forms: its
/// argument, or its body, holds a parser of the feature's own
/// ([`FlmArgumentParser`], [`FlmBodyParser`]), with its name, layout,
/// documentation, mode and scoped packages declared like any other. Only these
/// declarations reach techy: minting builds every specification from them.
/// An element declaration: the plain data a feature gives for an element, the
/// syntax it is written in (macro, environment or special character sequence),
/// its arguments (each with a parser, a layout, a parsing mode and the scoped
/// packages available inside it) and its body.
///
/// Besides the syntax ([`SyntaxDef`]), the declaration holds the feature that
/// declares it, the names the element is written under, the element value its
/// nodes carry, the parsing modes it is available in, and documentation. A
/// feature passes it to
/// [`FeatureBuilder::element`](crate::core::registry::FeatureBuilder::element),
/// or places it in a [`ScopedPackageDef`].
///
/// A declaration is available in text mode unless it lists its parsing modes
/// with [`ElementDef::modes`]: a feature that defines a command of mathematics
/// lists `[Mode::Math]`, and one that defines a command for both modes lists
/// both. Two declarations of one name whose modes do not overlap never clash,
/// whether they come from one feature or from two: a text-mode `\textbf` and
/// a mathematics-mode `\textbf` are two different commands. A declaration
/// with no syntax ([`SyntaxDef::Synthesized`]) carries the field and ignores
/// it.
///
/// A declaration is ordinary unless it is marked weak with
/// [`ElementDef::weak`]. A weak declaration of a name applies in a parsing
/// mode only where no feature gives that name an ordinary declaration in that
/// mode, whatever the order of the features: a mathematics processor declares
/// its notation weakly, so that a package or a document that defines `\ket`
/// replaces the processor's `\ket` without naming the processor in a feature
/// order. The [module documentation of minting](super::packages) states the
/// rule. A declaration in a [`ScopedPackageDef`] may not be weak
/// ([`MintError::WeakInScopedPackage`](super::MintError::WeakInScopedPackage)),
/// and a declaration with no syntax ignores the mark.
/// The callable kinds a fallback can be declared for: macros and
/// environments.
///
/// There is no fallback for special character sequences: a sequence of
/// characters that no declaration defines is not a special character
/// sequence, and is read as ordinary text.
/// A fallback: a feature's declaration of what a macro or an environment that
/// no declaration defines means, in the parsing modes the fallback lists.
///
/// In those modes, such a command of the fallback's kind
/// ([`kind`](Self::kind)) resolves to a specification that takes no argument
/// and carries `element`, and the parse reports nothing. A mathematics feature
/// uses a fallback to accept any command name inside `\(…\)` as content for
/// the mathematics renderer, while text mode still reports an unknown command.
///
/// Only one fallback may apply to one kind in one parsing mode across a
/// registry: a later fallback that overlaps an earlier one is reported as a
/// [`MintError::Clash`](super::MintError::Clash) and not installed. The
/// language has its own two fallbacks, consulted after every fallback a
/// feature declares, which apply in every mode and report the unknown command;
/// their nodes carry [`UNKNOWN_MACRO`](crate::lang::structural::UNKNOWN_MACRO) or
/// [`UNKNOWN_ENVIRONMENT`](crate::lang::structural::UNKNOWN_ENVIRONMENT). A feature
/// declares a fallback with
/// [`FeatureBuilder::fallback`](crate::core::registry::FeatureBuilder::fallback);
/// [`FlmFallbackProvider`](super::FlmFallbackProvider) applies them.
/// A scoped package: a set of element declarations that can be written only
/// inside the environment bodies and arguments whose declarations refer to the
/// package by name, for instance `\item` inside a list's body or `\caption`
/// inside a float's body.
///
/// A body refers to a scoped package with [`BodyDef::scoped`], an argument
/// with [`ArgDef::scoped`]. While such a body or argument is parsed, the
/// package's declarations are available there and shadow any other definition
/// of the same names; outside, they do not exist.
///
/// Minting builds one techy package per scoped package. That package is not
/// on the document's scope stack: it is added as the innermost techy package
/// only while a body or an argument that refers to it is parsed. Minting never
/// checks the declarations of a scoped package for clashes with those of
/// another scoped package or of a feature; only two scoped packages of one
/// name clash ([`ClashKind::ScopedPackage`](super::ClashKind::ScopedPackage)).
///
/// A feature declares a scoped package with
/// [`FeatureBuilder::scoped_package`](crate::core::registry::FeatureBuilder::scoped_package).
/// A body or an argument may refer to a scoped package that another feature
/// declares. The referring feature then declares the name as a peer
/// ([`FeatureBuilder::peer_scoped_package`](crate::core::registry::FeatureBuilder::peer_scoped_package)),
/// so that a registry without the owning feature still builds: the referring
/// bodies and arguments then make no command available through the package.
/// See also [scoped packages](crate::guide::concepts_overview#scoped-packages).
/// An override: the statement, by the feature `owner`, that its definition of
/// `name` replaces the definitions other features give that name.
///
/// Registration order decides which of several definitions of one name
/// applies, because a later feature's techy package is innermost on the scope
/// stack. Two features that define one name in one parsing mode are reported
/// as a [`MintError::Clash`](super::MintError::Clash), unless the feature
/// order places the feature registered last explicitly after the others
/// ([`crate::core::registry::order`]), or that feature declares an override.
/// An override thus prevents that report in each mode where `owner` is the
/// feature registered last among those that define the name; elsewhere it is
/// not consulted and has no effect. These rules decide among the ordinary
/// definitions of the name in a mode, or among its weak ones
/// ([`ElementDef::weak`]) where it has no ordinary one; an override never
/// makes a weak definition apply over an ordinary one. A feature declares an
/// override with
/// [`FeatureBuilder::override_callable`](crate::core::registry::FeatureBuilder::override_callable).