Search in sources :

Example 11 with ISourceRegion

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

the class CategorizerService method categorize.

@Override
public Iterable<IRegionCategory<IStrategoTerm>> categorize(ILanguageImpl language, ISpoofaxParseUnit parseResult) {
    final List<IRegionCategory<IStrategoTerm>> regionCategories = Lists.newLinkedList();
    if (!parseResult.valid()) {
        return regionCategories;
    }
    final StylerFacet facet = language.facet(StylerFacet.class);
    if (facet == null) {
        logger.error("Cannot categorize input of {}, it does not have a styler facet", language);
        // GTODO: throw exception instead
        return regionCategories;
    }
    final ImploderAttachment rootImploderAttachment = ImploderAttachment.get(parseResult.ast());
    if (rootImploderAttachment == null) {
        logger.error("Cannot categorize input {} of {}, it does not have an imploder attachment", parseResult, language);
        // GTODO: throw exception instead
        return regionCategories;
    }
    final ITokens tokenizer = rootImploderAttachment.getLeftToken().getTokenizer();
    if (tokenizer == null) {
        logger.error("Cannot categorize input {} of {}, it does not have a tokenizer", parseResult, language);
        // GTODO: throw exception instead
        return regionCategories;
    }
    final int tokenCount = tokenizer.getTokenCount();
    int offset = -1;
    for (int i = 0; i < tokenCount; ++i) {
        final IToken token = tokenizer.getTokenAt(i);
        if (tokenizer.isAmbigous() && token.getStartOffset() < offset) {
            // In case of ambiguities, tokens inside the ambiguity are duplicated, ignore.
            continue;
        }
        if (token.getStartOffset() > token.getEndOffset()) {
            // Indicates an invalid region. Empty lists have regions like this.
            continue;
        }
        if (offset >= token.getStartOffset()) {
            // Duplicate region, skip.
            continue;
        }
        offset = token.getEndOffset();
        final ICategory category = category(facet, token);
        if (category != null) {
            final ISourceRegion region = JSGLRSourceRegionFactory.fromToken(token);
            final IStrategoTerm term = (IStrategoTerm) token.getAstNode();
            regionCategories.add(new RegionCategory<>(region, category, term));
        }
    }
    return regionCategories;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IToken(org.spoofax.jsglr.client.imploder.IToken) ICategory(org.metaborg.core.style.ICategory) ISourceRegion(org.metaborg.core.source.ISourceRegion) IRegionCategory(org.metaborg.core.style.IRegionCategory) ImploderAttachment(org.spoofax.jsglr.client.imploder.ImploderAttachment) ITokens(org.spoofax.jsglr.client.imploder.ITokens)

Example 12 with ISourceRegion

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

the class JSGLRParseErrorHandler method reportErrorNearOffset.

private void reportErrorNearOffset(ITokens tokenizer, int offset, String message) {
    final IToken errorToken = ((AbstractTokenizer) tokenizer).getErrorTokenOrAdjunct(offset);
    final ISourceRegion region = JSGLRSourceRegionFactory.fromTokens(errorToken, errorToken);
    reportErrorAtRegion(region, message);
}
Also used : AbstractTokenizer(org.spoofax.jsglr.client.imploder.AbstractTokenizer) IToken(org.spoofax.jsglr.client.imploder.IToken) ISourceRegion(org.metaborg.core.source.ISourceRegion)

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