use of org.metaborg.spoofax.core.unit.ISpoofaxParseUnit in project spoofax by metaborg.
the class JSGLRCompletionService method get.
@Override
public Iterable<ICompletion> get(int position, ISpoofaxParseUnit parseInput, boolean nested) throws MetaborgException {
ISpoofaxParseUnit completionParseResult = null;
if (!nested && !parseInput.success()) {
final JSGLRParserConfiguration config = new JSGLRParserConfiguration(true, true, true, 3000, position);
final ISpoofaxInputUnit input = parseInput.input();
final ISpoofaxInputUnit modifiedInput = unitService.inputUnit(input.source(), input.text(), input.langImpl(), input.dialect(), config);
completionParseResult = syntaxService.parse(modifiedInput);
}
Collection<ICompletion> completions = Lists.newLinkedList();
// Completion in case of empty input
String inputText = parseInput.input().text();
if (inputText.trim().isEmpty()) {
final ILanguageImpl language = parseInput.input().langImpl();
final FileObject location = parseInput.source();
final Iterable<String> startSymbols = language.facet(SyntaxFacet.class).startSymbols;
completions.addAll(completionEmptyProgram(startSymbols, inputText.length(), language, location));
return completions;
}
if (completionParseResult != null && completionParseResult.ast() == null) {
return completions;
}
Collection<IStrategoTerm> nestedCompletionTerms = getNestedCompletionTermsFromAST(completionParseResult);
Collection<IStrategoTerm> completionTerms = getCompletionTermsFromAST(completionParseResult);
boolean blankLineCompletion = isCompletionBlankLine(position, parseInput.input().text());
if (!completionTerms.isEmpty()) {
completions.addAll(completionErroneousPrograms(position, completionTerms, completionParseResult));
}
if (!nestedCompletionTerms.isEmpty()) {
completions.addAll(completionErroneousProgramsNested(position, nestedCompletionTerms, completionParseResult));
}
if (completionTerms.isEmpty() && nestedCompletionTerms.isEmpty()) {
completions.addAll(completionCorrectPrograms(position, blankLineCompletion, parseInput));
}
return completions;
}
use of org.metaborg.spoofax.core.unit.ISpoofaxParseUnit in project spoofax by metaborg.
the class ParseFile method build.
@Override
protected Out<IStrategoTerm> build(Input input) throws Throwable {
requireBuild(input.requiredUnits);
require(input.file);
if (!FileCommands.exists(input.file)) {
return null;
}
final FileObject resource = context.resourceService().resolve(input.file);
final ILanguageImpl language = context.languageIdentifierService().identify(resource);
if (language == null) {
return null;
}
final String text = context.sourceTextService().text(resource);
final ISpoofaxInputUnit inputUnit = context.unitService().inputUnit(resource, text, language, null);
final ISpoofaxParseUnit result = context.syntaxService().parse(inputUnit);
if (!result.valid()) {
return null;
}
return input.persistResult ? OutputPersisted.of(result.ast()) : OutputTransient.of(result.ast());
}
use of org.metaborg.spoofax.core.unit.ISpoofaxParseUnit in project spoofax by metaborg.
the class TaskEngineAnalyzer method analyzeAll.
private ISpoofaxAnalyzeResults analyzeAll(Iterable<ISpoofaxParseUnit> inputs, IContext context, HybridInterpreter runtime, String strategy, ITermFactory termFactory) throws AnalysisException {
final Map<String, ISpoofaxParseUnit> inputsPerSource = Maps.newHashMap();
int detachedCounter = 0;
final Collection<IStrategoAppl> analysisInputs = Lists.newArrayList();
for (ISpoofaxParseUnit input : inputs) {
if (!input.valid()) {
logger.warn("Parse result for {} is invalid, cannot analyze", input.source());
continue;
}
final String pathString;
if (input.detached()) {
pathString = "detached-source-" + detachedCounter++;
logger.debug("Parse input is detached, using '{}' as path", pathString);
} else {
pathString = input.source().getName().getURI();
}
inputsPerSource.put(pathString, input);
final IStrategoString pathTerm = termFactory.makeString(pathString);
final IStrategoReal durationTerm = termFactory.makeReal(input.duration());
analysisInputs.add(termFactory.makeAppl(fileCons, pathTerm, input.ast(), durationTerm));
}
final IStrategoTerm inputTerm = termFactory.makeList(analysisInputs);
logger.trace("Invoking {} strategy", strategy);
final IStrategoTerm resultTerm;
try {
resultTerm = strategoCommon.invoke(runtime, inputTerm, strategy);
} catch (MetaborgException e) {
final String message = analysisCommon.analysisFailedMessage(runtime);
throw new AnalysisException(context, message, e);
}
if (resultTerm == null) {
final String message = analysisCommon.analysisFailedMessage(runtime);
throw new AnalysisException(context, message);
}
if (!(resultTerm instanceof IStrategoAppl) || resultTerm.getSubtermCount() != 5) {
final String message = logger.format("Unexpected results from analysis {}, expected 5-tuple", resultTerm);
throw new AnalysisException(context, message);
}
final IStrategoTerm resultsTerm = resultTerm.getSubterm(0);
final IStrategoTerm updateResultsTerm = resultTerm.getSubterm(1);
final Collection<ISpoofaxAnalyzeUnit> fileResults = Lists.newArrayListWithCapacity(resultsTerm.getSubtermCount());
for (IStrategoTerm result : resultsTerm) {
// HACK: analysis duration per parse unit is unknown, pass -1 as duration.
final ISpoofaxAnalyzeUnit fileResult = result(result, inputsPerSource, context, -1);
if (fileResult == null) {
continue;
}
fileResults.add(fileResult);
}
final Collection<ISpoofaxAnalyzeUnitUpdate> updateResults = Lists.newArrayListWithCapacity(updateResultsTerm.getSubtermCount());
for (IStrategoTerm result : updateResultsTerm) {
final ISpoofaxAnalyzeUnitUpdate updateResult = updateResult(result, context);
if (updateResult == null) {
continue;
}
updateResults.add(updateResult);
}
return new SpoofaxAnalyzeResults(fileResults, updateResults, context);
}
use of org.metaborg.spoofax.core.unit.ISpoofaxParseUnit in project spoofax by metaborg.
the class SpoofaxModule method bindAnalysis.
/**
* Overrides {@link MetaborgModule#bindAnalysis()} to provide Spoofax-specific bindings with Spoofax interfaces, and
* to provide analyzers.
*/
@Override
protected void bindAnalysis() {
// Analysis service
bind(SpoofaxAnalysisService.class).in(Singleton.class);
bind(ISpoofaxAnalysisService.class).to(SpoofaxAnalysisService.class);
bind(new TypeLiteral<IAnalysisService<ISpoofaxParseUnit, ISpoofaxAnalyzeUnit, ISpoofaxAnalyzeUnitUpdate>>() {
}).to(SpoofaxAnalysisService.class);
bind(new TypeLiteral<IAnalysisService<?, ?, ?>>() {
}).to(SpoofaxAnalysisService.class);
bind(IAnalysisService.class).to(SpoofaxAnalysisService.class);
// Stratego runtime
bind(StrategoRuntimeService.class).in(Singleton.class);
bind(IStrategoRuntimeService.class).to(StrategoRuntimeService.class);
languageCacheBinder.addBinding().to(StrategoRuntimeService.class);
// Utilities
bind(IStrategoCommon.class).to(StrategoCommon.class).in(Singleton.class);
bind(AnalysisCommon.class).in(Singleton.class);
// Stratego primitives
bind(ParseFileStrategy.class).in(Singleton.class);
bind(ParseStrategoFileStrategy.class).in(Singleton.class);
final Multibinder<IOperatorRegistry> libraryBinder = Multibinder.newSetBinder(binder(), IOperatorRegistry.class);
bindPrimitiveLibrary(libraryBinder, TaskLibrary.class);
bindPrimitiveLibrary(libraryBinder, LegacyIndexLibrary.class);
bindPrimitiveLibrary(libraryBinder, SpoofaxPrimitiveLibrary.class);
bindPrimitiveLibrary(libraryBinder, ScopeGraphLibrary.class);
bindPrimitiveLibrary(libraryBinder, FlowSpecLibrary.class);
bindPrimitiveLibrary(libraryBinder, LegacySpoofaxPrimitiveLibrary.class);
bindPrimitiveLibrary(libraryBinder, LegacySpoofaxJSGLRLibrary.class);
final Multibinder<AbstractPrimitive> spoofaxPrimitiveLibrary = Multibinder.newSetBinder(binder(), AbstractPrimitive.class, Names.named(SpoofaxPrimitiveLibrary.name));
bindPrimitive(spoofaxPrimitiveLibrary, DigestPrimitive.class);
bindPrimitive(spoofaxPrimitiveLibrary, LanguageComponentsPrimitive.class);
bindPrimitive(spoofaxPrimitiveLibrary, LanguageImplementationPrimitive.class);
bindPrimitive(spoofaxPrimitiveLibrary, LanguagePrimitive.class);
bindPrimitive(spoofaxPrimitiveLibrary, ProjectPathPrimitive.class);
bindPrimitive(spoofaxPrimitiveLibrary, LocalPathPrimitive.class);
bindPrimitive(spoofaxPrimitiveLibrary, LocalReplicatePrimitive.class);
bindPrimitive(spoofaxPrimitiveLibrary, AbsolutePathPrimitive.class);
bindPrimitive(spoofaxPrimitiveLibrary, LanguageSourceDirectoriesPrimitive.class);
bindPrimitive(spoofaxPrimitiveLibrary, LanguageSourceFilesPrimitive.class);
bindPrimitive(spoofaxPrimitiveLibrary, LanguageIncludeDirectoriesPrimitive.class);
bindPrimitive(spoofaxPrimitiveLibrary, LanguageIncludeFilesPrimitive.class);
bindPrimitive(spoofaxPrimitiveLibrary, RelativeSourcePath.class);
bindPrimitive(spoofaxPrimitiveLibrary, RelativeSourceOrIncludePath.class);
bindPrimitive(spoofaxPrimitiveLibrary, ParsePrimitive.class);
bindPrimitive(spoofaxPrimitiveLibrary, CallStrategyPrimitive.class);
bindPrimitive(spoofaxPrimitiveLibrary, IsLanguageActivePrimitive.class);
bindPrimitive(spoofaxPrimitiveLibrary, GetSortNamePrimitive.class);
final Multibinder<AbstractPrimitive> spoofaxScopeGraphLibrary = Multibinder.newSetBinder(binder(), AbstractPrimitive.class, Names.named("ScopeGraphLibrary"));
bindPrimitive(spoofaxScopeGraphLibrary, SG_analysis_has_errors.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_debug_constraints.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_debug_name_resolution.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_debug_scope_graph.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_debug_symbolic_constraints.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_debug_unifier.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_erase_ast_indices.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_fresh.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_focus_term.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_all_decls.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_all_refs.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_all_scopes.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_ast_analysis.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_ast_decls.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_ast_index.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_ast_property.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_ast_refs.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_ast_resolution.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_custom_analysis.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_decl_property.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_decl_scope.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_direct_edges_inv.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_direct_edges.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_export_edges_inv.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_export_edges.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_import_edges_inv.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_import_edges.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_reachable_decls.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_ref_resolution.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_ref_scope.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_resource_analysis.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_scope_decls.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_scope_refs.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_symbolic_facts.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_symbolic_goals.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_get_visible_decls.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_index_ast.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_is_debug_collection_enabled.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_is_debug_custom_enabled.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_is_debug_resolution_enabled.class);
bindPrimitive(spoofaxScopeGraphLibrary, SG_set_ast_index.class);
final Multibinder<AbstractPrimitive> spoofaxFlowSpecLibrary = Multibinder.newSetBinder(binder(), AbstractPrimitive.class, Names.named(FlowSpecLibrary.name));
bindPrimitive(spoofaxFlowSpecLibrary, FS_get_property_pre.class);
bindPrimitive(spoofaxFlowSpecLibrary, FS_get_property_post.class);
bindPrimitive(spoofaxFlowSpecLibrary, FS_show_control_flow_graph.class);
final Multibinder<AbstractPrimitive> legacySpoofaxLibrary = Multibinder.newSetBinder(binder(), AbstractPrimitive.class, Names.named(LegacySpoofaxPrimitiveLibrary.name));
bindPrimitive(legacySpoofaxLibrary, LegacyProjectPathPrimitive.class);
bindPrimitive(legacySpoofaxLibrary, LegacyLanguageSourceLocationsPrimitive.class);
bindPrimitive(legacySpoofaxLibrary, LegacyLanguageSourceLocationsPrimitive2.class);
bindPrimitive(legacySpoofaxLibrary, LegacyLanguageIncludeLocationsPrimitive.class);
bindPrimitive(legacySpoofaxLibrary, LegacyLanguageIncludeLocationsPrimitive2.class);
bindPrimitive(legacySpoofaxLibrary, LegacyLanguageSourceFilesPrimitive.class);
bindPrimitive(legacySpoofaxLibrary, LegacyLanguageIncludeFilesPrimitive.class);
bindPrimitive(legacySpoofaxLibrary, LegacyForeignCallPrimitive.class);
bindPrimitive(legacySpoofaxLibrary, new DummyPrimitive("SSL_EXT_set_total_work_units", 0, 0));
bindPrimitive(legacySpoofaxLibrary, new DummyPrimitive("SSL_EXT_set_markers", 0, 1));
bindPrimitive(legacySpoofaxLibrary, new DummyPrimitive("SSL_EXT_refreshresource", 0, 1));
bindPrimitive(legacySpoofaxLibrary, new DummyPrimitive("SSL_EXT_queue_strategy", 0, 2));
bindPrimitive(legacySpoofaxLibrary, new DummyPrimitive("SSL_EXT_complete_work_unit", 0, 0));
bindPrimitive(legacySpoofaxLibrary, new DummyPrimitive("SSL_EXT_pluginpath", 0, 0));
final Multibinder<AbstractPrimitive> legacySpoofaxJSGLRLibrary = Multibinder.newSetBinder(binder(), AbstractPrimitive.class, Names.named(LegacySpoofaxJSGLRLibrary.injectionName));
bindPrimitive(legacySpoofaxJSGLRLibrary, LegacyParseFilePrimitive.class);
bindPrimitive(legacySpoofaxJSGLRLibrary, LegacyParseFilePtPrimitive.class);
bindPrimitive(legacySpoofaxJSGLRLibrary, new DummyPrimitive("STRSGLR_open_parse_table", 0, 1));
bindPrimitive(legacySpoofaxJSGLRLibrary, new DummyPrimitive("STRSGLR_close_parse_table", 0, 1));
}
use of org.metaborg.spoofax.core.unit.ISpoofaxParseUnit in project spoofax by metaborg.
the class LegacyParseFilePrimitive method call.
@Override
public boolean call(IContext env, Strategy[] strategies, IStrategoTerm[] terms) throws InterpreterException {
if (!Tools.isTermString(terms[0]))
return false;
if (!Tools.isTermString(terms[3]))
return false;
try {
final String pathOrInput = Tools.asJavaString(terms[0]);
final String pathOrInput2 = Tools.asJavaString(terms[3]);
FileObject resource;
String text;
try {
resource = resourceService.resolve(pathOrInput);
if (!resource.exists() || resource.getType() != FileType.FILE) {
resource = resourceService.resolve(pathOrInput2);
text = pathOrInput;
} else {
text = sourceTextService.text(resource);
}
} catch (MetaborgRuntimeException | IOException e) {
resource = resourceService.resolve(pathOrInput2);
text = pathOrInput;
}
if (resource.getType() != FileType.FILE) {
return false;
}
final IdentifiedResource identifiedResource = languageIdentifierService.identifyToResource(resource);
if (identifiedResource == null) {
return false;
}
final ISpoofaxInputUnit input = unitService.inputUnit(resource, text, identifiedResource.language, identifiedResource.dialect);
final ISpoofaxParseUnit result = syntaxService.parse(input);
if (result.valid() && result.success()) {
env.setCurrent(result.ast());
} else {
return false;
}
} catch (ParseException | IOException e) {
throw new InterpreterException("Parsing failed unexpectedly", e);
}
return true;
}
Aggregations