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
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
// SPDX-FileCopyrightText: 2026 Dominik Schwimmbeck
// SPDX-License-Identifier: MIT OR Apache-2.0
//! The compiler: from the core language of a resolved grammar to a program.
//!
//! Each core form becomes a fixed piece of code. A shared part becomes a routine of its own
//! that every use calls, and every repetition compiles its expression once, so the program
//! grows linearly with the grammar.
use std::borrow::Cow;
use std::collections::HashMap;
use std::sync::Arc;
use super::program::{ByteTest, Class, Inst, Program, RuleInfo};
use super::{Memo, Options, Passes};
use crate::analysis::{self, Bytes, Start, Starts};
use crate::diag::{Diagnostic, DiagnosticCode};
use crate::grammar::{ClassItem, Span};
use crate::ir::{self, RuleId};
use crate::resolve::MAX_FLAGS;
use crate::tree::{FieldId, KindId};
/// The most expressions a rule may have, counting those of the rules it inlines in turn,
/// for its calls to compile as its body.
const MAX_INLINE_EXPRESSIONS: usize = 24;
/// Compile a resolved grammar, or report that it is too large to compile.
pub(crate) fn compile(
grammar: &ir::Grammar,
options: &Options,
) -> Result<Program, Vec<Diagnostic>> {
let facts = analysis::analyze(grammar);
let mut c = Compiler {
has_skip: grammar.skip.is_some(),
code: Vec::new(),
strings: Vec::new(),
string_ids: HashMap::new(),
strings_of: HashMap::new(),
classes: Vec::new(),
classes_of: HashMap::new(),
bounds: Vec::new(),
calls: Vec::new(),
shared: HashMap::new(),
queue: Vec::new(),
shared_pcs: Vec::new(),
subs: Vec::new(),
cuttable: false,
free_cuts: HashMap::new(),
passes: options.passes,
starts: None,
tests: Vec::new(),
test_ids: HashMap::new(),
rules: &grammar.rules,
recovered: (grammar.recoveries.iter()).map(Option::is_some).collect(),
error_alternatives: vec![None; grammar.error_alternatives as usize],
inline: Vec::new(),
seed_rule: None,
defines_shared: HashMap::new(),
defining: Vec::new(),
};
c.defining = (grammar.rules.iter())
.map(|rule| c.defines(&rule.body))
.collect();
let loops: Vec<Option<Loop<'_>>> = (0..grammar.rules.len())
.map(|i| {
// Each pass of the semantics starts with the captures of the call, which the passes
// of a loop would have to give back.
let candidate = options.passes.left_loop && facts.left_recursive[i] && !c.defining[i];
candidate
.then(|| c.loop_of(RuleId(i as u32), &facts))
.flatten()
})
.collect();
// A rule that runs as a loop has no frame of left recursion.
let growing: Vec<bool> = (facts.left_recursive.iter().zip(&loops))
.map(|(&recursive, looped)| recursive && looped.is_none())
.collect();
// What expressions start with serves dispatch, and the skip, which ends at once at a byte its
// expression cannot start with.
let starts =
(options.passes.dispatch || options.passes.skip).then(|| facts.starts(grammar, &growing));
let skip_first = skip_first(grammar, options, starts.as_ref());
if options.passes.dispatch {
c.starts = starts;
}
let twice = facts.tried_twice(grammar);
let memo: Vec<bool> = (0..grammar.rules.len())
.map(|i| remembered(options.memo, &grammar.rules[i], growing[i] || twice[i]))
.collect();
if options.passes.inline {
c.inline = c.inlinable(&memo, &facts.recursive);
}
let mut rules = Vec::new();
for (i, rule) in grammar.rules.iter().enumerate() {
rules.push(RuleInfo {
name: rule.name.clone(),
pc: c.here(),
memo: memo[i],
recursive: growing[i],
});
// A cut ends at the call of its rule, and so do its captures.
c.cuttable = false;
if c.defining[i] {
c.emit(Inst::CaptureMark);
}
match &loops[i] {
Some(looped) => c.left_loop(RuleId(i as u32), looped),
None => c.expr(&rule.body),
}
if c.defining[i] {
c.emit(Inst::CaptureDrop);
}
c.emit(Inst::Ret);
}
// Skipping is `e*` of the skip expression `e`, a span where `e` repeats a class.
let skip_class = match &grammar.skip {
Some(skip) if options.passes.skip => {
span(skip, 0).map(|(points, set, _)| c.class(points, &set))
}
_ => None,
};
let skip = grammar
.skip
.as_ref()
.filter(|_| skip_class.is_none())
.map(|skip| {
// The skip expression in plain mode, as often as it consumes input, recording nothing.
let pc = c.here();
c.emit(Inst::QuietOn);
let choice = c.emit(Inst::Choice(0));
let step = c.here();
c.cuttable = true;
// Where the next byte cannot start the skip expression, the skipping ends.
let test = (c.test_for(skip, true)).map(|test| c.emit(Inst::Test { test, target: 0 }));
// What a skip captures holds for that skip alone.
let defines = c.defines(skip);
if defines {
c.emit(Inst::CaptureMark);
}
// A failure after a cut ends the skipping as any other does, so it must not pass the
// entry of the loop.
c.cut_scope(skip);
if defines {
c.emit(Inst::CaptureDrop);
}
c.emit(Inst::LoopCommit(step));
c.patch(choice);
if let Some(test) = test {
c.patch(test);
}
c.emit(Inst::QuietOff);
c.emit(Inst::Ret);
pc
});
let mut names: Vec<&String> = grammar.entry.keys().collect();
names.sort();
let mut entries = HashMap::new();
// An entry runs once a parse, so it calls its rule rather than holding a copy of it.
let inline = std::mem::take(&mut c.inline);
for name in names {
entries.insert(name.clone(), c.here());
c.cuttable = false;
c.expr(&grammar.entry[name]);
if c.has_skip {
c.emit(Inst::Skip);
}
c.emit(Inst::End);
}
c.inline = inline;
// A recovery is matched as a rule body is, and gives back what it captured, as the
// semantics have it.
let recoveries = (grammar.recoveries.iter())
.map(|recovery| {
let recovery = recovery.as_ref()?;
let pc = c.here();
c.cuttable = false;
let defines = c.defines(recovery);
if defines {
c.emit(Inst::CaptureMark);
}
c.expr(recovery);
if defines {
c.emit(Inst::CaptureDrop);
}
c.emit(Inst::Ret);
Some(pc)
})
.collect();
// A test routine matches what an error alternative tests, as a parse of its own once the
// parse failed. Tests and shared parts may meet more of either.
let mut error_tests: Vec<Option<u32>> = vec![None; c.error_alternatives.len()];
loop {
let pending: Vec<(usize, &ir::Expr)> = (c.error_alternatives.iter().enumerate())
.filter(|&(index, _)| error_tests[index].is_none())
.filter_map(|(index, found)| Some((index, found.as_ref()?.0)))
.collect();
if pending.is_empty() && c.queue.is_empty() {
break;
}
for (index, test) in pending {
error_tests[index] = Some(c.here());
c.cuttable = false;
let defines = c.defines(test);
if defines {
c.emit(Inst::CaptureMark);
}
c.expr(test);
if defines {
c.emit(Inst::CaptureDrop);
}
c.emit(Inst::TestEnd);
}
while let Some((index, expr)) = c.queue.pop() {
c.shared_pcs[index] = c.here();
// A cut passes the call of a shared part, and what stands around its uses is not
// known here.
c.cuttable = true;
c.expr(expr);
c.emit(Inst::Ret);
}
}
// Where an error alternative's error starts after the ignored text, a test finds where
// that ends.
let skip_test = (c.has_skip && c.error_alternatives.iter().flatten().any(|found| found.2))
.then(|| {
let pc = c.here();
c.emit(Inst::Skip);
c.emit(Inst::TestEnd);
pc
});
for (at, rule) in std::mem::take(&mut c.calls) {
c.code[at] = match rules.get(rule.0 as usize) {
Some(info) if info.recursive => Inst::LrCall(rule.0),
Some(info) if info.memo => Inst::MemoCall(rule.0),
Some(info) => Inst::Call(info.pc),
// A call to a rule that does not exist fails, as the reference interpreter has it.
None => Inst::Fail,
};
}
for (at, index) in std::mem::take(&mut c.subs) {
c.code[at] = Inst::Sub(c.shared_pcs[index]);
}
if u32::try_from(c.code.len()).is_err() {
let message = "the grammar is too large to compile".to_owned();
return Err(vec![Diagnostic {
code: DiagnosticCode::TooLarge,
message,
span: Span::default(),
}]);
}
let labels = grammar.labels.clone();
Ok(Program {
code: c.code,
strings: c.strings,
classes: c.classes,
tests: c.tests,
bounds: c.bounds,
rules,
#[cfg(feature = "reference")]
shared: c.shared_pcs,
skip,
skip_class,
entries,
word: Class::new(&grammar.word),
reserved: ir::Reserved {
exact: grammar.reserved.exact.clone(),
folded: grammar.reserved.folded.clone(),
},
names: Arc::clone(&grammar.names),
kind_display: grammar.kind_display.clone(),
labels,
recoveries,
error_messages: (c.error_alternatives.iter())
.map(|found| found.map_or_else(String::new, |(_, message, _)| message.to_owned()))
.collect(),
error_skips: (c.error_alternatives.iter())
.map(|found| found.is_some_and(|(_, _, skip)| skip))
.collect(),
skip_test,
error_ranks: grammar.error_ranks.clone(),
error_tests,
explains: grammar.error_alternatives > 0,
kinds: grammar.kinds(),
single_item: grammar.single_item.clone(),
passes: options.passes,
tab_width: grammar.tab_width,
skip_first,
// A skip that checks a column or matches a capture may depend on the context. Where the
// captures stay inside the terminals it calls, the context at a skip is 0 all the same.
skip_context: (grammar.skip.as_ref()).is_some_and(|skip| {
analysis::reaches(grammar, skip, |expr| {
matches!(
expr,
ir::Expr::Indent { .. } | ir::Expr::Same { .. } | ir::Expr::Ref { .. }
)
})
}),
})
}
/// Whether the results of a rule are remembered, `repeated` where by default it is: where a
/// choice point can call it twice at one position, or it keeps the growing loop of left
/// recursion, whose pass that finds the match grown no further runs its body again.
fn remembered(memo: Memo, rule: &ir::Rule, repeated: bool) -> bool {
match (memo, rule.memo) {
(Memo::Off, _) | (Memo::Grammar, ir::MemoMark::NoMemo) => false,
(Memo::All, _) | (Memo::Grammar, ir::MemoMark::Memo) => true,
(Memo::Grammar, ir::MemoMark::None) => repeated,
}
}
/// The first bytes of the skip expression, where the skip pass runs its routine and the
/// expression cannot match nothing and fails plainly at its first byte: a skip at any other byte
/// ends where it starts, whatever the flags and the context, since the bytes hold for them all.
fn skip_first(
grammar: &ir::Grammar,
options: &Options,
starts: Option<&Starts<'_>>,
) -> Option<Bytes> {
let skip = grammar.skip.as_ref()?;
if !options.passes.skip || span(skip, 0).is_some() {
return None;
}
let start = starts?.of(skip);
(!start.nullable && start.plain && start.bytes != Bytes::ALL).then_some(start.bytes)
}
/// The bit of a flag in the machine's flag register. A resolved grammar has at most 64 flags,
/// so every flag has one.
fn flag_bit(flag: ir::FlagId) -> Option<u8> {
u8::try_from(flag.0)
.ok()
.filter(|&bit| usize::from(bit) < MAX_FLAGS)
}
struct Compiler<'f> {
has_skip: bool,
code: Vec<Inst>,
strings: Vec<Box<[u8]>>,
string_ids: HashMap<Box<[u8]>, u32>,
/// The string and the class of each literal and class compiled so far, by its address,
/// since a body compiled into its callers compiles again at each call.
strings_of: HashMap<*const ir::Expr, u32>,
classes: Vec<Class>,
classes_of: HashMap<*const ir::Expr, u32>,
bounds: Vec<(u32, u32)>,
/// Calls to point at the routine of the rule they call, once every rule has one.
calls: Vec<(usize, RuleId)>,
/// The index of each shared part met so far, and the parts whose routines are still to
/// be compiled.
shared: HashMap<*const ir::Expr, usize>,
queue: Vec<(usize, &'f ir::Expr)>,
shared_pcs: Vec<u32>,
/// Calls to point at the routine of the shared part they call, once every part has one.
subs: Vec<(usize, usize)>,
/// Whether a cut in the code being compiled could mark an entry of the routine: one of a
/// choice or repetition around it, with no lookahead between.
cuttable: bool,
/// Whether each shared part met so far holds a free cut.
free_cuts: HashMap<*const ir::Expr, bool>,
passes: Passes,
/// What each expression can start with, where dispatch is on.
starts: Option<Starts<'f>>,
tests: Vec<ByteTest>,
test_ids: HashMap<ByteTest, u32>,
rules: &'f [ir::Rule],
/// Whether each label has a recovery, by label id.
recovered: Vec<bool>,
/// What each error alternative met so far tests, and its message, by index.
error_alternatives: Vec<Option<(&'f ir::Expr, &'f str, bool)>>,
/// Whether the calls of each rule compile as its body.
inline: Vec<bool>,
/// The rule whose next call compiles as `SeedUse`: the leading call of a recursive
/// alternative of a loop.
seed_rule: Option<RuleId>,
/// Whether each shared part met so far can make a capture.
defines_shared: HashMap<*const ir::Expr, bool>,
/// Whether each rule can make a capture in its own activation.
defining: Vec<bool>,
}
/// A rule that runs as a loop: its recursive alternatives, each with the parts that follow
/// its leading call, and its base alternatives.
struct Loop<'f> {
recursive: &'f [ir::Expr],
rests: Vec<&'f [ir::Expr]>,
base: &'f [ir::Expr],
}
/// The parts that follow the leading call of `rule` in `expr`, if `expr` starts with one:
/// through nodes and fields, and as the first part of a sequence.
fn after_call(expr: &ir::Expr, rule: RuleId) -> Option<&[ir::Expr]> {
match expr {
ir::Expr::Call(called) if *called == rule => Some(&[]),
ir::Expr::Node { expr, .. }
| ir::Expr::Transparent { expr, .. }
| ir::Expr::Field { expr, .. } => after_call(expr, rule),
ir::Expr::Seq(parts) => {
let (first, rest) = parts.split_first()?;
match after_call(first, rule)? {
[] => Some(rest),
// A call nested deeper than the first part of this sequence has parts of its
// own after it, which this form does not take apart.
_ => None,
}
}
_ => None,
}
}
/// The code points of a class, or of a literal of one code point that ignores no case.
fn span_set(expr: &ir::Expr) -> Option<Cow<'_, ir::CharSet>> {
match expr {
ir::Expr::Class(set) => Some(Cow::Borrowed(set)),
ir::Expr::Literal {
text,
case_insensitive: false,
} => {
let mut chars = text.chars();
match (chars.next(), chars.next()) {
(Some(c), None) => Some(Cow::Owned(ir::CharSet {
negated: false,
items: vec![ClassItem::Range(c, c)],
})),
_ => None,
}
}
_ => None,
}
}
/// The expression of the code points, the code points and the fewest of them for a span that
/// matches `e{min,}` exactly: `c{min,}` of a class or one code point `c`, `(c*){min,}`, `(c+)*`
/// and `(c+)+`. A step that matches nothing is the last and satisfies the minimum, so
/// `(c*){min,}` takes the run whatever `min` says, while `(c+){2,}` never matches, since its
/// first step takes the whole run.
fn span(expr: &ir::Expr, min: u32) -> Option<(&ir::Expr, Cow<'_, ir::CharSet>, u32)> {
let (points, min) = match expr {
ir::Expr::Repeat {
expr: inner,
min: 0,
max: None,
} => (&**inner, 0),
ir::Expr::Repeat {
expr: inner,
min: 1,
max: None,
} if min <= 1 => (&**inner, min),
_ => (expr, min),
};
span_set(points).map(|set| (points, set, min))
}
impl<'f> Compiler<'f> {
/// The rules whose calls compile as their bodies: rules that are not remembered, call
/// themselves nowhere and hold no free cut, which would reach past the call, with at
/// most [`MAX_INLINE_EXPRESSIONS`] expressions, a call they inline counted with the body
/// it compiles as. So a call never compiles to more, and a chain of rules that call each
/// other breaks into calls every so many links. Each candidate is sized once those it
/// calls are, on an explicit stack, since chains of rules can be as long as the grammar.
fn inlinable(&mut self, memo: &[bool], recursive: &[bool]) -> Vec<bool> {
// The captures a rule makes end with its activation, which an inlined body has not.
let candidate: Vec<bool> = (0..self.rules.len())
.map(|i| {
!memo[i]
&& !recursive[i]
&& !self.defining[i]
&& !self.free_cut(&self.rules[i].body)
})
.collect();
let mut sizes: Vec<Option<usize>> = vec![None; self.rules.len()];
for root in 0..self.rules.len() {
let mut stack = vec![root];
while let Some(&rule) = stack.last() {
if !candidate[rule] || sizes[rule].is_some() {
stack.pop();
continue;
}
match self.inlined_size(rule, &candidate, &sizes) {
Ok(size) => sizes[rule] = Some(size),
Err(callee) => stack.push(callee),
}
}
}
let fits = |size: Option<usize>| size.is_some_and(|size| size <= MAX_INLINE_EXPRESSIONS);
(0..self.rules.len()).map(|i| fits(sizes[i])).collect()
}
/// How many expressions the body of a candidate compiles to, at most one past the most
/// that fit, or a candidate it calls that is not sized yet. A shared part counts once, as
/// its call. A repetition of no steps compiles to nothing, and the analysis sees no call in
/// it, so a candidate may call itself there.
fn inlined_size(
&self,
rule: usize,
candidate: &[bool],
sizes: &[Option<usize>],
) -> Result<usize, usize> {
let mut size = 0;
let mut todo = vec![&self.rules[rule].body];
while let Some(expr) = todo.pop() {
if size > MAX_INLINE_EXPRESSIONS {
break;
}
size += 1;
match expr {
ir::Expr::Call(callee) if candidate.get(callee.0 as usize) == Some(&true) => {
match sizes[callee.0 as usize] {
Some(inner) if inner <= MAX_INLINE_EXPRESSIONS => size += inner,
Some(_) => {}
None => return Err(callee.0 as usize),
}
}
ir::Expr::Shared(_) | ir::Expr::Repeat { max: Some(0), .. } => {}
_ => todo.extend(expr.children()),
}
}
Ok(size.min(MAX_INLINE_EXPRESSIONS + 1))
}
/// How `rule`, which lies on a left cycle, runs as a loop, if it can: where its body is a
/// choice of recursive alternatives, each starting with a call of the rule itself, before
/// base alternatives that cannot reach the rule where they start, and the rule cannot
/// match nothing and holds no free cut. That is the growing loop of the semantics for
/// this shape: in a pass with a seed every base alternative before the one that matched
/// fails again, the recursive ones are tried in order on the seed, and the first that
/// matches ends the pass, which grows only where it ends further. A pass in which none
/// matches leaves the seed, since the base alternative that matched would end where the
/// first seed ended.
fn loop_of(&mut self, rule: RuleId, facts: &analysis::Facts) -> Option<Loop<'f>> {
let rules: &'f [ir::Rule] = self.rules;
let ir::Expr::Choice(alternatives) = &rules.get(rule.0 as usize)?.body else {
return None;
};
let rests: Vec<&'f [ir::Expr]> = (alternatives.iter())
.map_while(|alternative| after_call(alternative, rule))
.collect();
let (recursive, base) = alternatives.split_at(rests.len());
let shaped = !recursive.is_empty() && !base.is_empty() && !facts.rule_nullable(rule);
if !shaped
|| base
.iter()
.any(|alternative| facts.reaches_at_start(alternative, rule))
|| alternatives
.iter()
.any(|alternative| self.free_cut(alternative))
{
return None;
}
Some(Loop {
recursive,
rests,
base,
})
}
/// Compile a rule as the loop of [`Compiler::loop_of`]: `SeedOpen`, the base, and then
/// `SeedKeep` and a choice of the recursive alternatives, each with `SeedUse` for its
/// leading call, that grows the seed and goes round again where it ends further, until
/// `SeedClose` gives the seed.
fn left_loop(&mut self, rule: RuleId, looped: &Loop<'f>) {
let Some((last, others)) = looped.recursive.split_last() else {
return;
};
self.emit(Inst::SeedOpen);
match looped.base {
[one] => self.expr(one),
_ => self.choice(looped.base),
}
let top = self.emit(Inst::SeedKeep) as u32;
// The tests look at what follows the seed, where the recursive alternatives go on.
let exit =
(self.rests_test(&looped.rests)).map(|test| self.emit(Inst::Test { test, target: 0 }));
let all = self.rests_bytes(&looped.rests);
let choice = self.emit(Inst::Choice(0));
let mut commits = Vec::new();
for (alternative, rest) in others.iter().zip(&looped.rests) {
let test = (self.rests_bytes(&[rest]) != all)
.then(|| self.rests_test(&[rest]))
.flatten()
.map(|test| self.emit(Inst::Test { test, target: 0 }));
let alt = self.emit(Inst::Choice(0));
self.seed_rule = Some(rule);
self.inside(true, alternative);
commits.push(self.emit(Inst::Commit(0)));
self.patch(alt);
if let Some(test) = test {
self.patch(test);
}
}
let rest = looped.rests.last().copied().unwrap_or_default();
if self.rests_bytes(&[rest]) != all
&& let Some(test) = self.rests_test(&[rest])
{
self.emit(Inst::TestFail(test));
}
self.seed_rule = Some(rule);
self.inside(true, last);
for commit in commits {
self.patch(commit);
}
self.emit(Inst::SeedGrow(top));
self.patch(choice);
if let Some(exit) = exit {
self.patch(exit);
}
self.emit(Inst::SeedClose);
}
/// The first bytes of code that goes on with one of these sequences of parts, where
/// dispatch is on.
fn rests_bytes(&self, rests: &[&[ir::Expr]]) -> Option<Bytes> {
let starts = self.starts.as_ref()?;
let union = |bytes: Bytes, parts: &&[ir::Expr]| bytes.union(starts.sequence(parts).0.bytes);
Some(rests.iter().fold(Bytes::default(), union))
}
/// The test before code that goes on with one of these sequences of parts, tried in
/// turn, if one is worth it.
fn rests_test(&mut self, rests: &[&[ir::Expr]]) -> Option<u32> {
let starts = self.starts.as_ref()?;
let mut found: Option<(Start, Option<Vec<KindId>>)> = None;
for parts in rests {
let (start, heads) = starts.sequence(parts);
found = Some(match found {
Some((known, known_heads)) => (
known.merge(start),
analysis::joined(known_heads, heads.as_deref()),
),
None => (start, heads),
});
}
let (start, heads) = found?;
self.test(start, heads, false, false)
}
/// The body that a call of `rule` compiles as, if it is inlined.
fn inlined(&self, rule: RuleId) -> Option<&'f ir::Expr> {
let rules: &'f [ir::Rule] = self.rules;
(self.inline.get(rule.0 as usize) == Some(&true))
.then(|| rules.get(rule.0 as usize).map(|r| &r.body))
.flatten()
}
/// Whether the code of `expr` starts with an instruction that tests a byte, so that a
/// test before it saves nothing.
fn immediate(&self, expr: &ir::Expr) -> bool {
match expr {
ir::Expr::Literal { text, .. } => !text.is_empty(),
ir::Expr::Class(_) | ir::Expr::Any => true,
ir::Expr::Seq(parts) => parts.first().is_some_and(|part| self.immediate(part)),
ir::Expr::Call(rule) => self.inlined(*rule).is_some_and(|body| self.immediate(body)),
_ => false,
}
}
fn here(&self) -> u32 {
// The length is checked against u32 once the program is complete.
self.code.len() as u32
}
/// Append an instruction and return where it stands.
fn emit(&mut self, inst: Inst) -> usize {
self.code.push(inst);
self.code.len() - 1
}
/// Point the jump at `at` to the current end of the code.
fn patch(&mut self, at: usize) {
let target = self.here();
match &mut self.code[at] {
Inst::Choice(t)
| Inst::Commit(t)
| Inst::PredChoice(t)
| Inst::BackCommit(t)
| Inst::LabelChoice(t)
| Inst::Test { target: t, .. }
| Inst::RepOpen { end: t, .. } => *t = target,
_ => {}
}
}
/// Compile an expression. It recurses as deeply as the expression nests, which the
/// resolver bounds.
fn expr(&mut self, expr: &'f ir::Expr) {
use ir::Expr as E;
match expr {
E::Empty => {}
E::Literal {
text,
case_insensitive,
} => self.literal(expr, text.as_bytes(), *case_insensitive),
E::Class(set) => {
let class = self.class(expr, set);
self.emit(Inst::Class(class));
}
E::Any => {
self.emit(Inst::Any);
}
E::Call(rule) if self.seed_rule == Some(*rule) => {
self.seed_rule = None;
self.emit(Inst::SeedUse);
}
// What stands around the call is not known to the body, as for a shared part.
E::Call(rule) => match self.inlined(*rule) {
Some(body) => self.inside(true, body),
None => {
let at = self.emit(Inst::Call(0));
self.calls.push((at, *rule));
}
},
E::Seq(parts) => {
for part in parts {
self.expr(part);
}
}
E::Choice(alternatives) => self.choice(alternatives),
E::Repeat { expr, min, max } => self.repeat(expr, *min, *max),
E::And(inner) => {
let choice = self.emit(Inst::PredChoice(0));
self.caught(false, inner, false);
let back = self.emit(Inst::BackCommit(0));
self.patch(choice);
self.emit(Inst::Fail);
self.patch(back);
}
E::Not(inner) => {
let choice = self.emit(Inst::PredChoice(0));
self.caught(false, inner, false);
self.emit(Inst::FailTwice);
self.patch(choice);
}
E::Cut => {
self.emit(Inst::Cut);
}
E::Skip => {
if self.has_skip {
self.emit(Inst::Skip);
}
}
E::Token { kind, hidden, expr } => {
self.emit(Inst::TokenOpen(*kind));
self.expr(expr);
self.emit(Inst::TokenClose { hidden: *hidden });
}
E::Node { kind, expr } => self.collect(expr, Inst::CloseNode(*kind)),
E::Transparent { kind, expr } => self.collect(expr, Inst::CloseTransparent(*kind)),
E::Field { field, expr } => self.collect(expr, Inst::CloseField(*field)),
E::WordEnd => {
self.emit(Inst::WordEnd);
}
E::Unreserved(inner) => {
self.emit(Inst::Mark);
self.expr(inner);
self.emit(Inst::Unreserved);
}
E::FlagOn { flag, expr } => match flag_bit(*flag) {
Some(bit) => {
self.emit(Inst::FlagOn(bit));
self.expr(expr);
self.emit(Inst::FlagsPop);
}
None => {
self.emit(Inst::Fail);
}
},
E::FlagOff { flag, expr } => match flag_bit(*flag) {
Some(bit) => {
self.emit(Inst::FlagOff(bit));
self.expr(expr);
self.emit(Inst::FlagsPop);
}
None => {
self.emit(Inst::Fail);
}
},
E::FlagTest { flag, negated } => match flag_bit(*flag) {
Some(flag) => {
self.emit(Inst::FlagTest {
flag,
negated: *negated,
});
}
None => {
self.emit(Inst::Fail);
}
},
E::Label { label, expr, skip } => {
let choice = self.emit(Inst::LabelChoice(0));
// A failure gives back what the expression captured before the skip that finds
// the error's position, which may match a capture.
let defines = self.defines(expr);
if defines {
self.emit(Inst::CaptureMark);
}
self.expr(expr);
if defines {
self.emit(Inst::CaptureKeep);
}
let commit = self.emit(Inst::Commit(0));
self.patch(choice);
if *skip && self.has_skip {
self.emit(Inst::Skip);
}
match self.recovered.get(label.0 as usize) {
Some(true) => {
let field = match &**expr {
E::Field { field, .. } => *field,
_ => FieldId::NONE,
};
self.emit(Inst::Recover {
label: label.0,
field,
});
}
_ => {
self.emit(Inst::Throw(label.0));
}
}
self.patch(commit);
}
E::ErrorAlternative {
expr,
message,
index,
skip,
} => {
if let Some(slot @ None) = self.error_alternatives.get_mut(*index as usize) {
*slot = Some((&**expr, message.as_str(), *skip));
}
self.emit(Inst::ErrorAttempt(*index));
}
E::Shared(expr) => {
let key = Arc::as_ptr(expr);
let index = match self.shared.get(&key) {
Some(&index) => index,
None => {
let index = self.shared_pcs.len();
self.shared_pcs.push(0);
self.shared.insert(key, index);
self.queue.push((index, &**expr));
index
}
};
let at = self.emit(Inst::Sub(0));
self.subs.push((at, index));
}
E::Indent { expr, skip } => {
let skip = self.skip_before(*skip);
self.emit(Inst::Indent { skip });
self.expr(expr);
self.emit(Inst::IndentEnd);
}
E::Same { skip } => {
let skip = self.skip_before(*skip);
self.emit(Inst::Same { skip });
}
E::End { skip } => {
let skip = self.skip_before(*skip);
self.emit(Inst::AtEnd { skip });
}
E::Def {
capture,
expr,
skip,
} => {
let skip = self.skip_before(*skip);
self.emit(Inst::DefOpen { skip });
self.expr(expr);
self.emit(Inst::DefClose(capture.0));
}
E::Ref { capture } => {
self.emit(Inst::Ref(capture.0));
}
}
}
/// Where a column check or a capture looks past ignored text first, the `Mark` and the skip
/// before it, and whether it has them: the column is checked, or the text starts, where the
/// skip ends, and what follows starts where it started, with the same skip, which the skip
/// cache answers.
fn skip_before(&mut self, skip: bool) -> bool {
let skip = skip && self.has_skip;
if skip {
self.emit(Inst::Mark);
self.emit(Inst::Skip);
}
skip
}
/// Compile `expr` where a cut can mark an entry of the routine, or cannot.
fn inside(&mut self, cuttable: bool, expr: &'f ir::Expr) {
let outer = std::mem::replace(&mut self.cuttable, cuttable);
self.expr(expr);
self.cuttable = outer;
}
/// As [`Compiler::inside`], for code whose failure an entry catches: where the code can make
/// a capture, a failure gives it back, and a success keeps it where `keep` says so, which a
/// lookahead does not.
fn caught(&mut self, cuttable: bool, expr: &'f ir::Expr, keep: bool) {
let defines = self.defines(expr);
if defines {
self.emit(Inst::CaptureMark);
}
self.inside(cuttable, expr);
if defines {
self.emit(if keep {
Inst::CaptureKeep
} else {
Inst::CaptureDrop
});
}
}
/// Whether `expr` can make a capture in the activation it runs in: through a `%def` in it or
/// in a shared part it calls. Not through a rule it calls, since a rule that makes one is
/// never compiled into its callers. A shared part is looked at once.
fn defines(&mut self, expr: &ir::Expr) -> bool {
match expr {
ir::Expr::Def { .. } => true,
ir::Expr::Call(_) => false,
ir::Expr::Shared(inner) => {
let key = Arc::as_ptr(inner);
if let Some(&known) = self.defines_shared.get(&key) {
return known;
}
let found = self.defines(inner);
self.defines_shared.insert(key, found);
found
}
_ => expr.children().iter().any(|child| self.defines(child)),
}
}
/// Compile an expression that is a choice point of the semantics and has no entry of its
/// own: the last alternative of a choice, an expression repeated exactly once, the skip
/// expression. A cut in it must not mark an entry around it, so where it holds a free cut
/// and there is such an entry, it gets a guard, a choice entry that resumes at a failure.
fn cut_scope(&mut self, expr: &'f ir::Expr) {
if !(self.cuttable && self.free_cut(expr)) {
self.expr(expr);
return;
}
let guard = self.emit(Inst::Choice(0));
self.expr(expr);
let commit = self.emit(Inst::Commit(0));
self.patch(guard);
self.emit(Inst::Fail);
self.patch(commit);
}
/// Whether `expr` holds a cut that no choice, repetition, lookahead or call inside it
/// takes. A shared part is looked at once.
fn free_cut(&mut self, expr: &ir::Expr) -> bool {
use ir::Expr as E;
match expr {
E::Cut => true,
E::Seq(parts) => parts.iter().any(|part| self.free_cut(part)),
E::Token { expr, .. }
| E::Node { expr, .. }
| E::Transparent { expr, .. }
| E::Field { expr, .. }
| E::Unreserved(expr)
| E::FlagOn { expr, .. }
| E::FlagOff { expr, .. }
| E::Indent { expr, .. }
| E::Def { expr, .. }
| E::Label { expr, .. } => self.free_cut(expr),
E::Shared(expr) => {
let key = Arc::as_ptr(expr);
if let Some(&known) = self.free_cuts.get(&key) {
return known;
}
let free = self.free_cut(expr);
self.free_cuts.insert(key, free);
free
}
E::Empty
| E::Literal { .. }
| E::Class(_)
| E::Any
| E::Call(_)
| E::Choice(_)
| E::Repeat { .. }
| E::And(_)
| E::Not(_)
| E::Skip
| E::WordEnd
| E::FlagTest { .. }
| E::Same { .. }
| E::End { .. }
| E::Ref { .. }
| E::ErrorAlternative { .. } => false,
}
}
/// Compile the literal `expr` of these bytes.
fn literal(&mut self, expr: &ir::Expr, bytes: &[u8], case_insensitive: bool) {
let folds = case_insensitive && bytes.iter().any(u8::is_ascii_alphabetic);
match bytes {
[] => {}
[byte] if byte.is_ascii() && !folds => {
self.emit(Inst::Char(*byte));
}
_ => {
let key = std::ptr::from_ref(expr);
let id = match self.strings_of.get(&key) {
Some(&id) => id,
None => {
let next = self.strings.len() as u32;
let id = *self.string_ids.entry(bytes.into()).or_insert(next);
if id == next {
self.strings.push(bytes.into());
}
self.strings_of.insert(key, id);
id
}
};
self.emit(if folds {
Inst::StrFold(id)
} else {
Inst::Str(id)
});
}
}
}
/// The test of the next byte before `expr`, if one is worth it: where `expr` cannot
/// succeed without consuming input, does not start with every byte or with an instruction
/// that tests the byte anyway, takes all its first bytes after a skip or all without one,
/// and a failure before its first byte is a plain one. Where `expr` would try tokens
/// there, the test records their failures, so it must surely try them, and with no rule
/// in between that keeps the growing loop. With `pop`, a mismatch pops the entry of the
/// loop the test is in.
fn test_for(&mut self, expr: &ir::Expr, pop: bool) -> Option<u32> {
let starts = self.starts.as_ref()?;
let start = starts.of(expr);
let heads = starts.heads(expr).map(<[KindId]>::to_vec);
let immediate = self.immediate(expr);
self.test(start, heads, immediate, pop)
}
/// The test before code with this start and these head kinds, as [`Compiler::test_for`]
/// finds it, `immediate` where the code tests a byte at once.
fn test(
&mut self,
start: Start,
heads: Option<Vec<KindId>>,
immediate: bool,
pop: bool,
) -> Option<u32> {
let after_skip = start.after_skip && self.has_skip;
let worth = !start.nullable && start.bytes != Bytes::ALL && !immediate;
if !worth || !start.plain || (after_skip && start.at_once) {
return None;
}
let kinds: Box<[KindId]> = match start.tokens {
false => Box::default(),
true if start.clean && !start.growing => heads?.into(),
true => return None,
};
let test = ByteTest {
bytes: start.bytes,
after_skip,
pop,
kinds,
};
let next = self.tests.len() as u32;
let id = *self.test_ids.entry(test.clone()).or_insert(next);
if id == next {
self.tests.push(test);
}
Some(id)
}
fn choice(&mut self, alternatives: &'f [ir::Expr]) {
let Some((last, rest)) = alternatives.split_last() else {
self.emit(Inst::Fail);
return;
};
// A test where an alternative starts with every byte the choice does fails only where
// every alternative does, and in a left-recursive level, whose alternatives all start
// with the level, only costs.
let starts = self.starts.as_ref();
let all = starts
.map(|s| (alternatives.iter()).fold(Bytes::default(), |u, a| u.union(s.of(a).bytes)));
let narrower = |c: &Self, alternative| {
(c.starts.as_ref()).is_some_and(|s| Some(s.of(alternative).bytes) != all)
};
let mut commits = Vec::new();
for alternative in rest {
let test = (narrower(self, alternative))
.then(|| self.test_for(alternative, false))
.flatten()
.map(|test| self.emit(Inst::Test { test, target: 0 }));
let choice = self.emit(Inst::Choice(0));
self.caught(true, alternative, true);
commits.push(self.emit(Inst::Commit(0)));
self.patch(choice);
if let Some(test) = test {
self.patch(test);
}
}
if narrower(self, last)
&& let Some(test) = self.test_for(last, false)
{
self.emit(Inst::TestFail(test));
}
self.cut_scope(last);
for commit in commits {
self.patch(commit);
}
}
/// The class of the first alternative of `expr` and the alternatives after it, where a
/// loop over `expr` can split as `c* (e c*)*`: the first alternative is a class or one
/// code point, maybe through a call compiled as its body, and no other holds a free cut.
/// Both forms take `c` where it matches and try `e` where it does not, and a step of `e`
/// that matches nothing ends both, since `c*` then matches nothing either.
fn split(&mut self, expr: &'f ir::Expr) -> Option<(u32, &'f [ir::Expr])> {
let ir::Expr::Choice(alternatives) = self.seen_through(expr) else {
return None;
};
let (first, rest) = alternatives.split_first()?;
let points = self.seen_through(first);
let set = span_set(points)?;
let free = rest.iter().any(|alternative| self.free_cut(alternative));
(self.passes.split && !rest.is_empty() && !free).then(|| (self.class(points, &set), rest))
}
/// `expr`, or the body it compiles as where it is a call compiled into its caller.
fn seen_through(&self, expr: &'f ir::Expr) -> &'f ir::Expr {
match expr {
ir::Expr::Call(rule) => {
(self.inlined(*rule)).map_or(expr, |body| self.seen_through(body))
}
_ => expr,
}
}
/// The class of `set`, the code points of the expression `points`, added to the program
/// once for each such expression.
fn class(&mut self, points: &ir::Expr, set: &ir::CharSet) -> u32 {
let key = std::ptr::from_ref(points);
if let Some(&class) = self.classes_of.get(&key) {
return class;
}
self.classes.push(Class::new(set));
let class = (self.classes.len() - 1) as u32;
self.classes_of.insert(key, class);
class
}
fn repeat(&mut self, expr: &'f ir::Expr, min: u32, max: Option<u32>) {
if self.passes.span
&& max.is_none()
&& let Some((points, set, min)) = span(expr, min)
{
let class = self.class(points, &set);
self.emit(Inst::Span { class, min });
return;
}
match (min, max) {
(0, Some(0)) => {}
// The resolver rejects a maximum below the minimum.
(_, Some(max)) if max < min => {
self.emit(Inst::Fail);
}
(1, Some(1)) => self.cut_scope(expr),
(0, Some(1)) => {
let test = (self.test_for(expr, false))
.map(|test| self.emit(Inst::Test { test, target: 0 }));
let choice = self.emit(Inst::Choice(0));
self.caught(true, expr, true);
let commit = self.emit(Inst::Commit(0));
self.patch(choice);
self.patch(commit);
if let Some(test) = test {
self.patch(test);
}
}
(0, None) if let Some((class, rest)) = self.split(expr) => {
self.emit(Inst::Span { class, min: 0 });
let choice = self.emit(Inst::Choice(0));
let step = self.here();
let test = match rest {
[one] => (self.test_for(one, true))
.map(|test| self.emit(Inst::Test { test, target: 0 })),
_ => None,
};
let defines = rest.iter().any(|alternative| self.defines(alternative));
if defines {
self.emit(Inst::CaptureMark);
}
match rest {
[one] => self.inside(true, one),
_ => self.inside_choice(rest),
}
if defines {
self.emit(Inst::CaptureKeep);
}
self.emit(Inst::Span { class, min: 0 });
self.emit(Inst::LoopCommit(step));
self.patch(choice);
if let Some(test) = test {
self.patch(test);
}
}
(0, None) => {
let choice = self.emit(Inst::Choice(0));
let step = self.here();
let test = (self.test_for(expr, true))
.map(|test| self.emit(Inst::Test { test, target: 0 }));
self.caught(true, expr, true);
self.emit(Inst::LoopCommit(step));
self.patch(choice);
if let Some(test) = test {
self.patch(test);
}
}
(min, max) => {
self.bounds.push((min, max.unwrap_or(u32::MAX)));
let bounds = (self.bounds.len() - 1) as u32;
let open = self.emit(Inst::RepOpen { end: 0, bounds });
let step = self.here();
self.caught(true, expr, true);
self.emit(Inst::RepNext(step));
self.patch(open);
}
}
}
/// The alternatives as a choice where a cut can mark an entry of the routine.
fn inside_choice(&mut self, alternatives: &'f [ir::Expr]) {
let outer = std::mem::replace(&mut self.cuttable, true);
self.choice(alternatives);
self.cuttable = outer;
}
/// `Open`, the expression, and the instruction that closes what it collected.
fn collect(&mut self, expr: &'f ir::Expr, close: Inst) {
self.emit(Inst::Open);
self.expr(expr);
self.emit(close);
}
}