public AbstractEcmascriptNode(T node) { super(node.getType()); this.node = node; }
void calculateLineNumbers(SourceCodePositioner positioner) { int startOffset = node.getAbsolutePosition(); int endOffset = startOffset + node.getLength(); this.beginLine = positioner.lineNumberFromOffset(startOffset); this.beginColumn = positioner.columnFromOffset(this.beginLine, startOffset); this.endLine = positioner.lineNumberFromOffset(endOffset); // end column is inclusive this.endColumn = positioner.columnFromOffset(this.endLine, endOffset) - 1; if (this.endColumn < 0) { this.endColumn = 0; } }
private <T extends AstNode> EcmascriptNode<T> buildInternal(T astNode) { // Create a Node EcmascriptNode<T> node = createNodeAdapter(astNode); // Append to parent Node parent = nodes.isEmpty() ? null : nodes.peek(); if (parent != null) { parent.jjtAddChild(node, parent.jjtGetNumChildren()); node.jjtSetParent(parent); } handleParseProblems(node); // Build the children... nodes.push(node); parents.push(astNode); astNode.visit(this); nodes.pop(); parents.pop(); return node; }
public boolean visit(AstNode node) { int tt = node.getType(); String name = Token.typeToName(tt); buffer.append(node.getAbsolutePosition()).append("\t"); buffer.append(makeIndent(node.depth())); buffer.append(name).append(" "); buffer.append(node.getPosition()).append(" "); buffer.append(node.getLength()); if (tt == Token.NAME) { buffer.append(" ").append(((Name)node).getIdentifier()); } buffer.append("\n"); return true; // process kids } }
public ParenthesizedExpression(AstNode expr) { this(expr != null ? expr.getPosition() : 0, expr != null ? expr.getLength() : 1, expr); }
private ITree buildTree(AstNode node) { ITree t = context.createTree(node.getType(), ITree.NO_LABEL, Token.typeToName(node.getType())); t.setPos(node.getAbsolutePosition()); t.setLength(node.getLength()); trees.put(node, t); return t; }
private Position makePosition(AstNode n) { if (n != null) { URL url = ((SourceModule)sourceModule).getURL(); int line = n.getLineno(); Position pos = new RangePosition(url, line, n.getAbsolutePosition(), n.getAbsolutePosition()+n.getLength()); if (sourceModule instanceof MappedSourceModule) { Position np = ((MappedSourceModule) sourceModule).getMapping().getIncludedPosition(pos); if (np != null) { return np; } } return pos; } else { return null; } }
/** * Sets loop body. Sets the parent of the body to this loop node, * and updates its offset to be relative. Extends the length of this * node to include the body. */ public void setBody(AstNode body) { this.body = body; int end = body.getPosition() + body.getLength(); this.setLength(end - this.getPosition()); body.setParent(this); }
/** * Returns the root of the tree containing this node. * @return the {@link AstRoot} at the root of this node's parent * chain, or {@code null} if the topmost parent is not an {@code AstRoot}. */ public AstRoot getAstRoot() { AstNode parent = this; // this node could be the AstRoot while (parent != null && !(parent instanceof AstRoot)) { parent = parent.getParent(); } return (AstRoot)parent; }
@Override public boolean hasSideEffects() { return node.hasSideEffects(); }
/** * Return the line number recorded for this node. * If no line number was recorded, searches the parent chain. * @return the nearest line number, or -1 if none was found */ @Override public int getLineno() { if (lineno != -1) return lineno; if (parent != null) return parent.getLineno(); return -1; }
/** * Prints the source indented to depth 0. */ public String toSource() { return this.toSource(0); }
/** * Creates and returns a node for the outline tree that corresponds to * specific AST nodes. * * @param nodes The AST nodes. * @return The outline tree node. */ private JavaScriptTreeNode createTreeNode(List<AstNode> nodes) { JavaScriptTreeNode tn = new JavaScriptTreeNode(nodes); try { int offs = nodes.get(0).getAbsolutePosition(); tn.setOffset(textArea.getDocument().createPosition(offs)); } catch (BadLocationException ble) { // Never happens ble.printStackTrace(); } return tn; }
/** * Helper to report error for given node position in ast tree. */ private RuntimeException reportError(final AstNode node, final String message, Object... params) { throw Context.reportRuntimeError(String.format(message, params), source.getAbsolutePath(), node.getLineno(), // seems to always be the line before? node.debugPrint(), 0 // line-offset is unknown? ); }
private AstNode expr() throws IOException { AstNode pn = assignExpr(); int pos = pn.getPosition(); while (matchToken(Token.COMMA)) { int opPos = ts.tokenBeg; if (compilerEnv.isStrictMode() && !pn.hasSideEffects()) addStrictWarning("msg.no.side.effects", "", pos, nodeEnd(pn) - pos); if (peekToken() == Token.YIELD) reportError("msg.yield.parenthesized"); pn = new InfixExpression(Token.COMMA, pn, assignExpr(), opPos); } return pn; }
private void print(FunctionNode node) throws IOException { if (!node.isMethod()) { writer.append("function"); } if (node.getFunctionName() != null) { writer.append(' '); print(node.getFunctionName()); } writer.append('('); printList(node.getParams()); writer.append(')').ws(); if (node.isExpressionClosure()) { if (node.getBody().getLastChild() instanceof ReturnStatement) { print(((ReturnStatement) node.getBody().getLastChild()).getReturnValue()); if (node.getFunctionType() == FunctionNode.FUNCTION_STATEMENT) { writer.append(";"); } } } else { print(node.getBody()); } }
@Override public CAstNode visitLetNode(LetNode node, WalkContext arg) { VariableDeclaration decl = node.getVariables(); int i = 0; CAstNode[] stmts = new CAstNode[ decl.getVariables().size() + 1 ]; for(VariableInitializer init : decl.getVariables()) { stmts[i++] = Ast.makeNode(CAstNode.DECL_STMT, Ast.makeConstant(new CAstSymbolImpl(init.getTarget().getString(), JSAstTranslator.Any)), visit(init, arg)); } stmts[i++] = visit(node.getBody(), arg); return Ast.makeNode(CAstNode.LOCAL_SCOPE, stmts); }
public boolean visit(AstNode node) { int tt = node.getType(); String name = Token.typeToName(tt); buffer.append(node.getAbsolutePosition()).append("\t"); buffer.append(makeIndent(node.depth())); buffer.append(name).append(" "); buffer.append(node.getPosition()).append(" "); buffer.append(node.getLength()); if (tt == Token.NAME) { buffer.append(" ").append(((Name)node).getIdentifier()); } buffer.append("\n"); return true; // process kids } }