@kawaijs/parser 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/compiler.d.ts +14 -0
- package/dist/compiler.d.ts.map +1 -0
- package/dist/compiler.js +198 -0
- package/dist/compiler.js.map +1 -0
- package/dist/diagnostic.d.ts +18 -0
- package/dist/diagnostic.d.ts.map +1 -0
- package/dist/diagnostic.js +37 -0
- package/dist/diagnostic.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/lexer.d.ts +32 -0
- package/dist/lexer.d.ts.map +1 -0
- package/dist/lexer.js +324 -0
- package/dist/lexer.js.map +1 -0
- package/dist/parser.d.ts +38 -0
- package/dist/parser.d.ts.map +1 -0
- package/dist/parser.js +459 -0
- package/dist/parser.js.map +1 -0
- package/dist/token.d.ts +9 -0
- package/dist/token.d.ts.map +1 -0
- package/dist/token.js +27 -0
- package/dist/token.js.map +1 -0
- package/package.json +39 -0
package/dist/lexer.js
ADDED
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
import { createLocation, createPosition } from '@kawaijs/ast';
|
|
2
|
+
import { KEYWORDS } from './token.js';
|
|
3
|
+
import { KawaError } from './diagnostic.js';
|
|
4
|
+
export class Lexer {
|
|
5
|
+
source;
|
|
6
|
+
file;
|
|
7
|
+
tabSize;
|
|
8
|
+
cursor = 0;
|
|
9
|
+
line = 1;
|
|
10
|
+
column = 1;
|
|
11
|
+
indentStack = [0];
|
|
12
|
+
pendingTokens = [];
|
|
13
|
+
isAtLineStart = true;
|
|
14
|
+
hasTokensOnCurrentLine = false;
|
|
15
|
+
constructor(source, file = '<anonymous>', options = {}) {
|
|
16
|
+
this.source = source;
|
|
17
|
+
this.file = file;
|
|
18
|
+
this.tabSize = options.tabSize ?? 4;
|
|
19
|
+
}
|
|
20
|
+
tokenize() {
|
|
21
|
+
const tokens = [];
|
|
22
|
+
while (true) {
|
|
23
|
+
const token = this.nextToken();
|
|
24
|
+
tokens.push(token);
|
|
25
|
+
if (token.type === 'EOF') {
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return tokens;
|
|
30
|
+
}
|
|
31
|
+
nextToken() {
|
|
32
|
+
if (this.pendingTokens.length > 0) {
|
|
33
|
+
return this.pendingTokens.shift();
|
|
34
|
+
}
|
|
35
|
+
while (!this.isEof()) {
|
|
36
|
+
if (this.isAtLineStart) {
|
|
37
|
+
this.handleIndentation();
|
|
38
|
+
if (this.pendingTokens.length > 0) {
|
|
39
|
+
return this.pendingTokens.shift();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const ch = this.peek();
|
|
43
|
+
// Skip non-newline whitespace
|
|
44
|
+
if (ch === ' ' || ch === '\t' || ch === '\r') {
|
|
45
|
+
this.advance();
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
// Comments (# to end of line)
|
|
49
|
+
if (ch === '#') {
|
|
50
|
+
this.skipComment();
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
// Newline
|
|
54
|
+
if (ch === '\n') {
|
|
55
|
+
const startLoc = this.getCurrentPosition();
|
|
56
|
+
this.advance();
|
|
57
|
+
this.isAtLineStart = true;
|
|
58
|
+
if (this.hasTokensOnCurrentLine) {
|
|
59
|
+
this.hasTokensOnCurrentLine = false;
|
|
60
|
+
return {
|
|
61
|
+
type: 'NEWLINE',
|
|
62
|
+
value: '\n',
|
|
63
|
+
loc: createLocation(this.file, startLoc, this.getCurrentPosition())
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
this.hasTokensOnCurrentLine = true;
|
|
69
|
+
// Strings (double or single quotes)
|
|
70
|
+
if (ch === '"' || ch === "'") {
|
|
71
|
+
return this.readString(ch);
|
|
72
|
+
}
|
|
73
|
+
// Numbers
|
|
74
|
+
if (this.isDigit(ch)) {
|
|
75
|
+
return this.readNumber();
|
|
76
|
+
}
|
|
77
|
+
// Identifiers / Keywords
|
|
78
|
+
if (this.isAlpha(ch) || ch === '_') {
|
|
79
|
+
return this.readIdentifier();
|
|
80
|
+
}
|
|
81
|
+
// Symbols
|
|
82
|
+
if (ch === ':') {
|
|
83
|
+
const startLoc = this.getCurrentPosition();
|
|
84
|
+
this.advance();
|
|
85
|
+
return {
|
|
86
|
+
type: 'COLON',
|
|
87
|
+
value: ':',
|
|
88
|
+
loc: createLocation(this.file, startLoc, this.getCurrentPosition())
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
if (ch === '=') {
|
|
92
|
+
const startLoc = this.getCurrentPosition();
|
|
93
|
+
this.advance();
|
|
94
|
+
return {
|
|
95
|
+
type: 'EQUALS',
|
|
96
|
+
value: '=',
|
|
97
|
+
loc: createLocation(this.file, startLoc, this.getCurrentPosition())
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
if (ch === ',') {
|
|
101
|
+
const startLoc = this.getCurrentPosition();
|
|
102
|
+
this.advance();
|
|
103
|
+
return {
|
|
104
|
+
type: 'COMMA',
|
|
105
|
+
value: ',',
|
|
106
|
+
loc: createLocation(this.file, startLoc, this.getCurrentPosition())
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
// Unknown character
|
|
110
|
+
const startLoc = this.getCurrentPosition();
|
|
111
|
+
this.advance();
|
|
112
|
+
throw new KawaError({
|
|
113
|
+
code: 'E0001',
|
|
114
|
+
message: `Unexpected character '${ch}'`,
|
|
115
|
+
severity: 'error',
|
|
116
|
+
loc: createLocation(this.file, startLoc, this.getCurrentPosition()),
|
|
117
|
+
hint: 'Check for unsupported symbols or typos.'
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
// Emit remaining DEDENT tokens at EOF
|
|
121
|
+
if (this.indentStack.length > 1) {
|
|
122
|
+
const pos = this.getCurrentPosition();
|
|
123
|
+
while (this.indentStack.length > 1) {
|
|
124
|
+
this.indentStack.pop();
|
|
125
|
+
this.pendingTokens.push({
|
|
126
|
+
type: 'DEDENT',
|
|
127
|
+
value: '',
|
|
128
|
+
loc: createLocation(this.file, pos, pos)
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
this.pendingTokens.push({
|
|
132
|
+
type: 'EOF',
|
|
133
|
+
value: '',
|
|
134
|
+
loc: createLocation(this.file, pos, pos)
|
|
135
|
+
});
|
|
136
|
+
return this.pendingTokens.shift();
|
|
137
|
+
}
|
|
138
|
+
const pos = this.getCurrentPosition();
|
|
139
|
+
return {
|
|
140
|
+
type: 'EOF',
|
|
141
|
+
value: '',
|
|
142
|
+
loc: createLocation(this.file, pos, pos)
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
handleIndentation() {
|
|
146
|
+
let indentWidth = 0;
|
|
147
|
+
const startLoc = this.getCurrentPosition();
|
|
148
|
+
while (!this.isEof()) {
|
|
149
|
+
const ch = this.peek();
|
|
150
|
+
if (ch === ' ') {
|
|
151
|
+
indentWidth += 1;
|
|
152
|
+
this.advance();
|
|
153
|
+
}
|
|
154
|
+
else if (ch === '\t') {
|
|
155
|
+
indentWidth += this.tabSize;
|
|
156
|
+
this.advance();
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
// If line is empty or just a comment, ignore its indentation
|
|
163
|
+
if (this.peek() === '\n' || this.peek() === '\r' || this.peek() === '#' || this.isEof()) {
|
|
164
|
+
this.isAtLineStart = false;
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
this.isAtLineStart = false;
|
|
168
|
+
const currentIndent = this.indentStack[this.indentStack.length - 1] ?? 0;
|
|
169
|
+
if (indentWidth > currentIndent) {
|
|
170
|
+
this.indentStack.push(indentWidth);
|
|
171
|
+
this.pendingTokens.push({
|
|
172
|
+
type: 'INDENT',
|
|
173
|
+
value: ' '.repeat(indentWidth - currentIndent),
|
|
174
|
+
loc: createLocation(this.file, startLoc, this.getCurrentPosition())
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
else if (indentWidth < currentIndent) {
|
|
178
|
+
while (this.indentStack.length > 1 &&
|
|
179
|
+
(this.indentStack[this.indentStack.length - 1] ?? 0) > indentWidth) {
|
|
180
|
+
this.indentStack.pop();
|
|
181
|
+
this.pendingTokens.push({
|
|
182
|
+
type: 'DEDENT',
|
|
183
|
+
value: '',
|
|
184
|
+
loc: createLocation(this.file, startLoc, this.getCurrentPosition())
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
const top = this.indentStack[this.indentStack.length - 1] ?? 0;
|
|
188
|
+
if (top !== indentWidth) {
|
|
189
|
+
throw new KawaError({
|
|
190
|
+
code: 'E0002',
|
|
191
|
+
message: `Inconsistent indentation level (${indentWidth} spaces, expected ${top})`,
|
|
192
|
+
severity: 'error',
|
|
193
|
+
loc: createLocation(this.file, startLoc, this.getCurrentPosition()),
|
|
194
|
+
hint: 'Ensure indentation uses consistent spaces across blocks.'
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
readString(quote) {
|
|
200
|
+
const startLoc = this.getCurrentPosition();
|
|
201
|
+
this.advance(); // consume opening quote
|
|
202
|
+
let value = '';
|
|
203
|
+
while (!this.isEof() && this.peek() !== quote) {
|
|
204
|
+
const ch = this.peek();
|
|
205
|
+
if (ch === '\n') {
|
|
206
|
+
throw new KawaError({
|
|
207
|
+
code: 'E0003',
|
|
208
|
+
message: 'Unterminated string literal (newline before closing quote)',
|
|
209
|
+
severity: 'error',
|
|
210
|
+
loc: createLocation(this.file, startLoc, this.getCurrentPosition()),
|
|
211
|
+
hint: 'Make sure your string closes with the matching quote on the same line.'
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
if (ch === '\\') {
|
|
215
|
+
this.advance();
|
|
216
|
+
if (this.isEof())
|
|
217
|
+
break;
|
|
218
|
+
const escapeCh = this.advance();
|
|
219
|
+
switch (escapeCh) {
|
|
220
|
+
case 'n':
|
|
221
|
+
value += '\n';
|
|
222
|
+
break;
|
|
223
|
+
case 't':
|
|
224
|
+
value += '\t';
|
|
225
|
+
break;
|
|
226
|
+
case 'r':
|
|
227
|
+
value += '\r';
|
|
228
|
+
break;
|
|
229
|
+
case '"':
|
|
230
|
+
value += '"';
|
|
231
|
+
break;
|
|
232
|
+
case "'":
|
|
233
|
+
value += "'";
|
|
234
|
+
break;
|
|
235
|
+
case '\\':
|
|
236
|
+
value += '\\';
|
|
237
|
+
break;
|
|
238
|
+
default:
|
|
239
|
+
value += escapeCh;
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
value += this.advance();
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (this.isEof()) {
|
|
248
|
+
throw new KawaError({
|
|
249
|
+
code: 'E0003',
|
|
250
|
+
message: 'Unterminated string literal at end of file',
|
|
251
|
+
severity: 'error',
|
|
252
|
+
loc: createLocation(this.file, startLoc, this.getCurrentPosition())
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
this.advance(); // consume closing quote
|
|
256
|
+
return {
|
|
257
|
+
type: 'STRING',
|
|
258
|
+
value,
|
|
259
|
+
loc: createLocation(this.file, startLoc, this.getCurrentPosition())
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
readNumber() {
|
|
263
|
+
const startLoc = this.getCurrentPosition();
|
|
264
|
+
let numStr = '';
|
|
265
|
+
while (!this.isEof() && (this.isDigit(this.peek()) || this.peek() === '.')) {
|
|
266
|
+
numStr += this.advance();
|
|
267
|
+
}
|
|
268
|
+
return {
|
|
269
|
+
type: 'NUMBER',
|
|
270
|
+
value: numStr,
|
|
271
|
+
loc: createLocation(this.file, startLoc, this.getCurrentPosition())
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
readIdentifier() {
|
|
275
|
+
const startLoc = this.getCurrentPosition();
|
|
276
|
+
let idStr = '';
|
|
277
|
+
while (!this.isEof() && (this.isAlphaNumeric(this.peek()) || this.peek() === '_' || this.peek() === '-')) {
|
|
278
|
+
idStr += this.advance();
|
|
279
|
+
}
|
|
280
|
+
const keywordType = KEYWORDS[idStr];
|
|
281
|
+
return {
|
|
282
|
+
type: keywordType ?? 'IDENTIFIER',
|
|
283
|
+
value: idStr,
|
|
284
|
+
loc: createLocation(this.file, startLoc, this.getCurrentPosition())
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
skipComment() {
|
|
288
|
+
while (!this.isEof() && this.peek() !== '\n') {
|
|
289
|
+
this.advance();
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
peek(offset = 0) {
|
|
293
|
+
const idx = this.cursor + offset;
|
|
294
|
+
return idx < this.source.length ? this.source[idx] : '\0';
|
|
295
|
+
}
|
|
296
|
+
advance() {
|
|
297
|
+
const ch = this.source[this.cursor] ?? '\0';
|
|
298
|
+
this.cursor += 1;
|
|
299
|
+
if (ch === '\n') {
|
|
300
|
+
this.line += 1;
|
|
301
|
+
this.column = 1;
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
this.column += 1;
|
|
305
|
+
}
|
|
306
|
+
return ch;
|
|
307
|
+
}
|
|
308
|
+
isEof() {
|
|
309
|
+
return this.cursor >= this.source.length;
|
|
310
|
+
}
|
|
311
|
+
isDigit(ch) {
|
|
312
|
+
return ch >= '0' && ch <= '9';
|
|
313
|
+
}
|
|
314
|
+
isAlpha(ch) {
|
|
315
|
+
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
|
|
316
|
+
}
|
|
317
|
+
isAlphaNumeric(ch) {
|
|
318
|
+
return this.isAlpha(ch) || this.isDigit(ch);
|
|
319
|
+
}
|
|
320
|
+
getCurrentPosition() {
|
|
321
|
+
return createPosition(this.line, this.column, this.cursor);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
//# sourceMappingURL=lexer.js.map
|
|
@@ -0,0 +1 @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G,CAAC,CAAC;QAClB,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC;QACnB,CAAC;QACD,OAAO,EAAE,CAAC;IACZ,CAAC;IAEO,KAAK;QACX,OAAO,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;IAC3C,CAAC;IAEO,OAAO,CAAC,EAAU;QACxB,OAAO,EAAE,IAAI,GAAG,IAAI,EAAE,IAAI,GAAG,CAAC;IAChC,CAAC;IAEO,OAAO,CAAC,EAAU;QACxB,OAAO,CAAC,EAAE,IAAI,GAAG,IAAI,EAAE,IAAI,GAAG,CAAC,IAAI,CAAC,EAAE,IAAI,GAAG,IAAI,EAAE,IAAI,GAAG,CAAC,CAAC;IAC9D,CAAC;IAEO,cAAc,CAAC,EAAU;QAC/B,OAAO,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,IAAI,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IAC9C,CAAC;IAEO,kBAAkB;QACxB,OAAO,cAAc,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;IAC7D,CAAC;CACF"}
|
package/dist/parser.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ProgramNode } from '@kawaijs/ast';
|
|
2
|
+
import { Token } from './token.js';
|
|
3
|
+
export declare class Parser {
|
|
4
|
+
private readonly tokens;
|
|
5
|
+
private readonly file;
|
|
6
|
+
private cursor;
|
|
7
|
+
constructor(tokens: Token[], file?: string);
|
|
8
|
+
static fromSource(source: string, file?: string): Parser;
|
|
9
|
+
parse(): ProgramNode;
|
|
10
|
+
private parseStatement;
|
|
11
|
+
private parseCharacterDecl;
|
|
12
|
+
private parseLabelDecl;
|
|
13
|
+
private parseSceneStmt;
|
|
14
|
+
private parseShowStmt;
|
|
15
|
+
private parseHideStmt;
|
|
16
|
+
private parseDialogueStmt;
|
|
17
|
+
private parseMenuStmt;
|
|
18
|
+
private parseJumpStmt;
|
|
19
|
+
private parseReturnStmt;
|
|
20
|
+
private parseSetStmt;
|
|
21
|
+
private parseIfStmt;
|
|
22
|
+
private parsePlayStmt;
|
|
23
|
+
private parseStopStmt;
|
|
24
|
+
private parseBlock;
|
|
25
|
+
private readUntilColon;
|
|
26
|
+
private skipNewlines;
|
|
27
|
+
private consumeOptionalNewline;
|
|
28
|
+
private match;
|
|
29
|
+
private check;
|
|
30
|
+
private consume;
|
|
31
|
+
private advance;
|
|
32
|
+
private isAtEnd;
|
|
33
|
+
private peek;
|
|
34
|
+
private previous;
|
|
35
|
+
private currentLocation;
|
|
36
|
+
private previousLocation;
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=parser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../src/parser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAYV,WAAW,EAQZ,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,KAAK,EAAa,MAAM,YAAY,CAAC;AAI9C,qBAAa,MAAM;IACjB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAU;IACjC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAC9B,OAAO,CAAC,MAAM,CAAK;gBAEP,MAAM,EAAE,KAAK,EAAE,EAAE,IAAI,SAAgB;WAKnC,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,SAAgB,GAAG,MAAM;IAM/D,KAAK,IAAI,WAAW;IAqB3B,OAAO,CAAC,cAAc;IAwDtB,OAAO,CAAC,kBAAkB;IAoB1B,OAAO,CAAC,cAAc;IAetB,OAAO,CAAC,cAAc;IAgCtB,OAAO,CAAC,aAAa;IA8BrB,OAAO,CAAC,aAAa;IAkBrB,OAAO,CAAC,iBAAiB;IAqBzB,OAAO,CAAC,aAAa;IAkCrB,OAAO,CAAC,aAAa;IAYrB,OAAO,CA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