Java Code Examples for jdk.nashorn.internal.ir.Node#accept()

The following examples show how to use jdk.nashorn.internal.ir.Node#accept() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JSONWriter.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean enterCaseNode(final CaseNode caseNode) {
    enterDefault(caseNode);

    type("SwitchCase");
    comma();

    final Node test = caseNode.getTest();
    property("test");
    if (test != null) {
        test.accept(this);
    } else {
        nullValue();
    }
    comma();

    array("consequent", caseNode.getBody().getStatements());

    return leave();
}
 
Example 2
Source File: JSONWriter.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean enterIfNode(final IfNode ifNode) {
    enterDefault(ifNode);

    type("IfStatement");
    comma();

    property("test");
    ifNode.getTest().accept(this);
    comma();

    property("consequent");
    ifNode.getPass().accept(this);
    final Node elsePart = ifNode.getFail();
    comma();

    property("alternate");
    if (elsePart != null) {
        elsePart.accept(this);
    } else {
        nullValue();
    }

    return leave();
}
 
Example 3
Source File: JSONWriter.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private void array(final String name, final List<? extends Node> nodes) {
    // The size, idx comparison is just to avoid trailing comma..
    final int size = nodes.size();
    int idx = 0;
    arrayStart(name);
    for (final Node node : nodes) {
        if (node != null) {
            node.accept(this);
        } else {
            nullValue();
        }
        if (idx != (size - 1)) {
            comma();
        }
        idx++;
    }
    arrayEnd();
}
 
Example 4
Source File: JSONWriter.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean enterExpressionStatement(final ExpressionStatement expressionStatement) {
    // handle debugger statement
    final Node expression = expressionStatement.getExpression();
    if (expression instanceof RuntimeNode) {
        expression.accept(this);
        return false;
    }

    enterDefault(expressionStatement);

    type("ExpressionStatement");
    comma();

    property("expression");
    expression.accept(this);

    return leave();
}
 
Example 5
Source File: JSONWriter.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void array(final String name, final List<? extends Node> nodes) {
    // The size, idx comparison is just to avoid trailing comma..
    final int size = nodes.size();
    int idx = 0;
    arrayStart(name);
    for (final Node node : nodes) {
        if (node != null) {
            node.accept(this);
        } else {
            nullValue();
        }
        if (idx != (size - 1)) {
            comma();
        }
        idx++;
    }
    arrayEnd();
}
 
Example 6
Source File: JSONWriter.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean enterCaseNode(final CaseNode caseNode) {
    enterDefault(caseNode);

    type("SwitchCase");
    comma();

    final Node test = caseNode.getTest();
    property("test");
    if (test != null) {
        test.accept(this);
    } else {
        nullValue();
    }
    comma();

    array("consequent", caseNode.getBody().getStatements());

    return leave();
}
 
Example 7
Source File: JSONWriter.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private boolean emitProgram(final FunctionNode functionNode) {
    enterDefault(functionNode);
    type("Program");
    comma();

    // body consists of nested functions and statements
    final List<Statement> stats = functionNode.getBody().getStatements();
    final int size = stats.size();
    int idx = 0;
    arrayStart("body");

    for (final Node stat : stats) {
        stat.accept(this);
        if (idx != (size - 1)) {
            comma();
        }
        idx++;
    }
    arrayEnd();

    return leave();
}
 
Example 8
Source File: JSONWriter.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean enterReturnNode(final ReturnNode returnNode) {
    enterDefault(returnNode);

    type("ReturnStatement");
    comma();

    final Node arg = returnNode.getExpression();
    property("argument");
    if (arg != null) {
        arg.accept(this);
    } else {
        nullValue();
    }

    return leave();
}
 
Example 9
Source File: JSONWriter.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean enterCatchNode(final CatchNode catchNode) {
    enterDefault(catchNode);

    type("CatchClause");
    comma();

    property("param");
    catchNode.getException().accept(this);
    comma();

    final Node guard = catchNode.getExceptionCondition();
    if (guard != null) {
        property("guard");
        guard.accept(this);
        comma();
    }

    property("body");
    catchNode.getBody().accept(this);

    return leave();
}
 
Example 10
Source File: PrintVisitor.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean enterVarNode(final VarNode varNode) {
    sb.append("var ");
    varNode.getName().toString(sb);
    final Node init = varNode.getInit();
    if (init != null) {
        sb.append(" = ");
        init.accept(this);
    }

    return false;
}
 
Example 11
Source File: WeighNodes.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public boolean enterLiteralNode(final LiteralNode literalNode) {
    weight += LITERAL_WEIGHT;

    if (literalNode instanceof ArrayLiteralNode) {
        final ArrayLiteralNode arrayLiteralNode = (ArrayLiteralNode)literalNode;
        final Node[]           value            = arrayLiteralNode.getValue();
        final int[]            postsets         = arrayLiteralNode.getPostsets();
        final List<Splittable.SplitRange>  units            = arrayLiteralNode.getSplitRanges();

        if (units == null) {
            for (final int postset : postsets) {
                weight += AASTORE_WEIGHT;
                final Node element = value[postset];

                if (element != null) {
                    element.accept(this);
                }
            }
        }

        return false;
    }

    return true;
}
 
Example 12
Source File: PrintVisitor.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean enterVarNode(final VarNode varNode) {
    sb.append("var ");
    varNode.getName().toString(sb, printTypes);
    printLocalVariableConversion(varNode.getName());
    final Node init = varNode.getInit();
    if (init != null) {
        sb.append(" = ");
        init.accept(this);
    }

    return false;
}
 
Example 13
Source File: JSONWriter.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean enterPropertyNode(final PropertyNode propertyNode) {
    final Node key = propertyNode.getKey();

    final Node value = propertyNode.getValue();
    if (value != null) {
        objectStart();
        location(propertyNode);

        property("key");
        key.accept(this);
        comma();

        property("value");
        value.accept(this);
        comma();

        property("kind", "init");

        objectEnd();
    } else {
        // getter
        final Node getter = propertyNode.getGetter();
        if (getter != null) {
            objectStart();
            location(propertyNode);

            property("key");
            key.accept(this);
            comma();

            property("value");
            getter.accept(this);
            comma();

            property("kind", "get");

            objectEnd();
        }

        // setter
        final Node setter = propertyNode.getSetter();
        if (setter != null) {
            if (getter != null) {
                comma();
            }
            objectStart();
            location(propertyNode);

            property("key");
            key.accept(this);
            comma();

            property("value");
            setter.accept(this);
            comma();

            property("kind", "set");

            objectEnd();
        }
    }

    return false;
}
 
Example 14
Source File: WeighNodes.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static long weigh(final Node node, final Map<Node, Long> weightCache) {
    final WeighNodes weighNodes = new WeighNodes(node instanceof FunctionNode ? (FunctionNode)node : null, weightCache);
    node.accept(weighNodes);
    return weighNodes.weight;
}
 
Example 15
Source File: PrintVisitor.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
private void visit(final Node root) {
    root.accept(this);
}
 
Example 16
Source File: PrintVisitor.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean enterBlock(final Block block) {
    sb.append(' ');
    //sb.append(Debug.id(block));
    sb.append('{');

    indent += TABWIDTH;

    final List<Statement> statements = block.getStatements();

    for (final Node statement : statements) {
        if (printLineNumbers && (statement instanceof Statement)) {
            final int lineNumber = ((Statement)statement).getLineNumber();
            sb.append('\n');
            if (lineNumber != lastLineNumber) {
                indent();
                sb.append("[|").append(lineNumber).append("|];").append('\n');
            }
            lastLineNumber = lineNumber;
        }
        indent();

        statement.accept(this);

        if (statement instanceof FunctionNode) {
            continue;
        }

        int  lastIndex = sb.length() - 1;
        char lastChar  = sb.charAt(lastIndex);
        while (Character.isWhitespace(lastChar) && lastIndex >= 0) {
            lastChar = sb.charAt(--lastIndex);
        }

        if (lastChar != '}' && lastChar != ';') {
            sb.append(';');
        }

        if (statement.hasGoto()) {
            sb.append(" [GOTO]");
        }

        if (statement.isTerminal()) {
            sb.append(" [TERMINAL]");
        }
    }

    indent -= TABWIDTH;

    sb.append(EOLN);
    indent();
    sb.append('}');
   // sb.append(Debug.id(block));

    return false;
}
 
Example 17
Source File: PrintVisitor.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean enterBlock(final Block block) {
    sb.append(' ');
    sb.append('{');

    indent += TABWIDTH;

    final List<Statement> statements = block.getStatements();

    for (final Node statement : statements) {
        if (printLineNumbers && (statement instanceof Statement)) {
            final int lineNumber = ((Statement)statement).getLineNumber();
            sb.append('\n');
            if (lineNumber != lastLineNumber) {
                indent();
                sb.append("[|").append(lineNumber).append("|];").append('\n');
            }
            lastLineNumber = lineNumber;
        }
        indent();

        statement.accept(this);

        if (statement instanceof FunctionNode) {
            continue;
        }

        int  lastIndex = sb.length() - 1;
        char lastChar  = sb.charAt(lastIndex);
        while (Character.isWhitespace(lastChar) && lastIndex >= 0) {
            lastChar = sb.charAt(--lastIndex);
        }

        if (lastChar != '}' && lastChar != ';') {
            sb.append(';');
        }

        if (statement.hasGoto()) {
            sb.append(" [GOTO]");
        }

        if (statement.isTerminal()) {
            sb.append(" [TERMINAL]");
        }
    }

    indent -= TABWIDTH;

    sb.append(EOLN);
    indent();
    sb.append('}');

    return false;
}
 
Example 18
Source File: PrintVisitor.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private void visit(final Node root) {
    root.accept(this);
}
 
Example 19
Source File: JSONWriter.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean enterVarNode(final VarNode varNode) {
    final Node init = varNode.getInit();
    if (init instanceof FunctionNode && ((FunctionNode)init).isDeclared()) {
        // function declaration - don't emit VariableDeclaration instead
        // just emit FunctionDeclaration using 'init' Node.
        init.accept(this);
        return false;
    }

    enterDefault(varNode);

    type("VariableDeclaration");
    comma();

    arrayStart("declarations");

    // VariableDeclarator
    objectStart();
    location(varNode.getName());

    type("VariableDeclarator");
    comma();

    property("id");
    varNode.getName().accept(this);
    comma();

    property("init");
    if (init != null) {
        init.accept(this);
    } else {
        nullValue();
    }

    // VariableDeclarator
    objectEnd();

    // declarations
    arrayEnd();

    return leave();
}
 
Example 20
Source File: JSONWriter.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean enterVarNode(final VarNode varNode) {
    final Node init = varNode.getInit();
    if (init instanceof FunctionNode && ((FunctionNode)init).isDeclared()) {
        // function declaration - don't emit VariableDeclaration instead
        // just emit FunctionDeclaration using 'init' Node.
        init.accept(this);
        return false;
    }

    enterDefault(varNode);

    type("VariableDeclaration");
    comma();

    arrayStart("declarations");

    // VariableDeclarator
    objectStart();
    location(varNode.getName());

    type("VariableDeclarator");
    comma();

    property("id");
    varNode.getName().accept(this);
    comma();

    property("init");
    if (init != null) {
        init.accept(this);
    } else {
        nullValue();
    }

    // VariableDeclarator
    objectEnd();

    // declarations
    arrayEnd();

    return leave();
}