parser.dart
26.6 KB
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
// The MIT License (MIT)
//
// Copyright (c) 2013-2019 Khan Academy and other contributors
// Copyright (c) 2020 znjameswu <znjameswu@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import 'dart:collection';
import 'dart:ui';
import 'package:collection/collection.dart';
import '../../ast/nodes/multiscripts.dart';
import '../../ast/nodes/over.dart';
import '../../ast/nodes/style.dart';
import '../../ast/nodes/symbol.dart';
import '../../ast/nodes/under.dart';
import '../../ast/options.dart';
import '../../ast/size.dart';
import '../../ast/style.dart';
import '../../ast/symbols/symbols_unicode.dart';
import '../../ast/syntax_tree.dart';
import '../../ast/types.dart';
import '../../font/metrics/unicode_scripts.dart';
import 'colors.dart';
import 'functions.dart';
import 'lexer.dart';
import 'macro_expander.dart';
import 'macros.dart';
import 'parse_error.dart';
import 'settings.dart';
import 'symbols.dart';
import 'token.dart';
import 'unicode_accents.dart';
/// Parser for TeX equations
///
/// Convert TeX string to Flutter Math's AST
class TexParser {
TexParser(String content, this.settings)
: this.leftrightDepth = 0,
this.mode = Mode.math,
this.macroExpander = MacroExpander(content, settings, Mode.math);
final TexParserSettings settings;
Mode mode;
int leftrightDepth;
final MacroExpander macroExpander;
Token? nextToken;
/// Get parse result
EquationRowNode parse() {
if (!this.settings.globalGroup) {
this.macroExpander.beginGroup();
}
if (this.settings.colorIsTextColor) {
this
.macroExpander
.macros
.set('\\color', MacroDefinition.fromString('\\textcolor'));
}
final parse = this.parseExpression(breakOnInfix: false);
this.expect('EOF');
if (!this.settings.globalGroup) {
this.macroExpander.endGroup();
}
return parse.wrapWithEquationRow();
}
List<GreenNode> parseExpression({
bool breakOnInfix = false,
String? breakOnTokenText,
bool infixArgumentMode = false,
}) {
final body = <GreenNode>[];
while (true) {
if (this.mode == Mode.math) {
this.consumeSpaces();
}
final lex = this.fetch();
if (endOfExpression.contains(lex.text)) {
break;
}
if (breakOnTokenText != null && lex.text == breakOnTokenText) {
break;
}
// Detects a infix function
final funcData = functions[lex.text];
if (funcData != null && funcData.infix == true) {
if (infixArgumentMode) {
throw ParseException('only one infix operator per group', lex);
}
if (breakOnInfix) {
break;
}
this.consume();
_enterArgumentParsingMode(lex.text, funcData);
try {
// A new way to handle infix operations
final atom = funcData.handler(
this,
FunctionContext(
funcName: lex.text,
breakOnTokenText: breakOnTokenText,
token: lex,
infixExistingArguments: List.of(body, growable: false),
),
);
body.clear();
body.add(atom);
} finally {
_leaveArgumentParsingMode(lex.text);
}
} else {
// Add a normal atom
final atom = this.parseAtom(breakOnTokenText);
if (atom == null) {
break;
}
body.add(atom);
}
}
return body;
// We will NOT handle ligatures between '-' and "'", as neither did MathJax.
// if (this.mode == Mode.text) {
// formLigatures(body);
// }
// We will not handle infix as well
// return handleInfixNodes(body);
}
static const Set<String> breakTokens = {
']',
'}',
'\\endgroup',
'\$',
'\\)',
'\\cr',
};
static const Set<String> endOfExpression = {
'}',
'\\endgroup',
'\\end',
'\\right',
'&',
};
static const Map<String, String> endOfGroup = {
'[': ']',
'{': '}',
'\\begingroup': '\\endgroup',
};
void expect(String text, {bool consume = true}) {
if (this.fetch().text != text) {
throw ParseException(
'Expected \'$text\', got \'${this.fetch().text}\'', this.fetch());
}
if (consume) {
this.consume();
}
}
void consumeSpaces() {
while (this.fetch().text == ' ') {
this.consume();
}
}
GreenNode? parseAtom(String? breakOnTokenText) {
final base = this.parseGroup('atom',
optional: false, greediness: null, breakOnTokenText: breakOnTokenText);
if (this.mode == Mode.text) {
return base;
}
final scriptsResult = parseScripts(
allowLimits:
base is EquationRowNode && base.overrideType == AtomType.op);
if (!scriptsResult.empty) {
if (scriptsResult.limits != true) {
return MultiscriptsNode(
base: base?.wrapWithEquationRow() ?? EquationRowNode.empty(),
sub: scriptsResult.subscript,
sup: scriptsResult.superscript,
);
} else {
var res = scriptsResult.superscript != null
? OverNode(
base: base?.wrapWithEquationRow() ?? EquationRowNode.empty(),
above: scriptsResult.superscript!)
: base;
res = scriptsResult.subscript != null
? UnderNode(
base: res?.wrapWithEquationRow() ?? EquationRowNode.empty(),
below: scriptsResult.subscript!)
: res;
return res;
}
} else {
return base;
}
}
/// The following functions are separated from parseAtoms in KaTeX
/// This function will only be invoked in math mode
ScriptsParsingResults parseScripts({bool allowLimits = false}) {
EquationRowNode? subscript;
EquationRowNode? superscript;
bool? limits;
loop:
while (true) {
this.consumeSpaces();
final lex = this.fetch();
switch (lex.text) {
case '\\limits':
case '\\nolimits':
if (!allowLimits) {
throw ParseException(
'Limit controls must follow a math operator', lex);
}
limits = lex.text == '\\limits';
this.consume();
break;
case '^':
if (superscript != null) {
throw ParseException('Double superscript', lex);
}
superscript = this._handleScript().wrapWithEquationRow();
break;
case '_':
if (subscript != null) {
throw ParseException('Double subscript', lex);
}
subscript = this._handleScript().wrapWithEquationRow();
break;
case "'":
if (superscript != null) {
throw ParseException('Double superscript', lex);
}
final primeCommand = texSymbolCommandConfigs[Mode.math]!['\\prime']!;
final superscriptList = <GreenNode>[
SymbolNode(
mode: mode,
symbol: primeCommand.symbol,
variantForm: primeCommand.variantForm,
overrideAtomType: primeCommand.type,
overrideFont: primeCommand.font,
),
];
this.consume();
while (this.fetch().text == "'") {
superscriptList.add(
SymbolNode(
mode: mode,
symbol: primeCommand.symbol,
variantForm: primeCommand.variantForm,
overrideAtomType: primeCommand.type,
overrideFont: primeCommand.font,
),
);
this.consume();
}
if (this.fetch().text == '^') {
superscriptList.addAll(this._handleScript().expandEquationRow());
}
superscript = superscriptList.wrapWithEquationRow();
break;
default:
break loop;
}
}
return ScriptsParsingResults(
subscript: subscript,
superscript: superscript,
limits: limits,
);
}
GreenNode _handleScript() {
final symbolToken = this.fetch();
final symbol = symbolToken.text;
this.consume();
final group = this.parseGroup(
symbol == '_' ? 'subscript' : 'superscript',
optional: false,
greediness: TexParser.supsubGreediness,
consumeSpaces: true,
);
if (group == null) {
throw ParseException("Expected group after '$symbol'", symbolToken);
}
return group;
}
static const supsubGreediness = 1;
Token fetch() {
final nextToken = this.nextToken;
if (nextToken == null) {
return this.nextToken = this.macroExpander.expandNextToken();
}
return nextToken;
}
void consume() {
this.nextToken = null;
}
/// [parseGroup] Return a row if encounters \[\] or {}. Returns single function
/// node or a single symbol otherwise.
///
///
/// If `optional` is false or absent, this parses an ordinary group,
/// which is either a single nucleus (like "x") or an expression
/// in braces (like "{x+y}") or an implicit group, a group that starts
/// at the current position, and ends right before a higher explicit
/// group ends, or at EOF.
/// If `optional` is true, it parses either a bracket-delimited expression
/// (like "[x+y]") or returns null to indicate the absence of a
/// bracket-enclosed group.
/// If `mode` is present, switches to that mode while parsing the group,
/// and switches back after.
GreenNode? parseGroup(
String name, {
required bool optional,
int? greediness,
String? breakOnTokenText,
Mode? mode,
bool consumeSpaces = false,
}) {
// Save current mode and restore after completion
final outerMode = this.mode;
if (mode != null) {
this.switchMode(mode);
}
// Consume spaces if requested, crucially *after* we switch modes,
// so that the next non-space token is parsed in the correct mode.
if (consumeSpaces == true) {
this.consumeSpaces();
}
// Get first token
final firstToken = this.fetch();
final text = firstToken.text;
GreenNode? result;
// Try to parse an open brace or \begingroup
if (optional ? text == '[' : text == '{' || text == '\\begingroup') {
this.consume();
final groupEnd = endOfGroup[text]!;
// Start a new group namespace
this.macroExpander.beginGroup();
// If we get a brace, parse an expression
final expression =
this.parseExpression(breakOnInfix: false, breakOnTokenText: groupEnd);
// final lastToken = this.fetch();
// Check that we got a matching closing brace
this.expect(groupEnd);
this.macroExpander.endGroup();
result = expression.wrapWithEquationRow();
} else if (optional) {
// Return nothing for an optional group
result = null;
} else {
// If there exists a function with this name, parse the function.
// Otherwise, just return a nucleus
result = this.parseFunction(breakOnTokenText, name, greediness) ??
this._parseSymbol();
if (result == null &&
text[0] == '\\' &&
!implicitCommands.contains(text)) {
if (this.settings.throwOnError) {
throw ParseException('Undefined control sequence: $text', firstToken);
}
result = this._formatUnsuppotedCmd(text);
this.consume();
}
}
if (mode != null) {
this.switchMode(outerMode);
}
return result;
}
///Parses an entire function, including its base and all of its arguments.
GreenNode? parseFunction(
String? breakOnTokenText, String? name, int? greediness) {
final token = this.fetch();
final func = token.text;
final funcData = functions[func];
if (funcData == null) {
return null;
}
this.consume();
if (greediness != null &&
// funcData.greediness != null &&
funcData.greediness <= greediness) {
throw ParseException(
'''Got function '$func' with no arguments ${name != null ? ' as $name' : ''}''',
token);
} else if (this.mode == Mode.text && !funcData.allowedInText) {
throw ParseException(
'''Can't use function '$func' in text mode''', token);
} else if (this.mode == Mode.math && funcData.allowedInMath == false) {
throw ParseException(
'''Can't use function '$func' in math mode''', token);
}
// final funcArgs = parseArgument(func, funcData);
final context = FunctionContext(
funcName: func,
token: token,
breakOnTokenText: breakOnTokenText,
);
// if (funcData.handler != null) {
_enterArgumentParsingMode(func, funcData);
try {
return funcData.handler(this, context);
} finally {
_leaveArgumentParsingMode(func);
}
// } else {
// throw ParseException('''No function handler for $name''');
// }
// return this.callFunction(func, token, breakOnTokenText);
}
final argParsingContexts = Queue<ArgumentParsingContext>();
ArgumentParsingContext get currArgParsingContext => argParsingContexts.last;
void _enterArgumentParsingMode(String name, FunctionSpec funcData) {
argParsingContexts
.addLast(ArgumentParsingContext(funcName: name, funcData: funcData));
}
void _leaveArgumentParsingMode(String name) {
assert(currArgParsingContext.funcName == name);
argParsingContexts.removeLast();
}
void _assertOptionalBeforeReturn(dynamic value, {required bool optional}) {
if (!optional && value == null) {
throw ParseException(
'Expected group after ${currArgParsingContext.funcName}',
this.fetch());
}
}
static final _parseColorRegex1 =
RegExp(r'^#([a-f0-9])([a-f0-9])([a-f0-9])$', caseSensitive: false);
static final _parseColorRegex2 = RegExp(
r'^#?([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$',
caseSensitive: false);
static final _parseColorRegex3 = RegExp(r'^([a-z]+)$', caseSensitive: false);
// static final _parseColorRegex =
// RegExp(r'^(#[a-f0-9]{3}|#?[a-f0-9]{6}|[a-z]+)$', caseSensitive: false);
// static final _matchColorRegex =
// RegExp(r'[0-9a-f]{6}', caseSensitive: false);
Color? parseArgColor({required bool optional}) {
currArgParsingContext.newArgument(optional: optional);
final i = currArgParsingContext.currArgNum;
final consumeSpaces =
(i > 0 && !optional) || (i == 0 && !optional && this.mode == Mode.math);
if (consumeSpaces) {
this.consumeSpaces();
}
// final res = this.parseColorGroup(optional: optional);
final res = this._parseStringGroup('color', optional: optional);
if (res == null) {
_assertOptionalBeforeReturn(null, optional: optional);
return null;
}
final match3 = _parseColorRegex3.firstMatch(res.text);
if (match3 != null) {
final color = colorByName[match3[0]!.toLowerCase()];
if (color != null) {
return color;
}
}
final match2 = _parseColorRegex2.firstMatch(res.text);
if (match2 != null) {
return Color.fromARGB(
0xff,
int.parse(match2[1]!, radix: 16),
int.parse(match2[2]!, radix: 16),
int.parse(match2[3]!, radix: 16),
);
}
final match1 = _parseColorRegex1.firstMatch(res.text);
if (match1 != null) {
return Color.fromARGB(
0xff,
int.parse(match1[1]! * 2, radix: 16),
int.parse(match1[2]! * 2, radix: 16),
int.parse(match1[3]! * 2, radix: 16),
);
}
throw ParseException("Invalid color: '${res.text}'");
}
static final _parseSizeRegex =
RegExp(r'^[-+]? *(?:$|\d+|\d+\.\d*|\.\d*) *[a-z]{0,2} *$');
static final _parseMeasurementRegex =
RegExp(r'([-+]?) *(\d+(?:\.\d*)?|\.\d+) *([a-z]{2})');
Measurement? parseArgSize({required bool optional}) {
currArgParsingContext.newArgument(optional: optional);
final i = currArgParsingContext.currArgNum;
final consumeSpaces =
(i > 0 && !optional) || (i == 0 && !optional && this.mode == Mode.math);
if (consumeSpaces) {
this.consumeSpaces();
}
// final res = this.parseSizeGroup(optional: optional);
Token? res;
if (!optional && this.fetch().text != '{') {
res = _parseRegexGroup(_parseSizeRegex, 'size');
} else {
res = _parseStringGroup('size', optional: optional);
}
if (res == null) {
_assertOptionalBeforeReturn(null, optional: optional);
return null;
}
if (!optional && res.text.isEmpty) {
// res.text = '0pt';
// This means default width for genfrac, and 0pt for above
return null;
}
final match = _parseMeasurementRegex.firstMatch(res.text);
if (match == null) {
throw ParseException("Invalid size: '${res.text}'", res);
}
final unit = match[3]!.parseUnit();
if (unit == null) {
throw ParseException("Invalid unit: '${match[3]}'", res);
}
final size =
Measurement(value: double.parse(match[1]! + match[2]!), unit: unit);
return size;
}
String parseArgUrl({required bool optional}) {
currArgParsingContext.newArgument(optional: optional);
// final i = currArgParsingContext.currArgNum;
// final consumeSpaces =
// (i > 0 && !optional) || (i == 0 && !optional && this.mode == Mode.math);
// if (consumeSpaces) {
// this.consumeSpaces();
// }
// final res = this.parseUrlGroup(optional: optional);
throw UnimplementedError();
}
GreenNode? parseArgNode({required Mode? mode, required bool optional}) {
currArgParsingContext.newArgument(optional: optional);
final i = currArgParsingContext.currArgNum;
final consumeSpaces =
(i > 0 && !optional) || (i == 0 && !optional && this.mode == Mode.math);
// if (consumeSpaces) {
// this.consumeSpaces();
// }
final res = this.parseGroup(
currArgParsingContext.name,
optional: optional,
greediness: currArgParsingContext.funcData.greediness,
mode: mode,
consumeSpaces: consumeSpaces,
);
_assertOptionalBeforeReturn(res, optional: optional);
return res;
}
GreenNode parseArgHbox({required bool optional}) {
final res = parseArgNode(mode: Mode.text, optional: optional);
if (res is EquationRowNode) {
return EquationRowNode(children: [
StyleNode(
optionsDiff: OptionsDiff(style: MathStyle.text),
children: res.children,
)
]);
} else {
return StyleNode(
optionsDiff: OptionsDiff(style: MathStyle.text),
children: res?.children.whereNotNull().toList(growable: false) ?? [],
);
}
}
String? parseArgRaw({required bool optional}) {
currArgParsingContext.newArgument(optional: optional);
final i = currArgParsingContext.currArgNum;
final consumeSpaces =
(i > 0 && !optional) || (i == 0 && !optional && this.mode == Mode.math);
if (consumeSpaces) {
this.consumeSpaces();
}
if (optional && this.fetch().text == '{') {
return null;
}
final token = this._parseStringGroup('raw', optional: optional);
if (token != null) {
return token.text;
} else {
throw ParseException('Expected raw group', this.fetch());
}
}
static final _parseStringGroupRegex = RegExp('''[^{}[\]]''');
Token? _parseStringGroup(String modeName,
{required bool optional, bool raw = false}) {
final groupBegin = optional ? '[' : '{';
final groupEnd = optional ? ']' : '}';
final beginToken = this.fetch();
if (beginToken.text != groupBegin) {
if (optional) {
return null;
} else if (raw &&
beginToken.text != 'EOF' &&
_parseStringGroupRegex.hasMatch(beginToken.text)) {
this.consume();
return beginToken;
}
}
final outerMode = this.mode;
this.mode = Mode.text;
this.expect(groupBegin);
var str = '';
final firstToken = this.fetch();
var nested = 0;
var lastToken = firstToken;
Token nextToken;
while ((nextToken = this.fetch()).text != groupEnd || (raw && nested > 0)) {
if (nextToken.text == 'EOF') {
throw ParseException('Unexpected end of input in $modeName',
Token.range(firstToken, lastToken, str));
} else if (nextToken.text == groupBegin) {
nested++;
} else if (nextToken.text == groupEnd) {
nested--;
}
lastToken = nextToken;
str += lastToken.text;
this.consume();
}
this.expect(groupEnd);
this.mode = outerMode;
return Token.range(firstToken, lastToken, str);
}
Token _parseRegexGroup(RegExp regex, String modeName) {
final outerMode = this.mode;
this.mode = Mode.text;
final firstToken = this.fetch();
var lastToken = firstToken;
var str = '';
Token nextToken;
while ((nextToken = this.fetch()).text != 'EOF' &&
regex.hasMatch(str + nextToken.text)) {
lastToken = nextToken;
str += lastToken.text;
this.consume();
}
if (str.isEmpty) {
throw ParseException(
"Invalid $modeName: '${firstToken.text}'", firstToken);
}
this.mode = outerMode;
return Token.range(firstToken, lastToken, str);
}
static final _parseVerbRegex = RegExp(r'^\\verb[^a-zA-Z]');
GreenNode? _parseSymbol() {
final nucleus = this.fetch();
var text = nucleus.text;
if (_parseVerbRegex.hasMatch(text)) {
this.consume();
var arg = text.substring(5);
final star = (arg[0] == '*'); //?
if (star) {
arg = arg.substring(1);
}
// Lexer's tokenRegex is constructed to always have matching
// first/last characters.
if (arg.length < 2 || arg[0] != arg[arg.length - 1]) {
throw ParseException('''\\verb assertion failed --
please report what input caused this bug''');
}
arg = arg.substring(1, arg.length - 1);
return EquationRowNode(
children: arg
.split('')
.map((char) => SymbolNode(
symbol: char,
overrideFont: const FontOptions(fontFamily: 'Typewriter'),
mode: Mode.text,
))
.toList(growable: false),
);
}
// At this point, we should have a symbol, possibly with accents.
// First expand any accented base symbol according to unicodeSymbols.
if (unicodeSymbols.containsKey(text[0]) &&
!texSymbolCommandConfigs[this.mode]!.containsKey(text[0])) {
if (this.mode == Mode.math) {
this.settings.reportNonstrict(
'unicodeTextInMathMode',
'Accented Unicode text character "${text[0]}" used in math mode',
nucleus);
}
// text = unicodeSymbols[text[0]] + text.substring(1);
}
// Strip off any combining characters
final match = combiningDiacriticalMarksEndRegex.firstMatch(text);
var combiningMarks = '';
if (match != null) {
text = text.substring(0, match.start);
for (var i = 0; i < match[0]!.length; i++) {
final accent = match[0]![i];
if (!unicodeAccents.containsKey(accent)) {
throw ParseException("Unknown accent ' $accent'", nucleus);
}
final command = unicodeAccents[accent]![this.mode];
if (command == null) {
throw ParseException(
'Accent $accent unsupported in ${this.mode} mode', nucleus);
}
}
combiningMarks = match[0]!;
}
// Recognize base symbol
GreenNode symbol;
final symbolCommandConfig = texSymbolCommandConfigs[this.mode]![text];
if (symbolCommandConfig != null) {
if (this.mode == Mode.math && extraLatin.contains(text)) {
this.settings.reportNonstrict(
'unicodeTextInMathMode',
'Latin-1/Unicode text character "${text[0]}" used in math mode',
nucleus);
}
// final loc = SourceLocation.range(nucleus);
symbol = SymbolNode(
mode: mode,
symbol: symbolCommandConfig.symbol + combiningMarks,
variantForm: symbolCommandConfig.variantForm,
overrideAtomType: symbolCommandConfig.type,
overrideFont: symbolCommandConfig.font,
);
} else if (text.isNotEmpty && text.codeUnitAt(0) >= 0x80) {
if (!supportedCodepoint(text.codeUnitAt(0))) {
this.settings.reportNonstrict(
'unknownSymbol',
'Unrecognized Unicode character "${text[0]}" '
'(${text.codeUnitAt(0)})',
nucleus);
} else if (this.mode == Mode.math) {
this.settings.reportNonstrict('unicodeTextInMathMode',
'Unicode text character "${text[0]} used in math mode"', nucleus);
}
symbol = SymbolNode(
symbol: text + combiningMarks,
overrideAtomType: AtomType.ord,
mode: mode);
} else {
return null;
}
this.consume();
return symbol;
}
void switchMode(Mode newMode) {
this.mode = newMode;
this.macroExpander.mode = newMode;
}
GreenNode _formatUnsuppotedCmd(String text) {
//TODO
throw UnimplementedError();
}
}
class ArgumentParsingContext {
final String funcName;
int currArgNum;
final FunctionSpec funcData;
bool get optional => _optional;
bool _optional;
set optional(bool value) {
assert(_optional || !value);
_optional = value;
}
String get name => 'argument to $funcName';
ArgumentParsingContext({
required this.funcData,
required this.funcName,
this.currArgNum = -1,
bool optional = true,
}) : _optional = optional;
void newArgument({required bool optional}) {
currArgNum++;
this.optional = optional;
}
}
class ScriptsParsingResults {
final EquationRowNode? subscript;
final EquationRowNode? superscript;
final bool? limits;
const ScriptsParsingResults({
required this.subscript,
required this.superscript,
this.limits,
});
bool get empty => subscript == null && superscript == null;
}
T assertNodeType<T extends GreenNode?>(GreenNode? node) {
if (node is T) {
return node;
}
throw ParseException(
'Expected node of type $T, but got node of type ${node.runtimeType}');
}