-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizer.ts
167 lines (150 loc) · 3.89 KB
/
tokenizer.ts
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
import {
Scanner, OPERATIONS, HOOK_MODE, IScanEnv,
IPattern, Token,
IPosition, ISourceLocation, UseKey,
} from 'astry'
const {
FINISH,
WRAP, UNWRAP, UNWRAP_ALL,
OPTION,
SPLIT,
NO_COLLECT, NO_CAPTURE,
MARK_AS_ROOT, FORK_IN_PARENT, FORK_IN_ROOT,
END, END_ON_LEFT,
useKey,
node, key, pick, hook, pipe, prev, merge,
MATCH_BEGIN,
MATCH_END,
MATCH_EOF,
MERGE_ALL_TOKENS
} = OPERATIONS;
const MATCH_WHITE_SPACE_CHARACHER: IPattern = [` `, `\t`]; // 换行在这里作为断句符单独处理
const CLEAR_WHITE_SPACE_CHARACHER: IPattern = [
[NO_COLLECT, MATCH_WHITE_SPACE_CHARACHER]
];
const THROW_TOKEN_ERROR = hook(function (env, start, end) {
console.log("Invalid or unexpected token", start, end);
}, HOOK_MODE.RESOLVE);
const MATCH_QUOTES = [
["'", WRAP, "'", UNWRAP]
];
const MATCH_RANGE_DEFINITION_NOTATION = [
[
NO_COLLECT, "[", WRAP,
[
[pipe(parseNumber), NO_COLLECT, ","],
[pipe(parseNumber), NO_COLLECT, "]", UNWRAP]
],
]
];
const MATCH_DATA_TYPES = [
[
node("DataTypes"),
NO_COLLECT, "<", WRAP,
pipe(function (token: Token) {
return useKey("name", token.value);
}),
[
[">", pipe(function () { return useKey("value", "<>"); }), UNWRAP],
[CLEAR_WHITE_SPACE_CHARACHER],
[
pick(function (range: Array<number>) {
return useKey("range", range)
}),
MATCH_RANGE_DEFINITION_NOTATION
],
[pick(function (tokens: Array<Token>) {
const token = tokens.slice(1, -1)[0];
if (token) {
return [
useKey("name", token.value),
useKey("useQuote", true),
]
}
}), MATCH_QUOTES]
]
]
];
const MATCH_BRACKETS = [
[
node(
"Brackets",
function ([tokens]: [Array<Token[]>]) {
return [useKey("content", tokens), useKey("value", "[]")];
}
),
NO_COLLECT, "[", WRAP,
FORK_IN_ROOT,
NO_COLLECT, "]", UNWRAP
]
];
const MATCH_CURLY_BRACES = [
[
node(
"CurlyBraces",
function ([tokens]: [Array<Token[]>]) {
return [useKey("content", tokens), useKey("value", "{}")];
}
),
NO_COLLECT, "{", WRAP,
[
[CLEAR_WHITE_SPACE_CHARACHER],
[pipe(parseNumber), ","],
[pipe(parseNumber), NO_COLLECT, "}", UNWRAP]
]
]
];
const MATCH_STATEMENT_END = [
"\n",
[MATCH_EOF]
];
export default new Scanner([
[CLEAR_WHITE_SPACE_CHARACHER],
[
pick(function (tokens: Array<Token>) {
if (tokens.length) {
return tokens;
}
}),
MATCH_BEGIN, WRAP,
MARK_AS_ROOT,
[
[NO_COLLECT, MATCH_STATEMENT_END, UNWRAP],
[CLEAR_WHITE_SPACE_CHARACHER],
[MATCH_QUOTES],
[MATCH_DATA_TYPES],
[MATCH_BRACKETS],
[MATCH_CURLY_BRACES],
"*",
"+",
"?",
"!",
[
[
",",
"&&",
"||",
"|",
"=",
],
NO_COLLECT,
OPTION,
[
[
[` `, `\t`, `\n`],
FORK_IN_PARENT
]
]
]
]
],
]);
function parseNumber({ value }: Token) {
if (value === "∞" || value === "+∞") {
return Number.POSITIVE_INFINITY;
}
if (value === "−∞") {
return Number.NEGATIVE_INFINITY;
}
return Number(value);
}