Java Code Examples for com.oracle.truffle.api.source.SourceSection#getSource()

The following examples show how to use com.oracle.truffle.api.source.SourceSection#getSource() . 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: FrameInfo.java    From netbeans with Apache License 2.0 6 votes vote down vote up
FrameInfo(DebugStackFrame topStackFrame, Iterable<DebugStackFrame> stackFrames) {
    SourceSection topSS = topStackFrame.getSourceSection();
    SourcePosition position = new SourcePosition(topSS);
    ArrayList<DebugStackFrame> stackFramesArray = new ArrayList<>();
    for (DebugStackFrame sf : stackFrames) {
        if (sf == topStackFrame) {
            continue;
        }
        SourceSection ss = sf.getSourceSection();
        // Ignore frames without sources:
        if (ss == null || ss.getSource() == null) {
            continue;
        }
        stackFramesArray.add(sf);
    }
    frame = topStackFrame;
    stackTrace = stackFramesArray.toArray(new DebugStackFrame[stackFramesArray.size()]);
    LanguageInfo sfLang = topStackFrame.getLanguage();
    topFrame = topStackFrame.getName() + "\n" +
               ((sfLang != null) ? sfLang.getId() + " " + sfLang.getName() : "") + "\n" +
               DebuggerVisualizer.getSourceLocation(topSS) + "\n" +
               position.id + "\n" + position.name + "\n" + position.path + "\n" +
               position.uri.toString() + "\n" + position.sourceSection +/* "," + position.startColumn + "," +
               position.endLine + "," + position.endColumn +*/ "\n" + isInternal(topStackFrame);
    topVariables = JPDATruffleAccessor.getVariables(topStackFrame);
}
 
Example 3
Source File: MumblerReadException.java    From mumbler with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Throwable fillInStackTrace() {
    SourceSection sourceSection = this.getSourceSection();
    Source source = sourceSection != null ? sourceSection.getSource() : null;
    String sourceName = source != null ? source.getName() : null;
    int lineNumber;
    try {
        lineNumber = sourceSection != null ? sourceSection.getStartLine() : -1;
    } catch (UnsupportedOperationException e) {
        /*
         * SourceSection#getLineLocation() may throw an UnsupportedOperationException.
         */
        lineNumber = -1;
    }
    StackTraceElement[] traces = new StackTraceElement[] {
            new StackTraceElement(filename(sourceName),
                    this.getMethodName(),
                    sourceName,
                    lineNumber)
    };
    this.setStackTrace(traces);
    return this;
}
 
Example 4
Source File: HashemStatementNode.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@Override
@TruffleBoundary
public final SourceSection getSourceSection() {
    if (sourceCharIndex == NO_SOURCE) {
        // AST node without source
        return null;
    }
    RootNode rootNode = getRootNode();
    if (rootNode == null) {
        // not yet adopted yet
        return null;
    }
    SourceSection rootSourceSection = rootNode.getSourceSection();
    if (rootSourceSection == null) {
        return null;
    }
    Source source = rootSourceSection.getSource();
    if (sourceCharIndex == UNAVAILABLE_SOURCE) {
        if (hasRootTag && !rootSourceSection.isAvailable()) {
            return rootSourceSection;
        } else {
            return source.createUnavailableSection();
        }
    } else {
        return source.createSection(sourceCharIndex, sourceLength);
    }
}
 
Example 5
Source File: SourceMapping.java    From nodeprof.js with Apache License 2.0 5 votes vote down vote up
@TruffleBoundary
public static DynamicObject getJSObjectForSourceSection(SourceSection section) {
    if (section == null) {
        return Undefined.instance;
    }

    JSContext ctx = GlobalObjectCache.getInstance().getJSContext();
    Source source = section.getSource();
    DynamicObject o = getJSObjectForSource(source);

    DynamicObject range = JSArray.createConstant(ctx, new Object[]{section.getCharIndex(), section.getCharEndIndex()});
    JSObject.set(o, "range", range);

    DynamicObject loc = JSUserObject.create(ctx);
    DynamicObject start = JSUserObject.create(ctx);
    DynamicObject end = JSUserObject.create(ctx);
    JSObject.set(start, "line", section.getStartLine());
    JSObject.set(start, "column", section.getStartColumn());
    JSObject.set(end, "line", section.getEndLine());
    JSObject.set(end, "column", section.getEndColumn());
    JSObject.set(loc, "start", start);
    JSObject.set(loc, "end", end);
    JSObject.set(o, "loc", loc);
    if (syntheticLocations.containsKey(section)) {
        JSObject.set(o, "symbolic", syntheticLocations.get(section));
    }
    return o;
}
 
Example 6
Source File: SourcePosition.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public SourcePosition(SourceSection sourceSection) {
    Source source = sourceSection.getSource();
    this.id = getId(source);
    this.name = source.getName();
    String sourcePath = source.getPath();
    if (sourcePath == null) {
        sourcePath = name;
    }
    this.path = sourcePath;
    this.sourceSection = sourceSection.getStartLine() + "," + sourceSection.getStartColumn() + "," + sourceSection.getEndLine() + "," + sourceSection.getEndColumn();
    this.code = source.getCharacters().toString();
    this.uri = source.getURI();
}