@Override public String getLanguage() { return key.base.getLanguage(); }
@Override public boolean test(Source source) { String language = source.getLanguage(); if (language != null) { for (String otherLanguage : languageIds) { if (otherLanguage.equals(language)) { return true; } } } return false; }
@Override public boolean test(Source source) { String language = source.getLanguage(); if (language != null) { for (String otherLanguage : languageIds) { if (otherLanguage.equals(language)) { return true; } } } return false; }
@Override public String getLanguage() { return sourceSection.getSource().getLanguage(); } }
public boolean test(com.oracle.truffle.api.source.Source s) { String language = s.getLanguage(); if (language == null) { return false; } else if (!engine.idToLanguage.containsKey(language)) { return false; } else if (sourceFilter != null) { try { return sourceFilter.test(engineImpl.getPolyglotSource(s)); } catch (Throwable e) { if (config.closing) { // configuration is closing ignore errors. return false; } throw new HostException(e); } } else { return true; } } });
final boolean equalAttributes(Source other) { return Objects.equals(getMimeType(), other.getMimeType()) && Objects.equals(getLanguage(), other.getLanguage()) && Objects.equals(getName(), other.getName()) && Objects.equals(getPath(), other.getPath()) && isInternal() == other.isInternal() && isInteractive() == other.isInteractive(); }
@Override public CallTarget parseForLanguage(Object vmObject, Source source, String[] argumentNames) { PolyglotLanguageContext sourceContext = (PolyglotLanguageContext) vmObject; PolyglotLanguage targetLanguage = sourceContext.context.engine.findLanguage(source.getLanguage(), source.getMimeType(), true); PolyglotLanguageContext targetContext = sourceContext.context.getContextInitialized(targetLanguage, sourceContext.language); return targetContext.parseCached(sourceContext.language, source, argumentNames); }
org.graalvm.polyglot.Source getPolyglotSource(Source source) { org.graalvm.polyglot.Source polyglotSource = VMAccessor.SOURCE.getPolyglotSource(source); if (polyglotSource == null) { polyglotSource = getAPIAccess().newSource(source.getLanguage(), source); VMAccessor.SOURCE.setPolyglotSource(source, polyglotSource); } return polyglotSource; }
@Override public org.graalvm.polyglot.SourceSection createSourceSection(Object vmObject, org.graalvm.polyglot.Source source, SourceSection sectionImpl) { org.graalvm.polyglot.Source polyglotSource = source; if (polyglotSource == null) { com.oracle.truffle.api.source.Source sourceImpl = sectionImpl.getSource(); polyglotSource = ((VMObject) vmObject).getAPIAccess().newSource(sourceImpl.getLanguage(), sourceImpl); } return ((VMObject) vmObject).getAPIAccess().newSourceSection(polyglotSource, sectionImpl); }
@Override public org.graalvm.polyglot.SourceSection createSourceSection(Object vmObject, org.graalvm.polyglot.Source source, SourceSection sectionImpl) { org.graalvm.polyglot.Source polyglotSource = source; if (polyglotSource == null) { com.oracle.truffle.api.source.Source sourceImpl = sectionImpl.getSource(); polyglotSource = ((VMObject) vmObject).getAPIAccess().newSource(sourceImpl.getLanguage(), sourceImpl); } return ((VMObject) vmObject).getAPIAccess().newSourceSection(polyglotSource, sectionImpl); }
/** * Evaluates source of (potentially different) language using the current context.The names * of arguments are parameters for the resulting {#link CallTarget} that allow the * <code>source</code> to reference the actual parameters passed to * {@link CallTarget#call(java.lang.Object...)}. * * @param source the source to evaluate * @param argumentNames the names of {@link CallTarget#call(java.lang.Object...)} arguments * that can be referenced from the source * @return the call target representing the parsed result * @throws IOException if the parsing or evaluation fails for some reason * @since 0.12 */ public CallTarget parse(Source source, String... argumentNames) throws IOException { TruffleLanguage.Env env = AccessorInstrumentHandler.engineAccess().getEnvForInstrument(vmObject, source.getLanguage(), source.getMimeType()); return AccessorInstrumentHandler.langAccess().parse(env, source, null, argumentNames); }
/** * Evaluates source of (potentially different) language using the current context.The names * of arguments are parameters for the resulting {#link CallTarget} that allow the * <code>source</code> to reference the actual parameters passed to * {@link CallTarget#call(java.lang.Object...)}. * * @param source the source to evaluate * @param argumentNames the names of {@link CallTarget#call(java.lang.Object...)} arguments * that can be referenced from the source * @return the call target representing the parsed result * @throws IOException if the parsing or evaluation fails for some reason * @since 0.12 */ public CallTarget parse(Source source, String... argumentNames) throws IOException { TruffleLanguage.Env env = AccessorInstrumentHandler.engineAccess().getEnvForInstrument(vmObject, source.getLanguage(), source.getMimeType()); return AccessorInstrumentHandler.langAccess().parse(env, source, null, argumentNames); }
/** * Evaluates source of (potentially different) language. The {@link Source#getMimeType() * MIME type} is used to identify the {@link TruffleLanguage} to use to perform the * {@link #parse(com.oracle.truffle.api.TruffleLanguage.ParsingRequest)} . The names of * arguments are parameters for the resulting {#link CallTarget} that allow the * <code>source</code> to reference the actual parameters passed to * {@link CallTarget#call(java.lang.Object...)}. * * @param source the source to evaluate * @param argumentNames the names of {@link CallTarget#call(java.lang.Object...)} arguments * that can be referenced from the source * @return the call target representing the parsed result * @since 0.8 or earlier */ @TruffleBoundary public CallTarget parse(Source source, String... argumentNames) { CompilerAsserts.neverPartOfCompilation(); checkDisposed(); return AccessAPI.engineAccess().getEnvForLanguage(vmObject, source.getLanguage(), source.getMimeType()).spi.parse(source, null, null, argumentNames); }
static JSONObject sourceSectionToJSON(SourceSection sourceSection) { JSONObject sourceSectionJson = new JSONObject(); if (sourceSection != null) { Source source = sourceSection.getSource(); if (source != null) { if (source.getLanguage() != null) { sourceSectionJson.put("language", source.getLanguage().toString()); } String path = source.getPath(); if (path != null) { sourceSectionJson.put("path", path); } } sourceSectionJson.put("source_name", sourceSection.getSource().getName()); sourceSectionJson.put("start_line", sourceSection.getStartLine()); sourceSectionJson.put("end_line", sourceSection.getEndLine()); sourceSectionJson.put("start_column", sourceSection.getStartColumn()); sourceSectionJson.put("end_column", sourceSection.getEndColumn()); } return sourceSectionJson; }
/** * {@inheritDoc} * * @since 1.0 */ @Override public String toString() { return "Source [language=" + getLanguage() + ", name=" + getName() + ", path=" + getPath() + ", internal=" + isInternal() + ", cached=" + isCached() + ", interactive=" + isInteractive() + ", hasBytes=" + hasBytes() + ", hasCharacters=" + hasCharacters() + ", URL=" + getURL() + ", URI=" + getURI() + ", mimeType=" + getMimeType() + "]"; }
/** * Parses source snippet of the node's language at the provided node location. The result is * an AST fragment represented by {@link ExecutableNode} that accepts frames valid at the * provided node location, or <code>null</code> when inline parsing is not supported by the * language. * * @param source a source snippet to parse at the provided node location * @param node a context location where the source is parsed at, must not be * <code>null</code> * @param frame a frame location where the source is parsed at, can be <code>null</code> * @return the executable fragment representing the parsed result, or <code>null</code> * @since 0.31 */ public ExecutableNode parseInline(Source source, Node node, MaterializedFrame frame) { if (node == null) { throw new IllegalArgumentException("Node must not be null."); } TruffleLanguage.Env env = AccessorInstrumentHandler.engineAccess().getEnvForInstrument(vmObject, source.getLanguage(), source.getMimeType()); // Assert that the languages match: assert AccessorInstrumentHandler.langAccess().getLanguageInfo(env) == node.getRootNode().getLanguageInfo(); ExecutableNode fragment = AccessorInstrumentHandler.langAccess().parseInline(env, source, node, frame); if (fragment != null) { TruffleLanguage<?> languageSPI = AccessorInstrumentHandler.langAccess().getSPI(env); fragment = new GuardedExecutableNode(languageSPI, fragment, frame); } return fragment; }
private void initializeConditional(MaterializedFrame frame) { Node instrumentedNode = context.getInstrumentedNode(); final RootNode rootNode = instrumentedNode.getRootNode(); if (rootNode == null) { throw new IllegalStateException("Probe was disconnected from the AST."); } Source instrumentedSource = context.getInstrumentedSourceSection().getSource(); Source conditionSource; synchronized (breakpoint) { conditionSource = Source.newBuilder(instrumentedSource.getLanguage(), breakpoint.condition, "breakpoint condition").mimeType(instrumentedSource.getMimeType()).build(); if (conditionSource == null) { throw new IllegalStateException("Condition is not resolved " + rootNode); } conditionUnchanged = breakpoint.getConditionUnchanged(); } ExecutableNode snippet = breakpoint.debugger.getEnv().parseInline(conditionSource, instrumentedNode, frame); if (snippet != null) { conditionSnippet = insert(snippet); notifyInserted(snippet); } else { CallTarget callTarget = Debugger.ACCESSOR.parse(conditionSource, instrumentedNode, new String[0]); conditionCallNode = insert(Truffle.getRuntime().createDirectCallNode(callTarget)); } } }
/** * Parses source snippet of the node's language at the provided node location. The result is * an AST fragment represented by {@link ExecutableNode} that accepts frames valid at the * provided node location, or <code>null</code> when inline parsing is not supported by the * language. * * @param source a source snippet to parse at the provided node location * @param node a context location where the source is parsed at, must not be * <code>null</code> * @param frame a frame location where the source is parsed at, can be <code>null</code> * @return the executable fragment representing the parsed result, or <code>null</code> * @since 0.31 */ public ExecutableNode parseInline(Source source, Node node, MaterializedFrame frame) { if (node == null) { throw new IllegalArgumentException("Node must not be null."); } TruffleLanguage.Env env = AccessorInstrumentHandler.engineAccess().getEnvForInstrument(vmObject, source.getLanguage(), source.getMimeType()); // Assert that the languages match: assert AccessorInstrumentHandler.langAccess().getLanguageInfo(env) == node.getRootNode().getLanguageInfo(); ExecutableNode fragment = AccessorInstrumentHandler.langAccess().parseInline(env, source, node, frame); if (fragment != null) { TruffleLanguage<?> languageSPI = AccessorInstrumentHandler.langAccess().getSPI(env); fragment = new GuardedExecutableNode(languageSPI, fragment, frame); } return fragment; }
private void initializeConditional(MaterializedFrame frame) { Node instrumentedNode = context.getInstrumentedNode(); final RootNode rootNode = instrumentedNode.getRootNode(); if (rootNode == null) { throw new IllegalStateException("Probe was disconnected from the AST."); } Source instrumentedSource = context.getInstrumentedSourceSection().getSource(); Source conditionSource; synchronized (breakpoint) { conditionSource = Source.newBuilder(breakpoint.condition).language(instrumentedSource.getLanguage()).mimeType(instrumentedSource.getMimeType()).name( "breakpoint condition").build(); if (conditionSource == null) { throw new IllegalStateException("Condition is not resolved " + rootNode); } conditionUnchanged = breakpoint.getConditionUnchanged(); } ExecutableNode snippet = breakpoint.debugger.getEnv().parseInline(conditionSource, instrumentedNode, frame); if (snippet != null) { conditionSnippet = insert(snippet); notifyInserted(snippet); } else { CallTarget callTarget = Debugger.ACCESSOR.parse(conditionSource, instrumentedNode, new String[0]); conditionCallNode = insert(Truffle.getRuntime().createDirectCallNode(callTarget)); } } }
/** * Uses configuration of this builder to create new {@link Source} object. The method throws * an {@link IOException} if an error loading the source occured. * * @return the source object * @throws IOException if an error reading the content occurred * @throws SecurityException if this {@link SourceBuilder} was created for a * {@link TruffleFile} and the used {@link org.graalvm.polyglot.io.FileSystem * filesystem} denied its reading * @since 1.0 */ public Source build() throws IOException { assert this.language != null; Source source = buildSource(this.language, this.origin, this.name, this.mimeType, this.content, this.uri, this.internal, this.interactive, this.cached, false); // make sure origin is not consumed again if builder is used twice if (source.hasBytes()) { this.content = source.getBytes(); } else if (source.hasCharacters()) { this.content = source.getCharacters(); } assert source.getName() != null; assert !source.hasCharacters() || source.getCharacters() != null; assert !source.hasBytes() || source.getBytes() != null; assert source.getLanguage() != null; return source; }