use of org.spoofax.interpreter.core.InterpreterException in project spoofax by metaborg.
the class LegacyParseFilePrimitive method call.
@Override
public boolean call(IContext env, Strategy[] strategies, IStrategoTerm[] terms) throws InterpreterException {
if (!Tools.isTermString(terms[0]))
return false;
if (!Tools.isTermString(terms[3]))
return false;
try {
final String pathOrInput = Tools.asJavaString(terms[0]);
final String pathOrInput2 = Tools.asJavaString(terms[3]);
FileObject resource;
String text;
try {
resource = resourceService.resolve(pathOrInput);
if (!resource.exists() || resource.getType() != FileType.FILE) {
resource = resourceService.resolve(pathOrInput2);
text = pathOrInput;
} else {
text = sourceTextService.text(resource);
}
} catch (MetaborgRuntimeException | IOException e) {
resource = resourceService.resolve(pathOrInput2);
text = pathOrInput;
}
if (resource.getType() != FileType.FILE) {
return false;
}
final IdentifiedResource identifiedResource = languageIdentifierService.identifyToResource(resource);
if (identifiedResource == null) {
return false;
}
final ISpoofaxInputUnit input = unitService.inputUnit(resource, text, identifiedResource.language, identifiedResource.dialect);
final ISpoofaxParseUnit result = syntaxService.parse(input);
if (result.valid() && result.success()) {
env.setCurrent(result.ast());
} else {
return false;
}
} catch (ParseException | IOException e) {
throw new InterpreterException("Parsing failed unexpectedly", e);
}
return true;
}
use of org.spoofax.interpreter.core.InterpreterException 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);
}
use of org.spoofax.interpreter.core.InterpreterException in project nabl by metaborg.
the class SG_get_resource_analysis method call.
@Override
public Optional<? extends IStrategoTerm> call(IScopeGraphContext<?> context, IStrategoTerm sterm, List<IStrategoTerm> sterms, ITermFactory factory) throws InterpreterException {
if (!Tools.isTermString(sterm)) {
throw new InterpreterException("Expect a resource path.");
}
String resource = Tools.asJavaString(sterm);
final IScopeGraphUnit unit = context.unit(resource);
return Optional.of(new StrategoBlob(unit));
}
Aggregations