Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/Parser.java.old @ 12752:71991b7a0f14
SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Mon, 11 Nov 2013 21:34:44 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
12712:882a0aadfed6 | 12752:71991b7a0f14 |
---|---|
1 /* | |
2 * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 | |
24 // The content of this file is automatically generated. DO NOT EDIT. | |
25 | |
26 | |
27 package com.oracle.truffle.sl.parser; | |
28 | |
29 import java.util.*; | |
30 | |
31 import com.oracle.truffle.sl.*; | |
32 import com.oracle.truffle.sl.nodes.*; | |
33 | |
34 // Checkstyle: stop | |
35 // @formatter:off | |
36 public class Parser { | |
37 public static final int _EOF = 0; | |
38 public static final int _identifier = 1; | |
39 public static final int _stringLiteral = 2; | |
40 public static final int _numericLiteral = 3; | |
41 public static final int maxT = 29; | |
42 | |
43 static final boolean T = true; | |
44 static final boolean x = false; | |
45 static final int minErrDist = 2; | |
46 | |
47 public Token t; // last recognized token | |
48 public Token la; // lookahead token | |
49 int errDist = minErrDist; | |
50 | |
51 public final Scanner scanner; | |
52 public final Errors errors; | |
53 private final SLNodeFactory factory; | |
54 | |
55 public Parser(Scanner scanner, SLNodeFactory factory) { | |
56 this.scanner = scanner; | |
57 this.factory = factory; | |
58 errors = new Errors(); | |
59 } | |
60 | |
61 void SynErr(int n) { | |
62 if (errDist >= minErrDist) | |
63 errors.SynErr(la.line, la.col, n); | |
64 errDist = 0; | |
65 } | |
66 | |
67 public void SemErr(String msg) { | |
68 if (errDist >= minErrDist) | |
69 errors.SemErr(t.line, t.col, msg); | |
70 errDist = 0; | |
71 } | |
72 | |
73 void Get() { | |
74 for (;;) { | |
75 t = la; | |
76 la = scanner.Scan(); | |
77 if (la.kind <= maxT) { | |
78 ++errDist; | |
79 break; | |
80 } | |
81 | |
82 la = t; | |
83 } | |
84 } | |
85 | |
86 void Expect(int n) { | |
87 if (la.kind == n) | |
88 Get(); | |
89 else { | |
90 SynErr(n); | |
91 } | |
92 } | |
93 | |
94 boolean StartOf(int s) { | |
95 return set[s][la.kind]; | |
96 } | |
97 | |
98 void ExpectWeak(int n, int follow) { | |
99 if (la.kind == n) | |
100 Get(); | |
101 else { | |
102 SynErr(n); | |
103 while (!StartOf(follow)) | |
104 Get(); | |
105 } | |
106 } | |
107 | |
108 boolean WeakSeparator(int n, int syFol, int repFol) { | |
109 int kind = la.kind; | |
110 if (kind == n) { | |
111 Get(); | |
112 return true; | |
113 } else if (StartOf(repFol)) | |
114 return false; | |
115 else { | |
116 SynErr(n); | |
117 while (!(set[syFol][kind] || set[repFol][kind] || set[0][kind])) { | |
118 Get(); | |
119 kind = la.kind; | |
120 } | |
121 return StartOf(syFol); | |
122 } | |
123 } | |
124 | |
125 void SimpleLanguage() { | |
126 Function(); | |
127 while (la.kind == 4) { | |
128 Function(); | |
129 } | |
130 } | |
131 | |
132 void Function() { | |
133 Expect(4); | |
134 factory.startFunction(); | |
135 Expect(1); | |
136 String name = t.val; | |
137 List<String> parameterNames = new ArrayList<>(); | |
138 if (la.kind == 5) { | |
139 Get(); | |
140 if (la.kind == 1) { | |
141 Get(); | |
142 parameterNames.add(t.val); | |
143 } | |
144 while (la.kind == 6) { | |
145 Get(); | |
146 Expect(1); | |
147 parameterNames.add(t.val); | |
148 } | |
149 Expect(7); | |
150 } | |
151 StatementNode body = Block(); | |
152 factory.createFunction(body, name, parameterNames.toArray(new String[parameterNames.size()])); | |
153 } | |
154 | |
155 StatementNode Block() { | |
156 StatementNode result; | |
157 List<StatementNode> statements = new ArrayList<>(); | |
158 Expect(8); | |
159 while (StartOf(1)) { | |
160 StatementNode statement = Statement(); | |
161 statements.add(statement); | |
162 } | |
163 Expect(9); | |
164 result = factory.createBlock(statements); | |
165 return result; | |
166 } | |
167 | |
168 StatementNode Statement() { | |
169 StatementNode result; | |
170 result = null; | |
171 if (la.kind == 13) { | |
172 result = WhileStatement(); | |
173 } else if (la.kind == 11) { | |
174 result = IfStatement(); | |
175 } else if (la.kind == 14) { | |
176 result = ReturnStatement(); | |
177 } else if (StartOf(2)) { | |
178 result = Expression(); | |
179 Expect(10); | |
180 } else SynErr(30); | |
181 return result; | |
182 } | |
183 | |
184 StatementNode WhileStatement() { | |
185 StatementNode result; | |
186 Expect(13); | |
187 Expect(5); | |
188 ConditionNode condition = Expression(); | |
189 Expect(7); | |
190 StatementNode body = Block(); | |
191 result = factory.createWhile(condition, body); | |
192 return result; | |
193 } | |
194 | |
195 StatementNode IfStatement() { | |
196 StatementNode result; | |
197 Expect(11); | |
198 Expect(5); | |
199 ConditionNode condition = Expression(); | |
200 Expect(7); | |
201 StatementNode thenNode = null; StatementNode elseNode = null; | |
202 thenNode = Block(); | |
203 if (la.kind == 12) { | |
204 Get(); | |
205 elseNode = Block(); | |
206 } | |
207 result = factory.createIf(condition, thenNode, elseNode); | |
208 return result; | |
209 } | |
210 | |
211 StatementNode ReturnStatement() { | |
212 StatementNode result; | |
213 Expect(14); | |
214 TypedNode value = Expression(); | |
215 Expect(10); | |
216 result = factory.createReturn(value); | |
217 return result; | |
218 } | |
219 | |
220 TypedNode Expression() { | |
221 TypedNode result; | |
222 result = ValueExpression(); | |
223 if (StartOf(3)) { | |
224 switch (la.kind) { | |
225 case 15: { | |
226 Get(); | |
227 break; | |
228 } | |
229 case 16: { | |
230 Get(); | |
231 break; | |
232 } | |
233 case 17: { | |
234 Get(); | |
235 break; | |
236 } | |
237 case 18: { | |
238 Get(); | |
239 break; | |
240 } | |
241 case 19: { | |
242 Get(); | |
243 break; | |
244 } | |
245 case 20: { | |
246 Get(); | |
247 break; | |
248 } | |
249 } | |
250 String op = t.val; | |
251 TypedNode right = ValueExpression(); | |
252 result = factory.createBinary(op, result, right); | |
253 } | |
254 return result; | |
255 } | |
256 | |
257 TypedNode ValueExpression() { | |
258 TypedNode result; | |
259 result = Term(); | |
260 while (la.kind == 21 || la.kind == 22) { | |
261 if (la.kind == 21) { | |
262 Get(); | |
263 } else { | |
264 Get(); | |
265 } | |
266 String op = t.val; | |
267 TypedNode right = Term(); | |
268 result = factory.createBinary(op, result, right); | |
269 } | |
270 return result; | |
271 } | |
272 | |
273 TypedNode Term() { | |
274 TypedNode result; | |
275 result = Factor(); | |
276 while (la.kind == 23 || la.kind == 24) { | |
277 if (la.kind == 23) { | |
278 Get(); | |
279 } else { | |
280 Get(); | |
281 } | |
282 String op = t.val; | |
283 TypedNode right = Factor(); | |
284 result = factory.createBinary(op, result, right); | |
285 } | |
286 return result; | |
287 } | |
288 | |
289 TypedNode Factor() { | |
290 TypedNode result; | |
291 result = null; | |
292 if (la.kind == 1) { | |
293 result = VariableRefOrCall(); | |
294 } else if (la.kind == 2) { | |
295 result = StringLiteral(); | |
296 } else if (la.kind == 3) { | |
297 result = NumericLiteral(); | |
298 } else if (la.kind == 25) { | |
299 result = Ternary(); | |
300 } else if (la.kind == 5) { | |
301 Get(); | |
302 result = Expression(); | |
303 Expect(7); | |
304 } else SynErr(31); | |
305 return result; | |
306 } | |
307 | |
308 TypedNode VariableRefOrCall() { | |
309 TypedNode result; | |
310 result = VariableRef(); | |
311 if (la.kind == 5 || la.kind == 28) { | |
312 if (la.kind == 5) { | |
313 TypedNode[] parameters = Parameters(); | |
314 result = factory.createCall(result, parameters); | |
315 } else { | |
316 Get(); | |
317 TypedNode assignment = Expression(); | |
318 result = factory.createAssignment(result, assignment); | |
319 } | |
320 } | |
321 return result; | |
322 } | |
323 | |
324 TypedNode StringLiteral() { | |
325 TypedNode result; | |
326 Expect(2); | |
327 result = factory.createStringLiteral(t.val.substring(1, t.val.length() - 1)); | |
328 return result; | |
329 } | |
330 | |
331 TypedNode NumericLiteral() { | |
332 TypedNode result; | |
333 Expect(3); | |
334 result = factory.createNumericLiteral(t.val); | |
335 return result; | |
336 } | |
337 | |
338 TypedNode Ternary() { | |
339 TypedNode result; | |
340 TypedNode condition, thenPart, elsePart; | |
341 Expect(25); | |
342 condition = Expression(); | |
343 Expect(26); | |
344 thenPart = Expression(); | |
345 Expect(27); | |
346 elsePart = Expression(); | |
347 result = factory.createTernary(condition, thenPart, elsePart); | |
348 return result; | |
349 } | |
350 | |
351 TypedNode VariableRef() { | |
352 TypedNode result; | |
353 Expect(1); | |
354 result = factory.createLocal(t.val); | |
355 return result; | |
356 } | |
357 | |
358 TypedNode[] Parameters() { | |
359 TypedNode[] result; | |
360 Expect(5); | |
361 List<TypedNode> parameters = new ArrayList<>(); | |
362 if (StartOf(2)) { | |
363 TypedNode e1 = Expression(); | |
364 parameters.add(e1); | |
365 while (la.kind == 6) { | |
366 Get(); | |
367 TypedNode e2 = Expression(); | |
368 parameters.add(e2); | |
369 } | |
370 } | |
371 result = parameters.toArray(new TypedNode[parameters.size()]); | |
372 Expect(7); | |
373 return result; | |
374 } | |
375 | |
376 | |
377 | |
378 public void Parse() { | |
379 la = new Token(); | |
380 la.val = ""; | |
381 Get(); | |
382 SimpleLanguage(); | |
383 Expect(0); | |
384 | |
385 } | |
386 | |
387 private static final boolean[][] set = { | |
388 {T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x}, | |
389 {x,T,T,T, x,T,x,x, x,x,x,T, x,T,T,x, x,x,x,x, x,x,x,x, x,T,x,x, x,x,x}, | |
390 {x,T,T,T, x,T,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, x,T,x,x, x,x,x}, | |
391 {x,x,x,x, x,x,x,x, x,x,x,x, x,x,x,T, T,T,T,T, T,x,x,x, x,x,x,x, x,x,x} | |
392 | |
393 }; | |
394 | |
395 public String ParseErrors() { | |
396 java.io.PrintStream oldStream = System.out; | |
397 | |
398 java.io.OutputStream out = new java.io.ByteArrayOutputStream(); | |
399 java.io.PrintStream newStream = new java.io.PrintStream(out); | |
400 | |
401 errors.errorStream = newStream; | |
402 | |
403 Parse(); | |
404 | |
405 String errorStream = out.toString(); | |
406 errors.errorStream = oldStream; | |
407 | |
408 return errorStream; | |
409 | |
410 } | |
411 } // end Parser | |
412 | |
413 class Errors { | |
414 | |
415 public int count = 0; // number of errors detected | |
416 public java.io.PrintStream errorStream = System.out; // error messages go to this stream | |
417 public String errMsgFormat = "-- line {0} col {1}: {2}"; // 0=line, 1=column, 2=text | |
418 | |
419 protected void printMsg(int line, int column, String msg) { | |
420 StringBuffer b = new StringBuffer(errMsgFormat); | |
421 int pos = b.indexOf("{0}"); | |
422 if (pos >= 0) { | |
423 b.delete(pos, pos + 3); | |
424 b.insert(pos, line); | |
425 } | |
426 pos = b.indexOf("{1}"); | |
427 if (pos >= 0) { | |
428 b.delete(pos, pos + 3); | |
429 b.insert(pos, column); | |
430 } | |
431 pos = b.indexOf("{2}"); | |
432 if (pos >= 0) | |
433 b.replace(pos, pos + 3, msg); | |
434 errorStream.println(b.toString()); | |
435 } | |
436 | |
437 public void SynErr(int line, int col, int n) { | |
438 String s; | |
439 switch (n) { | |
440 case 0: s = "EOF expected"; break; | |
441 case 1: s = "identifier expected"; break; | |
442 case 2: s = "stringLiteral expected"; break; | |
443 case 3: s = "numericLiteral expected"; break; | |
444 case 4: s = "\"function\" expected"; break; | |
445 case 5: s = "\"(\" expected"; break; | |
446 case 6: s = "\",\" expected"; break; | |
447 case 7: s = "\")\" expected"; break; | |
448 case 8: s = "\"{\" expected"; break; | |
449 case 9: s = "\"}\" expected"; break; | |
450 case 10: s = "\";\" expected"; break; | |
451 case 11: s = "\"if\" expected"; break; | |
452 case 12: s = "\"else\" expected"; break; | |
453 case 13: s = "\"while\" expected"; break; | |
454 case 14: s = "\"return\" expected"; break; | |
455 case 15: s = "\"<\" expected"; break; | |
456 case 16: s = "\">\" expected"; break; | |
457 case 17: s = "\"<=\" expected"; break; | |
458 case 18: s = "\">=\" expected"; break; | |
459 case 19: s = "\"==\" expected"; break; | |
460 case 20: s = "\"!=\" expected"; break; | |
461 case 21: s = "\"+\" expected"; break; | |
462 case 22: s = "\"-\" expected"; break; | |
463 case 23: s = "\"*\" expected"; break; | |
464 case 24: s = "\"/\" expected"; break; | |
465 case 25: s = "\"#\" expected"; break; | |
466 case 26: s = "\"?\" expected"; break; | |
467 case 27: s = "\":\" expected"; break; | |
468 case 28: s = "\"=\" expected"; break; | |
469 case 29: s = "??? expected"; break; | |
470 case 30: s = "invalid Statement"; break; | |
471 case 31: s = "invalid Factor"; break; | |
472 default: | |
473 s = "error " + n; | |
474 break; | |
475 } | |
476 printMsg(line, col, s); | |
477 count++; | |
478 } | |
479 | |
480 public void SemErr(int line, int col, String s) { | |
481 printMsg(line, col, s); | |
482 count++; | |
483 } | |
484 | |
485 public void SemErr(String s) { | |
486 errorStream.println(s); | |
487 count++; | |
488 } | |
489 | |
490 public void Warning(int line, int col, String s) { | |
491 printMsg(line, col, s); | |
492 } | |
493 | |
494 public void Warning(String s) { | |
495 errorStream.println(s); | |
496 } | |
497 } // Errors | |
498 | |
499 class FatalError extends RuntimeException { | |
500 | |
501 public static final long serialVersionUID = 1L; | |
502 | |
503 public FatalError(String s) { | |
504 super(s); | |
505 } | |
506 } |