use of org.metaborg.core.language.ILanguageImpl in project spoofax by metaborg.
the class AbstractConstraintAnalyzer method analyzeAll.
@SuppressWarnings("unchecked")
@Override
public ISpoofaxAnalyzeResults analyzeAll(Iterable<ISpoofaxParseUnit> inputs, IContext genericContext, IProgress progress, ICancel cancel) throws AnalysisException {
C context;
try {
context = (C) genericContext;
} catch (ClassCastException ex) {
throw new AnalysisException(genericContext, "Scope graph context required for constraint analysis.", ex);
}
final ILanguageImpl langImpl = context.language();
final FacetContribution<AnalysisFacet> facetContribution = langImpl.facetContribution(AnalysisFacet.class);
if (facetContribution == null) {
logger.debug("No analysis required for {}", langImpl);
return new SpoofaxAnalyzeResults(context);
}
final AnalysisFacet facet = facetContribution.facet;
final HybridInterpreter runtime;
try {
runtime = runtimeService.runtime(facetContribution.contributor, context, false);
} catch (MetaborgException e) {
throw new AnalysisException(context, "Failed to get Stratego runtime", e);
}
Map<String, ISpoofaxParseUnit> changed = Maps.newHashMap();
Set<String> removed = Sets.newHashSet();
for (ISpoofaxParseUnit input : inputs) {
final String source;
if (input.detached()) {
source = "detached-" + UUID.randomUUID().toString();
} else {
source = resource(input.source(), context);
}
if (!input.valid() || isEmptyAST(input.ast())) {
removed.add(source);
} else {
changed.put(source, input);
}
}
return analyzeAll(changed, removed, context, runtime, facet.strategyName, progress, cancel);
}
use of org.metaborg.core.language.ILanguageImpl 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);
}
use of org.metaborg.core.language.ILanguageImpl in project spoofax by metaborg.
the class StrategoAnalyzer method analyzeAll.
@Override
public ISpoofaxAnalyzeResults analyzeAll(Iterable<ISpoofaxParseUnit> inputs, IContext context, IProgress progress, ICancel cancel) throws AnalysisException, InterruptedException {
cancel.throwIfCancelled();
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);
return new SpoofaxAnalyzeResults(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);
}
final int size = Iterables.size(inputs);
progress.setWorkRemaining(size);
final Collection<ISpoofaxAnalyzeUnit> results = Lists.newArrayListWithCapacity(size);
for (ISpoofaxParseUnit input : inputs) {
cancel.throwIfCancelled();
if (!input.valid()) {
logger.warn("Parse input for {} is invalid, cannot analyze", input.source());
// TODO: throw exception instead?
progress.work(1);
continue;
}
final ISpoofaxAnalyzeUnit result = analyze(input, context, runtime, facet.strategyName, termFactory);
results.add(result);
progress.work(1);
}
return new SpoofaxAnalyzeResults(results, context);
}
use of org.metaborg.core.language.ILanguageImpl 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.core.language.ILanguageImpl in project spoofax by metaborg.
the class JSGLRCompletionService method completionCorrectPrograms.
public Collection<ICompletion> completionCorrectPrograms(int position, boolean blankLineCompletion, ISpoofaxParseUnit parseResult) throws MetaborgException {
Collection<ICompletion> completions = Sets.newHashSet();
final FileObject location = parseResult.source();
final ILanguageImpl language = parseResult.input().langImpl();
final String languageName = language.belongsTo().name();
for (ILanguageComponent component : language.components()) {
final HybridInterpreter runtime = strategoRuntimeService.runtime(component, location, false);
final ITermFactory termFactory = termFactoryService.get(component, null, false);
final Map<IStrategoTerm, Boolean> leftRecursiveTerms = new HashMap<IStrategoTerm, Boolean>();
final Map<IStrategoTerm, Boolean> rightRecursiveTerms = new HashMap<IStrategoTerm, Boolean>();
final Iterable<IStrategoTerm> terms = tracingTermsCompletions(position, parseResult.ast(), new SourceRegion(position), runtime, termFactory, languageName, leftRecursiveTerms, rightRecursiveTerms);
final IStrategoAppl placeholder = getPlaceholder(position, terms);
final Iterable<IStrategoList> lists = getLists(terms, leftRecursiveTerms, rightRecursiveTerms);
final Iterable<IStrategoTerm> optionals = getOptionals(terms, leftRecursiveTerms, rightRecursiveTerms);
final Iterable<IStrategoTerm> leftRecursive = getLeftRecursiveTerms(position, terms, leftRecursiveTerms);
final Iterable<IStrategoTerm> rightRecursive = getRightRecursiveTerms(position, terms, rightRecursiveTerms);
if (placeholder != null) {
completions.addAll(placeholderCompletions(placeholder, languageName, component, location));
} else {
if (Iterables.size(lists) != 0) {
completions.addAll(listsCompletions(position, blankLineCompletion, lists, languageName, component, location));
}
if (Iterables.size(optionals) != 0) {
completions.addAll(optionalCompletions(optionals, blankLineCompletion, languageName, component, location));
}
// TODO Improve recursive completions
// if(Iterables.size(leftRecursive) != 0 || Iterables.size(rightRecursive) != 0) {
// completions
// .addAll(recursiveCompletions(leftRecursive, rightRecursive, languageName, component, location));
// }
}
}
return completions;
}
Aggregations