Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/Parser.java @ 16675:0fc43b066eee
SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
author | Michael Van De Vanter <michael.van.de.vanter@oracle.com> |
---|---|
date | Fri, 01 Aug 2014 16:30:22 -0700 |
parents | 618d92152d3c |
children | d654cd5ed05a |
comparison
equal
deleted
inserted
replaced
16674:70f47dbbcabd | 16675:0fc43b066eee |
---|---|
51 int errDist = minErrDist; | 51 int errDist = minErrDist; |
52 | 52 |
53 public final Scanner scanner; | 53 public final Scanner scanner; |
54 public final Errors errors; | 54 public final Errors errors; |
55 private final SLNodeFactory factory; | 55 private final SLNodeFactory factory; |
56 | 56 |
57 public Parser(SLContext context, Source source, SLNodeProber astProber) { | 57 public Parser(SLContext context, Source source, SLNodeProber astProber) { |
58 this.scanner = new Scanner(source.getInputStream()); | 58 this.scanner = new Scanner(source.getInputStream()); |
59 this.factory = new SLNodeFactory(context, source, astProber); | 59 this.factory = new SLNodeFactory(context, source, astProber); |
60 errors = new Errors(); | 60 errors = new Errors(); |
61 } | 61 } |
132 } | 132 } |
133 | 133 |
134 void Function() { | 134 void Function() { |
135 Expect(4); | 135 Expect(4); |
136 Expect(1); | 136 Expect(1); |
137 factory.startFunction(t); | 137 factory.startFunction(t); |
138 Expect(5); | 138 Expect(5); |
139 if (la.kind == 1) { | 139 if (la.kind == 1) { |
140 Get(); | 140 Get(); |
141 factory.addFormalParameter(t); | 141 factory.addFormalParameter(t); |
142 while (la.kind == 6) { | 142 while (la.kind == 6) { |
143 Get(); | 143 Get(); |
144 Expect(1); | 144 Expect(1); |
145 factory.addFormalParameter(t); | 145 factory.addFormalParameter(t); |
146 } | 146 } |
147 } | 147 } |
148 Expect(7); | 148 Expect(7); |
149 SLStatementNode body = Block(false); | 149 SLStatementNode body = Block(false); |
150 factory.finishFunction(body); | 150 factory.finishFunction(body); |
151 } | 151 } |
152 | 152 |
153 SLStatementNode Block(boolean inLoop) { | 153 SLStatementNode Block(boolean inLoop) { |
154 SLStatementNode result; | 154 SLStatementNode result; |
155 factory.startBlock(); | 155 factory.startBlock(); |
156 List<SLStatementNode> body = new ArrayList<>(); | 156 List<SLStatementNode> body = new ArrayList<>(); |
157 Expect(8); | 157 Expect(8); |
158 int lBracePos = t.charPos; | 158 int start = t.charPos; |
159 while (StartOf(1)) { | 159 while (StartOf(1)) { |
160 SLStatementNode s = Statement(inLoop); | 160 SLStatementNode s = Statement(inLoop); |
161 body.add(s); | 161 body.add(s); |
162 } | 162 } |
163 Expect(9); | 163 Expect(9); |
164 int length = (t.charPos + t.val.length()) - lBracePos; | 164 int length = (t.charPos + t.val.length()) - start; |
165 result = factory.finishBlock(body, lBracePos, length); | 165 result = factory.finishBlock(body, start, length); |
166 return result; | 166 return result; |
167 } | 167 } |
168 | 168 |
169 SLStatementNode Statement(boolean inLoop) { | 169 SLStatementNode Statement(boolean inLoop) { |
170 SLStatementNode result; | 170 SLStatementNode result; |
171 result = null; | 171 result = null; |
172 switch (la.kind) { | 172 switch (la.kind) { |
173 case 13: { | 173 case 13: { |
174 result = WhileStatement(); | 174 result = WhileStatement(); |
175 break; | 175 break; |
176 } | 176 } |
177 case 10: { | 177 case 10: { |
178 Get(); | 178 Get(); |
179 if (inLoop) { result = factory.createBreak(t); } else { SemErr("break used outside of loop"); } | 179 if (inLoop) { result = factory.createBreak(t); } else { SemErr("break used outside of loop"); } |
180 Expect(11); | 180 Expect(11); |
181 break; | 181 break; |
182 } | 182 } |
183 case 12: { | 183 case 12: { |
184 Get(); | 184 Get(); |
185 if (inLoop) { result = factory.createContinue(t); } else { SemErr("continue used outside of loop"); } | 185 if (inLoop) { result = factory.createContinue(t); } else { SemErr("continue used outside of loop"); } |
186 Expect(11); | 186 Expect(11); |
187 break; | 187 break; |
188 } | 188 } |
189 case 14: { | 189 case 14: { |
190 result = IfStatement(inLoop); | 190 result = IfStatement(inLoop); |
205 } | 205 } |
206 | 206 |
207 SLStatementNode WhileStatement() { | 207 SLStatementNode WhileStatement() { |
208 SLStatementNode result; | 208 SLStatementNode result; |
209 Expect(13); | 209 Expect(13); |
210 Token whileToken = t; | |
210 Expect(5); | 211 Expect(5); |
211 Token whileToken = t; | |
212 SLExpressionNode condition = Expression(); | 212 SLExpressionNode condition = Expression(); |
213 Expect(7); | 213 Expect(7); |
214 SLStatementNode body = Block(true); | 214 SLStatementNode body = Block(true); |
215 result = factory.createWhile(whileToken, condition, body); | 215 result = factory.createWhile(whileToken, condition, body); |
216 return result; | 216 return result; |
217 } | 217 } |
218 | 218 |
219 SLStatementNode IfStatement(boolean inLoop) { | 219 SLStatementNode IfStatement(boolean inLoop) { |
220 SLStatementNode result; | 220 SLStatementNode result; |
221 Expect(14); | 221 Expect(14); |
222 Expect(5); | 222 Expect(5); |
223 Token ifToken = t; | 223 Token ifToken = t; |
224 SLExpressionNode condition = Expression(); | 224 SLExpressionNode condition = Expression(); |
225 Expect(7); | 225 Expect(7); |
226 SLStatementNode thenPart = Block(inLoop); | 226 SLStatementNode thenPart = Block(inLoop); |
227 SLStatementNode elsePart = null; | 227 SLStatementNode elsePart = null; |
228 if (la.kind == 15) { | 228 if (la.kind == 15) { |
229 Get(); | 229 Get(); |
230 elsePart = Block(inLoop); | 230 elsePart = Block(inLoop); |
231 } | 231 } |
232 result = factory.createIf(ifToken, condition, thenPart, elsePart); | 232 result = factory.createIf(ifToken, condition, thenPart, elsePart); |
233 return result; | 233 return result; |
234 } | 234 } |
235 | 235 |
236 SLStatementNode ReturnStatement() { | 236 SLStatementNode ReturnStatement() { |
237 SLStatementNode result; | 237 SLStatementNode result; |
238 Expect(16); | 238 Expect(16); |
239 Token returnToken = t; | 239 Token returnToken = t; |
240 SLExpressionNode value = null; | 240 SLExpressionNode value = null; |
241 if (StartOf(2)) { | 241 if (StartOf(2)) { |
242 value = Expression(); | 242 value = Expression(); |
243 } | 243 } |
244 result = factory.createReturn(returnToken, value); | 244 result = factory.createReturn(returnToken, value); |
245 Expect(11); | 245 Expect(11); |
246 return result; | 246 return result; |
247 } | 247 } |
248 | 248 |
249 SLExpressionNode Expression() { | 249 SLExpressionNode Expression() { |
250 SLExpressionNode result; | 250 SLExpressionNode result; |
251 result = LogicTerm(); | 251 result = LogicTerm(); |
252 while (la.kind == 17) { | 252 while (la.kind == 17) { |
253 Get(); | 253 Get(); |
254 Token op = t; | 254 Token op = t; |
255 SLExpressionNode right = LogicTerm(); | 255 SLExpressionNode right = LogicTerm(); |
256 result = factory.createBinary(op, result, right); | 256 result = factory.createBinary(op, result, right); |
257 } | 257 } |
258 return result; | 258 return result; |
259 } | 259 } |
260 | 260 |
261 SLExpressionNode LogicTerm() { | 261 SLExpressionNode LogicTerm() { |
262 SLExpressionNode result; | 262 SLExpressionNode result; |
263 result = LogicFactor(); | 263 result = LogicFactor(); |
264 while (la.kind == 18) { | 264 while (la.kind == 18) { |
265 Get(); | 265 Get(); |
266 Token op = t; | 266 Token op = t; |
267 SLExpressionNode right = LogicFactor(); | 267 SLExpressionNode right = LogicFactor(); |
268 result = factory.createBinary(op, result, right); | 268 result = factory.createBinary(op, result, right); |
269 } | 269 } |
270 return result; | 270 return result; |
271 } | 271 } |
272 | 272 |
273 SLExpressionNode LogicFactor() { | 273 SLExpressionNode LogicFactor() { |
298 case 24: { | 298 case 24: { |
299 Get(); | 299 Get(); |
300 break; | 300 break; |
301 } | 301 } |
302 } | 302 } |
303 Token op = t; | 303 Token op = t; |
304 SLExpressionNode right = Arithmetic(); | 304 SLExpressionNode right = Arithmetic(); |
305 result = factory.createBinary(op, result, right); | 305 result = factory.createBinary(op, result, right); |
306 } | 306 } |
307 return result; | 307 return result; |
308 } | 308 } |
309 | 309 |
310 SLExpressionNode Arithmetic() { | 310 SLExpressionNode Arithmetic() { |
314 if (la.kind == 25) { | 314 if (la.kind == 25) { |
315 Get(); | 315 Get(); |
316 } else { | 316 } else { |
317 Get(); | 317 Get(); |
318 } | 318 } |
319 Token op = t; | 319 Token op = t; |
320 SLExpressionNode right = Term(); | 320 SLExpressionNode right = Term(); |
321 result = factory.createBinary(op, result, right); | 321 result = factory.createBinary(op, result, right); |
322 } | 322 } |
323 return result; | 323 return result; |
324 } | 324 } |
325 | 325 |
326 SLExpressionNode Term() { | 326 SLExpressionNode Term() { |
330 if (la.kind == 27) { | 330 if (la.kind == 27) { |
331 Get(); | 331 Get(); |
332 } else { | 332 } else { |
333 Get(); | 333 Get(); |
334 } | 334 } |
335 Token op = t; | 335 Token op = t; |
336 SLExpressionNode right = Factor(); | 336 SLExpressionNode right = Factor(); |
337 result = factory.createBinary(op, result, right); | 337 result = factory.createBinary(op, result, right); |
338 } | 338 } |
339 return result; | 339 return result; |
340 } | 340 } |
341 | 341 |
342 SLExpressionNode Factor() { | 342 SLExpressionNode Factor() { |
343 SLExpressionNode result; | 343 SLExpressionNode result; |
344 result = null; | 344 result = null; |
345 if (la.kind == 1) { | 345 if (la.kind == 1) { |
346 Get(); | 346 Get(); |
347 Token nameToken = t; | 347 Token nameToken = t; |
348 if (la.kind == 5) { | 348 if (la.kind == 5) { |
349 Get(); | 349 Get(); |
350 List<SLExpressionNode> parameters = new ArrayList<>(); | 350 List<SLExpressionNode> parameters = new ArrayList<>(); |
351 SLExpressionNode parameter; | 351 SLExpressionNode parameter; |
352 if (StartOf(2)) { | 352 if (StartOf(2)) { |
353 parameter = Expression(); | 353 parameter = Expression(); |
354 parameters.add(parameter); | 354 parameters.add(parameter); |
355 while (la.kind == 6) { | 355 while (la.kind == 6) { |
356 Get(); | 356 Get(); |
357 parameter = Expression(); | 357 parameter = Expression(); |
358 parameters.add(parameter); | 358 parameters.add(parameter); |
359 } | 359 } |
360 } | 360 } |
361 Expect(7); | 361 Expect(7); |
362 Token finalToken = t; | 362 Token finalToken = t; |
363 result = factory.createCall(nameToken, parameters, finalToken); | 363 result = factory.createCall(nameToken, parameters, finalToken); |
364 } else if (la.kind == 29) { | 364 } else if (la.kind == 29) { |
365 Get(); | 365 Get(); |
366 SLExpressionNode value = Expression(); | 366 SLExpressionNode value = Expression(); |
367 result = factory.createAssignment(nameToken, value); | 367 result = factory.createAssignment(nameToken, value); |
368 } else if (StartOf(4)) { | 368 } else if (StartOf(4)) { |
369 result = factory.createRead(nameToken); | 369 result = factory.createRead(nameToken); |
370 } else SynErr(32); | 370 } else SynErr(32); |
371 } else if (la.kind == 2) { | 371 } else if (la.kind == 2) { |
372 Get(); | 372 Get(); |
373 result = factory.createStringLiteral(t); | 373 result = factory.createStringLiteral(t); |
374 } else if (la.kind == 3) { | 374 } else if (la.kind == 3) { |
375 Get(); | 375 Get(); |
376 result = factory.createNumericLiteral(t); | 376 result = factory.createNumericLiteral(t); |
377 } else if (la.kind == 5) { | 377 } else if (la.kind == 5) { |
378 Get(); | 378 Get(); |
379 int start = t.charPos; | |
379 result = Expression(); | 380 result = Expression(); |
381 SLExpressionNode expr = result; | |
380 Expect(7); | 382 Expect(7); |
383 int length = (t.charPos + t.val.length()) - start; | |
384 result = factory.createParenExpression(expr, start, length); | |
381 } else SynErr(33); | 385 } else SynErr(33); |
382 return result; | 386 return result; |
383 } | 387 } |
384 | 388 |
385 | 389 |