encoder.dart
2.04 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
import '../ast/syntax_tree.dart';
import '../parser/tex/settings.dart';
import '../utils/log.dart';
import 'exception.dart';
abstract class EncodeResult {
const EncodeResult();
String stringify(covariant EncodeConf conf);
}
class StaticEncodeResult extends EncodeResult {
const StaticEncodeResult(this.string);
final String string;
@override
String stringify(EncodeConf conf) => string;
}
class NonStrictEncodeResult extends EncodeResult {
final String errorCode;
final String errorMsg;
final EncodeResult placeHolder;
const NonStrictEncodeResult(
this.errorCode,
this.errorMsg, [
this.placeHolder = const StaticEncodeResult(''),
]);
NonStrictEncodeResult.string(
this.errorCode,
this.errorMsg, [
String placeHolder = '',
]) : this.placeHolder = StaticEncodeResult(placeHolder);
@override
String stringify(EncodeConf conf) {
conf.reportNonstrict(errorCode, errorMsg);
return placeHolder.stringify(conf);
}
}
typedef EncoderFun<T extends GreenNode> = EncodeResult Function(T node);
typedef StrictFun = Strict Function(String errorCode, String errorMsg,
[dynamic token]);
abstract class EncodeConf {
final Strict strict;
final StrictFun? strictFun;
const EncodeConf({
this.strict = Strict.warn,
this.strictFun,
});
void reportNonstrict(String errorCode, String errorMsg, [dynamic token]) {
final strict = this.strict != Strict.function
? this.strict
: (strictFun?.call(errorCode, errorMsg, token) ?? Strict.warn);
switch (strict) {
case Strict.ignore:
return;
case Strict.error:
throw EncoderException(
"Nonstrict Tex encoding and strict mode is set to 'error': "
'$errorMsg [$errorCode]',
token);
case Strict.warn:
warn("Nonstrict Tex encoding and strict mode is set to 'warn': "
'$errorMsg [$errorCode]');
break;
default:
warn('Nonstrict Tex encoding and strict mode is set to '
"unrecognized '$strict': $errorMsg [$errorCode]");
}
}
}