-
Notifications
You must be signed in to change notification settings - Fork 0
/
anatomize.ts
305 lines (236 loc) · 6.5 KB
/
anatomize.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
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
/*
Anatomize.js v1.1.1 | A JavaScript-based framework for building parsers
Copyright (C) 2022 CubesAndCubes
Anatomize is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Anatomize is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Anatomize. If not, see <https://www.gnu.org/licenses/>.
*/
interface TokenType {
name: string;
matcher: object;
hidden: boolean;
}
interface Token {
type: string;
value: string;
hidden: boolean;
}
export class Anatomize {
#main: Function;
constructor(main: Function) {
if (typeof main !== 'function')
throw TypeError(
`Unexpected Main Function; Expected "function", got "${typeof main}"`
);
this.#main = main;
}
static #TokenizerClass = class {
TokenTypes: Array<TokenType> = [];
#buffer: string;
#index: number;
#lookaheadBuffer: Array<Token>;
#lookaheadIndex: number;
initialize(source: string) {
this.#buffer = String(source);
this.#index = 0;
this.#lookaheadBuffer = [];
this.#lookaheadIndex = 0;
while(!this.#isEOF())
this.#lookaheadBuffer.push(this.#readToken());
}
#isEOF() {
return this.#index >= this.#buffer.length;
}
#readToken(): Token {
const INPUT = this.#buffer.slice(this.#index);
for (const TokenType of this.TokenTypes) {
let tokenValue: string = null;
let tokenLength = 0;
if (TokenType.matcher instanceof Anatomize.#Matcher) {
const MatchResults = TokenType.matcher.match(INPUT);
if (![false, null].includes(MatchResults[0])) {
tokenValue = MatchResults[1];
tokenLength = MatchResults[2];
}
}
else if (TokenType.matcher instanceof RegExp) {
tokenValue = INPUT.match(TokenType.matcher)?.[0] ?? null;
tokenLength = tokenValue?.length ?? 0;
}
else throw TypeError('Unexpected Matcher');
if (!tokenValue && 1 > tokenLength)
continue;
this.#index += tokenLength;
if (!TokenType.name || !tokenValue) {
if (this.#isEOF())
return null;
return this.#readToken();
}
return {
type: TokenType.name,
value: tokenValue,
hidden: TokenType.hidden,
};
}
throw SyntaxError(
`Undefined Token "${INPUT}"`
);
}
peekToken(offset = 0): Token {
return this.#lookaheadBuffer[this.#lookaheadIndex + offset] ?? null;
}
nextToken(): Token {
if (this.#lookaheadIndex >= this.#lookaheadBuffer.length)
return null;
return this.#lookaheadBuffer[this.#lookaheadIndex++];
}
}
#Tokenizer = new Anatomize.#TokenizerClass();
#errorIfParsing() {
if (this.#parsing)
throw SyntaxError(
'Unexpected Method Call; Method may not be called whilst parsing'
);
}
#parsing = false;
parse(source: string) {
this.#errorIfParsing();
this.#parsing = true;
this.#Tokenizer.initialize(source);
try {
return this.#main();
}
finally {
this.#parsing = false;
}
}
static #Matcher = class {
#pattern: Function;
#buffer: string;
#index: number;
#result: string;
#isEOF = () => this.#index >= this.#buffer.length;
#charAt = (index: number) => this.#buffer[index];
#peekChar = (offset = 0) => this.#buffer[this.#index + offset];
static #constraintCheck = (
input: string,
constraint: string | Array<string>,
) => {
if (Array.isArray(constraint))
if (constraint.includes(input))
return true;
else
return false;
if (String(constraint).slice(0, 1) === input)
return true;
else
return false;
};
#readChar = (constraint: string | Array<string> = null) => {
if (this.#isEOF())
throw RangeError(
'Unexpected End of Input; Cannot read another Character'
);
if (constraint)
if (!Anatomize.#Matcher.#constraintCheck(this.#buffer[this.#index], constraint))
return null;
this.#result += this.#buffer[this.#index];
return this.#buffer[this.#index++];
};
#omitChar = (constraint: string = null) => {
if (this.#isEOF())
throw RangeError(
'Unexpected End of Input; Cannot omit another Character'
);
if (constraint)
if (!Anatomize.#Matcher.#constraintCheck(this.#buffer[this.#index], constraint))
return null;
return this.#buffer[this.#index++];
}
#getIndex = () => this.#index;
constructor(pattern: Function) {
this.#pattern = pattern;
}
match(input: string) {
this.#buffer = input;
this.#index = 0;
this.#result = '';
return [
this.#pattern(
this.#readChar,
this.#peekChar,
this.#omitChar,
this.#isEOF,
this.#charAt,
this.#getIndex,
),
this.#result || null,
this.#index,
];
}
}
#addTokenType(name: string, matcher: Function | object, hidden: boolean) {
this.#errorIfParsing();
if (typeof matcher !== 'function' && !(matcher instanceof RegExp))
throw TypeError(
`Unexpected Type of Matcher; Expected "function" or RegExp, got "${typeof matcher}"`
);
if (typeof matcher === 'function')
matcher = new Anatomize.#Matcher(matcher);
this.#Tokenizer.TokenTypes.push({
name,
matcher,
hidden,
});
}
registerToken(name: string, matcher: RegExp | Function) {
this.#addTokenType(name, matcher, false);
}
registerHiddenToken(name: string, matcher: RegExp | Function) {
this.#addTokenType(name, matcher, true);
}
#errorIfNotParsing() {
if (!this.#parsing)
throw SyntaxError(
'Unexpected Method Call; Method may only be called whilst parsing'
);
}
peek(offset = 0) {
this.#errorIfNotParsing();
let Token: Token;
do {
Token = this.#Tokenizer.peekToken(offset++);
} while (Token?.hidden)
return Token?.hidden ? null : Token;
}
isEOF() {
this.#errorIfNotParsing();
return !this.peek();
}
isPeekType(tokenType: string, offset = 0) {
return this.peek(offset)?.type === tokenType;
}
read(tokenType: string) {
this.#errorIfNotParsing();
let Token: Token;
do {
Token = this.#Tokenizer.nextToken();
} while (Token?.hidden && Token?.type !== tokenType)
if (!Token)
throw SyntaxError(
'Unexpected End of Input; Cannot read another Token'
);
if (Token?.type !== tokenType)
throw SyntaxError(
`Unexpected Token; Expected "${tokenType}", got "${Token.type}"`
);
return Token;
}
}