use of org.spoofax.interpreter.terms.ITermFactory in project spoofax by metaborg.
the class JSGLRParseService method getCompletionParserConfig.
public IParserConfig getCompletionParserConfig(ILanguageImpl lang, ISpoofaxInputUnit input) throws ParseException {
IParserConfig config = completionParserConfigs.get(lang);
if (config == null) {
final ITermFactory termFactory = termFactoryService.getGeneric().getFactoryWithStorageType(IStrategoTerm.MUTABLE);
final SyntaxFacet facet = lang.facet(SyntaxFacet.class);
FileObject completionParseTable = null;
boolean incrementalPTGen = false;
for (ILanguageComponent component : lang.components()) {
if (component.config().sdfEnabled() && component.config().sdf2tableVersion() == Sdf2tableVersion.incremental) {
incrementalPTGen = true;
}
}
if (facet.completionParseTable == null) {
try {
boolean multipleTables = false;
for (ILanguageComponent component : lang.components()) {
if (component.config().sdfEnabled()) {
if (component.config().completionsParseTable() != null) {
if (multipleTables) {
logger.error("Different components are specifying multiple completion parse tables.");
throw new ParseException(input);
}
completionParseTable = component.location().resolveFile(component.config().completionsParseTable());
multipleTables = true;
}
}
}
} catch (FileSystemException e) {
logger.error("Completion parse table not found or sdf is not enabled for this language.");
throw new ParseException(input, e);
}
} else {
completionParseTable = facet.completionParseTable;
}
try {
if (completionParseTable == null || !completionParseTable.exists()) {
logger.error("Completion parse table not found or sdf is not enabled for this language.");
throw new ParseException(input);
}
} catch (FileSystemException e) {
logger.error("Completion parse table not found or sdf is not enabled for this language.");
throw new ParseException(input, e);
}
final IParseTableProvider provider;
final ParseTable referenceParseTable = referenceCompletionParseTables.get(lang);
if (referenceParseTable != null && incrementalPTGen) {
provider = new JSGLR1IncrementalParseTableProvider(completionParseTable, termFactory, referenceParseTable);
} else {
provider = new JSGLR1FileParseTableProvider(completionParseTable, termFactory);
}
config = new ParserConfig(Iterables.get(facet.startSymbols, 0), provider);
completionParserConfigs.put(lang, config);
}
return config;
}
use of org.spoofax.interpreter.terms.ITermFactory in project spoofax by metaborg.
the class HoverService method hover.
@Override
public Hover hover(int offset, ISpoofaxParseUnit result) throws MetaborgException {
if (!result.valid()) {
return null;
}
final FileObject source = result.source();
final IProject project = projectService.get(source);
final ILanguageImpl langImpl = result.input().langImpl();
@Nullable IContext context;
if (project == null) {
context = null;
} else {
try {
context = contextService.get(source, project, langImpl);
} catch (ContextException | MetaborgRuntimeException e) {
// Failed to get a context, ignore and use the source file to get a stratego runtime later.
context = null;
}
}
final FacetContribution<HoverFacet> facetContrib = facet(langImpl);
final HoverFacet facet = facetContrib.facet;
final ILanguageComponent contributor = facetContrib.contributor;
final String strategy = facet.strategyName;
try {
final ITermFactory termFactory = termFactoryService.get(contributor, project, true);
final HybridInterpreter interpreter;
if (context == null) {
interpreter = strategoRuntimeService.runtime(contributor, source, true);
} else {
interpreter = strategoRuntimeService.runtime(contributor, context, true);
}
final Iterable<IStrategoTerm> inRegion = tracingService.fragments(result, new SourceRegion(offset));
final TermWithRegion tuple = common.outputs(termFactory, interpreter, context.location(), source, result.ast(), inRegion, strategy);
return hover(tuple);
} catch (MetaborgException e) {
throw new MetaborgException("Getting hover tooltip information failed unexpectedly", e);
}
}
use of org.spoofax.interpreter.terms.ITermFactory in project spoofax by metaborg.
the class ResolverService method resolve.
@Override
public Resolution resolve(int offset, ISpoofaxAnalyzeUnit result) throws MetaborgException {
if (!result.valid() || !result.hasAst()) {
return null;
}
final FileObject source = result.source();
final IContext context = result.context();
final IProject project = context.project();
final ILanguageImpl language = context.language();
final FacetContribution<ResolverFacet> facetContrib = facet(language);
final ResolverFacet facet = facetContrib.facet;
final String strategy = facet.strategyName;
try {
final ITermFactory termFactory = termFactoryService.get(facetContrib.contributor, project, true);
final HybridInterpreter interpreter = strategoRuntimeService.runtime(facetContrib.contributor, context, true);
final Iterable<IStrategoTerm> inRegion = tracingService.fragments(result, new SourceRegion(offset));
final TermWithRegion tuple;
try (IClosableLock lock = context.read()) {
tuple = common.outputs(termFactory, interpreter, source, source, result.ast(), inRegion, strategy);
}
return resolve(tuple);
} catch (MetaborgException e) {
throw new MetaborgException("Reference resolution failed", e);
}
}
use of org.spoofax.interpreter.terms.ITermFactory in project spoofax by metaborg.
the class GetContextualGrammarPrimitive method call.
@Override
public boolean call(IContext env, Strategy[] svars, IStrategoTerm[] tvars) throws InterpreterException {
org.metaborg.core.context.IContext context = (org.metaborg.core.context.IContext) env.contextObject();
String path = ((IStrategoString) tvars[0]).stringValue();
ResourceService rs = context.injector().getInstance(ResourceService.class);
FileObject fo = rs.resolve(path + "/target/metaborg/ctxgrammar.aterm");
InputStream inputStream;
String text = "";
try {
inputStream = fo.getContent().getInputStream();
text = IOUtils.toString(inputStream, StandardCharsets.UTF_8.name());
} catch (IOException e) {
e.printStackTrace();
}
final ITermFactory tf = env.getFactory();
env.setCurrent(tf.parseFromString(text));
return true;
}
use of org.spoofax.interpreter.terms.ITermFactory in project spoofax by metaborg.
the class StrategoAnalyzer method analyze.
@Override
public ISpoofaxAnalyzeResult analyze(ISpoofaxParseUnit input, IContext context, IProgress progress, ICancel cancel) throws AnalysisException, InterruptedException {
cancel.throwIfCancelled();
if (!input.valid()) {
final String message = logger.format("Parse input for {} is invalid, cannot analyze", input.source());
throw new AnalysisException(context, message);
}
final ILanguageImpl language = context.language();
final ITermFactory termFactory = termFactoryService.getGeneric();
final FacetContribution<AnalysisFacet> facetContribution = language.facetContribution(AnalysisFacet.class);
if (facetContribution == null) {
logger.debug("No analysis required for {}", language);
final ISpoofaxAnalyzeUnit emptyUnit = unitService.emptyAnalyzeUnit(input, context);
return new SpoofaxAnalyzeResult(emptyUnit, context);
}
final AnalysisFacet facet = facetContribution.facet;
cancel.throwIfCancelled();
final HybridInterpreter runtime;
try {
runtime = runtimeService.runtime(facetContribution.contributor, context, true);
} catch (MetaborgException e) {
throw new AnalysisException(context, "Failed to get Stratego runtime", e);
}
cancel.throwIfCancelled();
final ISpoofaxAnalyzeUnit result = analyze(input, context, runtime, facet.strategyName, termFactory);
return new SpoofaxAnalyzeResult(result, context);
}
Aggregations