@Override public String toSource(int depth) { StringBuilder sb = new StringBuilder(); sb.append(makeIndent(depth)); sb.append("for "); if (isForEach()) { sb.append("each "); } sb.append("("); sb.append(iterator.toSource(0)); if (isForOf) { sb.append(" of "); } else { sb.append(" in "); } sb.append(iteratedObject.toSource(0)); sb.append(") "); if (body.getType() == Token.BLOCK) { sb.append(body.toSource(depth).trim()).append("\n"); } else { sb.append("\n").append(body.toSource(depth+1)); } return sb.toString(); }
@Override public String toSource(int depth) { StringBuilder sb = new StringBuilder(); sb.append(makeIndent(depth)); int type = getType(); if (!isPostfix) { sb.append(operatorToString(type)); if (type == Token.TYPEOF || type == Token.DELPROP || type == Token.VOID) { sb.append(" "); } } sb.append(operand.toSource()); if (isPostfix) { sb.append(operatorToString(type)); } return sb.toString(); }
@Override public String toSource(int depth) { StringBuilder sb = new StringBuilder(); sb.append(makeIndent(depth)); switch (getType()) { case Token.THIS: sb.append("this"); break; case Token.NULL: sb.append("null"); break; case Token.TRUE: sb.append("true"); break; case Token.FALSE: sb.append("false"); break; case Token.DEBUGGER: sb.append("debugger;\n"); break; default: throw new IllegalStateException("Invalid keyword literal type: " + getType()); } return sb.toString(); }
@Override public String toSource(int depth) { StringBuilder sb = new StringBuilder(); sb.append("\n"); sb.append(makeIndent(depth+1)); if (isGetterMethod()) { sb.append("get "); } else if (isSetterMethod()) { sb.append("set "); } sb.append(left.toSource(getType()==Token.COLON ? 0 : depth)); if (type == Token.COLON) { sb.append(": "); } sb.append(right.toSource(getType()==Token.COLON ? 0 : depth+1)); return sb.toString(); }
@Override public String toSource(int depth) { StringBuilder sb = new StringBuilder(); sb.append(makeIndent(depth)); sb.append("for ("); sb.append(initializer.toSource(0)); sb.append("; "); sb.append(condition.toSource(0)); sb.append("; "); sb.append(increment.toSource(0)); sb.append(") "); if (body.getType() == Token.BLOCK) { sb.append(body.toSource(depth).trim()).append("\n"); } else { sb.append("\n").append(body.toSource(depth+1)); } return sb.toString(); }
@Override public String toSource(int depth) { StringBuilder sb = new StringBuilder(); sb.append(makeIndent(depth)); if (!isPostfix) { sb.append(operatorToString(getType())); if (getType() == Token.TYPEOF || getType() == Token.DELPROP) { sb.append(" "); } } sb.append(operand.toSource()); if (isPostfix) { sb.append(operatorToString(getType())); } return sb.toString(); }
@Override public String toSource(int depth) { StringBuilder sb = new StringBuilder(); sb.append(makeIndent(depth)); switch (getType()) { case Token.THIS: sb.append("this"); break; case Token.NULL: sb.append("null"); break; case Token.TRUE: sb.append("true"); break; case Token.FALSE: sb.append("false"); break; case Token.DEBUGGER: sb.append("debugger"); break; default: throw new IllegalStateException("Invalid keyword literal type: " + getType()); } return sb.toString(); }
@Override public String toSource(int depth) { StringBuilder sb = new StringBuilder(); sb.append(makeIndent(depth)); if (isGetter()) { sb.append("get "); } else if (isSetter()) { sb.append("set "); } sb.append(left.toSource(0)); if (type == Token.COLON) { sb.append(": "); } sb.append(right.toSource(0)); return sb.toString(); }
public void testLinenoGetProp() { AstRoot root = parse("\nfoo.bar"); ExpressionStatement st = (ExpressionStatement) root.getFirstChild(); AstNode n = st.getExpression(); assertTrue(n instanceof PropertyGet); assertEquals(Token.GETPROP, n.getType()); assertEquals(1, n.getLineno()); PropertyGet getprop = (PropertyGet) n; AstNode m = getprop.getRight(); assertTrue(m instanceof Name); assertEquals(Token.NAME, m.getType()); // used to be Token.STRING! assertEquals(1, m.getLineno()); }
/** * Checks the parsed file against the API Map. * * @param p * an instance of {@link JavaScriptParser} to get the tree representation of it * @param jsFileName * the JavaScript file name that is being checked * @throws IOException */ private void checkJS(JavaScriptParser p, String jsFileName) throws IOException { ScriptOrFnNode nodeTree = p.parse(); for (Node cursor = nodeTree.getFirstChild(); cursor != null; cursor = cursor.getNext()) { StringBuffer sb = new StringBuffer(); if (cursor.getType() == Token.FUNCTION) { int fnIndex = cursor.getExistingIntProp(Node.FUNCTION_PROP); FunctionNode fn = nodeTree.getFunctionNode(fnIndex); sb.append("FUNCTION: " + fn.getFunctionName()); } else if (cursor.getType() == Token.VAR) { Node vn = cursor.getFirstChild(); sb.append("VAR: " + vn.getString()); } apiMap.remove(jsFileName + sb); } }
/** * @return a string with the global variables and function definitions */ public String getStringDetails() { if (jsFile == null) { throw new RuntimeException("You need to specify the file to parse"); } if (details == null) { details = new StringBuffer(); try { parse(); } catch (IOException e) { e.printStackTrace(); } for (Node cursor = nodeTree.getFirstChild(); cursor != null; cursor = cursor.getNext()) { if (cursor.getType() == Token.FUNCTION) { int fnIndex = cursor.getExistingIntProp(Node.FUNCTION_PROP); FunctionNode fn = nodeTree.getFunctionNode(fnIndex); details.append("FUNCTION: " + fn.getFunctionName() + "\n"); } else if (cursor.getType() == Token.VAR) { Node vn = cursor.getFirstChild(); details.append("VAR: " + vn.getString() + "\n"); } } } return details.toString(); }
public static TypeErrorMessage binaryOperatorMisuse(InfixExpression node, Type t1, Type t2, Type outType) { TypeErrorMessage msg = genericTypeError("misuse of binary operator " + AstNode.operatorToString(node.getOperator()) + " in \"" + shortSrc(node) + '"', locationOf(node)); if (node.getOperator() == Token.IN) { if (!unconstrained(t1) && !Types.isEqual(t1, StringType.make())) { msg = msg.withNote("left operand has type " + describeType(t1) + " instead of " + StringType.make()); } if (!unconstrained(t2) && !Types.isMapType(t2)) { msg = msg.withNote("right operand has type " + describeType(t2) + " instead of " + new MapType(new DefaultType())); } } else { if (!unconstrained(t1)) { msg = msg.withNote("left operand has type " + describeType(t1)); } if (!unconstrained(t2)) { msg = msg.withNote("right operand has type " + describeType(t2)); } } if (!unconstrained(outType)) { msg = msg.withNote("result type is " + describeType(outType)); } return msg; }
/** * Debugging function to check that the parser has set the parent * link for every node in the tree. * @throws IllegalStateException if a parent link is missing */ public void checkParentLinks() { this.visit(new NodeVisitor() { @Override public boolean visit(AstNode node) { int type = node.getType(); if (type == Token.SCRIPT) return true; if (node.getParent() == null) throw new IllegalStateException ("No parent for node: " + node + "\n" + node.toSource(0)); return true; } }); }
public static boolean isTrivial (String formula) { // No formulas, just one constant or one variable. final ArrayList<AstNode> result = new ArrayList<AstNode>(); parseIntoTree(formula).visit(new NodeVisitor(){ @Override public boolean visit(AstNode node) { if(node.depth()>1){ result.add(node); return false; } return true; } }); if(result.size()>0){ switch (result.get(0).getType()) { case Token.NUMBER: return true; case Token.NAME: return true; case Token.STRING: return true; default: return false; } } return false; }
/** * * @param n * @param objectName * @return */ private static void getScriptObjectName( Node n, String objectName, Set nameSet ) { if ( n == null ) return; String result = null; if ( n.getType( ) == Token.NAME ) { if ( objectName.equals( n.getString( ) ) ) { Node dimNameNode = n.getNext( ); if ( dimNameNode == null || dimNameNode.getType( ) != Token.STRING ) return; nameSet.add( dimNameNode.getString( ) ); } } getScriptObjectName( n.getFirstChild( ), objectName, nameSet ); getScriptObjectName( n.getNext( ), objectName, nameSet ); getScriptObjectName( n.getLastChild( ), objectName, nameSet ); }
@Override public String toSource(int depth) { StringBuilder sb = new StringBuilder(); sb.append(makeIndent(depth)); sb.append("for "); if (isForEach()) { sb.append("each "); } sb.append("("); sb.append(iterator.toSource(0)); sb.append(" in "); sb.append(iteratedObject.toSource(0)); sb.append(") "); if (body.getType() == Token.BLOCK) { sb.append(body.toSource(depth).trim()).append("\n"); } else { sb.append("\n").append(body.toSource(depth+1)); } return sb.toString(); }
/** * * @param n * @param objectName * @return */ private static String getScriptObjectName( Node n, String objectName ) { if ( n == null ) return null; String result = null; if ( n.getType( ) == Token.NAME ) { if ( objectName.equals( n.getString( ) ) ) { Node dimNameNode = n.getNext( ); if ( dimNameNode == null || dimNameNode.getType( ) != Token.STRING ) return null; return dimNameNode.getString( ); } } result = getScriptObjectName( n.getFirstChild( ), objectName ); if ( result == null ) result = getScriptObjectName( n.getLastChild( ), objectName ); return result; }
protected AggregateExpression compileAggregateExpr( Context context, Node parent, Node callNode ) throws DataException { assert( callNode.getType() == Token.CALL ); IAggrFunction aggregation = getAggregationFunction( callNode ); // not an aggregation function being called, then it's considered // a complex expression if( aggregation == null ) return null; AggregateExpression aggregateExpression = new AggregateExpression( aggregation ); extractArguments( context, aggregateExpression, callNode ); replaceAggregateNode( aggregateExpression, parent, callNode ); return aggregateExpression; }
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 }
@Override public String toSource(int depth) { StringBuilder sb = new StringBuilder(); sb.append(makeIndent(depth)); sb.append("while ("); sb.append(condition.toSource(0)); sb.append(") "); if (body.getType() == Token.BLOCK) { sb.append(body.toSource(depth).trim()); sb.append("\n"); } else { sb.append("\n").append(body.toSource(depth+1)); } return sb.toString(); }
void addSymbol(Symbol symbol) { if (variableNames != null) codeBug(); if (symbol.getDeclType() == Token.LP) { paramCount++; } symbols.add(symbol); }
/** * Sets the node type. * @throws IllegalArgumentException if {@code nodeType} is not one of * {@link Token#VAR}, {@link Token#CONST}, or {@link Token#LET} */ public void setNodeType(int nodeType) { if (nodeType != Token.VAR && nodeType != Token.CONST && nodeType != Token.LET) throw new IllegalArgumentException("invalid node type"); setType(nodeType); }
@Override public boolean hasSideEffects() { // the null-checks are for malformed expressions in IDE-mode switch (getType()) { case Token.COMMA: return right != null && right.hasSideEffects(); case Token.AND: case Token.OR: return left != null && left.hasSideEffects() || (right != null && right.hasSideEffects()); default: return super.hasSideEffects(); } }
/** * Sets node token type * @throws IllegalArgumentException if {@code nodeType} is unsupported */ @Override public KeywordLiteral setType(int nodeType) { if (!(nodeType == Token.THIS || nodeType == Token.NULL || nodeType == Token.TRUE || nodeType == Token.FALSE || nodeType == Token.DEBUGGER)) throw new IllegalArgumentException("Invalid node type: " + nodeType); type = nodeType; return this; }