Search in sources :

Example 1 with ICategory

use of org.metaborg.core.style.ICategory in project spoofax by metaborg.

the class StylerService method style.

@Nullable
private IRegionStyle<IStrategoTerm> style(StylerFacet facet, IRegionCategory<IStrategoTerm> regionCategory) {
    if (regionCategory.region().length() == 0) {
        // Skip empty regions for styling.
        return null;
    }
    final ICategory category = regionCategory.category();
    // HACK: instanceof checks are nasty, but required since we do not have separate specifications for categories
    // and styles, they are intertwined.
    final IStyle style;
    if (category instanceof SortConsCategory) {
        final SortConsCategory cat = (SortConsCategory) category;
        style = facet.sortConsStyle(cat.sort, cat.cons);
    } else if (category instanceof ConsCategory) {
        final ConsCategory cat = (ConsCategory) category;
        style = facet.consStyle(cat.cons);
    } else if (category instanceof SortCategory) {
        final SortCategory cat = (SortCategory) category;
        style = facet.sortStyle(cat.sort);
    } else if (category instanceof TokenCategory) {
        final TokenCategory cat = (TokenCategory) category;
        style = facet.tokenStyle(cat.token);
    } else {
        style = null;
    }
    if (style == null) {
        logger.warn("Cannot determine style for category " + category.name());
        return null;
    }
    return new RegionStyle<>(regionCategory.region(), style, regionCategory.fragment());
}
Also used : IStyle(org.metaborg.core.style.IStyle) IRegionStyle(org.metaborg.core.style.IRegionStyle) RegionStyle(org.metaborg.core.style.RegionStyle) ICategory(org.metaborg.core.style.ICategory) Nullable(javax.annotation.Nullable)

Example 2 with ICategory

use of org.metaborg.core.style.ICategory 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 3 with ICategory

use of org.metaborg.core.style.ICategory in project spoofax by metaborg.

the class CategorizerService method category.

private ICategory category(StylerFacet facet, IToken token) {
    IStrategoTerm term = (IStrategoTerm) token.getAstNode();
    if (term == null) {
        return tokenCategory(token);
    }
    final int termType = term.getTermType();
    if (termType != IStrategoTerm.APPL && termType != IStrategoTerm.TUPLE && termType != IStrategoTerm.LIST) {
        // Try to use the parent of terminal nodes, mimicking behavior of old Spoofax/IMP runtime.
        final IStrategoTerm parentTerm = ParentAttachment.getParent(term);
        if (parentTerm != null) {
            final ICategory category = sortConsCategory(facet, parentTerm);
            if (category != null) {
                return category;
            }
        }
    }
    final ICategory category = sortConsCategory(facet, term);
    if (category != null) {
        return category;
    }
    return tokenCategory(token);
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ICategory(org.metaborg.core.style.ICategory)

Aggregations

ICategory (org.metaborg.core.style.ICategory)3 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)2 Nullable (javax.annotation.Nullable)1 ISourceRegion (org.metaborg.core.source.ISourceRegion)1 IRegionCategory (org.metaborg.core.style.IRegionCategory)1 IRegionStyle (org.metaborg.core.style.IRegionStyle)1 IStyle (org.metaborg.core.style.IStyle)1 RegionStyle (org.metaborg.core.style.RegionStyle)1 IToken (org.spoofax.jsglr.client.imploder.IToken)1 ITokens (org.spoofax.jsglr.client.imploder.ITokens)1 ImploderAttachment (org.spoofax.jsglr.client.imploder.ImploderAttachment)1