@indispec/fs-compat 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +188 -2
- package/package.json +46 -3
- package/src/engine/assembly.js +481 -0
- package/src/engine/csg.js +580 -0
- package/src/engine/interp.js +1713 -0
- package/src/engine/lex.js +166 -0
- package/src/engine/parse.js +539 -0
- package/src/engine/project.js +409 -0
- package/src/engine/regions.js +923 -0
- package/src/engine/trace.js +211 -0
- package/src/engine/values.js +494 -0
- package/src/index.d.ts +746 -0
- package/src/index.js +108 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// Tokenizer for the FeatureScript subset.
|
|
2
|
+
//
|
|
3
|
+
// Tokens are plain objects {kind, val, line, col, start, end}; kinds: number, string,
|
|
4
|
+
// keyword, ident, op, eof. Line/col are 1-based; a newline resets col to 1.
|
|
5
|
+
// `start`/`end` are absolute character offsets (end exclusive) into the source, so every
|
|
6
|
+
// AST node built from tokens can carry a precise, patchable span.
|
|
7
|
+
|
|
8
|
+
import { FsError } from './values.js';
|
|
9
|
+
|
|
10
|
+
export const KEYWORDS = new Set([
|
|
11
|
+
'FeatureScript', 'import', 'export', 'const', 'var', 'function', 'returns',
|
|
12
|
+
'precondition', 'annotation', 'enum', 'if', 'else', 'for', 'in', 'return',
|
|
13
|
+
'true', 'false', 'undefined', 'is', 'as', 'break', 'continue', 'while',
|
|
14
|
+
]);
|
|
15
|
+
|
|
16
|
+
// longest first so '==' wins over '='
|
|
17
|
+
export const OPERATORS = [
|
|
18
|
+
'==', '!=', '<=', '>=', '&&', '||', '~', '+', '-', '*', '/', '%', '^',
|
|
19
|
+
'=', '<', '>', '!', '(', ')', '[', ']', '{', '}', '.', ',', ';', ':', '?',
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
// str.isalpha()/isalnum() are unicode-aware; mirror with \p{L}/\p{N}.
|
|
23
|
+
const IS_ALPHA = /[\p{L}_]/u;
|
|
24
|
+
const IS_ALNUM = /[\p{L}\p{N}_]/u;
|
|
25
|
+
const IS_DIGIT = /\p{Nd}/u;
|
|
26
|
+
|
|
27
|
+
const STRING_ESCAPES = { n: '\n', t: '\t', '"': '"', "'": "'", '\\': '\\' };
|
|
28
|
+
|
|
29
|
+
// repr() of a short string, for error messages (quote style included).
|
|
30
|
+
function pyRepr(s) {
|
|
31
|
+
const hasSingle = s.includes("'");
|
|
32
|
+
const hasDouble = s.includes('"');
|
|
33
|
+
const q = hasSingle && !hasDouble ? '"' : "'";
|
|
34
|
+
let out = q;
|
|
35
|
+
for (const ch of s) {
|
|
36
|
+
if (ch === '\\' || ch === q) out += '\\' + ch;
|
|
37
|
+
else if (ch === '\n') out += '\\n';
|
|
38
|
+
else if (ch === '\t') out += '\\t';
|
|
39
|
+
else if (ch === '\r') out += '\\r';
|
|
40
|
+
else out += ch;
|
|
41
|
+
}
|
|
42
|
+
return out + q;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function tokenize(src, sourceName = '<fs>') {
|
|
46
|
+
const toks = [];
|
|
47
|
+
let i = 0;
|
|
48
|
+
let line = 1;
|
|
49
|
+
let col = 1;
|
|
50
|
+
const n = src.length;
|
|
51
|
+
|
|
52
|
+
const err = (msg) => {
|
|
53
|
+
throw new FsError(msg, line, col, sourceName);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
while (i < n) {
|
|
57
|
+
// Absolute character offset of this token, so AST spans can index the source.
|
|
58
|
+
const start = i;
|
|
59
|
+
const c = src[i];
|
|
60
|
+
// whitespace
|
|
61
|
+
if (c === ' ' || c === '\t' || c === '\r') {
|
|
62
|
+
i += 1;
|
|
63
|
+
col += 1;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (c === '\n') {
|
|
67
|
+
i += 1;
|
|
68
|
+
line += 1;
|
|
69
|
+
col = 1;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
// comments
|
|
73
|
+
if (src.startsWith('//', i)) {
|
|
74
|
+
while (i < n && src[i] !== '\n') i += 1;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (src.startsWith('/*', i)) {
|
|
78
|
+
i += 2;
|
|
79
|
+
while (i < n && !src.startsWith('*/', i)) {
|
|
80
|
+
if (src[i] === '\n') {
|
|
81
|
+
line += 1;
|
|
82
|
+
col = 1;
|
|
83
|
+
}
|
|
84
|
+
i += 1;
|
|
85
|
+
}
|
|
86
|
+
i += 2;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
// strings
|
|
90
|
+
if (c === '"' || c === "'") {
|
|
91
|
+
const quote = c;
|
|
92
|
+
i += 1;
|
|
93
|
+
col += 1;
|
|
94
|
+
const buf = [];
|
|
95
|
+
while (i < n && src[i] !== quote) {
|
|
96
|
+
if (src[i] === '\\' && i + 1 < n) {
|
|
97
|
+
const nxt = src[i + 1];
|
|
98
|
+
buf.push(STRING_ESCAPES[nxt] !== undefined ? STRING_ESCAPES[nxt] : nxt);
|
|
99
|
+
i += 2;
|
|
100
|
+
col += 2;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (src[i] === '\n') err('unterminated string literal');
|
|
104
|
+
buf.push(src[i]);
|
|
105
|
+
i += 1;
|
|
106
|
+
col += 1;
|
|
107
|
+
}
|
|
108
|
+
if (i >= n) err('unterminated string literal');
|
|
109
|
+
i += 1;
|
|
110
|
+
col += 1;
|
|
111
|
+
toks.push({ kind: 'string', val: buf.join(''), line, col, start, end: i });
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
// numbers
|
|
115
|
+
if (
|
|
116
|
+
IS_DIGIT.test(c) ||
|
|
117
|
+
(c === '.' && i + 1 < n && IS_DIGIT.test(src[i + 1]))
|
|
118
|
+
) {
|
|
119
|
+
let j = i;
|
|
120
|
+
while (j < n && (IS_DIGIT.test(src[j]) || src[j] === '.')) j += 1;
|
|
121
|
+
if (j < n && (src[j] === 'e' || src[j] === 'E')) {
|
|
122
|
+
let k = j + 1;
|
|
123
|
+
if (k < n && (src[k] === '+' || src[k] === '-')) k += 1;
|
|
124
|
+
if (k < n && IS_DIGIT.test(src[k])) {
|
|
125
|
+
j = k;
|
|
126
|
+
while (j < n && IS_DIGIT.test(src[j])) j += 1;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const text = src.slice(i, j);
|
|
130
|
+
// The scan above can capture text a float() would reject
|
|
131
|
+
// (e.g. "1..5"). Fail loudly
|
|
132
|
+
// instead of silently mis-parsing like parseFloat would.
|
|
133
|
+
if (!/^(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?$/.test(text)) {
|
|
134
|
+
err(`invalid number literal '${text}'`);
|
|
135
|
+
}
|
|
136
|
+
toks.push({ kind: 'number', val: parseFloat(text), line, col, start, end: j });
|
|
137
|
+
col += j - i;
|
|
138
|
+
i = j;
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
// identifiers / keywords
|
|
142
|
+
if (IS_ALPHA.test(c)) {
|
|
143
|
+
let j = i;
|
|
144
|
+
while (j < n && IS_ALNUM.test(src[j])) j += 1;
|
|
145
|
+
const word = src.slice(i, j);
|
|
146
|
+
toks.push({ kind: KEYWORDS.has(word) ? 'keyword' : 'ident', val: word, line, col, start, end: j });
|
|
147
|
+
col += j - i;
|
|
148
|
+
i = j;
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
// operators
|
|
152
|
+
let matched = null;
|
|
153
|
+
for (const op of OPERATORS) {
|
|
154
|
+
if (src.startsWith(op, i)) {
|
|
155
|
+
matched = op;
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (matched === null) err(`unexpected character ${pyRepr(c)}`);
|
|
160
|
+
toks.push({ kind: 'op', val: matched, line, col, start, end: i + matched.length });
|
|
161
|
+
i += matched.length;
|
|
162
|
+
col += matched.length;
|
|
163
|
+
}
|
|
164
|
+
toks.push({ kind: 'eof', val: null, line, col, start: i, end: i });
|
|
165
|
+
return toks;
|
|
166
|
+
}
|
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
// Recursive-descent parser for the FeatureScript subset.
|
|
2
|
+
//
|
|
3
|
+
// AST nodes are PLAIN OBJECTS {kind, line, start, end, ...fields} with kind names
|
|
4
|
+
// the value classes are Num, Str, Bool, Undefined, Ident,
|
|
5
|
+
// ArrayLit, MapLit, Call, Member, Index, Bin, Un, Cast, IsType, FuncLit,
|
|
6
|
+
// VarDecl, Assign, If, For, Return, ExprStmt, Block, FuncDecl, ConstDecl,
|
|
7
|
+
// EnumDecl, Annotation, Skip. Anything outside the subset raises FsError
|
|
8
|
+
// with a line/column rather than being silently mis-parsed.
|
|
9
|
+
//
|
|
10
|
+
// `start`/`end` are absolute character offsets into the source (end exclusive), stamped
|
|
11
|
+
// by `span()`. Line numbers are for diagnostics; spans are for editing and source maps.
|
|
12
|
+
|
|
13
|
+
import { tokenize } from './lex.js';
|
|
14
|
+
import { FsError } from './values.js';
|
|
15
|
+
|
|
16
|
+
class Parser {
|
|
17
|
+
constructor(src, sourceName = '<fs>') {
|
|
18
|
+
this.source = sourceName;
|
|
19
|
+
this.toks = tokenize(src, sourceName);
|
|
20
|
+
this.i = 0;
|
|
21
|
+
/** Last consumed token; its `end` closes a node's span. */
|
|
22
|
+
this.prev = null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Stamp an AST node with its absolute source span.
|
|
27
|
+
*
|
|
28
|
+
* `start` is the offset of the node's first token; the end is the end of the most
|
|
29
|
+
* recently consumed token. Spans are what lets an editor patch source text (a
|
|
30
|
+
* parameter literal, a statement to move or suppress) instead of reprinting the AST.
|
|
31
|
+
*
|
|
32
|
+
* @param node Node object to annotate (a plain object, mutated in place).
|
|
33
|
+
* @param start Absolute character offset of the node's first token.
|
|
34
|
+
* @returns The same node, so callers can use it in `return`/`e =` expressions.
|
|
35
|
+
*/
|
|
36
|
+
span(node, start) {
|
|
37
|
+
node.start = start;
|
|
38
|
+
node.end = this.prev ? this.prev.end : start;
|
|
39
|
+
return node;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// -- token helpers ---------------------------------------------------------
|
|
43
|
+
get tok() {
|
|
44
|
+
return this.toks[this.i];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
at(kind, val = null) {
|
|
48
|
+
const t = this.tok;
|
|
49
|
+
return t.kind === kind && (val === null || t.val === val);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
atOp(...vals) {
|
|
53
|
+
const t = this.tok;
|
|
54
|
+
return t.kind === 'op' && vals.includes(t.val);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
atKw(...vals) {
|
|
58
|
+
const t = this.tok;
|
|
59
|
+
return t.kind === 'keyword' && vals.includes(t.val);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
next() {
|
|
63
|
+
const t = this.tok;
|
|
64
|
+
this.i += 1;
|
|
65
|
+
this.prev = t;
|
|
66
|
+
return t;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
acceptOp(val) {
|
|
70
|
+
if (this.atOp(val)) return this.next();
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
acceptKw(val) {
|
|
75
|
+
if (this.atKw(val)) return this.next();
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
expectOp(val) {
|
|
80
|
+
if (!this.atOp(val)) this.fail(`expected '${val}'`);
|
|
81
|
+
return this.next();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
expectIdent() {
|
|
85
|
+
if (this.tok.kind !== 'ident' && this.tok.kind !== 'keyword') {
|
|
86
|
+
this.fail('expected an identifier');
|
|
87
|
+
}
|
|
88
|
+
return this.next().val;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
fail(msg) {
|
|
92
|
+
const t = this.tok;
|
|
93
|
+
throw new FsError(
|
|
94
|
+
t.val !== null ? `${msg}, found '${t.val}'` : msg,
|
|
95
|
+
t.line,
|
|
96
|
+
t.col,
|
|
97
|
+
this.source,
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// -- program ---------------------------------------------------------------
|
|
102
|
+
parseProgram() {
|
|
103
|
+
const stmts = [];
|
|
104
|
+
while (!this.at('eof')) {
|
|
105
|
+
stmts.push(this.parseStatement());
|
|
106
|
+
}
|
|
107
|
+
return stmts;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
parseStatement() {
|
|
111
|
+
const t = this.tok;
|
|
112
|
+
if (this.atOp(';')) {
|
|
113
|
+
this.next();
|
|
114
|
+
return this.span({ kind: 'Skip', line: t.line }, t.start);
|
|
115
|
+
}
|
|
116
|
+
if (this.atOp('{')) {
|
|
117
|
+
return this.parseBlock();
|
|
118
|
+
}
|
|
119
|
+
if (this.atKw('annotation')) {
|
|
120
|
+
this.next();
|
|
121
|
+
const m = this.parseMapLiteral();
|
|
122
|
+
const inner = this.parseStatement();
|
|
123
|
+
return this.span({ kind: 'Annotation', line: t.line, map: m, stmt: inner }, t.start);
|
|
124
|
+
}
|
|
125
|
+
if (this.atKw('FeatureScript')) {
|
|
126
|
+
this.next();
|
|
127
|
+
while (!this.at('eof') && !this.atOp(';')) {
|
|
128
|
+
this.next();
|
|
129
|
+
}
|
|
130
|
+
this.acceptOp(';');
|
|
131
|
+
return this.span({ kind: 'Skip', line: t.line }, t.start);
|
|
132
|
+
}
|
|
133
|
+
if (this.atKw('import')) {
|
|
134
|
+
this.next();
|
|
135
|
+
let depth = 0;
|
|
136
|
+
while (!this.at('eof')) {
|
|
137
|
+
if (this.atOp('(')) {
|
|
138
|
+
depth += 1;
|
|
139
|
+
} else if (this.atOp(')')) {
|
|
140
|
+
depth -= 1;
|
|
141
|
+
if (depth === 0) {
|
|
142
|
+
this.next();
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
this.next();
|
|
147
|
+
}
|
|
148
|
+
this.acceptOp(';');
|
|
149
|
+
return this.span({ kind: 'Skip', line: t.line }, t.start);
|
|
150
|
+
}
|
|
151
|
+
if (this.atKw('export')) {
|
|
152
|
+
this.next();
|
|
153
|
+
if (this.atKw('enum')) {
|
|
154
|
+
return this.parseEnum(t.line, t.start);
|
|
155
|
+
}
|
|
156
|
+
if (this.atKw('function')) {
|
|
157
|
+
// `export` only widens a declaration's scope; for a single-module
|
|
158
|
+
// run it changes nothing.
|
|
159
|
+
return this.parseFuncDecl();
|
|
160
|
+
}
|
|
161
|
+
return this.parseDecl(true, t.line, t.start);
|
|
162
|
+
}
|
|
163
|
+
if (this.atKw('enum')) {
|
|
164
|
+
return this.parseEnum(t.line, t.start);
|
|
165
|
+
}
|
|
166
|
+
if (this.atKw('const')) {
|
|
167
|
+
return this.parseDecl(false, t.line, t.start);
|
|
168
|
+
}
|
|
169
|
+
if (this.atKw('function')) {
|
|
170
|
+
return this.parseFuncDecl();
|
|
171
|
+
}
|
|
172
|
+
if (this.atKw('var')) {
|
|
173
|
+
this.next();
|
|
174
|
+
const name = this.expectIdent();
|
|
175
|
+
let expr = null;
|
|
176
|
+
if (this.acceptOp('=')) {
|
|
177
|
+
expr = this.parseExpr();
|
|
178
|
+
}
|
|
179
|
+
this.acceptOp(';');
|
|
180
|
+
return this.span({ kind: 'VarDecl', line: t.line, name, expr }, t.start);
|
|
181
|
+
}
|
|
182
|
+
if (this.atKw('if')) {
|
|
183
|
+
return this.parseIf();
|
|
184
|
+
}
|
|
185
|
+
if (this.atKw('for')) {
|
|
186
|
+
return this.parseFor();
|
|
187
|
+
}
|
|
188
|
+
if (this.atKw('return')) {
|
|
189
|
+
this.next();
|
|
190
|
+
let expr = null;
|
|
191
|
+
if (!this.atOp(';')) {
|
|
192
|
+
expr = this.parseExpr();
|
|
193
|
+
}
|
|
194
|
+
this.acceptOp(';');
|
|
195
|
+
return this.span({ kind: 'Return', line: t.line, expr }, t.start);
|
|
196
|
+
}
|
|
197
|
+
if (this.atKw('break') || this.atKw('continue')) {
|
|
198
|
+
this.next();
|
|
199
|
+
this.acceptOp(';');
|
|
200
|
+
return this.span({ kind: 'Skip', line: t.line }, t.start);
|
|
201
|
+
}
|
|
202
|
+
// assignment or expression statement
|
|
203
|
+
const expr = this.parseExpr();
|
|
204
|
+
if (this.atOp('=')) {
|
|
205
|
+
this.next();
|
|
206
|
+
const rhs = this.parseExpr();
|
|
207
|
+
this.acceptOp(';');
|
|
208
|
+
return this.span({ kind: 'Assign', line: t.line, target: expr, expr: rhs }, t.start);
|
|
209
|
+
}
|
|
210
|
+
this.acceptOp(';');
|
|
211
|
+
return this.span({ kind: 'ExprStmt', line: t.line, expr }, t.start);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
parseDecl(exported, line, start) {
|
|
215
|
+
if (!this.atKw('const') && !this.atKw('var')) {
|
|
216
|
+
this.fail('expected const/var after export');
|
|
217
|
+
}
|
|
218
|
+
this.next();
|
|
219
|
+
const name = this.expectIdent();
|
|
220
|
+
let expr = null;
|
|
221
|
+
if (this.acceptOp('=')) {
|
|
222
|
+
expr = this.parseExpr();
|
|
223
|
+
}
|
|
224
|
+
this.acceptOp(';');
|
|
225
|
+
return this.span({ kind: 'ConstDecl', line, name, expr, exported }, start);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
parseEnum(line, start) {
|
|
229
|
+
this.next(); // enum
|
|
230
|
+
// (also handles the `export enum` case, where export was consumed)
|
|
231
|
+
if (this.atKw('export')) {
|
|
232
|
+
this.next();
|
|
233
|
+
}
|
|
234
|
+
const name = this.expectIdent();
|
|
235
|
+
this.expectOp('{');
|
|
236
|
+
const members = [];
|
|
237
|
+
while (!this.atOp('}') && !this.at('eof')) {
|
|
238
|
+
if (this.atKw('annotation')) {
|
|
239
|
+
this.next();
|
|
240
|
+
this.parseMapLiteral();
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
if (this.atOp(',')) {
|
|
244
|
+
this.next();
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
members.push(this.expectIdent());
|
|
248
|
+
}
|
|
249
|
+
this.expectOp('}');
|
|
250
|
+
return this.span({ kind: 'EnumDecl', line, name, members }, start);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
parseFuncDecl() {
|
|
254
|
+
const start = this.tok.start;
|
|
255
|
+
const line = this.tok.line;
|
|
256
|
+
this.next(); // function
|
|
257
|
+
const name = this.expectIdent();
|
|
258
|
+
const params = this.parseParams();
|
|
259
|
+
if (this.acceptKw('returns')) {
|
|
260
|
+
this.expectIdent();
|
|
261
|
+
}
|
|
262
|
+
const body = this.parseBlock();
|
|
263
|
+
return this.span({ kind: 'FuncDecl', line, name, params, body }, start);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
parseParams() {
|
|
267
|
+
this.expectOp('(');
|
|
268
|
+
const params = [];
|
|
269
|
+
while (!this.atOp(')')) {
|
|
270
|
+
const pname = this.expectIdent();
|
|
271
|
+
if (this.acceptKw('is')) {
|
|
272
|
+
this.expectIdent();
|
|
273
|
+
}
|
|
274
|
+
params.push(pname);
|
|
275
|
+
if (!this.acceptOp(',')) break;
|
|
276
|
+
}
|
|
277
|
+
this.expectOp(')');
|
|
278
|
+
return params;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
parseIf() {
|
|
282
|
+
const start = this.tok.start;
|
|
283
|
+
const line = this.tok.line;
|
|
284
|
+
this.next();
|
|
285
|
+
this.expectOp('(');
|
|
286
|
+
const cond = this.parseExpr();
|
|
287
|
+
this.expectOp(')');
|
|
288
|
+
const then = this.parseBlock();
|
|
289
|
+
let els = null;
|
|
290
|
+
if (this.acceptKw('else')) {
|
|
291
|
+
if (this.atKw('if')) {
|
|
292
|
+
els = this.span({ kind: 'Block', line, stmts: [this.parseIf()] }, this.tok.start);
|
|
293
|
+
} else {
|
|
294
|
+
els = this.parseBlock();
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return this.span({ kind: 'If', line, cond, then, els }, start);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
parseFor() {
|
|
301
|
+
const start = this.tok.start;
|
|
302
|
+
const line = this.tok.line;
|
|
303
|
+
this.next();
|
|
304
|
+
this.expectOp('(');
|
|
305
|
+
this.acceptKw('var');
|
|
306
|
+
const v = this.expectIdent();
|
|
307
|
+
if (!this.acceptKw('in')) {
|
|
308
|
+
this.fail("expected 'in' in for-loop");
|
|
309
|
+
}
|
|
310
|
+
const iterable = this.parseExpr();
|
|
311
|
+
this.expectOp(')');
|
|
312
|
+
const body = this.parseBlock();
|
|
313
|
+
return this.span({ kind: 'For', line, var: v, iterable, body }, start);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
parseBlock() {
|
|
317
|
+
const start = this.tok.start;
|
|
318
|
+
const line = this.tok.line;
|
|
319
|
+
this.expectOp('{');
|
|
320
|
+
const stmts = [];
|
|
321
|
+
while (!this.atOp('}') && !this.at('eof')) {
|
|
322
|
+
stmts.push(this.parseStatement());
|
|
323
|
+
}
|
|
324
|
+
this.expectOp('}');
|
|
325
|
+
return this.span({ kind: 'Block', line, stmts }, start);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// -- expressions -----------------------------------------------------------
|
|
329
|
+
parseExpr() {
|
|
330
|
+
const e = this.parseOr();
|
|
331
|
+
if (this.atKw('as')) {
|
|
332
|
+
this.next();
|
|
333
|
+
const tname = this.expectIdent();
|
|
334
|
+
return this.span({ kind: 'Cast', line: e.line, expr: e, typename: tname }, e.start);
|
|
335
|
+
}
|
|
336
|
+
return e;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
parseOr() {
|
|
340
|
+
let e = this.parseAnd();
|
|
341
|
+
while (this.atOp('||')) {
|
|
342
|
+
const op = this.next();
|
|
343
|
+
e = this.span({ kind: 'Bin', line: op.line, op: '||', l: e, r: this.parseAnd() }, e.start);
|
|
344
|
+
}
|
|
345
|
+
return e;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
parseAnd() {
|
|
349
|
+
let e = this.parseEquality();
|
|
350
|
+
while (this.atOp('&&')) {
|
|
351
|
+
const op = this.next();
|
|
352
|
+
e = this.span({ kind: 'Bin', line: op.line, op: '&&', l: e, r: this.parseEquality() }, e.start);
|
|
353
|
+
}
|
|
354
|
+
return e;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
parseEquality() {
|
|
358
|
+
let e = this.parseRelational();
|
|
359
|
+
while (this.atOp('==', '!=')) {
|
|
360
|
+
const op = this.next();
|
|
361
|
+
e = this.span({ kind: 'Bin', line: op.line, op: op.val, l: e, r: this.parseRelational() }, e.start);
|
|
362
|
+
}
|
|
363
|
+
return e;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
parseRelational() {
|
|
367
|
+
let e = this.parseAdditive();
|
|
368
|
+
for (;;) {
|
|
369
|
+
if (this.atKw('is')) {
|
|
370
|
+
this.next();
|
|
371
|
+
e = this.span({ kind: 'IsType', line: e.line, expr: e, typename: this.expectIdent() }, e.start);
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
if (this.atOp('<', '>', '<=', '>=')) {
|
|
375
|
+
const op = this.next();
|
|
376
|
+
e = this.span({ kind: 'Bin', line: op.line, op: op.val, l: e, r: this.parseAdditive() }, e.start);
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
return e;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
parseAdditive() {
|
|
384
|
+
let e = this.parseMultiplicative();
|
|
385
|
+
while (this.atOp('+', '-', '~')) {
|
|
386
|
+
const op = this.next();
|
|
387
|
+
e = this.span({ kind: 'Bin', line: op.line, op: op.val, l: e, r: this.parseMultiplicative() }, e.start);
|
|
388
|
+
}
|
|
389
|
+
return e;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
parseMultiplicative() {
|
|
393
|
+
let e = this.parseUnary();
|
|
394
|
+
while (this.atOp('*', '/', '%')) {
|
|
395
|
+
const op = this.next();
|
|
396
|
+
e = this.span({ kind: 'Bin', line: op.line, op: op.val, l: e, r: this.parseUnary() }, e.start);
|
|
397
|
+
}
|
|
398
|
+
return e;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
parseUnary() {
|
|
402
|
+
if (this.atOp('-')) {
|
|
403
|
+
const t = this.next();
|
|
404
|
+
return this.span({ kind: 'Un', line: t.line, op: '-', e: this.parseUnary() }, t.start);
|
|
405
|
+
}
|
|
406
|
+
if (this.atOp('!')) {
|
|
407
|
+
const t = this.next();
|
|
408
|
+
return this.span({ kind: 'Un', line: t.line, op: '!', e: this.parseUnary() }, t.start);
|
|
409
|
+
}
|
|
410
|
+
return this.parsePostfix();
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
parsePostfix() {
|
|
414
|
+
let e = this.parsePrimary();
|
|
415
|
+
for (;;) {
|
|
416
|
+
if (this.atOp('(')) {
|
|
417
|
+
const line = this.tok.line;
|
|
418
|
+
this.next();
|
|
419
|
+
const args = [];
|
|
420
|
+
while (!this.atOp(')')) {
|
|
421
|
+
args.push(this.parseExpr());
|
|
422
|
+
if (!this.acceptOp(',')) break;
|
|
423
|
+
}
|
|
424
|
+
this.expectOp(')');
|
|
425
|
+
e = this.span({ kind: 'Call', line, callee: e, args }, e.start);
|
|
426
|
+
continue;
|
|
427
|
+
}
|
|
428
|
+
if (this.atOp('.')) {
|
|
429
|
+
this.next();
|
|
430
|
+
e = this.span({ kind: 'Member', line: e.line, obj: e, name: this.expectIdent() }, e.start);
|
|
431
|
+
continue;
|
|
432
|
+
}
|
|
433
|
+
if (this.atOp('[')) {
|
|
434
|
+
this.next();
|
|
435
|
+
const idx = this.parseExpr();
|
|
436
|
+
this.expectOp(']');
|
|
437
|
+
e = this.span({ kind: 'Index', line: e.line, obj: e, idx }, e.start);
|
|
438
|
+
continue;
|
|
439
|
+
}
|
|
440
|
+
return e;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
parsePrimary() {
|
|
445
|
+
const t = this.tok;
|
|
446
|
+
if (t.kind === 'number') {
|
|
447
|
+
this.next();
|
|
448
|
+
return this.span({ kind: 'Num', line: t.line, value: t.val }, t.start);
|
|
449
|
+
}
|
|
450
|
+
if (t.kind === 'string') {
|
|
451
|
+
this.next();
|
|
452
|
+
return this.span({ kind: 'Str', line: t.line, value: t.val }, t.start);
|
|
453
|
+
}
|
|
454
|
+
if (t.kind === 'ident') {
|
|
455
|
+
this.next();
|
|
456
|
+
return this.span({ kind: 'Ident', line: t.line, name: t.val }, t.start);
|
|
457
|
+
}
|
|
458
|
+
if (t.kind === 'keyword') {
|
|
459
|
+
if (t.val === 'true') {
|
|
460
|
+
this.next();
|
|
461
|
+
return this.span({ kind: 'Bool', line: t.line, value: true }, t.start);
|
|
462
|
+
}
|
|
463
|
+
if (t.val === 'false') {
|
|
464
|
+
this.next();
|
|
465
|
+
return this.span({ kind: 'Bool', line: t.line, value: false }, t.start);
|
|
466
|
+
}
|
|
467
|
+
if (t.val === 'undefined') {
|
|
468
|
+
this.next();
|
|
469
|
+
return this.span({ kind: 'Undefined', line: t.line }, t.start);
|
|
470
|
+
}
|
|
471
|
+
if (t.val === 'function') {
|
|
472
|
+
return this.parseFuncLiteral();
|
|
473
|
+
}
|
|
474
|
+
if (t.val === 'annotation') {
|
|
475
|
+
// annotation inside an enum body etc.
|
|
476
|
+
this.next();
|
|
477
|
+
this.parseMapLiteral();
|
|
478
|
+
return this.span({ kind: 'Undefined', line: t.line }, t.start);
|
|
479
|
+
}
|
|
480
|
+
// a keyword used as a name (e.g. an enum member called `in`)
|
|
481
|
+
this.next();
|
|
482
|
+
return this.span({ kind: 'Ident', line: t.line, name: t.val }, t.start);
|
|
483
|
+
}
|
|
484
|
+
if (this.atOp('(')) {
|
|
485
|
+
this.next();
|
|
486
|
+
const e = this.parseExpr();
|
|
487
|
+
this.expectOp(')');
|
|
488
|
+
return e;
|
|
489
|
+
}
|
|
490
|
+
if (this.atOp('[')) {
|
|
491
|
+
this.next();
|
|
492
|
+
const items = [];
|
|
493
|
+
while (!this.atOp(']')) {
|
|
494
|
+
items.push(this.parseExpr());
|
|
495
|
+
if (!this.acceptOp(',')) break;
|
|
496
|
+
}
|
|
497
|
+
this.expectOp(']');
|
|
498
|
+
return this.span({ kind: 'ArrayLit', line: t.line, items }, t.start);
|
|
499
|
+
}
|
|
500
|
+
if (this.atOp('{')) {
|
|
501
|
+
return this.parseMapLiteral();
|
|
502
|
+
}
|
|
503
|
+
this.fail('expected an expression');
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
parseFuncLiteral() {
|
|
507
|
+
const start = this.tok.start;
|
|
508
|
+
const line = this.tok.line;
|
|
509
|
+
this.next(); // function
|
|
510
|
+
const params = this.parseParams();
|
|
511
|
+
let precondition = null;
|
|
512
|
+
if (this.atKw('precondition')) {
|
|
513
|
+
this.next();
|
|
514
|
+
precondition = this.parseBlock();
|
|
515
|
+
}
|
|
516
|
+
const body = this.parseBlock();
|
|
517
|
+
return this.span({ kind: 'FuncLit', line, params, precondition, body }, start);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
parseMapLiteral() {
|
|
521
|
+
const start = this.tok.start;
|
|
522
|
+
const line = this.tok.line;
|
|
523
|
+
this.expectOp('{');
|
|
524
|
+
const pairs = [];
|
|
525
|
+
while (!this.atOp('}')) {
|
|
526
|
+
const key = this.parseExpr();
|
|
527
|
+
this.expectOp(':');
|
|
528
|
+
const val = this.parseExpr();
|
|
529
|
+
pairs.push([key, val]);
|
|
530
|
+
if (!this.acceptOp(',')) break;
|
|
531
|
+
}
|
|
532
|
+
this.expectOp('}');
|
|
533
|
+
return this.span({ kind: 'MapLit', line, pairs }, start);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
export function parse(src, sourceName = '<fs>') {
|
|
538
|
+
return new Parser(src, sourceName).parseProgram();
|
|
539
|
+
}
|