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