use of org.spoofax.interpreter.terms.IStrategoTerm 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;
}
use of org.spoofax.interpreter.terms.IStrategoTerm 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);
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class JSGLR1I method parse.
public ParseContrib parse(@Nullable JSGLRParserConfiguration parserConfig) throws IOException {
if (parserConfig == null) {
parserConfig = new JSGLRParserConfiguration();
}
final String fileName = resource != null ? resource.getName().getURI() : null;
final JSGLRParseErrorHandler errorHandler = new JSGLRParseErrorHandler(this, resource, getParseTable(config.getParseTableProvider()).hasRecovers());
final Timer timer = new Timer(true);
SGLRParseResult result;
try {
// should throw a fatal, or return a non-null result
result = actuallyParse(input, fileName, parserConfig);
assert result != null;
} catch (SGLRException | InterruptedException e) {
result = null;
errorHandler.setRecoveryFailed(parserConfig.recovery);
errorHandler.processFatalException(new NullTokenizer(input, fileName), e);
}
final long duration = timer.stop();
final IStrategoTerm ast;
if (result != null) {
// No fatals occurred, so either parsing succeeded or recovery succeeded
ast = (IStrategoTerm) result.output;
if (ast == null) {
// so we have nothing to do
assert parser.getTreeBuilder() instanceof NullTreeBuilder;
} else {
// in case recovery was required, collect the recoverable errors
errorHandler.setRecoveryFailed(false);
errorHandler.gatherNonFatalErrors(ast);
if (resource != null) {
SourceAttachment.putSource(ast, resource);
}
}
} else {
ast = null;
}
final boolean hasAst = ast != null;
final Iterable<IMessage> messages = errorHandler.messages();
final boolean hasErrors = MessageUtils.containsSeverity(messages, MessageSeverity.ERROR);
return new ParseContrib(hasAst, hasAst && !hasErrors, ast, messages, duration);
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class JSGLR2I method parse.
public ParseContrib parse(@Nullable JSGLRParserConfiguration parserConfig) throws IOException {
if (parserConfig == null) {
parserConfig = new JSGLRParserConfiguration();
}
final String fileName = resource != null ? resource.getName().getURI() : null;
String startSymbol = getOrDefaultStartSymbol(parserConfig);
final Timer timer = new Timer(true);
final IStrategoTerm ast = parser.parse(input, fileName, startSymbol);
final long duration = timer.stop();
final boolean hasAst = ast != null;
final boolean hasErrors = ast == null;
final Iterable<IMessage> messages;
if (hasErrors)
messages = Collections.singletonList(MessageFactory.newParseErrorAtTop(resource, "Invalid syntax", null));
else
messages = Collections.emptyList();
return new ParseContrib(hasAst, hasAst && !hasErrors, ast, messages, duration);
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project nabl by metaborg.
the class ScopeGraphContextPrimitive method call.
@Override
public final boolean call(IContext env, Strategy[] svars, IStrategoTerm[] tvars) throws InterpreterException {
final Object contextObj = env.contextObject();
if (contextObj == null) {
logger.warn("Context is null.");
return false;
}
if (!(contextObj instanceof IScopeGraphContext)) {
throw new InterpreterException("Context does not implement IScopeGraphContext");
}
final IScopeGraphContext<?> context = (IScopeGraphContext<?>) env.contextObject();
List<IStrategoTerm> termArgs = Arrays.asList(tvars);
Optional<? extends IStrategoTerm> result;
try (IClosableLock lock = context.guard()) {
result = call(context, env.current(), termArgs, env.getFactory());
}
return result.map(t -> {
env.setCurrent(t);
return true;
}).orElse(false);
}
Aggregations