Search in sources :

Example 1 with ISourceLocation

use of org.metaborg.core.source.ISourceLocation in project spoofax by metaborg.

the class ResolverService method resolve.

private Resolution resolve(@Nullable TermWithRegion tuple) {
    if (tuple == null) {
        return null;
    }
    final IStrategoTerm output = tuple.term;
    final ISourceRegion offsetRegion = tuple.region;
    final Collection<ISourceLocation> targets = Lists.newLinkedList();
    if (output.getTermType() == IStrategoTerm.LIST) {
        for (IStrategoTerm subterm : output) {
            final ISourceLocation targetLocation = common.getTargetLocation(subterm);
            if (targetLocation == null) {
                logger.debug("Cannot get target location for {}", subterm);
                continue;
            }
            targets.add(targetLocation);
        }
    } else {
        final ISourceLocation targetLocation = common.getTargetLocation(output);
        if (targetLocation == null) {
            logger.debug("Reference resolution failed, cannot get target location for {}", output);
            return null;
        }
        targets.add(targetLocation);
    }
    if (targets.isEmpty()) {
        logger.debug("Reference resolution failed, cannot get target locations for {}", output);
        return null;
    }
    return new Resolution(offsetRegion, targets);
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ISourceRegion(org.metaborg.core.source.ISourceRegion) ISourceLocation(org.metaborg.core.source.ISourceLocation) Resolution(org.metaborg.core.tracing.Resolution)

Example 2 with ISourceLocation

use of org.metaborg.core.source.ISourceLocation in project spoofax by metaborg.

the class TracingService method tokenLocation.

private ISourceLocation tokenLocation(IStrategoTerm fragment) {
    final IToken left = ImploderAttachment.getLeftToken(fragment);
    final IToken right = ImploderAttachment.getRightToken(fragment);
    if (left == null || right == null) {
        return null;
    }
    final ISourceRegion region = JSGLRSourceRegionFactory.fromTokens(left, right);
    final FileObject resource = SourceAttachment.getResource(fragment, resourceService);
    return new SourceLocation(region, resource);
}
Also used : SourceLocation(org.metaborg.core.source.SourceLocation) ISourceLocation(org.metaborg.core.source.ISourceLocation) IToken(org.spoofax.jsglr.client.imploder.IToken) ISourceRegion(org.metaborg.core.source.ISourceRegion) FileObject(org.apache.commons.vfs2.FileObject)

Example 3 with ISourceLocation

use of org.metaborg.core.source.ISourceLocation in project spoofax by metaborg.

the class JSGLRCompletionService method fromTokens.

// TODO: Do these strategies need to be specific for a language
@Nullable
protected ISourceLocation fromTokens(IStrategoTerm fragment, HybridInterpreter runtime, ITermFactory termFactory, int position, String languageName, Map<IStrategoTerm, Boolean> leftRecursiveTerms, Map<IStrategoTerm, Boolean> rightRecursiveTerms) {
    final FileObject resource = SourceAttachment.getResource(fragment, resourceService);
    final IToken left = ImploderAttachment.getLeftToken(fragment);
    final IToken right = ImploderAttachment.getRightToken(fragment);
    ITokens tokenizer = ImploderAttachment.getTokenizer(fragment);
    IToken leftmostValid = left;
    IToken rightmostValid = right;
    boolean isList = (fragment instanceof IStrategoList) ? true : false;
    boolean isOptional = false;
    String sort = ImploderAttachment.getSort(fragment);
    IStrategoTerm input = termFactory.makeString(sort);
    boolean isLeftRecursive = false;
    if (fragment instanceof IStrategoAppl && position > right.getEndOffset()) {
        try {
            isLeftRecursive = strategoCommon.invoke(runtime, input, "is-left-recursive") != null;
        } catch (MetaborgException e) {
            logger.error("Failed to check recursivity for term {} of sort {} - syntactic completion not activated for this language, please import the completion stratego library", fragment, sort);
        }
    }
    boolean isRightRecursive = false;
    if (fragment instanceof IStrategoAppl && position <= left.getStartOffset()) {
        try {
            isRightRecursive = strategoCommon.invoke(runtime, input, "is-right-recursive") != null;
        } catch (MetaborgException e) {
            logger.error("Failed to check recursivity for term {} of sort {} - syntactic completion not activated for this language, please import the completion stratego library", fragment, sort);
        }
    }
    if (isLeftRecursive) {
        leftRecursiveTerms.put(fragment, true);
    }
    if (isRightRecursive) {
        rightRecursiveTerms.put(fragment, true);
    }
    if (left == null || right == null) {
        return null;
    }
    if (!isList && left == right && left.getEndOffset() < left.getStartOffset()) {
        isOptional = true;
    }
    // if it's a list or a node that is empty make the element includes the surrounding layout tokens
    if (left.getStartOffset() > right.getEndOffset() || isList || isOptional || (isLeftRecursive && isRightRecursive)) {
        for (int i = left.getIndex() - 1; i >= 0; i--) {
            if (tokenizer.getTokenAt(i).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(i).getKind() == IToken.TK_ERROR) {
                leftmostValid = tokenizer.getTokenAt(i);
            } else {
                break;
            }
        }
        for (int i = right.getIndex() + 1; i < tokenizer.getTokenCount(); i++) {
            if (tokenizer.getTokenAt(i).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(i).getKind() == IToken.TK_EOF || tokenizer.getTokenAt(i).getKind() == IToken.TK_ERROR) {
                rightmostValid = tokenizer.getTokenAt(i);
            } else {
                break;
            }
        }
    // if it is left recursive include the layout only on the right
    } else if (isLeftRecursive) {
        for (int i = left.getIndex(); i < right.getIndex(); i++) {
            if (tokenizer.getTokenAt(i).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(i).getKind() == IToken.TK_ERROR) {
                leftmostValid = tokenizer.getTokenAt(i + 1);
            } else {
                break;
            }
        }
        for (int i = right.getIndex() + 1; i < tokenizer.getTokenCount(); i++) {
            if (tokenizer.getTokenAt(i).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(i).getKind() == IToken.TK_EOF || tokenizer.getTokenAt(i).getKind() == IToken.TK_ERROR) {
                rightmostValid = tokenizer.getTokenAt(i);
            } else {
                break;
            }
        }
    // if it is right recursive include the layout only on the left
    } else if (isRightRecursive) {
        for (int i = left.getIndex() - 1; i >= 0; i--) {
            if (tokenizer.getTokenAt(i).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(i).getKind() == IToken.TK_ERROR) {
                leftmostValid = tokenizer.getTokenAt(i);
            } else {
                break;
            }
        }
        for (int i = right.getIndex(); i > left.getIndex(); i--) {
            if (tokenizer.getTokenAt(i).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(i).getKind() == IToken.TK_EOF || tokenizer.getTokenAt(i).getKind() == IToken.TK_ERROR) {
                rightmostValid = tokenizer.getTokenAt(i - 1);
            } else {
                break;
            }
        }
    } else // if not make it stripes the surrounding layout tokens
    {
        for (int i = left.getIndex(); i < right.getIndex(); i++) {
            if (tokenizer.getTokenAt(i).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(i).getKind() == IToken.TK_ERROR) {
                leftmostValid = tokenizer.getTokenAt(i + 1);
            } else {
                break;
            }
        }
        for (int i = right.getIndex(); i > left.getIndex(); i--) {
            if (tokenizer.getTokenAt(i).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(i).getKind() == IToken.TK_EOF || tokenizer.getTokenAt(i).getKind() == IToken.TK_ERROR) {
                rightmostValid = tokenizer.getTokenAt(i - 1);
            } else {
                break;
            }
        }
    }
    final ISourceRegion region = JSGLRSourceRegionFactory.fromTokensLayout(leftmostValid, rightmostValid, (isOptional || isList || isLeftRecursive || isRightRecursive));
    return new SourceLocation(region, resource);
}
Also used : SourceLocation(org.metaborg.core.source.SourceLocation) ISourceLocation(org.metaborg.core.source.ISourceLocation) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IToken(org.spoofax.jsglr.client.imploder.IToken) MetaborgException(org.metaborg.core.MetaborgException) ISourceRegion(org.metaborg.core.source.ISourceRegion) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) FileObject(org.apache.commons.vfs2.FileObject) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ITokens(org.spoofax.jsglr.client.imploder.ITokens) Nullable(javax.annotation.Nullable)

Example 4 with ISourceLocation

use of org.metaborg.core.source.ISourceLocation in project spoofax by metaborg.

the class JSGLRCompletionService method tracingTermsCompletions.

private Iterable<IStrategoTerm> tracingTermsCompletions(final int position, Object result, final ISourceRegion region, final HybridInterpreter runtime, final ITermFactory termFactory, final String languageName, final Map<IStrategoTerm, Boolean> leftRecursiveTerms, final Map<IStrategoTerm, Boolean> rightRecursiveTerms) {
    if (result == null || region == null) {
        return Iterables2.empty();
    }
    final Collection<IStrategoTerm> parsed = Lists.newLinkedList();
    final IStrategoTermVisitor visitor = new AStrategoTermVisitor() {

        @Override
        public boolean visit(IStrategoTerm term) {
            final ISourceLocation location = fromTokens(term, runtime, termFactory, position, languageName, leftRecursiveTerms, rightRecursiveTerms);
            if (location != null && location.region().contains(region)) {
                parsed.add(term);
                return false;
            }
            return true;
        }
    };
    StrategoTermVisitee.bottomup(visitor, (IStrategoTerm) result);
    return parsed;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) AStrategoTermVisitor(org.spoofax.terms.visitor.AStrategoTermVisitor) IStrategoTermVisitor(org.spoofax.terms.visitor.IStrategoTermVisitor) ISourceLocation(org.metaborg.core.source.ISourceLocation)

Example 5 with ISourceLocation

use of org.metaborg.core.source.ISourceLocation in project spoofax by metaborg.

the class AnalysisCommon method messages.

public Collection<IMessage> messages(FileObject resource, MessageSeverity severity, IStrategoTerm messagesTerm) {
    final Collection<IMessage> messages = Lists.newArrayListWithExpectedSize(messagesTerm.getSubtermCount());
    for (IStrategoTerm term : messagesTerm.getAllSubterms()) {
        final IStrategoTerm originTerm;
        final String message;
        if (term.getSubtermCount() == 2) {
            originTerm = term.getSubterm(0);
            message = toString(term.getSubterm(1));
        } else {
            originTerm = term;
            message = toString(term) + " (no tree node indicated)";
        }
        if (originTerm != null) {
            final ISourceLocation location = tracingService.location(originTerm);
            if (location != null) {
                final ISourceRegion region = location.region();
                messages.add(message(resource, region, message, severity));
            } else {
                messages.add(message(resource, message, severity));
            }
        } else {
            messages.add(message(resource, message, severity));
        }
    }
    return messages;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IMessage(org.metaborg.core.messages.IMessage) ISourceRegion(org.metaborg.core.source.ISourceRegion) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ISourceLocation(org.metaborg.core.source.ISourceLocation)

Aggregations

ISourceLocation (org.metaborg.core.source.ISourceLocation)9 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)8 ISourceRegion (org.metaborg.core.source.ISourceRegion)5 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)3 AStrategoTermVisitor (org.spoofax.terms.visitor.AStrategoTermVisitor)3 IStrategoTermVisitor (org.spoofax.terms.visitor.IStrategoTermVisitor)3 FileObject (org.apache.commons.vfs2.FileObject)2 IMessage (org.metaborg.core.messages.IMessage)2 SourceLocation (org.metaborg.core.source.SourceLocation)2 IToken (org.spoofax.jsglr.client.imploder.IToken)2 Nullable (javax.annotation.Nullable)1 MetaborgException (org.metaborg.core.MetaborgException)1 Resolution (org.metaborg.core.tracing.Resolution)1 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)1 IStrategoList (org.spoofax.interpreter.terms.IStrategoList)1 ITokens (org.spoofax.jsglr.client.imploder.ITokens)1 TermVisitor (org.spoofax.terms.TermVisitor)1