Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/expression/Parser.java @ 19283:08aa0372dad4
Truffle-DSL: implement new guard expression syntax.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Fri, 23 Jan 2015 02:55:23 +0100 |
parents | |
children | 62c43fcf5be2 |
comparison
equal
deleted
inserted
replaced
19282:ae81dd154fb6 | 19283:08aa0372dad4 |
---|---|
1 /* | |
2 * Copyright (c) 2015, 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.dsl.processor.expression; | |
27 | |
28 import java.util.*; | |
29 import java.io.*; | |
30 import java.nio.charset.*; | |
31 | |
32 import com.oracle.truffle.dsl.processor.expression.DSLExpression.*; | |
33 | |
34 // Checkstyle: stop | |
35 // @formatter:off | |
36 class Parser { | |
37 public static final int _EOF = 0; | |
38 public static final int _identifier = 1; | |
39 public static final int _numericLiteral = 2; | |
40 public static final int maxT = 14; | |
41 | |
42 static final boolean _T = true; | |
43 static final boolean _x = false; | |
44 static final int minErrDist = 2; | |
45 | |
46 public Token t; // last recognized token | |
47 public Token la; // lookahead token | |
48 int errDist = minErrDist; | |
49 | |
50 public final Scanner scanner; | |
51 public final Errors errors; | |
52 | |
53 | |
54 public Parser(InputStream input) { | |
55 this.scanner = new Scanner(input); | |
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 DSLExpression Expression() { | |
124 DSLExpression result; | |
125 result = NegateFactor(); | |
126 return result; | |
127 } | |
128 | |
129 DSLExpression NegateFactor() { | |
130 DSLExpression result; | |
131 boolean negated = false; | |
132 if (la.kind == 3) { | |
133 Get(); | |
134 negated = true; | |
135 } | |
136 result = LogicFactor(); | |
137 result = negated ? new Negate(result) : result; | |
138 return result; | |
139 } | |
140 | |
141 DSLExpression LogicFactor() { | |
142 DSLExpression result; | |
143 result = Factor(); | |
144 if (StartOf(1)) { | |
145 switch (la.kind) { | |
146 case 4: { | |
147 Get(); | |
148 break; | |
149 } | |
150 case 5: { | |
151 Get(); | |
152 break; | |
153 } | |
154 case 6: { | |
155 Get(); | |
156 break; | |
157 } | |
158 case 7: { | |
159 Get(); | |
160 break; | |
161 } | |
162 case 8: { | |
163 Get(); | |
164 break; | |
165 } | |
166 case 9: { | |
167 Get(); | |
168 break; | |
169 } | |
170 } | |
171 Token op = t; | |
172 DSLExpression right = Factor(); | |
173 result = new Binary(op.val, result, right); | |
174 } | |
175 return result; | |
176 } | |
177 | |
178 DSLExpression Factor() { | |
179 DSLExpression result; | |
180 result = null; | |
181 if (la.kind == 1) { | |
182 result = MemberExpression(result); | |
183 } else if (la.kind == 2) { | |
184 Get(); | |
185 result = new IntLiteral(t.val); | |
186 } else if (la.kind == 10) { | |
187 Get(); | |
188 result = Expression(); | |
189 Expect(11); | |
190 } else SynErr(15); | |
191 return result; | |
192 } | |
193 | |
194 DSLExpression MemberExpression(DSLExpression receiver) { | |
195 DSLExpression result; | |
196 result = null; | |
197 Expect(1); | |
198 Variable variable = new Variable(receiver, t.val); | |
199 result = variable; | |
200 if (la.kind == 10) { | |
201 Get(); | |
202 List<DSLExpression> parameters = new ArrayList<>(); | |
203 DSLExpression parameter; | |
204 if (StartOf(2)) { | |
205 parameter = Expression(); | |
206 parameters.add(parameter); | |
207 while (la.kind == 12) { | |
208 Get(); | |
209 parameter = Expression(); | |
210 parameters.add(parameter); | |
211 } | |
212 } | |
213 Expect(11); | |
214 result = new Call(variable.getReceiver(), variable.getName(), parameters); | |
215 } | |
216 if (la.kind == 13) { | |
217 Get(); | |
218 result = MemberExpression(result); | |
219 } | |
220 return result; | |
221 } | |
222 | |
223 | |
224 | |
225 private DSLExpression parseImpl() { | |
226 la = new Token(); | |
227 la.val = ""; | |
228 Get(); | |
229 DSLExpression result = Expression(); | |
230 Expect(0); | |
231 | |
232 return result; | |
233 } | |
234 | |
235 private static final boolean[][] set = { | |
236 {_T,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x}, | |
237 {_x,_x,_x,_x, _T,_T,_T,_T, _T,_T,_x,_x, _x,_x,_x,_x}, | |
238 {_x,_T,_T,_T, _x,_x,_x,_x, _x,_x,_T,_x, _x,_x,_x,_x} | |
239 | |
240 }; | |
241 | |
242 public static DSLExpression parse(InputStream input) { | |
243 Parser parser = new Parser(input); | |
244 DSLExpression result = parser.parseImpl(); | |
245 if (parser.errors.errors.size() > 0) { | |
246 StringBuilder msg = new StringBuilder(); | |
247 for (String error : parser.errors.errors) { | |
248 msg.append(error).append("\n"); | |
249 } | |
250 throw new InvalidExpressionException(msg.toString()); | |
251 } | |
252 return result; | |
253 } | |
254 | |
255 public static DSLExpression parse(String s) { | |
256 return parse(new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8))); | |
257 } | |
258 } // end Parser | |
259 | |
260 class Errors { | |
261 | |
262 protected final List<String> errors = new ArrayList<>(); | |
263 public String errMsgFormat = "-- line {0} col {1}: {2}"; // 0=line, 1=column, 2=text | |
264 | |
265 protected void printMsg(int line, int column, String msg) { | |
266 StringBuffer b = new StringBuffer(errMsgFormat); | |
267 int pos = b.indexOf("{0}"); | |
268 if (pos >= 0) { | |
269 b.delete(pos, pos + 3); | |
270 b.insert(pos, line); | |
271 } | |
272 pos = b.indexOf("{1}"); | |
273 if (pos >= 0) { | |
274 b.delete(pos, pos + 3); | |
275 b.insert(pos, column); | |
276 } | |
277 pos = b.indexOf("{2}"); | |
278 if (pos >= 0) | |
279 b.replace(pos, pos + 3, msg); | |
280 errors.add(b.toString()); | |
281 } | |
282 | |
283 public void SynErr(int line, int col, int n) { | |
284 String s; | |
285 switch (n) { | |
286 case 0: s = "EOF expected"; break; | |
287 case 1: s = "identifier expected"; break; | |
288 case 2: s = "numericLiteral expected"; break; | |
289 case 3: s = "\"!\" expected"; break; | |
290 case 4: s = "\"<\" expected"; break; | |
291 case 5: s = "\"<=\" expected"; break; | |
292 case 6: s = "\">\" expected"; break; | |
293 case 7: s = "\">=\" expected"; break; | |
294 case 8: s = "\"==\" expected"; break; | |
295 case 9: s = "\"!=\" expected"; break; | |
296 case 10: s = "\"(\" expected"; break; | |
297 case 11: s = "\")\" expected"; break; | |
298 case 12: s = "\",\" expected"; break; | |
299 case 13: s = "\".\" expected"; break; | |
300 case 14: s = "??? expected"; break; | |
301 case 15: s = "invalid Factor"; break; | |
302 default: | |
303 s = "error " + n; | |
304 break; | |
305 } | |
306 printMsg(line, col, s); | |
307 } | |
308 | |
309 public void SemErr(int line, int col, String s) { | |
310 printMsg(line, col, s); | |
311 } | |
312 | |
313 public void SemErr(String s) { | |
314 errors.add(s); | |
315 } | |
316 | |
317 public void Warning(int line, int col, String s) { | |
318 printMsg(line, col, s); | |
319 } | |
320 | |
321 public void Warning(String s) { | |
322 errors.add(s); | |
323 } | |
324 } // Errors | |
325 | |
326 class FatalError extends RuntimeException { | |
327 | |
328 public static final long serialVersionUID = 1L; | |
329 | |
330 public FatalError(String s) { | |
331 super(s); | |
332 } | |
333 } |