Search in sources :

Example 26 with ILanguageImpl

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);
}
Also used : ISpoofaxAnalyzeResults(org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzeResults) SpoofaxAnalyzeResults(org.metaborg.spoofax.core.analysis.SpoofaxAnalyzeResults) ISpoofaxParseUnit(org.metaborg.spoofax.core.unit.ISpoofaxParseUnit) MetaborgException(org.metaborg.core.MetaborgException) HybridInterpreter(org.strategoxt.HybridInterpreter) AnalysisFacet(org.metaborg.spoofax.core.analysis.AnalysisFacet) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) AnalysisException(org.metaborg.core.analysis.AnalysisException)

Example 27 with ILanguageImpl

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);
}
Also used : ILanguageImpl(org.metaborg.core.language.ILanguageImpl) MetaborgException(org.metaborg.core.MetaborgException) AnalysisException(org.metaborg.core.analysis.AnalysisException) HybridInterpreter(org.strategoxt.HybridInterpreter) SpoofaxAnalyzeResult(org.metaborg.spoofax.core.analysis.SpoofaxAnalyzeResult) ISpoofaxAnalyzeResult(org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzeResult) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) AnalysisFacet(org.metaborg.spoofax.core.analysis.AnalysisFacet) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) ISpoofaxAnalyzeUnit(org.metaborg.spoofax.core.unit.ISpoofaxAnalyzeUnit)

Example 28 with ILanguageImpl

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);
}
Also used : ISpoofaxAnalyzeResults(org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzeResults) SpoofaxAnalyzeResults(org.metaborg.spoofax.core.analysis.SpoofaxAnalyzeResults) ISpoofaxParseUnit(org.metaborg.spoofax.core.unit.ISpoofaxParseUnit) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) MetaborgException(org.metaborg.core.MetaborgException) HybridInterpreter(org.strategoxt.HybridInterpreter) AnalysisException(org.metaborg.core.analysis.AnalysisException) AnalysisFacet(org.metaborg.spoofax.core.analysis.AnalysisFacet) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) ISpoofaxAnalyzeUnit(org.metaborg.spoofax.core.unit.ISpoofaxAnalyzeUnit)

Example 29 with ILanguageImpl

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;
}
Also used : ISpoofaxParseUnit(org.metaborg.spoofax.core.unit.ISpoofaxParseUnit) SyntaxFacet(org.metaborg.spoofax.core.syntax.SyntaxFacet) ICompletion(org.metaborg.core.completion.ICompletion) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) JSGLRParserConfiguration(org.metaborg.spoofax.core.syntax.JSGLRParserConfiguration) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) ISpoofaxInputUnit(org.metaborg.spoofax.core.unit.ISpoofaxInputUnit) FileObject(org.apache.commons.vfs2.FileObject)

Example 30 with ILanguageImpl

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;
}
Also used : ICompletion(org.metaborg.core.completion.ICompletion) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) HashMap(java.util.HashMap) HybridInterpreter(org.strategoxt.HybridInterpreter) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) SourceRegion(org.metaborg.core.source.SourceRegion) ISourceRegion(org.metaborg.core.source.ISourceRegion) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) FileObject(org.apache.commons.vfs2.FileObject) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Aggregations

ILanguageImpl (org.metaborg.core.language.ILanguageImpl)53 FileObject (org.apache.commons.vfs2.FileObject)32 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)24 MetaborgException (org.metaborg.core.MetaborgException)17 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)15 ILanguage (org.metaborg.core.language.ILanguage)14 HybridInterpreter (org.strategoxt.HybridInterpreter)13 LanguageIdentifier (org.metaborg.core.language.LanguageIdentifier)11 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)11 Test (org.junit.Test)10 IContext (org.metaborg.core.context.IContext)10 LanguageVersion (org.metaborg.core.language.LanguageVersion)10 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)9 MetaborgTest (org.metaborg.core.test.MetaborgTest)9 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)9 IProject (org.metaborg.core.project.IProject)8 Nullable (javax.annotation.Nullable)6 ISpoofaxParseUnit (org.metaborg.spoofax.core.unit.ISpoofaxParseUnit)6 IdentifiedResource (org.metaborg.core.language.IdentifiedResource)5 ISourceRegion (org.metaborg.core.source.ISourceRegion)5