UNPKG

10.9 kBJavaScriptView Raw
1"use strict";
2/**
3 * Consumes (and documents) the messy output produced by the parser, and turns it into parsedsyntax.ts types. This file could easily produce garbage output if there's a mismatch between the documented types and the types that the parser produces. This file should only throw errors to document invariants of the parser; user errors should be thrown in restrictsyntax.ts.
4 *
5 * The structure of this file should match parsedsyntax.ts as much as practical.
6 */
7Object.defineProperty(exports, "__esModule", { value: true });
8function Identifier([{ value, text, offset, lineBreaks, line, col }]) {
9 return {
10 tag: "Identifier",
11 name: text
12 };
13}
14exports.Identifier = Identifier;
15function IntType([tok]) {
16 return {
17 tag: "IntType"
18 };
19}
20exports.IntType = IntType;
21function BoolType([tok]) {
22 return {
23 tag: "BoolType"
24 };
25}
26exports.BoolType = BoolType;
27function StringType([tok]) {
28 return {
29 tag: "StringType"
30 };
31}
32exports.StringType = StringType;
33function CharType([tok]) {
34 return {
35 tag: "CharType"
36 };
37}
38exports.CharType = CharType;
39function VoidType([tok]) {
40 return {
41 tag: "VoidType"
42 };
43}
44exports.VoidType = VoidType;
45function PointerType([tp, s, tok]) {
46 return {
47 tag: "PointerType",
48 argument: tp
49 };
50}
51exports.PointerType = PointerType;
52function ArrayType([tp, s1, l, s2, r]) {
53 return {
54 tag: "ArrayType",
55 argument: tp
56 };
57}
58exports.ArrayType = ArrayType;
59function StructType([str, s, id]) {
60 return {
61 tag: "StructType",
62 id: id
63 };
64}
65exports.StructType = StructType;
66function IntLiteral([{ value, text, offset, lineBreaks, line, col }]) {
67 return {
68 tag: "IntLiteral",
69 raw: text
70 };
71}
72exports.IntLiteral = IntLiteral;
73function CharLiteral([[start, [tok], end]]) {
74 return {
75 tag: "CharLiteral",
76 raw: tok.value
77 };
78}
79exports.CharLiteral = CharLiteral;
80function StringLiteral([[start, toks, end]]) {
81 return {
82 tag: "StringLiteral",
83 raw: toks.map(x => x[0].value)
84 };
85}
86exports.StringLiteral = StringLiteral;
87function BoolLiteral([t]) {
88 return {
89 tag: "BoolLiteral",
90 value: t.value === "true"
91 };
92}
93exports.BoolLiteral = BoolLiteral;
94function NullLiteral() {
95 return {
96 tag: "NullLiteral"
97 };
98}
99exports.NullLiteral = NullLiteral;
100function ArrayMemberExpression([object, s1, l, s2, index, s3, r]) {
101 return {
102 tag: "ArrayMemberExpression",
103 object: object,
104 index: index
105 };
106}
107exports.ArrayMemberExpression = ArrayMemberExpression;
108function Arguments([l, s1, args, r]) {
109 if (args === null)
110 return [];
111 return [args[0]].concat(args[2].map(x => x[2]));
112}
113exports.Arguments = Arguments;
114function StructMemberExpression([object, s1, deref, s2, field]) {
115 return {
116 tag: "StructMemberExpression",
117 deref: deref instanceof Array,
118 object: object,
119 field: field
120 };
121}
122exports.StructMemberExpression = StructMemberExpression;
123function CallExpression([f, ws, args]) {
124 return {
125 tag: "CallExpression",
126 callee: f,
127 arguments: Arguments(args)
128 };
129}
130exports.CallExpression = CallExpression;
131function IndirectCallExpression([l, s1, s, s2, f, s3, r, s4, args]) {
132 return {
133 tag: "IndirectCallExpression",
134 callee: f,
135 arguments: Arguments(args)
136 };
137}
138exports.IndirectCallExpression = IndirectCallExpression;
139function UnaryExpression([operator, s, argument]) {
140 if (operator.length == 1) {
141 switch (operator[0].value) {
142 case "&":
143 case "!":
144 case "~":
145 case "-":
146 case "*":
147 return {
148 tag: "UnaryExpression",
149 operator: operator[0].value,
150 argument: argument
151 };
152 default:
153 throw new Error(operator[0].value);
154 }
155 }
156 else {
157 return {
158 tag: "CastExpression",
159 kind: operator[2],
160 argument: argument
161 };
162 }
163}
164exports.UnaryExpression = UnaryExpression;
165function BinaryExpression([left, s1, opertoks, s2, right]) {
166 const operator = opertoks.map((tok) => tok.text).join("");
167 switch (operator) {
168 case "*":
169 case "/":
170 case "%":
171 case "+":
172 case "-":
173 case "<<":
174 case ">>":
175 case "<":
176 case "<=":
177 case ">=":
178 case ">":
179 case "==":
180 case "!=":
181 case "&":
182 case "^":
183 case "|":
184 return {
185 tag: "BinaryExpression",
186 operator: operator,
187 left: left,
188 right: right
189 };
190 case "&&":
191 case "||":
192 return {
193 tag: "LogicalExpression",
194 operator: operator,
195 left: left,
196 right: right
197 };
198 case "=":
199 case "+=":
200 case "-=":
201 case "*=":
202 case "/=":
203 case "%=":
204 case "&=":
205 case "^=":
206 case "|=":
207 case "<<=":
208 case ">>=":
209 return {
210 tag: "AssignmentExpression",
211 operator: operator,
212 left: left,
213 right: right
214 };
215 default:
216 throw new Error(operator);
217 }
218}
219exports.BinaryExpression = BinaryExpression;
220function ConditionalExpression([test, s1, op1, s2, consequent, s3, op2, s4, alternate]) {
221 return {
222 tag: "ConditionalExpression",
223 test: test,
224 consequent: consequent,
225 alternate: alternate
226 };
227}
228exports.ConditionalExpression = ConditionalExpression;
229function AllocExpression([alloc, s1, l, s2, typ, s3, r]) {
230 return {
231 tag: "AllocExpression",
232 kind: typ
233 };
234}
235exports.AllocExpression = AllocExpression;
236function AllocArrayExpression([alloc, s1, l, s2, typ, s3, c, s4, size, sp, r]) {
237 return {
238 tag: "AllocArrayExpression",
239 kind: typ,
240 size: size
241 };
242}
243exports.AllocArrayExpression = AllocArrayExpression;
244function ResultExpression([b, res]) {
245 return {
246 tag: "ResultExpression"
247 };
248}
249exports.ResultExpression = ResultExpression;
250function LengthExpression([b, length, s1, l, s2, argument, s3, r]) {
251 return {
252 tag: "LengthExpression",
253 argument: argument
254 };
255}
256exports.LengthExpression = LengthExpression;
257function HasTagExpression([b, hastag, s1, l, s2, typ, s3, c, s4, argument, s5, r]) {
258 return {
259 tag: "HasTagExpression",
260 kind: typ,
261 argument: argument
262 };
263}
264exports.HasTagExpression = HasTagExpression;
265function UpdateExpression([argument, s1, op1, op2]) {
266 return {
267 tag: "UpdateExpression",
268 argument: argument,
269 operator: op1.value === "+" ? "++" : "--"
270 };
271}
272exports.UpdateExpression = UpdateExpression;
273function AssertExpression([assert, s1, l, s2, test, s3, r]) {
274 return {
275 tag: "AssertExpression",
276 test: test
277 };
278}
279exports.AssertExpression = AssertExpression;
280function ErrorExpression([error, s1, l, s2, argument, s3, r]) {
281 return {
282 tag: "ErrorExpression",
283 argument: argument
284 };
285}
286exports.ErrorExpression = ErrorExpression;
287function SimpleStatement([stm, s1, semi]) {
288 if (stm instanceof Array) {
289 const init = stm[3];
290 return {
291 tag: "VariableDeclaration",
292 kind: stm[0],
293 id: stm[2],
294 init: init === null ? null : init[3]
295 };
296 }
297 else {
298 return {
299 tag: "ExpressionStatement",
300 expression: stm
301 };
302 }
303}
304exports.SimpleStatement = SimpleStatement;
305function IfStatement([i, s1, l, s2, test, s3, r, s4, [annos, consequent]]) {
306 return {
307 tag: "IfStatement",
308 test: test,
309 consequent: [annos, consequent]
310 };
311}
312exports.IfStatement = IfStatement;
313function IfElseStatement([i, s1, l1, s2, test, s3, r, annos1, s4, consequent, s5, e, annos2, s6, alternate]) {
314 return {
315 tag: "IfStatement",
316 test: test,
317 consequent: [annos1, consequent],
318 alternate: [annos2, alternate]
319 };
320}
321exports.IfElseStatement = IfElseStatement;
322function WhileStatement([w, s1, l, s2, test, s3, r, annos, s4, body]) {
323 return {
324 tag: "WhileStatement",
325 test: test,
326 annos: annos,
327 body: body
328 };
329}
330exports.WhileStatement = WhileStatement;
331function ForStatement([f, s1, l, init, s2, semi1, s3, test, s4, semi2, update, s5, r, annos, s6, body]) {
332 return {
333 tag: "ForStatement",
334 init: init === null ? null : SimpleStatement([init[1], s2, semi1]),
335 test: test,
336 update: update === null ? null : update[1],
337 annos: annos,
338 body: body
339 };
340}
341exports.ForStatement = ForStatement;
342function ReturnStatement([r, argument, s1, semi]) {
343 return {
344 tag: "ReturnStatement",
345 argument: argument === null ? null : argument[1]
346 };
347}
348exports.ReturnStatement = ReturnStatement;
349function BlockStatement([l, stms, annos, s, r]) {
350 const stms1 = stms.map(x => x[1][0].map((y) => ({ tag: "AnnoStatement", anno: y })).concat([x[1][1]]));
351 const stms2 = annos.map((x) => ({
352 tag: "AnnoStatement",
353 anno: x[1][0]
354 }));
355 const stmsAll = stms1
356 .concat([stms2])
357 .reduce((collect, stms) => collect.concat(stms), []);
358 return {
359 tag: "BlockStatement",
360 body: stmsAll
361 };
362}
363exports.BlockStatement = BlockStatement;
364function BreakStatement([stm, s1, semi]) {
365 return { tag: "BreakStatement" };
366}
367exports.BreakStatement = BreakStatement;
368function ContinueStatement([stm, s1, semi]) {
369 return { tag: "ContinueStatement" };
370}
371exports.ContinueStatement = ContinueStatement;
372function Anno1(annos) {
373 const start = annos[0];
374 const end = annos[6] ? annos[6] : annos[4];
375 if (start.type === "anno_line_start" && start.line !== end.line)
376 throw new Error(`Single-line annotations cannot be extended onto multiple lines with multiline comments.`);
377 return annos[2];
378}
379exports.Anno1 = Anno1;
380function FunctionDeclarationArgs([l, s1, params, r]) {
381 if (params === null)
382 return [];
383 const first = {
384 tag: "VariableDeclaration",
385 kind: params[0],
386 id: params[2]
387 };
388 return [first].concat(params[4].map(x => ({
389 tag: "VariableDeclaration",
390 kind: x[2],
391 id: x[4]
392 })));
393}
394exports.FunctionDeclarationArgs = FunctionDeclarationArgs;
395function FunctionDeclaration([ty, s1, f, s2, args, annos, s3, def]) {
396 return {
397 tag: "FunctionDeclaration",
398 returns: ty,
399 id: f,
400 params: args,
401 annos: annos,
402 body: def
403 };
404}
405exports.FunctionDeclaration = FunctionDeclaration;
406//# sourceMappingURL=parser-util.js.map
\No newline at end of file