comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/Parser.java @ 7292:213c1297a814

Simple Language: A simple dynamic programming language to demonstrate Truffle features
author Christian Wimmer <christian.wimmer@oracle.com>
date Fri, 21 Dec 2012 10:45:37 -0800
parents
children 31da1716950f
comparison
equal deleted inserted replaced
7291:a748e4d44694 7292:213c1297a814
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 package com.oracle.truffle.sl.parser;
27 import java.util.*;
28
29 import com.oracle.truffle.sl.*;
30 import com.oracle.truffle.sl.nodes.*;
31 import com.oracle.truffle.sl.types.*;
32
33 // Checkstyle: stop
34 public class Parser {
35 public static final int _EOF = 0;
36 public static final int _identifier = 1;
37 public static final int _stringLiteral = 2;
38 public static final int _numericLiteral = 3;
39 public static final int maxT = 25;
40
41 static final boolean T = true;
42 static final boolean x = false;
43 static final int minErrDist = 2;
44
45 public Token t; // last recognized token
46 public Token la; // lookahead token
47 int errDist = minErrDist;
48
49 public final Scanner scanner;
50 public final Errors errors;
51 private final NodeFactory factory;
52
53 public Parser(Scanner scanner, NodeFactory factory) {
54 this.scanner = scanner;
55 this.factory = factory;
56 errors = new Errors();
57 }
58
59 void SynErr(int n) {
60 if (errDist >= minErrDist)
61 errors.SynErr(la.line, la.col, n);
62 errDist = 0;
63 }
64
65 public void SemErr(String msg) {
66 if (errDist >= minErrDist)
67 errors.SemErr(t.line, t.col, msg);
68 errDist = 0;
69 }
70
71 void Get() {
72 for (;;) {
73 t = la;
74 la = scanner.Scan();
75 if (la.kind <= maxT) {
76 ++errDist;
77 break;
78 }
79
80 la = t;
81 }
82 }
83
84 void Expect(int n) {
85 if (la.kind == n)
86 Get();
87 else {
88 SynErr(n);
89 }
90 }
91
92 boolean StartOf(int s) {
93 return set[s][la.kind];
94 }
95
96 void ExpectWeak(int n, int follow) {
97 if (la.kind == n)
98 Get();
99 else {
100 SynErr(n);
101 while (!StartOf(follow))
102 Get();
103 }
104 }
105
106 boolean WeakSeparator(int n, int syFol, int repFol) {
107 int kind = la.kind;
108 if (kind == n) {
109 Get();
110 return true;
111 } else if (StartOf(repFol))
112 return false;
113 else {
114 SynErr(n);
115 while (!(set[syFol][kind] || set[repFol][kind] || set[0][kind])) {
116 Get();
117 kind = la.kind;
118 }
119 return StartOf(syFol);
120 }
121 }
122
123 void SimpleLanguage() {
124 Function();
125 while (la.kind == 4) {
126 Function();
127 }
128 }
129
130 void Function() {
131 Expect(4);
132 factory.startFunction();
133 Expect(1);
134 String name = t.val;
135 StatementNode body = Block();
136 factory.createFunction(body, name);
137 }
138
139 StatementNode Block() {
140 StatementNode result;
141 List<StatementNode> statements = new ArrayList<>();
142 Expect(5);
143 while (StartOf(1)) {
144 StatementNode statement = Statement();
145 statements.add(statement);
146 }
147 Expect(6);
148 result = factory.createBlock(statements);
149 return result;
150 }
151
152 StatementNode Statement() {
153 StatementNode result;
154 result = null;
155 if (la.kind == 7) {
156 result = WhileStatement();
157 } else if (la.kind == 1) {
158 result = AssignmentStatement();
159 } else if (la.kind == 12) {
160 result = OutputStatement();
161 } else if (la.kind == 13) {
162 result = ReturnStatement();
163 } else SynErr(26);
164 return result;
165 }
166
167 StatementNode WhileStatement() {
168 StatementNode result;
169 Expect(7);
170 Expect(8);
171 ConditionNode condition = Expression();
172 Expect(9);
173 StatementNode body = Block();
174 result = factory.createWhile(condition, body);
175 return result;
176 }
177
178 StatementNode AssignmentStatement() {
179 StatementNode result;
180 Expect(1);
181 String name = t.val;
182 Expect(10);
183 TypedNode rvalue = Expression();
184 Expect(11);
185 result = factory.createAssignment(name, rvalue);
186 return result;
187 }
188
189 StatementNode OutputStatement() {
190 StatementNode result;
191 List<TypedNode> expressions = new ArrayList<>();
192 Expect(12);
193 while (StartOf(2)) {
194 TypedNode value = Expression();
195 expressions.add(value);
196 }
197 Expect(11);
198 result = factory.createPrint(expressions);
199 return result;
200 }
201
202 StatementNode ReturnStatement() {
203 StatementNode result;
204 Expect(13);
205 TypedNode value = Expression();
206 Expect(11);
207 result = factory.createReturn(value);
208 return result;
209 }
210
211 TypedNode Expression() {
212 TypedNode result;
213 result = ValueExpression();
214 if (StartOf(3)) {
215 switch (la.kind) {
216 case 14: {
217 Get();
218 break;
219 }
220 case 15: {
221 Get();
222 break;
223 }
224 case 16: {
225 Get();
226 break;
227 }
228 case 17: {
229 Get();
230 break;
231 }
232 case 18: {
233 Get();
234 break;
235 }
236 case 19: {
237 Get();
238 break;
239 }
240 }
241 String op = t.val;
242 TypedNode right = ValueExpression();
243 result = factory.createBinary(op, result, right);
244 }
245 return result;
246 }
247
248 TypedNode ValueExpression() {
249 TypedNode result;
250 result = Term();
251 while (la.kind == 20 || la.kind == 21) {
252 if (la.kind == 20) {
253 Get();
254 } else {
255 Get();
256 }
257 String op = t.val;
258 TypedNode right = Term();
259 result = factory.createBinary(op, result, right);
260 }
261 return result;
262 }
263
264 TypedNode Term() {
265 TypedNode result;
266 result = Factor();
267 while (la.kind == 22 || la.kind == 23) {
268 if (la.kind == 22) {
269 Get();
270 } else {
271 Get();
272 }
273 String op = t.val;
274 TypedNode right = Factor();
275 result = factory.createBinary(op, result, right);
276 }
277 return result;
278 }
279
280 TypedNode Factor() {
281 TypedNode result;
282 result = null;
283 if (la.kind == 24) {
284 result = TimeRef();
285 } else if (la.kind == 1) {
286 result = VariableRef();
287 } else if (la.kind == 2) {
288 result = StringLiteral();
289 } else if (la.kind == 3) {
290 result = NumericLiteral();
291 } else if (la.kind == 8) {
292 Get();
293 result = Expression();
294 Expect(9);
295 } else SynErr(27);
296 return result;
297 }
298
299 TypedNode TimeRef() {
300 TypedNode result;
301 Expect(24);
302 result = factory.createTime();
303 return result;
304 }
305
306 TypedNode VariableRef() {
307 TypedNode result;
308 Expect(1);
309 result = factory.createLocal(t.val);
310 return result;
311 }
312
313 TypedNode StringLiteral() {
314 TypedNode result;
315 Expect(2);
316 result = factory.createStringLiteral(t.val.substring(1, t.val.length() - 1));
317 return result;
318 }
319
320 TypedNode NumericLiteral() {
321 TypedNode result;
322 Expect(3);
323 result = factory.createNumericLiteral(t.val);
324 return result;
325 }
326
327
328
329 public void Parse() {
330 la = new Token();
331 la.val = "";
332 Get();
333 SimpleLanguage();
334 Expect(0);
335
336 }
337
338 private static final boolean[][] set = {
339 {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},
340 {x,T,x,x, x,x,x,T, x,x,x,x, T,T,x,x, x,x,x,x, x,x,x,x, x,x,x},
341 {x,T,T,T, x,x,x,x, T,x,x,x, x,x,x,x, x,x,x,x, x,x,x,x, T,x,x},
342 {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}
343
344 };
345
346 public String ParseErrors() {
347 java.io.PrintStream oldStream = System.out;
348
349 java.io.OutputStream out = new java.io.ByteArrayOutputStream();
350 java.io.PrintStream newStream = new java.io.PrintStream(out);
351
352 errors.errorStream = newStream;
353
354 Parse();
355
356 String errorStream = out.toString();
357 errors.errorStream = oldStream;
358
359 return errorStream;
360
361 }
362 } // end Parser
363
364 class Errors {
365
366 public int count = 0; // number of errors detected
367 public java.io.PrintStream errorStream = System.out; // error messages go to this stream
368 public String errMsgFormat = "-- line {0} col {1}: {2}"; // 0=line, 1=column, 2=text
369
370 protected void printMsg(int line, int column, String msg) {
371 StringBuffer b = new StringBuffer(errMsgFormat);
372 int pos = b.indexOf("{0}");
373 if (pos >= 0) {
374 b.delete(pos, pos + 3);
375 b.insert(pos, line);
376 }
377 pos = b.indexOf("{1}");
378 if (pos >= 0) {
379 b.delete(pos, pos + 3);
380 b.insert(pos, column);
381 }
382 pos = b.indexOf("{2}");
383 if (pos >= 0)
384 b.replace(pos, pos + 3, msg);
385 errorStream.println(b.toString());
386 }
387
388 public void SynErr(int line, int col, int n) {
389 String s;
390 switch (n) {
391 case 0: s = "EOF expected"; break;
392 case 1: s = "identifier expected"; break;
393 case 2: s = "stringLiteral expected"; break;
394 case 3: s = "numericLiteral expected"; break;
395 case 4: s = "\"function\" expected"; break;
396 case 5: s = "\"{\" expected"; break;
397 case 6: s = "\"}\" expected"; break;
398 case 7: s = "\"while\" expected"; break;
399 case 8: s = "\"(\" expected"; break;
400 case 9: s = "\")\" expected"; break;
401 case 10: s = "\"=\" expected"; break;
402 case 11: s = "\";\" expected"; break;
403 case 12: s = "\"print\" expected"; break;
404 case 13: s = "\"return\" expected"; break;
405 case 14: s = "\"<\" expected"; break;
406 case 15: s = "\">\" expected"; break;
407 case 16: s = "\"<=\" expected"; break;
408 case 17: s = "\">=\" expected"; break;
409 case 18: s = "\"==\" expected"; break;
410 case 19: s = "\"!=\" expected"; break;
411 case 20: s = "\"+\" expected"; break;
412 case 21: s = "\"-\" expected"; break;
413 case 22: s = "\"*\" expected"; break;
414 case 23: s = "\"/\" expected"; break;
415 case 24: s = "\"time\" expected"; break;
416 case 25: s = "??? expected"; break;
417 case 26: s = "invalid Statement"; break;
418 case 27: s = "invalid Factor"; break;
419 default:
420 s = "error " + n;
421 break;
422 }
423 printMsg(line, col, s);
424 count++;
425 }
426
427 public void SemErr(int line, int col, String s) {
428 printMsg(line, col, s);
429 count++;
430 }
431
432 public void SemErr(String s) {
433 errorStream.println(s);
434 count++;
435 }
436
437 public void Warning(int line, int col, String s) {
438 printMsg(line, col, s);
439 }
440
441 public void Warning(String s) {
442 errorStream.println(s);
443 }
444 } // Errors
445
446 class FatalError extends RuntimeException {
447
448 public static final long serialVersionUID = 1L;
449
450 public FatalError(String s) {
451 super(s);
452 }
453 }