-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.ts
341 lines (313 loc) · 7.8 KB
/
parser.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
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
import { STANCE_KEYWORDS } from './constants/stances';
import {
type ConsumeTokenResult,
type InstructionMatchResult,
InstructionSubType,
type InstructionToken,
InstructionTypes,
MetaTypes,
type ParsedInstruction,
type ParserResult,
type TekkenKeywordsList,
} from './types';
/**
* Creates a regular expression that matches a list of keywords.
* @param keywords
* @param flags
*/
const expressionFromKeywords = (keywords: string[], flags = 'gi') =>
new RegExp(`^(${keywords.join('|')})`, flags);
/**
* Creates a regular expression that matches a list of movement pairs.
* @param pairs
*/
const expressionFromMovementPairs = (pairs: string[][]) => {
const expressions = pairs.map((pair) => pair.join('/?')).join('|');
return new RegExp(`^(${expressions})`, 'gi');
};
/**
* Separator character for moves in a combo.
*/
const MOVE_SEPARATOR = ';';
/**
* List of special keywords, like WS, WR, etc.
*/
const SPECIAL_KEYWORDS: TekkenKeywordsList = {
WS: {
value: 'While Standing',
},
WR: {
value: 'While Running',
},
iWS: {
value: 'Instant While Standing',
},
iWR: {
value: 'Instant While Running',
},
FC: {
value: 'Full Crouch',
},
iFC: {
value: 'Instant Full Crouch',
},
CH: {
value: 'Counter Hit',
},
CD: {
value: 'Crouch Dash',
},
BT: {
value: 'Back-Turned',
},
SSL: {
value: 'Side Step Left',
},
SWL: {
value: 'Side Walk Left',
},
SSR: {
value: 'Side Step Right',
},
SWR: {
value: 'Side Walk Right',
},
DASH: {
value: 'Dash',
},
BACKDASH: {
value: 'Backdash',
},
WAVEDASH: {
value: 'Wavedash',
},
};
/**
* List of Rage keywords.
*/
const RAGE_KEYWORDS: TekkenKeywordsList = {
'R!': {
notation: 'R!',
value: 'Rage Art',
},
'RAGE': {
notation: 'R!',
value: 'Rage Art',
},
'RAGEART': {
notation: 'R!',
value: 'Rage Art',
},
};
/**
* List of Heat keywords.
*/
const HEAT_KEYWORDS: TekkenKeywordsList = {
'H!': {
notation: 'H!',
value: 'Heat',
},
'HEAT': {
notation: 'H!',
value: 'Heat',
},
};
/**
* List of Tornado keywords.
*/
const TORNADO_KEYWORDS: TekkenKeywordsList = {
'T!': {
notation: 'T!',
value: 'Tornado',
},
'TORNADO': {
notation: 'T!',
value: 'Tornado',
},
};
/**
* Tokens used for parsing instructions.
* The order is relevant, since it implies the token precedence.
*/
const TOKENS: InstructionToken[] = [
{
type: InstructionTypes.SPECIAL,
subType: InstructionSubType.NONE,
expression: expressionFromKeywords(Object.keys(SPECIAL_KEYWORDS)),
keywords: SPECIAL_KEYWORDS,
},
{
type: InstructionTypes.SPECIAL,
subType: InstructionSubType.STANCE,
expression: expressionFromKeywords(Object.keys(STANCE_KEYWORDS)),
keywords: STANCE_KEYWORDS,
},
{
type: InstructionTypes.SPECIAL,
subType: InstructionSubType.RAGE,
expression: expressionFromKeywords(Object.keys(RAGE_KEYWORDS)),
keywords: RAGE_KEYWORDS,
},
{
type: InstructionTypes.SPECIAL,
subType: InstructionSubType.HEAT,
expression: expressionFromKeywords(Object.keys(HEAT_KEYWORDS)),
keywords: HEAT_KEYWORDS,
},
{
type: InstructionTypes.SPECIAL,
subType: InstructionSubType.TORNADO,
expression: expressionFromKeywords(Object.keys(TORNADO_KEYWORDS)),
keywords: TORNADO_KEYWORDS,
},
{
type: InstructionTypes.CONTROL,
subType: InstructionSubType.BRACKET_LEFT,
expression: /^(\[)/g,
},
{
type: InstructionTypes.CONTROL,
subType: InstructionSubType.BRACKET_RIGHT,
expression: /^(])/g,
},
{
type: InstructionTypes.TEXT,
expression: /^\((.+)\)/gi,
},
{
type: InstructionTypes.MOVEMENT,
expression: expressionFromMovementPairs([
['u', 'f'],
['u', 'b'],
['d', 'f'],
['d', 'b'],
]),
},
{
type: InstructionTypes.MOVEMENT,
expression: /^[ufdbn]/gi,
},
{
type: InstructionTypes.ATTACK,
expression: /^([1234])(([+])([1234]))+/gi,
},
{
type: InstructionTypes.ATTACK,
expression: /^([1234])/gi,
},
{
type: InstructionTypes.COMBINATOR,
expression: /^([:~<])/gi,
},
{
type: InstructionTypes.ALTERNATIVE,
expression: /^(_)/gi,
},
{
type: InstructionTypes.HIDDEN,
expression: /^([+.,])/gi,
},
];
/**
* Sanitizes the notation to be easier to search via RegExp.
* @param notation
*/
const sanitize = (notation: string) => {
return notation.replace(/,/g, '').trim();
};
/**
* Tries to consume the specified token from the start of the notation.
* On success, it will return the matching token as well as the remainder
* of the notation after the match.
* @param token
* @param notation
*/
const consumeToken = (token: RegExp, notation: string): ConsumeTokenResult | null => {
const matches = notation.match(token);
if (matches === null) {
return null;
}
const value = matches.at(0);
if (value === undefined) {
return null;
}
const index = notation.indexOf(value);
const remainder = sanitize(notation.substring(index + value.length));
return {
index,
value,
remainder,
};
};
/**
* Iterates all possible instructions and returns the first matching one.
* @param notation
*/
const matchInstruction = (notation: string): InstructionMatchResult => {
const result: InstructionMatchResult = {
remainder: notation,
instruction: null,
};
for (const token of TOKENS) {
const match = consumeToken(token.expression, notation);
if (!match) {
continue;
}
// Try to find the visual representation of the keyword, such as "While Standing" for WS.
const value =
token.keywords && token.keywords[match.value]
? token.keywords[match.value].value
: match.value;
result.remainder = match.remainder;
result.instruction = {
$type: MetaTypes.INSTRUCTION,
type: token.type,
subType: token.subType,
notation: match.value,
value,
};
break;
}
return result;
};
/**
* Parses the notation of a move to split it into separate instructions.
* @param notation
* @param result
*/
const parseInstructions = (
notation: string,
result: ParsedInstruction[] = [],
): ParsedInstruction[] => {
if (notation.length === 0) {
return result;
}
const { instruction, remainder } = matchInstruction(notation);
if (!instruction || remainder === notation) {
return result;
}
const instructions =
instruction.type === InstructionTypes.HIDDEN ? result : result.concat(instruction);
return parseInstructions(remainder, instructions);
};
/**
* Parses a Tekken notation string and returns an array of parsed moves.
*
* @param {string} notation - The Tekken notation to parse.
* @returns {ParsedMove[]} - An array of parsed moves.
*/
export const parseTekkenNotation = (notation: string): ParserResult => {
const moveNotations: string[] = notation.split(MOVE_SEPARATOR);
const moves = moveNotations.map((moveNotation: string) => {
const instructions = parseInstructions(moveNotation);
return {
$type: MetaTypes.MOVE,
notation: moveNotation,
instructions,
};
});
return {
notation,
moves,
};
};