com.oracle.truffle.api.source.SourceSection Java Examples

The following examples show how to use com.oracle.truffle.api.source.SourceSection. 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: 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 #3
Source File: MumblerReadException.java    From mumbler with GNU General Public License v3.0 6 votes vote down vote up
public static void throwReaderException(String message, Syntax<?> syntax,
        Namespace ns) {
    throw new MumblerReadException(message) {
        private static final long serialVersionUID = 1L;

        @Override
        public SourceSection getSourceSection() {
            return syntax.getSourceSection();
        }

        @Override
        public String getMethodName() {
            return ns.getFunctionName();
        }
    };
}
 
Example #4
Source File: Logger.java    From nodeprof.js with Apache License 2.0 6 votes vote down vote up
/**
 *
 * print source section with code
 *
 * should not be used in performance-critical parts
 *
 * @param sourceSection
 * @return string builder for the code
 */
@TruffleBoundary
public static StringBuilder printSourceSectionWithCode(
                SourceSection sourceSection) {
    StringBuilder b = new StringBuilder();
    if (sourceSection != null) {
        String fileName = sourceSection.getSource().getName();
        b.append(fileName.substring(fileName.lastIndexOf('/') + 1));
        b.append("(line ");
        b.append(sourceSection.getStartLine()).append("):");
        String code = sourceSection.getCharacters().toString().replaceAll("\\s", "");
        if (code.length() > 20) {
            code = code.substring(0, 20);
        }
        b.append(code);
    }
    return b;
}
 
Example #5
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 #6
Source File: ExecuteContextNode.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Override
public SourceSection getSourceSection() {
    if (section == null) {
        final Source source = code.getSource();
        section = source.createSection(1, 1, source.getLength());
    }
    return section;
}
 
Example #7
Source File: SourceMapping.java    From nodeprof.js with Apache License 2.0 5 votes vote down vote up
public static boolean isModuleOrWrapper(SourceSection section) {
    if (section == null) {
        return false;
    }
    int start = section.getCharIndex();
    return start <= 1;
}
 
Example #8
Source File: SourceMapping.java    From nodeprof.js with Apache License 2.0 5 votes vote down vote up
private static StringBuilder makeSectionString(SourceSection sourceSection) {
    StringBuilder b = new StringBuilder();
    if (GlobalConfiguration.SYMBOLIC_LOCATIONS && syntheticLocations.containsKey(sourceSection)) {
        b.append("{{");
        b.append(syntheticLocations.get(sourceSection));
        return b.append("}}");
    }
    b.append(sourceSection.getStartLine()).append(":").append(sourceSection.getStartColumn()).append(":").append(sourceSection.getEndLine()).append(":").append(sourceSection.getEndColumn() + 1);
    return b;
}
 
Example #9
Source File: SourceMapping.java    From nodeprof.js with Apache License 2.0 5 votes vote down vote up
@TruffleBoundary
public static StringBuilder makeLocationString(SourceSection sourceSection) {
    StringBuilder b = new StringBuilder();
    String fileName = sourceSection.getSource().getName();
    boolean isInternal = isInternal(sourceSection.getSource());
    b.append("(");
    if (isInternal) {
        b.append("*");
    }
    b.append(shortPath(fileName));
    b.append(":").append(makeSectionString(sourceSection));
    b.append(")");
    return b;
}
 
Example #10
Source File: SourceMapping.java    From nodeprof.js with Apache License 2.0 5 votes vote down vote up
public static void addSyntheticLocation(SourceSection sourceSection, String name) {
    assert GlobalConfiguration.SYMBOLIC_LOCATIONS : "SYMBOLIC_LOCATIONS not enabled";
    boolean added = syntheticLocations.put(sourceSection, name) != null;
    if (added) {
        // invalidate cache
        iidToLocationCache.remove(sourceSet.get(sourceSection));
    }
}
 
Example #11
Source File: DebuggerVisualizer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** &lt;File name&gt;:&lt;line number&gt; */
static String getSourceLocation(SourceSection ss) {
    if (ss == null) {
        //System.err.println("No source section for node "+n);
        return "unknown";
    }
    return ss.getSource().getName() + ":" + ss.getStartLine();
}
 
Example #12
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();
}
 
Example #13
Source File: SqueakExceptions.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Override
public SourceSection getSourceSection() {
    if (sourceSection == null) {
        // - 1 for previous character.
        sourceSection = SqueakLanguage.getContext().getLastParseRequestSource().createSection(Math.max(sourceOffset - 1, 0), 1);
    }
    return sourceSection;
}
 
Example #14
Source File: HashemLanguage.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@Override
protected SourceSection findSourceLocation(HashemContext context, Object value) {
    if (value instanceof HashemBebin) {
        return ((HashemBebin) value).getDeclaredLocation();
    }
    return null;
}
 
Example #15
Source File: AbstractBytecodeNode.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Override
public final SourceSection getSourceSection() {
    CompilerAsserts.neverPartOfCompilation();
    if (sourceSection == null) {
        final Source source = code.getSource();
        if (CompiledCodeObject.SOURCE_UNAVAILABLE_CONTENTS.equals(source.getCharacters())) {
            sourceSection = source.createUnavailableSection();
        } else {
            final int lineNumber = SqueakBytecodeDecoder.findLineNumber(code, index);
            sourceSection = source.createSection(lineNumber);
        }
    }
    return sourceSection;
}
 
Example #16
Source File: MumblerException.java    From mumbler with GNU General Public License v3.0 5 votes vote down vote up
public static Throwable fillInMumblerStackTrace(Throwable t) {
    final List<StackTraceElement> stackTrace = new ArrayList<>();
    Truffle.getRuntime().iterateFrames((FrameInstanceVisitor<Void>) frame -> {
        Node callNode = frame.getCallNode();
        if (callNode == null) {
            return null;
        }
        RootNode root = callNode.getRootNode();

        /*
         * There should be no RootNodes other than SLRootNodes on the stack. Just for the
         * case if this would change.
         */
        String methodName = "$unknownFunction";
        if (root instanceof MumblerRootNode) {
            methodName = ((MumblerRootNode) root).name;
        }

        SourceSection sourceSection = callNode.getEncapsulatingSourceSection();
        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;
        }
        stackTrace.add(new StackTraceElement("mumbler", methodName, sourceName, lineNumber));
        return null;
    });
    t.setStackTrace(stackTrace.toArray(new StackTraceElement[stackTrace.size()]));
    return t;
}
 
Example #17
Source File: Converter.java    From mumbler with GNU General Public License v3.0 5 votes vote down vote up
private InvokeNode convertInvoke(MumblerList<? extends Syntax<?>> list,
        SourceSection sourceSection, Namespace ns) {

    MumblerNode functionNode = convert(list.car(), ns);
    MumblerNode[] arguments = StreamSupport
            .stream(list.cdr().spliterator(), false)
            .map(syn-> convert(syn, ns))
            .toArray(MumblerNode[]::new);
    if (isTailCallOptimizationEnabled) {
        return new TCOInvokeNode(functionNode, arguments, sourceSection);
    } else {
        return new InvokeNode(functionNode, arguments, sourceSection);
    }
}
 
Example #18
Source File: IfNode.java    From mumbler with GNU General Public License v3.0 5 votes vote down vote up
public IfNode(MumblerNode testNode, MumblerNode thenNode,
        MumblerNode elseNode, SourceSection sourceSection) {
    this.testNode = testNode;
    this.thenNode = thenNode;
    this.elseNode = elseNode;
    setSourceSection(sourceSection);
}
 
Example #19
Source File: InvokeNode.java    From mumbler with GNU General Public License v3.0 5 votes vote down vote up
public InvokeNode(MumblerNode functionNode, MumblerNode[] argumentNodes,
        SourceSection sourceSection) {
    this.functionNode = functionNode;
    this.argumentNodes = argumentNodes;
    this.dispatchNode = new UninitializedDispatchNode();
    setSourceSection(sourceSection);
}
 
Example #20
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 #21
Source File: SourceMapping.java    From nodeprof.js with Apache License 2.0 5 votes vote down vote up
@TruffleBoundary
public static int getIIDForSourceSection(SourceSection sourceSection) {
    if (sourceSet.containsKey(sourceSection)) {
        return sourceSet.get(sourceSection);
    }
    int newIId = ++iidGen;
    assert (newIId < Integer.MAX_VALUE);
    sourceSet.put(sourceSection, newIId);
    idToSource.put(newIId, sourceSection);
    return newIId;
}
 
Example #22
Source File: HashemDebugDirectTest.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
private void assertLocation(final String name, final int line, final boolean isBefore, final String code, final Object... expectedFrame) {
    run.addLast(() -> {
        assertNotNull(suspendedEvent);

        final SourceSection suspendedSourceSection = suspendedEvent.getSourceSection();
        Assert.assertEquals(line, suspendedSourceSection.getStartLine());
        Assert.assertEquals(code, suspendedSourceSection.getCharacters());

        Assert.assertEquals(isBefore, suspendedEvent.getSuspendAnchor() == SuspendAnchor.BEFORE);
        final DebugStackFrame frame = suspendedEvent.getTopStackFrame();
        assertEquals(name, frame.getName());

        for (int i = 0; i < expectedFrame.length; i = i + 2) {
            final String expectedIdentifier = (String) expectedFrame[i];
            final Object expectedValue = expectedFrame[i + 1];
            DebugScope scope = frame.getScope();
            DebugValue slot = scope.getDeclaredValue(expectedIdentifier);
            while (slot == null && (scope = scope.getParent()) != null) {
                slot = scope.getDeclaredValue(expectedIdentifier);
            }
            if (expectedValue != UNASSIGNED) {
                Assert.assertNotNull(expectedIdentifier, slot);
                final String slotValue = slot.as(String.class);
                Assert.assertEquals(expectedValue, slotValue);
            } else {
                Assert.assertNull(expectedIdentifier, slot);
            }
        }
        run.removeFirst().run();
    });
}
 
Example #23
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 #24
Source File: ListSyntax.java    From mumbler with GNU General Public License v3.0 4 votes vote down vote up
public ListSyntax(MumblerList<? extends Syntax<?>> value,
        SourceSection sourceSection) {
    super(value, sourceSection);
}
 
Example #25
Source File: Reader.java    From mumbler with GNU General Public License v3.0 4 votes vote down vote up
private SourceSection createSourceSection(ParserRuleContext ctx) {
    return source.createSection(
            ctx.start.getLine(),
            ctx.start.getCharPositionInLine() + 1,
            ctx.stop.getStopIndex() - ctx.start.getStartIndex());
}
 
Example #26
Source File: Syntax.java    From mumbler with GNU General Public License v3.0 4 votes vote down vote up
public Syntax(T value, SourceSection sourceSection) {
    this.value = value;
    this.sourceSection = sourceSection;
}
 
Example #27
Source File: BooleanSyntax.java    From mumbler with GNU General Public License v3.0 4 votes vote down vote up
public BooleanSyntax(boolean value, SourceSection source) {
	super(value, source);
}
 
Example #28
Source File: SymbolSyntax.java    From mumbler with GNU General Public License v3.0 4 votes vote down vote up
public SymbolSyntax(MumblerSymbol value, SourceSection source) {
	super(value, source);
}
 
Example #29
Source File: LongSyntax.java    From mumbler with GNU General Public License v3.0 4 votes vote down vote up
public LongSyntax(long value, SourceSection source) {
	super(value, source);
}
 
Example #30
Source File: BigIntegerSyntax.java    From mumbler with GNU General Public License v3.0 4 votes vote down vote up
public BigIntegerSyntax(BigInteger value, SourceSection source) {
	super(value, source);
}