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()); } }
private FunctionCall getFunctionCall() { FunctionCall functionCall = new FunctionCall(); Name name = new Name(); name.setIdentifier("initialize"); functionCall.setTarget(name); StringLiteral arg = new StringLiteral(); arg.setValue("value"); arg.setQuoteCharacter('"'); functionCall.addArgument(arg); return functionCall; } class FunctionVisitor implements NodeVisitor { @Override public boolean visit(AstNode node) { if (node.getClass() == FunctionNode.class) { FunctionNode fun = (FunctionNode) node; if(fun.getName().equals("initialize")) //prevents infinit loop { return true; } fun.getBody().addChildrenToFront(new EmptyStatement()); // adds ';', I don't know if required fun.getBody().addChildrenToFront(getFunctionCall());//no fun.addChildrenToFront } return true; } }
Options options = new Options("nashorn"); options.set("anon.functions", true); options.set("parse.only", true); options.set("scripting", true); ErrorManager errors = new ErrorManager(); Context context = new Context(options, errors, Thread.currentThread().getContextClassLoader()); Source source = new Source("test", "var a = 10; var b = a + 1;" + "function someFunction() { return b + 1; } "); Parser parser = new Parser(context.getEnv(), source, errors); FunctionNode functionNode = parser.parse(); Block block = functionNode.getBody(); List<Statement> statements = block.getStatements();
curScopeTreeNode = tn; FunctionNode func = (FunctionNode)memberValue; func.getBody().visit(this); curScopeTreeNode = prevScopeTreeNode;
/** * Visits this node, the function name node if supplied, * the parameters, and the body. If there is a member-expr node, * it is visited last. */ @Override public void visit(NodeVisitor v) { if (v.visit(this)) { if (functionName != null) { functionName.visit(v); } for (AstNode param : getParams()) { param.visit(v); } getBody().visit(v); if (!isExpressionClosure) { if (memberExprNode != null) { memberExprNode.visit(v); } } } } }
/** * Visits this node, the function name node if supplied, * the parameters, and the body. If there is a member-expr node, * it is visited last. */ @Override public void visit(NodeVisitor v) { if (v.visit(this)) { if (functionName != null) { functionName.visit(v); } for (AstNode param : getParams()) { param.visit(v); } getBody().visit(v); if (!isExpressionClosure) { if (memberExprNode != null) { memberExprNode.visit(v); } } } } }
/** * Visits this node, the function name node if supplied, * the parameters, and the body. If there is a member-expr node, * it is visited last. */ @Override public void visit(NodeVisitor v) { if (v.visit(this)) { if (functionName != null) { functionName.visit(v); } for (AstNode param : getParams()) { param.visit(v); } getBody().visit(v); if (!isExpressionClosure) { if (memberExprNode != null) { memberExprNode.visit(v); } } } } }
/** * Visits this node, the function name node if supplied, * the parameters, and the body. If there is a member-expr node, * it is visited last. */ @Override public void visit(NodeVisitor v) { if (v.visit(this)) { if (functionName != null) { functionName.visit(v); } for (AstNode param : getParams()) { param.visit(v); } getBody().visit(v); if (!isExpressionClosure) { if (memberExprNode != null) { memberExprNode.visit(v); } } } } }
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()); } }
WalkContext child = new FunctionContext(context, fn); List<CAstNode> body = new ArrayList<>(); body.add(visit(fn.getBody(), child));
/** * Visits a function AST node. This will create a tree node for the * function, and also visit the AST nodes in the function's body, to * identify local variables, nested functions, etc. * * @param fn The function node. * @return <code>false</code> always, as this method manually visits * the function AST node's child nodes. */ private boolean visitFunction(FunctionNode fn) { Name funcName = fn.getFunctionName(); // Happens with certain syntax errors, such as // "function function foo() {". if (funcName!=null) { String text = fn.getName() + RhinoUtil.getFunctionArgsString(fn); JavaScriptTreeNode tn = createTreeNode(funcName); tn.setText(text); tn.setIcon(IconFactory.getIcon(IconFactory.DEFAULT_FUNCTION_ICON)); tn.setSortPriority(JavaScriptOutlineTree.PRIORITY_FUNCTION); curScopeTreeNode.add(tn); curScopeTreeNode = tn; fn.getBody().visit(this); curScopeTreeNode = (JavaScriptTreeNode)curScopeTreeNode.getParent(); } // Never visit children; we do this manually so we know when scope ends return false; }
func.getBody().visit(this); curScopeTreeNode = (JavaScriptTreeNode)curScopeTreeNode.getParent();