comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/Parser.java @ 16880:7661cc464239

Truffle/Instrumentation: Added Instrumentable interface and LineLocationToSourceSections map SL: Updated implementation to use new Instrumentable interface
author David Piorkowski <david.piorkowski@oracle.com>
date Thu, 21 Aug 2014 13:28:22 -0700
parents 7c8ddb4233cd
children dc2e000bed40
comparison
equal deleted inserted replaced
16867:7af9301efe6b 16880:7661cc464239
28 import java.util.*; 28 import java.util.*;
29 29
30 import com.oracle.truffle.api.source.*; 30 import com.oracle.truffle.api.source.*;
31 import com.oracle.truffle.sl.*; 31 import com.oracle.truffle.sl.*;
32 import com.oracle.truffle.sl.nodes.*; 32 import com.oracle.truffle.sl.nodes.*;
33 import com.oracle.truffle.sl.nodes.instrument.*;
34 import com.oracle.truffle.sl.runtime.*; 33 import com.oracle.truffle.sl.runtime.*;
35 34
36 // Checkstyle: stop 35 // Checkstyle: stop
37 // @formatter:off 36 // @formatter:off
38 public class Parser { 37 public class Parser {
51 int errDist = minErrDist; 50 int errDist = minErrDist;
52 51
53 public final Scanner scanner; 52 public final Scanner scanner;
54 public final Errors errors; 53 public final Errors errors;
55 private final SLNodeFactory factory; 54 private final SLNodeFactory factory;
56 55
57 public Parser(SLContext context, Source source, SLNodeProber astProber) { 56 public Parser(SLContext context, Source source) {
58 this.scanner = new Scanner(source.getInputStream()); 57 this.scanner = new Scanner(source.getInputStream());
59 this.factory = new SLNodeFactory(context, source, astProber); 58 this.factory = new SLNodeFactory(context, source);
60 errors = new Errors(); 59 errors = new Errors();
61 } 60 }
62 61
63 void SynErr(int n) { 62 void SynErr(int n) {
64 if (errDist >= minErrDist) 63 if (errDist >= minErrDist)
132 } 131 }
133 132
134 void Function() { 133 void Function() {
135 Expect(4); 134 Expect(4);
136 Expect(1); 135 Expect(1);
137 Token identifierToken = t; 136 Token identifierToken = t;
138 Expect(5); 137 Expect(5);
139 int bodyStartPos = t.charPos; 138 int bodyStartPos = t.charPos;
140 factory.startFunction(identifierToken, bodyStartPos); 139 factory.startFunction(identifierToken, bodyStartPos);
141 if (la.kind == 1) { 140 if (la.kind == 1) {
142 Get(); 141 Get();
143 factory.addFormalParameter(t); 142 factory.addFormalParameter(t);
144 while (la.kind == 6) { 143 while (la.kind == 6) {
145 Get(); 144 Get();
146 Expect(1); 145 Expect(1);
147 factory.addFormalParameter(t); 146 factory.addFormalParameter(t);
148 } 147 }
149 } 148 }
150 Expect(7); 149 Expect(7);
151 SLStatementNode body = Block(false); 150 SLStatementNode body = Block(false);
152 factory.finishFunction(body); 151 factory.finishFunction(body);
153 } 152 }
154 153
155 SLStatementNode Block(boolean inLoop) { 154 SLStatementNode Block(boolean inLoop) {
156 SLStatementNode result; 155 SLStatementNode result;
157 factory.startBlock(); 156 factory.startBlock();
158 List<SLStatementNode> body = new ArrayList<>(); 157 List<SLStatementNode> body = new ArrayList<>();
159 Expect(8); 158 Expect(8);
160 int start = t.charPos; 159 int start = t.charPos;
161 while (StartOf(1)) { 160 while (StartOf(1)) {
162 SLStatementNode s = Statement(inLoop); 161 SLStatementNode s = Statement(inLoop);
163 body.add(s); 162 body.add(s);
164 } 163 }
165 Expect(9); 164 Expect(9);
166 int length = (t.charPos + t.val.length()) - start; 165 int length = (t.charPos + t.val.length()) - start;
167 result = factory.finishBlock(body, start, length); 166 result = factory.finishBlock(body, start, length);
168 return result; 167 return result;
169 } 168 }
170 169
171 SLStatementNode Statement(boolean inLoop) { 170 SLStatementNode Statement(boolean inLoop) {
172 SLStatementNode result; 171 SLStatementNode result;
173 result = null; 172 result = null;
174 switch (la.kind) { 173 switch (la.kind) {
175 case 13: { 174 case 13: {
176 result = WhileStatement(); 175 result = WhileStatement();
177 break; 176 break;
178 } 177 }
179 case 10: { 178 case 10: {
180 Get(); 179 Get();
181 if (inLoop) { result = factory.createBreak(t); } else { SemErr("break used outside of loop"); } 180 if (inLoop) { result = factory.createBreak(t); } else { SemErr("break used outside of loop"); }
182 Expect(11); 181 Expect(11);
183 break; 182 break;
184 } 183 }
185 case 12: { 184 case 12: {
186 Get(); 185 Get();
187 if (inLoop) { result = factory.createContinue(t); } else { SemErr("continue used outside of loop"); } 186 if (inLoop) { result = factory.createContinue(t); } else { SemErr("continue used outside of loop"); }
188 Expect(11); 187 Expect(11);
189 break; 188 break;
190 } 189 }
191 case 14: { 190 case 14: {
192 result = IfStatement(inLoop); 191 result = IfStatement(inLoop);
207 } 206 }
208 207
209 SLStatementNode WhileStatement() { 208 SLStatementNode WhileStatement() {
210 SLStatementNode result; 209 SLStatementNode result;
211 Expect(13); 210 Expect(13);
212 Token whileToken = t; 211 Token whileToken = t;
213 Expect(5); 212 Expect(5);
214 SLExpressionNode condition = Expression(); 213 SLExpressionNode condition = Expression();
215 Expect(7); 214 Expect(7);
216 SLStatementNode body = Block(true); 215 SLStatementNode body = Block(true);
217 result = factory.createWhile(whileToken, condition, body); 216 result = factory.createWhile(whileToken, condition, body);
218 return result; 217 return result;
219 } 218 }
220 219
221 SLStatementNode IfStatement(boolean inLoop) { 220 SLStatementNode IfStatement(boolean inLoop) {
222 SLStatementNode result; 221 SLStatementNode result;
223 Expect(14); 222 Expect(14);
224 Token ifToken = t; 223 Token ifToken = t;
225 Expect(5); 224 Expect(5);
226 SLExpressionNode condition = Expression(); 225 SLExpressionNode condition = Expression();
227 Expect(7); 226 Expect(7);
228 SLStatementNode thenPart = Block(inLoop); 227 SLStatementNode thenPart = Block(inLoop);
229 SLStatementNode elsePart = null; 228 SLStatementNode elsePart = null;
230 if (la.kind == 15) { 229 if (la.kind == 15) {
231 Get(); 230 Get();
232 elsePart = Block(inLoop); 231 elsePart = Block(inLoop);
233 } 232 }
234 result = factory.createIf(ifToken, condition, thenPart, elsePart); 233 result = factory.createIf(ifToken, condition, thenPart, elsePart);
235 return result; 234 return result;
236 } 235 }
237 236
238 SLStatementNode ReturnStatement() { 237 SLStatementNode ReturnStatement() {
239 SLStatementNode result; 238 SLStatementNode result;
240 Expect(16); 239 Expect(16);
241 Token returnToken = t; 240 Token returnToken = t;
242 SLExpressionNode value = null; 241 SLExpressionNode value = null;
243 if (StartOf(2)) { 242 if (StartOf(2)) {
244 value = Expression(); 243 value = Expression();
245 } 244 }
246 result = factory.createReturn(returnToken, value); 245 result = factory.createReturn(returnToken, value);
247 Expect(11); 246 Expect(11);
248 return result; 247 return result;
249 } 248 }
250 249
251 SLExpressionNode Expression() { 250 SLExpressionNode Expression() {
252 SLExpressionNode result; 251 SLExpressionNode result;
253 result = LogicTerm(); 252 result = LogicTerm();
254 while (la.kind == 17) { 253 while (la.kind == 17) {
255 Get(); 254 Get();
256 Token op = t; 255 Token op = t;
257 SLExpressionNode right = LogicTerm(); 256 SLExpressionNode right = LogicTerm();
258 result = factory.createBinary(op, result, right); 257 result = factory.createBinary(op, result, right);
259 } 258 }
260 return result; 259 return result;
261 } 260 }
262 261
263 SLExpressionNode LogicTerm() { 262 SLExpressionNode LogicTerm() {
264 SLExpressionNode result; 263 SLExpressionNode result;
265 result = LogicFactor(); 264 result = LogicFactor();
266 while (la.kind == 18) { 265 while (la.kind == 18) {
267 Get(); 266 Get();
268 Token op = t; 267 Token op = t;
269 SLExpressionNode right = LogicFactor(); 268 SLExpressionNode right = LogicFactor();
270 result = factory.createBinary(op, result, right); 269 result = factory.createBinary(op, result, right);
271 } 270 }
272 return result; 271 return result;
273 } 272 }
274 273
275 SLExpressionNode LogicFactor() { 274 SLExpressionNode LogicFactor() {
300 case 24: { 299 case 24: {
301 Get(); 300 Get();
302 break; 301 break;
303 } 302 }
304 } 303 }
305 Token op = t; 304 Token op = t;
306 SLExpressionNode right = Arithmetic(); 305 SLExpressionNode right = Arithmetic();
307 result = factory.createBinary(op, result, right); 306 result = factory.createBinary(op, result, right);
308 } 307 }
309 return result; 308 return result;
310 } 309 }
311 310
312 SLExpressionNode Arithmetic() { 311 SLExpressionNode Arithmetic() {
316 if (la.kind == 25) { 315 if (la.kind == 25) {
317 Get(); 316 Get();
318 } else { 317 } else {
319 Get(); 318 Get();
320 } 319 }
321 Token op = t; 320 Token op = t;
322 SLExpressionNode right = Term(); 321 SLExpressionNode right = Term();
323 result = factory.createBinary(op, result, right); 322 result = factory.createBinary(op, result, right);
324 } 323 }
325 return result; 324 return result;
326 } 325 }
327 326
328 SLExpressionNode Term() { 327 SLExpressionNode Term() {
332 if (la.kind == 27) { 331 if (la.kind == 27) {
333 Get(); 332 Get();
334 } else { 333 } else {
335 Get(); 334 Get();
336 } 335 }
337 Token op = t; 336 Token op = t;
338 SLExpressionNode right = Factor(); 337 SLExpressionNode right = Factor();
339 result = factory.createBinary(op, result, right); 338 result = factory.createBinary(op, result, right);
340 } 339 }
341 return result; 340 return result;
342 } 341 }
343 342
344 SLExpressionNode Factor() { 343 SLExpressionNode Factor() {
345 SLExpressionNode result; 344 SLExpressionNode result;
346 result = null; 345 result = null;
347 if (la.kind == 1) { 346 if (la.kind == 1) {
348 Get(); 347 Get();
349 Token nameToken = t; 348 Token nameToken = t;
350 if (la.kind == 5) { 349 if (la.kind == 5) {
351 Get(); 350 Get();
352 List<SLExpressionNode> parameters = new ArrayList<>(); 351 List<SLExpressionNode> parameters = new ArrayList<>();
353 SLExpressionNode parameter; 352 SLExpressionNode parameter;
354 if (StartOf(2)) { 353 if (StartOf(2)) {
355 parameter = Expression(); 354 parameter = Expression();
356 parameters.add(parameter); 355 parameters.add(parameter);
357 while (la.kind == 6) { 356 while (la.kind == 6) {
358 Get(); 357 Get();
359 parameter = Expression(); 358 parameter = Expression();
360 parameters.add(parameter); 359 parameters.add(parameter);
361 } 360 }
362 } 361 }
363 Expect(7); 362 Expect(7);
364 Token finalToken = t; 363 Token finalToken = t;
365 result = factory.createCall(nameToken, parameters, finalToken); 364 result = factory.createCall(nameToken, parameters, finalToken);
366 } else if (la.kind == 29) { 365 } else if (la.kind == 29) {
367 Get(); 366 Get();
368 SLExpressionNode value = Expression(); 367 SLExpressionNode value = Expression();
369 result = factory.createAssignment(nameToken, value); 368 result = factory.createAssignment(nameToken, value);
370 } else if (StartOf(4)) { 369 } else if (StartOf(4)) {
371 result = factory.createRead(nameToken); 370 result = factory.createRead(nameToken);
372 } else SynErr(32); 371 } else SynErr(32);
373 } else if (la.kind == 2) { 372 } else if (la.kind == 2) {
374 Get(); 373 Get();
375 result = factory.createStringLiteral(t); 374 result = factory.createStringLiteral(t);
376 } else if (la.kind == 3) { 375 } else if (la.kind == 3) {
377 Get(); 376 Get();
378 result = factory.createNumericLiteral(t); 377 result = factory.createNumericLiteral(t);
379 } else if (la.kind == 5) { 378 } else if (la.kind == 5) {
380 Get(); 379 Get();
381 int start = t.charPos; 380 int start = t.charPos;
382 result = Expression(); 381 result = Expression();
383 SLExpressionNode expr = result; 382 SLExpressionNode expr = result;
384 Expect(7); 383 Expect(7);
385 int length = (t.charPos + t.val.length()) - start; 384 int length = (t.charPos + t.val.length()) - start;
386 result = factory.createParenExpression(expr, start, length); 385 result = factory.createParenExpression(expr, start, length);
387 } else SynErr(33); 386 } else SynErr(33);
388 return result; 387 return result;
389 } 388 }
390 389
391 390
406 {x,x,x,x, 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}, 405 {x,x,x,x, 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},
407 {x,x,x,x, x,x,T,T, x,x,x,T, x,x,x,x, x,T,T,T, T,T,T,T, T,T,T,T, T,x,x,x} 406 {x,x,x,x, x,x,T,T, x,x,x,T, x,x,x,x, x,T,T,T, T,T,T,T, T,T,T,T, T,x,x,x}
408 407
409 }; 408 };
410 409
411 public static void parseSL(SLContext context, Source source, SLNodeProber astProber) { 410 public static void parseSL(SLContext context, Source source) {
412 Parser parser = new Parser(context, source, astProber); 411 Parser parser = new Parser(context, source);
413 parser.Parse(); 412 parser.Parse();
414 if (parser.errors.errors.size() > 0) { 413 if (parser.errors.errors.size() > 0) {
415 StringBuilder msg = new StringBuilder("Error(s) parsing script:\n"); 414 StringBuilder msg = new StringBuilder("Error(s) parsing script:\n");
416 for (String error : parser.errors.errors) { 415 for (String error : parser.errors.errors) {
417 msg.append(error).append("\n"); 416 msg.append(error).append("\n");