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
//! Expansion (*TeX: The Program* §366–§401, with e-TeX and LuaTeX
//! additions): `expand`, macro calls, `\csname`, `\noexpand`, `\input`.
use std::rc::Rc;
use crate::Engine;
use crate::cmd::Cmd;
use crate::hash::{CsId, NO_CS};
use crate::input::{Level, ScannerStatus, TokKind};
use crate::primitives_gen::assign_int as ip;
use crate::token::{Token, TokenList};
/// Maximum expansion nesting before LuaTeX gives up.
pub const EXPAND_DEPTH: u32 = 10000;
/// `cs_name` modifiers.
pub mod cs_name_codes {
/// `\csname`.
pub const CSNAME: u32 = 0;
/// `\lastnamedcs`.
pub const LAST_NAMED_CS: u32 = 1;
/// `\begincsname`.
pub const BEGIN_CSNAME: u32 = 2;
}
/// Whether `t` is a `match` or `end_match` token.
#[inline]
fn is_match_or_end(t: Token) -> bool {
!t.is_cs() && (t.cmd_code() == Cmd::ParEnd as u32 || t.cmd_code() == Cmd::Stop as u32)
}
/// Whether `t` is a left or right brace character token
/// (`cur_tok < right_brace_limit`).
#[inline]
fn is_brace(t: Token) -> bool {
!t.is_cs() && (t.cmd_code() == Cmd::LeftBrace as u32 || t.cmd_code() == Cmd::RightBrace as u32)
}
#[inline]
fn is_left_brace(t: Token) -> bool {
!t.is_cs() && t.cmd_code() == Cmd::LeftBrace as u32
}
impl Engine {
/// Gets the next token, expanding macros and other expandable commands
/// (`get_x_token`).
pub fn get_x_token(&mut self) {
self.get_next();
if self.cur_cmd > Cmd::MAX_COMMAND {
self.get_x_token_expanding();
} else {
self.cur_tok = self.make_cur_tok();
}
}
/// [`get_x_token`](Self::get_x_token) once an expandable token has been
/// read.
#[inline(never)]
fn get_x_token_expanding(&mut self) {
loop {
if self.cur_cmd >= Cmd::Call {
if self.cur_cmd < Cmd::EndTemplate {
self.macro_call();
} else {
self.cur_cs = self.cs.frozen_endv;
self.cur_cmd = Cmd::Endv;
break;
}
} else {
// Primitive expansion (\expandafter, \csname, \ifnum, …)
// may expand macros whose bodies are not executed.
let exec = std::mem::replace(&mut self.exec_context, false);
self.expand();
self.exec_context = exec;
}
self.get_next();
if self.cur_cmd <= Cmd::MAX_COMMAND {
break;
}
}
self.cur_tok = self.make_cur_tok();
}
/// Expands the current token while it is expandable (`x_token`).
pub fn x_token(&mut self) {
while self.cur_cmd > Cmd::MAX_COMMAND {
if self.cur_cmd == Cmd::EndTemplate {
// LuaTeX converts `\endtemplate` in place (as
// `get_x_token` does) instead of backing up `\endv`.
self.cur_cs = self.cs.frozen_endv;
self.cur_cmd = Cmd::Endv;
break;
}
if self.cur_cmd >= Cmd::Call {
self.expand();
} else {
// As in `get_x_token`: what a primitive expands is not
// executed by main control.
let exec = std::mem::replace(&mut self.exec_context, false);
self.expand();
self.exec_context = exec;
}
self.get_next();
}
self.cur_tok = self.make_cur_tok();
}
/// Expands the current (expandable) command (`expand`).
pub fn expand(&mut self) {
let cv = self.cur_val;
let cvl = self.cur_val_level;
let cg = self.cur_glue;
// Only this expansion is a single step: expansions nested in it
// (a number scanned by `\romannumeral`, …) are not.
let one_step = std::mem::take(&mut self.one_step);
self.expand_depth += 1;
if self.expand_depth >= EXPAND_DEPTH {
self.overflow("expansion depth", i64::from(EXPAND_DEPTH));
}
loop {
if self.cur_cmd < Cmd::Call {
if self.eqtb.int(ip::TRACINGCOMMANDS) > 1 {
self.show_cur_cmd_chr();
}
match self.cur_cmd {
Cmd::TopBotMark => self.insert_mark_text(),
Cmd::ExpandAfter => {
if self.cur_chr == 0 {
// \expandafter
self.get_token();
let t = self.cur_tok;
self.get_token();
if self.cur_cmd > Cmd::MAX_COMMAND {
self.one_step = true;
self.expand();
} else {
self.back_input();
}
self.cur_tok = t;
self.back_input();
} else {
// \unless
self.get_token();
if self.cur_cmd == Cmd::IfTest
&& self.cur_chr != crate::cond::if_codes::IF_CASE
{
self.cur_chr += crate::cond::UNLESS_CODE;
continue; // reswitch
}
self.print_err("You can't use `");
self.print_esc_str("unless");
self.printer.print_str("' before `");
self.print_cmd_chr(self.cur_cmd, self.cur_chr);
self.printer.print_char('\'' as u32);
self.help(&["Continue, and I'll forget that it ever happened."]);
self.back_error();
}
}
Cmd::NoExpand => {
if self.cur_chr == 0 {
self.no_expand();
} else {
self.primitive_cmd();
if self.cur_cmd > Cmd::MAX_COMMAND && self.cur_cmd != Cmd::NoExpand {
continue;
}
if self.cur_cmd <= Cmd::MAX_COMMAND {
self.back_input();
}
}
}
Cmd::CsName => self.manufacture_cs_name(),
Cmd::Convert => self.conv_toks(),
Cmd::The => self.ins_the_toks(),
Cmd::IfTest => self.conditional(),
Cmd::FiOrElse => self.fi_or_else(),
Cmd::Input => self.input_command(),
Cmd::LuaExpandableCall | Cmd::LuaLocalCall => self.lua_expandable_call(),
Cmd::Variable | Cmd::Feedback => self.backend_expandable(),
_ => {
self.print_err("Undefined control sequence");
self.help(&[
"The control sequence at the end of the top line",
"of your error message was never \\def'ed. If you have",
"misspelled it (e.g., `\\hobx'), type `I' and the correct",
"spelling (e.g., `I\\hbox'). Otherwise just continue,",
"and I'll forget about whatever was undefined.",
]);
self.error();
}
}
} else if self.cur_cmd < Cmd::EndTemplate {
self.one_step = one_step;
self.macro_call();
} else {
self.cur_tok = Token::cs(self.cs.frozen_endv);
self.back_input();
}
break;
}
self.expand_depth -= 1;
self.cur_val = cv;
self.cur_val_level = cvl;
self.cur_glue = cg;
}
/// `\noexpand`: makes the next token unexpandable once (§369).
fn no_expand(&mut self) {
let save = self.input.scanner_status;
self.input.scanner_status = ScannerStatus::Normal;
self.get_token();
self.input.scanner_status = save;
let t = self.cur_tok;
if t.is_cs() {
// Pop exhausted lists as back_input would.
while let Some(Level::Tokens(l)) = self.input.stack.last() {
if l.loc < l.list.len() || l.kind == TokKind::VTemplate {
break;
}
self.end_token_list();
}
let list: TokenList = Rc::from(vec![Token::cs(self.cs.frozen_dont_expand), t]);
self.begin_token_list(list, TokKind::BackedUp);
} else {
self.back_input();
}
}
/// `\primitive`: replaces the next control sequence by the primitive of
/// that name, if there is one (LuaTeX).
fn primitive_cmd(&mut self) {
let save = self.input.scanner_status;
self.input.scanner_status = ScannerStatus::Normal;
self.get_token();
self.input.scanner_status = save;
if self.cur_cs != NO_CS
&& let Some(p) = crate::primitives::lookup(self.hash.name(self.cur_cs))
{
self.cur_cmd = p.cmd;
self.cur_chr = p.chr;
// The frozen copy carries the primitive meaning.
let frozen = self.frozen_primitive(self.cur_cs);
self.cur_cs = frozen;
self.cur_tok = Token::cs(frozen);
if self.cur_cmd > Cmd::MAX_COMMAND {
return;
}
return;
}
self.cur_cmd = Cmd::Relax;
self.cur_chr = 0;
self.back_input();
self.cur_cmd = Cmd::NoExpand;
}
/// Looks up the control sequence named by `\ifcsname` or
/// `\begincsname` and records it for `\lastnamedcs`: an undefined
/// name gives the undefined control sequence, an empty name none.
pub(crate) fn lookup_named_cs(&mut self, name: &[u8]) -> CsId {
let cs = if name.is_empty() {
NO_CS
} else {
match self.hash.lookup(name) {
Some(cs) if self.eqtb.meaning(cs).cmd != Cmd::UndefinedCs => cs,
_ => crate::hash::UNDEFINED_CS,
}
};
self.last_cs_name = cs;
cs
}
/// Builds the control sequence for `\csname…\endcsname` (§372).
fn manufacture_cs_name(&mut self) {
if self.cur_chr == cs_name_codes::LAST_NAMED_CS {
if self.last_cs_name != NO_CS {
self.cur_tok = Token::cs(self.last_cs_name);
self.back_input();
}
return;
}
let begin = self.cur_chr == cs_name_codes::BEGIN_CSNAME;
let e = self.cond.is_in_csname;
self.cond.is_in_csname = true;
let mut name: Vec<u8> = Vec::new();
loop {
self.get_x_token();
if self.cur_cs != NO_CS {
break;
}
crate::utf8x::push(&mut name, self.cur_chr);
}
if self.cur_cmd != Cmd::EndCsName {
self.complain_missing_endcsname();
}
self.cond.is_in_csname = e;
if begin {
let cs = self.lookup_named_cs(&name);
if cs != NO_CS && cs != crate::hash::UNDEFINED_CS {
self.cur_tok = Token::cs(cs);
self.back_input();
}
return;
}
let cs = self.hash.lookup_or_insert(&name);
self.last_cs_name = cs;
if self.eqtb.meaning(cs).cmd == Cmd::UndefinedCs {
self.eq_define(
crate::eqtb::Loc::Cs(cs),
crate::eqtb::Val::Meaning(crate::eqtb::Meaning::prim(Cmd::Relax, RELAX_CHR)),
);
}
self.cur_tok = Token::cs(cs);
self.back_input();
}
/// Reports a missing `\endcsname`.
pub fn complain_missing_endcsname(&mut self) {
self.print_err("Missing ");
self.print_esc_str("endcsname");
self.printer.print_str(" inserted");
self.help(&[
"The control sequence marked <to be read again> should",
"not appear between \\csname and \\endcsname.",
]);
self.back_error();
}
/// Inserts `\relax` after the current token (`insert_relax`).
pub fn insert_relax(&mut self) {
self.cur_tok = Token::cs(self.cur_cs);
self.back_input();
self.cur_tok = Token::cs(self.cs.frozen_relax);
self.back_input();
if let Some(Level::Tokens(t)) = self.input.stack.last_mut() {
t.kind = TokKind::Inserted;
}
}
/// `\input`, `\endinput`, `\scantokens`, `\scantextokens`.
fn input_command(&mut self) {
match self.cur_chr {
1 => self.input.force_eof = true,
2 | 3 => self.pseudo_start(self.cur_chr == 3),
_ => {
if self.files.name_in_progress {
self.insert_relax();
} else {
self.start_input();
}
}
}
}
/// Inserts the text of a mark (`top_bot_mark`, §386).
fn insert_mark_text(&mut self) {
let mut code = self.cur_chr;
let class = if code >= 5 {
code -= 5;
self.scan_register_num();
self.cur_val as u32
} else {
0
};
if let Some(list) = self.marks.get(class, code)
&& !list.is_empty()
{
self.begin_token_list(list, TokKind::MarkText);
}
}
/// Calls a user macro (`macro_call`, §389).
pub fn macro_call(&mut self) {
let one_step = std::mem::take(&mut self.one_step);
self.check_snapshot();
if self.profiler.is_some() {
self.profile_call(self.cur_cs);
}
// Fast path: a macro without parameters, not traced.
if let Some(mac) = &self.eqtb.meaning(self.cur_cs).mac
&& mac.tokens[0] == Token::END_MATCH
&& self.eqtb.int(ip::TRACINGMACROS) <= 0
{
let tokens = mac.tokens.clone();
// A native implementation (`crate::native`); 1 = known to
// have none.
let native = (mac.native.get() != 1).then(|| Rc::clone(mac));
while let Some(Level::Tokens(t)) = self.input.stack.last() {
if t.loc < t.list.len() || t.kind == TokKind::VTemplate {
break;
}
self.end_token_list();
}
if let Some(mac) = native
&& let Some(i) = self.native_of(&mac, self.cur_cs)
&& self.run_native(i, &[], one_step)
{
return;
}
let param_start = self.input.param_stack.len();
self.begin_macro(tokens, 1, self.cur_cs, param_start);
return;
}
// The argument being scanned is kept in `runaway_list` while
// tokens are read, to be shown if it runs away; an enclosing
// scan's list is put back afterwards.
let saved_runaway = std::mem::take(&mut self.runaway_list);
self.macro_call_with_args(one_step);
self.runaway_list = saved_runaway;
}
/// [`macro_call`](Self::macro_call) for a macro with parameters or
/// traced; `one_step` if its body will not be expanded further (see
/// [`Engine::one_step`]).
fn macro_call_with_args(&mut self, one_step: bool) {
let save_scanner_status = self.input.scanner_status;
let save_warning_index = self.input.warning_index;
let save_cs = self.cur_cs;
self.input.warning_index = self.cur_cs;
let Some(mac) = self.eqtb.meaning(self.cur_cs).mac.clone() else {
self.input.scanner_status = save_scanner_status;
self.input.warning_index = save_warning_index;
return;
};
let tokens = mac.tokens.clone();
if self.eqtb.int(ip::TRACINGMACROS) > 0 {
self.begin_diagnostic();
self.printer.print_ln();
self.print_input_level();
self.print_cs(self.input.warning_index);
self.token_show_list(&tokens, 0, tokens.len(), 10_000_000);
self.end_diagnostic(false);
}
let mut r = 0usize;
// The arguments, in a buffer reused from call to call.
let mut pstack: Vec<TokenList> = std::mem::take(&mut self.param_scratch);
pstack.clear();
if tokens[0] != Token::END_MATCH {
self.input.scanner_status = ScannerStatus::Matching;
let mut unbalance: i32 = 0;
self.long_state = self.cur_cmd;
if self.long_state >= Cmd::OuterCall {
self.long_state = if self.long_state == Cmd::OuterCall {
Cmd::Call
} else {
Cmd::LongCall
};
}
let par_token = Token::cs(self.cs.par);
loop {
// One parameter (or the leading compulsory delimiter). The
// scratch buffer is reused from call to call.
// The argument is built in `runaway_list`, where a runaway
// error (in get_token) shows it.
self.runaway_list = std::mem::take(&mut self.arg_scratch);
self.runaway_list.clear();
let s: Option<usize>;
let mut match_chr = 0u32;
let mut m = 0i32;
let mut rbrace_ptr = 0usize;
if !is_match_or_end(tokens[r]) || tokens[r] == Token::END_MATCH {
s = None;
} else {
match_chr = tokens[r].chr();
s = Some(r + 1);
r += 1;
}
'cont: loop {
self.get_token();
if self.cur_tok == tokens[r] {
r += 1;
if is_match_or_end(tokens[r]) {
if is_left_brace(self.cur_tok) {
self.input.align_state -= 1;
}
break 'cont; // found
}
continue 'cont;
}
// Contribute recently matched tokens.
if s != Some(r) {
match s {
None => {
self.print_err("Use of ");
self.sprint_cs(self.input.warning_index);
self.printer.print_str(" doesn't match its definition");
self.help(&[
"If you say, e.g., `\\def\\a1{...}', then you must always",
"put `1' after `\\a', since control sequence names are",
"made up of letters only. The macro here has not been",
"followed by the required stuff, so I'm ignoring it.",
]);
self.error();
self.runaway_list.clear();
self.input.scanner_status = save_scanner_status;
self.input.warning_index = save_warning_index;
return;
}
Some(s) => {
let mut t = s;
loop {
self.runaway_list.push(tokens[t]);
m += 1;
let mut u = t + 1;
let mut v = s;
let mut resumed = false;
loop {
if u == r {
if self.cur_tok != tokens[v] {
break;
}
r = v + 1;
resumed = true;
break;
}
if tokens[u] != tokens[v] {
break;
}
u += 1;
v += 1;
}
if resumed {
continue 'cont;
}
t += 1;
if t == r {
break;
}
}
r = s;
}
}
}
if self.cur_tok == par_token && self.long_state != Cmd::LongCall {
self.report_runaway_argument();
self.input.align_state -= unbalance;
self.input.scanner_status = save_scanner_status;
self.input.warning_index = save_warning_index;
return;
}
if is_brace(self.cur_tok) {
if is_left_brace(self.cur_tok) {
// Contribute an entire group.
unbalance = 1;
loop {
self.runaway_list.push(self.cur_tok);
self.get_token();
if self.cur_tok == par_token && self.long_state != Cmd::LongCall {
self.report_runaway_argument();
self.input.align_state -= unbalance;
self.input.scanner_status = save_scanner_status;
self.input.warning_index = save_warning_index;
return;
}
if is_brace(self.cur_tok) {
if is_left_brace(self.cur_tok) {
unbalance += 1;
} else {
unbalance -= 1;
if unbalance == 0 {
break;
}
}
}
}
rbrace_ptr = self.runaway_list.len();
self.runaway_list.push(self.cur_tok);
} else {
// An extra right brace.
self.back_input();
self.print_err("Argument of ");
self.sprint_cs(self.input.warning_index);
self.printer.print_str(" has an extra }");
self.help(&[
"I've run across a `}' that doesn't seem to match anything.",
"For example, `\\def\\a#1{...}' and `\\a}' would produce",
"this error. If you simply proceed now, the `\\par' that",
"I've just inserted will cause me to report a runaway",
"argument that might be the root of the problem. But if",
"your `}' was spurious, just type `2' and it will go away.",
]);
self.input.align_state += 1;
self.long_state = Cmd::Call;
self.cur_tok = par_token;
self.ins_error();
continue 'cont;
}
} else {
if self.cur_tok == Token::SPACE && is_match_or_end(tokens[r]) {
continue 'cont;
}
self.runaway_list.push(self.cur_tok);
}
m += 1;
if !is_match_or_end(tokens[r]) {
continue 'cont;
}
break 'cont;
}
// found:
if s.is_some() {
// Tidy up the parameter just scanned.
let param: TokenList = if m == 1
&& self.runaway_list.last().is_some_and(|&t| is_brace(t))
&& rbrace_ptr > 0
{
Rc::from(&self.runaway_list[1..rbrace_ptr])
} else {
Rc::from(&self.runaway_list[..])
};
pstack.push(param);
let param = &pstack[pstack.len() - 1];
if self.eqtb.int(ip::TRACINGMACROS) > 0 {
self.begin_diagnostic();
if !self.print_input_level() {
self.printer.begin_line();
}
self.printer.print_char(match_chr);
self.printer.print_int(pstack.len() as i64);
self.printer.print_str("<-");
self.show_token_list(param, 0, param.len(), 1000);
self.end_diagnostic(false);
}
}
self.arg_scratch = std::mem::take(&mut self.runaway_list);
if tokens[r] == Token::END_MATCH {
break;
}
}
}
// Feed the macro body and its parameters to the scanner.
while let Some(Level::Tokens(t)) = self.input.stack.last() {
if t.loc < t.list.len() || t.kind == TokKind::VTemplate {
break;
}
self.end_token_list();
}
if let Some(i) = self.native_of(&mac, save_cs)
&& self.run_native(i, &pstack, one_step)
{
pstack.clear();
self.param_scratch = pstack;
self.input.scanner_status = save_scanner_status;
self.input.warning_index = save_warning_index;
return;
}
let param_start = self.input.param_stack.len();
self.input.param_stack.append(&mut pstack);
self.param_scratch = pstack;
self.begin_macro(tokens, r + 1, self.input.warning_index, param_start);
self.input.scanner_status = save_scanner_status;
self.input.warning_index = save_warning_index;
}
/// Reports a runaway argument ("Paragraph ended before …").
fn report_runaway_argument(&mut self) {
if self.long_state == Cmd::Call {
self.runaway();
self.print_err("Paragraph ended before ");
self.sprint_cs(self.input.warning_index);
self.printer.print_str(" was complete");
self.help(&[
"I suspect you've forgotten a `}', causing me to apply this",
"control sequence to too much text. How can we recover?",
"My plan is to forget the whole thing and hope for the best.",
]);
self.back_error();
}
}
/// Prints the text that has run away (`runaway`).
pub fn runaway(&mut self) {
let what = match self.input.scanner_status {
ScannerStatus::Defining => "definition",
ScannerStatus::Matching => "argument",
ScannerStatus::Aligning => "preamble",
ScannerStatus::Absorbing => "text",
_ => return,
};
self.printer.print_nl("Runaway ");
self.printer.print_str(what);
self.printer.print_char('?' as u32);
self.printer.print_ln();
let list = std::mem::take(&mut self.runaway_list);
let limit = self.printer.error_line.saturating_sub(10);
self.show_token_list(&list, 0, list.len(), limit as i64);
self.runaway_list = list;
}
/// Checks whether an `\outer` macro or end of file is allowed here
/// (`check_outer_validity`, §336).
pub fn check_outer_validity(&mut self) {
if self.input.scanner_status == ScannerStatus::Normal {
return;
}
if self.eqtb.int(ip::SUPPRESSOUTERERROR) > 0 && self.cur_cs != NO_CS {
return;
}
self.err.deletions_allowed = false;
// Back up an outer control sequence so that it can be reread.
if self.cur_cs != NO_CS {
let reread = match self.input.stack.last() {
Some(Level::Tokens(_)) => true,
Some(Level::Text(t)) => !matches!(
t.source,
crate::input::LineSource::Terminal | crate::input::LineSource::Read(_)
),
None => true,
};
if reread {
let list: TokenList = Rc::from(vec![Token::cs(self.cur_cs)]);
self.back_list(list);
}
self.cur_cmd = Cmd::Spacer;
self.cur_chr = ' ' as u32;
}
if self.input.scanner_status > ScannerStatus::Skipping {
self.runaway();
if self.cur_cs == NO_CS {
self.print_err("File ended");
} else {
self.cur_cs = NO_CS;
self.print_err("Forbidden control sequence found");
}
self.printer.print_str(" while scanning ");
let mut ins: Vec<Token> = Vec::new();
match self.input.scanner_status {
ScannerStatus::Defining => {
self.printer.print_str("definition");
ins.push(Token::RIGHT_BRACE);
}
ScannerStatus::Matching => {
self.printer.print_str("use");
ins.push(Token::cs(self.cs.par));
self.long_state = Cmd::OuterCall;
}
ScannerStatus::Aligning => {
self.printer.print_str("preamble");
ins.push(Token::cs(self.cs.frozen_cr));
ins.push(Token::RIGHT_BRACE);
self.input.align_state = -1_000_000;
}
_ => {
self.printer.print_str("text");
ins.push(Token::RIGHT_BRACE);
}
}
self.ins_list(Rc::from(ins));
self.printer.print_str(" of ");
self.sprint_cs(self.input.warning_index);
self.help(&[
"I suspect you have forgotten a `}', causing me",
"to read past where you wanted me to stop.",
"I'll try to recover; but if the error is serious,",
"you'd better type `E' or `X' now and fix your file.",
]);
self.error();
} else {
self.print_err("Incomplete ");
self.print_cmd_chr(Cmd::IfTest, self.cond.cur_if);
self.printer.print_str("; all text was ignored after line ");
self.printer.print_int(self.cond.skip_line.into());
let third = if self.cur_cs != NO_CS {
self.cur_cs = NO_CS;
"the matching `\\fi'. I've inserted a `\\fi'; this might work."
} else {
"The file ended while I was skipping conditional text."
};
self.help(&[
"A forbidden control sequence occurred in skipped text.",
"This kind of error happens when you say `\\if...' and forget",
third,
]);
self.cur_tok = Token::cs(self.cs.frozen_fi);
self.ins_error();
}
self.err.deletions_allowed = true;
}
/// Returns the frozen control sequence that carries the primitive
/// meaning of the primitive named like `cs` (for `\primitive`).
fn frozen_primitive(&mut self, cs: CsId) -> CsId {
let name = self.hash.name(cs).to_vec();
if let Some(&f) = self.frozen_prims.get(&name) {
return f;
}
let f = self.hash.new_frozen(&name);
if let Some(p) = crate::primitives::lookup(&name) {
self.eqtb.ensure_cs(f);
self.eqtb.cs[f.0 as usize].val = crate::eqtb::Meaning::prim(p.cmd, p.chr);
}
self.frozen_prims.insert(name, f);
f
}
}
/// The modifier of `\relax` (LuaTeX uses one past the largest character).
pub const RELAX_CHR: u32 = 0x11_0000;