use of org.metaborg.spoofax.core.unit.ISpoofaxInputUnit 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;
}
Aggregations