use of org.metaborg.core.style.IRegionCategory in project spoofax by metaborg.
the class CategorizerValidator method validate.
public static <T> Iterable<IRegionCategory<T>> validate(Iterable<IRegionCategory<T>> categorization) {
int offset = -1;
final Collection<IRegionCategory<T>> validated = Lists.newLinkedList();
for (IRegionCategory<T> regionCategory : categorization) {
final ISourceRegion region = regionCategory.region();
if (offset >= region.startOffset()) {
logger.warn("Invalid {}, starting offset is greater than offset in previous regions, " + "region category will be skipped", regionCategory);
} else if (offset >= region.endOffset()) {
logger.warn("Invalid {}, ending offset is greater than offset in previous regions, " + "region category will be skipped", regionCategory);
} else if (region.startOffset() > region.endOffset()) {
logger.warn("Invalid {}, starting offset is greater than ending offset, " + "region category will be skipped", regionCategory);
} else {
validated.add(regionCategory);
offset = regionCategory.region().endOffset();
}
}
return validated;
}
use of org.metaborg.core.style.IRegionCategory 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;
}
Aggregations