comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/SLNodeFactory.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
97 /* 97 /*
98 * Method parameters are assigned to local variables at the beginning of the method. This 98 * Method parameters are assigned to local variables at the beginning of the method. This
99 * ensures that accesses to parameters are specialized the same way as local variables are 99 * ensures that accesses to parameters are specialized the same way as local variables are
100 * specialized. 100 * specialized.
101 */ 101 */
102 final SourceSection src = source.createSection(nameToken.val, nameToken.charPos, nameToken.val.length()); 102 final SourceSection src = srcFromToken(nameToken);
103 SLReadArgumentNode readArg = new SLReadArgumentNode(src, parameterCount); 103 SLReadArgumentNode readArg = new SLReadArgumentNode(src, parameterCount);
104 methodNodes.add(createAssignment(nameToken, readArg)); 104 methodNodes.add(createAssignment(nameToken, readArg));
105 parameterCount++; 105 parameterCount++;
106 } 106 }
107 107
164 flattenedNodes.add((SLStatementNode) n); 164 flattenedNodes.add((SLStatementNode) n);
165 } 165 }
166 } 166 }
167 } 167 }
168 168
169 public SLStatementNode createBreak(Token t) { 169 public SLStatementNode createBreak(Token breakToken) {
170 return new SLBreakNode(source.createSection(t.val, t.charPos, t.val.length())); 170 return new SLBreakNode(srcFromToken(breakToken));
171 } 171 }
172 172
173 public SLStatementNode createContinue(Token t) { 173 public SLStatementNode createContinue(Token continueToken) {
174 return new SLContinueNode(source.createSection(t.val, t.charPos, t.val.length())); 174 return new SLContinueNode(srcFromToken(continueToken));
175 } 175 }
176 176
177 public SLStatementNode createWhile(Token t, SLExpressionNode conditionNode, SLStatementNode bodyNode) { 177 public SLStatementNode createWhile(Token whileToken, SLExpressionNode conditionNode, SLStatementNode bodyNode) {
178 final int start = t.charPos; 178 final int start = whileToken.charPos;
179 final int end = bodyNode.getSourceSection().getCharEndIndex(); 179 final int end = bodyNode.getSourceSection().getCharEndIndex();
180 return new SLWhileNode(source.createSection(t.val, start, end - start), conditionNode, bodyNode); 180 return new SLWhileNode(source.createSection(whileToken.val, start, end - start), conditionNode, bodyNode);
181 } 181 }
182 182
183 public SLStatementNode createIf(Token t, SLExpressionNode conditionNode, SLStatementNode thenPartNode, SLStatementNode elsePartNode) { 183 public SLStatementNode createIf(Token ifToken, SLExpressionNode conditionNode, SLStatementNode thenPartNode, SLStatementNode elsePartNode) {
184 final int start = t.charPos; 184 final int start = ifToken.charPos;
185 final int end = elsePartNode == null ? thenPartNode.getSourceSection().getCharEndIndex() : elsePartNode.getSourceSection().getCharEndIndex(); 185 final int end = elsePartNode == null ? thenPartNode.getSourceSection().getCharEndIndex() : elsePartNode.getSourceSection().getCharEndIndex();
186 186
187 // if (prober != null) { 187 // if (prober != null) {
188 // SLStatementNode wrappedThenNode = prober.probeAsStatement(thenPartNode); 188 // SLStatementNode wrappedThenNode = prober.probeAsStatement(thenPartNode);
189 // // SLStatementNode wrappedElseNode = prober.probeAsStatement(elsePartNode); 189 // // SLStatementNode wrappedElseNode = prober.probeAsStatement(elsePartNode);
190 // return new SLIfNode(source.createSection(t.val, start, end - start), conditionNode, 190 // return new SLIfNode(source.createSection(t.val, start, end - start), conditionNode,
191 // wrappedThenNode, elsePartNode); 191 // wrappedThenNode, elsePartNode);
192 // } 192 // }
193 193
194 return new SLIfNode(source.createSection(t.val, start, end - start), conditionNode, thenPartNode, elsePartNode); 194 return new SLIfNode(source.createSection(ifToken.val, start, end - start), conditionNode, thenPartNode, elsePartNode);
195 } 195 }
196 196
197 public SLStatementNode createReturn(Token t, SLExpressionNode valueNode) { 197 public SLStatementNode createReturn(Token t, SLExpressionNode valueNode) {
198 final int start = t.charPos; 198 final int start = t.charPos;
199 final int length = valueNode == null ? t.val.length() : valueNode.getSourceSection().getCharEndIndex() - start; 199 final int length = valueNode == null ? t.val.length() : valueNode.getSourceSection().getCharEndIndex() - start;
258 return SLWriteLocalVariableNodeFactory.create(source.createSection("=", start, length), valueNode, frameSlot); 258 return SLWriteLocalVariableNodeFactory.create(source.createSection("=", start, length), valueNode, frameSlot);
259 } 259 }
260 260
261 public SLExpressionNode createRead(Token nameToken) { 261 public SLExpressionNode createRead(Token nameToken) {
262 final FrameSlot frameSlot = lexicalScope.locals.get(nameToken.val); 262 final FrameSlot frameSlot = lexicalScope.locals.get(nameToken.val);
263 final SourceSection src = source.createSection(nameToken.val, nameToken.charPos, nameToken.val.length()); 263 final SourceSection src = srcFromToken(nameToken);
264 if (frameSlot != null) { 264 if (frameSlot != null) {
265 /* Read of a local variable. */ 265 /* Read of a local variable. */
266 return SLReadLocalVariableNodeFactory.create(src, frameSlot); 266 return SLReadLocalVariableNodeFactory.create(src, frameSlot);
267 } else { 267 } else {
268 /* Read of a global name. In our language, the only global names are functions. */ 268 /* Read of a global name. In our language, the only global names are functions. */
272 272
273 public SLExpressionNode createStringLiteral(Token literalToken) { 273 public SLExpressionNode createStringLiteral(Token literalToken) {
274 /* Remove the trailing and ending " */ 274 /* Remove the trailing and ending " */
275 String literal = literalToken.val; 275 String literal = literalToken.val;
276 assert literal.length() >= 2 && literal.startsWith("\"") && literal.endsWith("\""); 276 assert literal.length() >= 2 && literal.startsWith("\"") && literal.endsWith("\"");
277 final SourceSection src = source.createSection(literalToken.val, literalToken.charPos, literalToken.val.length()); 277 final SourceSection src = srcFromToken(literalToken);
278 literal = literal.substring(1, literal.length() - 1); 278 literal = literal.substring(1, literal.length() - 1);
279 279
280 return new SLStringLiteralNode(src, literal); 280 return new SLStringLiteralNode(src, literal);
281 } 281 }
282 282
283 public SLExpressionNode createNumericLiteral(Token literalToken) { 283 public SLExpressionNode createNumericLiteral(Token literalToken) {
284 final SourceSection src = source.createSection(literalToken.val, literalToken.charPos, literalToken.val.length()); 284 final SourceSection src = srcFromToken(literalToken);
285 try { 285 try {
286 /* Try if the literal is small enough to fit into a long value. */ 286 /* Try if the literal is small enough to fit into a long value. */
287 return new SLLongLiteralNode(src, Long.parseLong(literalToken.val)); 287 return new SLLongLiteralNode(src, Long.parseLong(literalToken.val));
288 } catch (NumberFormatException ex) { 288 } catch (NumberFormatException ex) {
289 /* Overflow of long value, so fall back to BigInteger. */ 289 /* Overflow of long value, so fall back to BigInteger. */
290 return new SLBigIntegerLiteralNode(src, new BigInteger(literalToken.val)); 290 return new SLBigIntegerLiteralNode(src, new BigInteger(literalToken.val));
291 } 291 }
292 } 292 }
293 293
294 public SLExpressionNode createParenExpression(SLExpressionNode expressionNode, int start, int length) {
295 final SourceSection src = source.createSection("()", start, length);
296 return new SLParenExpressionNode(src, expressionNode);
297 }
298
299 /**
300 * Creates source description of a single token.
301 */
302 private SourceSection srcFromToken(Token token) {
303 return source.createSection(token.val, token.charPos, token.val.length());
304 }
305
294 } 306 }