Java Code Examples for com.oracle.truffle.api.nodes.Node#getSourceSection()

The following examples show how to use com.oracle.truffle.api.nodes.Node#getSourceSection() . 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: HashemStatementNode.java    From mr-hashemi with Universal Permissive License v1.0 6 votes vote down vote up
/**
 * Formats a source section of a node in human readable form. If no source section could be
 * found it looks up the parent hierarchy until it finds a source section. Nodes where this was
 * required append a <code>'~'</code> at the end.
 *
 * @param node the node to format.
 * @return a formatted source section string
 */
public static String formatSourceSection(Node node) {
    if (node == null) {
        return "<unknown>";
    }
    SourceSection section = node.getSourceSection();
    boolean estimated = false;
    if (section == null) {
        section = node.getEncapsulatingSourceSection();
        estimated = true;
    }

    if (section == null || section.getSource() == null) {
        return "<unknown source>";
    } else {
        String sourceName = section.getSource().getName();
        int startLine = section.getStartLine();
        return String.format("%s:%d%s", sourceName, startLine, estimated ? "~" : "");
    }
}
 
Example 2
Source File: FunctionRootEventHandler.java    From nodeprof.js with Apache License 2.0 6 votes vote down vote up
/**
 * @return the source of the instrumented node (or its closest parent), or null if no source is
 *         available
 */
public Source getSource() {
    if (isRegularExpression() || this.isBuiltin) {
        return null;
    }

    Node n = context.getInstrumentedNode();
    while (n != null && !(n instanceof FunctionBodyNode)) {
        n = n.getParent();
    }

    if (n == null) {
        return null;
    }

    if (n.getSourceSection() == null) {
        return null;
    }

    return n.getSourceSection().getSource();
}
 
Example 3
Source File: TruffleAST.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static void fillNode(Node node, StringBuilder nodes) {
    nodes.append(node.getClass().getName());
    nodes.append('\n');
    nodes.append(node.getDescription());
    nodes.append('\n');
    SourceSection ss = node.getSourceSection();
    if (ss == null) {
        nodes.append('\n');
    } else {
        nodes.append(ss.getSource().getURI().toString());
        nodes.append('\n');
        nodes.append(Integer.toString(ss.getStartLine()));
        nodes.append(':');
        nodes.append(Integer.toString(ss.getStartColumn()));
        nodes.append('-');
        nodes.append(Integer.toString(ss.getEndLine()));
        nodes.append(':');
        nodes.append(Integer.toString(ss.getEndColumn()));
        nodes.append('\n');
        //nodes.add(ss.getCode());
    }
    // TAGS:
    try {
        StringBuilder tags = new StringBuilder();
        if (node instanceof InstrumentableNode) {
            InstrumentableNode inode = (InstrumentableNode) node;
            for (Class<?> tag : StandardTags.class.getDeclaredClasses()) {
                if (Tag.class.isAssignableFrom(tag)) {
                    if (inode.hasTag(tag.asSubclass(Tag.class))) {
                        if (tags.length() > 0) {
                            tags.append(',');
                        }
                        tags.append(tag.getSimpleName());
                    }
                }
            }
        }
        nodes.append(tags);
    } catch (Throwable t) {
    }
    nodes.append('\n');
    List<Node> ch = NodeUtil.findNodeChildren(node);
    nodes.append(Integer.toString(ch.size()));
    nodes.append('\n');
    for (Node n : ch) {
        fillNode(n, nodes);
    }
}