Search in sources :

Example 6 with ISourceRegion

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

the class StreamMessagePrinter method print.

@Override
public void print(IMessage message, boolean pardoned) {
    final StringBuilder sb = new StringBuilder();
    final MessageSeverity severity = message.severity();
    sb.append(severity);
    if (severity != MessageSeverity.NOTE && pardoned) {
        sb.append(" (pardoned)");
    }
    final FileObject source = message.source();
    final ISourceRegion region = message.region();
    if (source != null) {
        sb.append(" in ");
        sb.append(source.getName().getURI());
        if (region != null) {
            sb.append(":").append(region.startRow());
        }
        sb.append('\n');
    }
    if (printHighlight) {
        try {
            final String sourceText = sourceTextService.text(source);
            if (region != null) {
                final String affected = AffectedSourceHelper.affectedSourceText(message.region(), sourceText, "    ");
                if (affected != null) {
                    sb.append(affected);
                }
            }
        } catch (IOException e) {
        }
    }
    print(sb, message.message(), message.exception(), message.severity(), pardoned);
    switch(severity) {
        case NOTE:
            ++notes;
            break;
        case WARNING:
            if (pardoned) {
                ++warningsPardoned;
            } else {
                ++warnings;
            }
            break;
        case ERROR:
            if (pardoned) {
                ++errorsPardoned;
            } else {
                ++errors;
            }
            break;
    }
}
Also used : ISourceRegion(org.metaborg.core.source.ISourceRegion) FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException)

Example 7 with ISourceRegion

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

the class AbstractConstraintAnalyzer method message.

private IMessage message(ITerm originatingTerm, IMessageInfo messageInfo, MessageSeverity severity, IUnifier unifier, C context, FileObject defaultLocation) {
    Optional<TermOrigin> maybeOrigin = TermOrigin.get(originatingTerm);
    if (maybeOrigin.isPresent()) {
        TermOrigin origin = maybeOrigin.get();
        ISourceRegion region = JSGLRSourceRegionFactory.fromTokens(origin.getLeftToken(), origin.getRightToken());
        FileObject resource = resourceService.resolve(context.location(), origin.getResource());
        String message = messageInfo.getContent().apply(unifier::findRecursive).toString(prettyprint(context, resource(resource, context)));
        return MessageFactory.newAnalysisMessage(resource, region, message, severity, null);
    } else {
        String message = messageInfo.getContent().apply(unifier::findRecursive).toString(prettyprint(context, null));
        return MessageFactory.newAnalysisMessageAtTop(defaultLocation, message, severity, null);
    }
}
Also used : ISourceRegion(org.metaborg.core.source.ISourceRegion) FileObject(org.apache.commons.vfs2.FileObject) TermOrigin(mb.nabl2.stratego.TermOrigin)

Example 8 with ISourceRegion

use of org.metaborg.core.source.ISourceRegion 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 9 with ISourceRegion

use of org.metaborg.core.source.ISourceRegion 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)

Example 10 with ISourceRegion

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

the class AnalysisCommon method ambiguityMessages.

public Collection<IMessage> ambiguityMessages(final FileObject resource, IStrategoTerm ast) {
    final Collection<IMessage> messages = Lists.newLinkedList();
    final TermVisitor termVisitor = new TermVisitor() {

        private IStrategoTerm ambStart;

        @Override
        public void preVisit(IStrategoTerm term) {
            if (ambStart == null && "amb".equals(Term.tryGetName(term))) {
                final String text = "Fragment is ambiguous: " + ambToString(term);
                final ISourceLocation location = tracingService.location(term);
                if (location != null) {
                    final ISourceRegion region = location.region();
                    messages.add(message(resource, region, text, MessageSeverity.WARNING));
                } else {
                    messages.add(message(resource, text, MessageSeverity.WARNING));
                }
                ambStart = term;
            }
        }

        @Override
        public void postVisit(IStrategoTerm term) {
            if (term == ambStart) {
                ambStart = null;
            }
        }

        private String ambToString(IStrategoTerm amb) {
            final String result = amb.toString();
            return result.length() > 5000 ? result.substring(0, 5000) + "..." : result;
        }
    };
    termVisitor.visit(ast);
    return messages;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IMessage(org.metaborg.core.messages.IMessage) ISourceRegion(org.metaborg.core.source.ISourceRegion) TermVisitor(org.spoofax.terms.TermVisitor) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ISourceLocation(org.metaborg.core.source.ISourceLocation)

Aggregations

ISourceRegion (org.metaborg.core.source.ISourceRegion)12 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)7 FileObject (org.apache.commons.vfs2.FileObject)5 ISourceLocation (org.metaborg.core.source.ISourceLocation)5 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)4 IToken (org.spoofax.jsglr.client.imploder.IToken)4 Nullable (javax.annotation.Nullable)2 IMessage (org.metaborg.core.messages.IMessage)2 SourceLocation (org.metaborg.core.source.SourceLocation)2 IRegionCategory (org.metaborg.core.style.IRegionCategory)2 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)2 ITokens (org.spoofax.jsglr.client.imploder.ITokens)2 IOException (java.io.IOException)1 TermOrigin (mb.nabl2.stratego.TermOrigin)1 MetaborgException (org.metaborg.core.MetaborgException)1 IOutlineNode (org.metaborg.core.outline.IOutlineNode)1 OutlineNode (org.metaborg.core.outline.OutlineNode)1 ICategory (org.metaborg.core.style.ICategory)1 Hover (org.metaborg.core.tracing.Hover)1 Resolution (org.metaborg.core.tracing.Resolution)1