use of org.spoofax.terms.ParseError in project spoofax by metaborg.
the class DialectIdentifier method identify.
@Override
public IdentifiedDialect identify(FileObject resource) throws MetaborgException {
final ILanguage strategoLanguage = languageService.getLanguage(SpoofaxConstants.LANG_STRATEGO_NAME);
if (strategoLanguage == null) {
final String message = logger.format("Could not find Stratego language, Stratego dialects cannot be identified for resource: {}", resource);
throw new MetaborgRuntimeException(message);
}
// GTODO: use identifier service instead, but that introduces a cyclic dependency. Could use a provider.
final ILanguageImpl strategoImpl = strategoLanguage.activeImpl();
if (strategoImpl == null) {
return null;
}
// HACK: assuming single identification facet
final IdentificationFacet facet = strategoImpl.facet(IdentificationFacet.class);
if (facet == null || !facet.identify(resource)) {
return null;
}
try {
final FileObject metaResource = metaResource(resource);
if (metaResource == null) {
return null;
}
final TermReader termReader = new TermReader(termFactoryService.getGeneric());
final IStrategoTerm term = termReader.parseFromStream(metaResource.getContent().getInputStream());
final String name = getSyntaxName(term.getSubterm(0));
if (name == null) {
return null;
}
final ILanguageImpl dialect = dialectService.getDialect(name);
if (dialect == null) {
final String message = String.format("Resource %s requires dialect %s, but that dialect does not exist", resource, name);
throw new MetaborgException(message);
}
final ILanguageImpl base = dialectService.getBase(dialect);
return new IdentifiedDialect(dialect, base);
} catch (ParseError | IOException e) {
throw new MetaborgException("Unable to open or parse .meta file", e);
}
}
use of org.spoofax.terms.ParseError in project spoofax by metaborg.
the class AbstractConstraintAnalyzer method getFlowSpecTransferFunctions.
protected Optional<TFFileInfo> getFlowSpecTransferFunctions(ILanguageComponent component) {
TFFileInfo transferFunctions = flowSpecTransferFunctionCache.get(component);
if (transferFunctions != null) {
return Optional.of(transferFunctions);
}
FileObject tfs = resourceService.resolve(component.location(), TRANSFER_FUNCTIONS_FILE);
try {
IStrategoTerm sTerm = termFactory.parseFromString(IOUtils.toString(tfs.getContent().getInputStream(), StandardCharsets.UTF_8));
ITerm term = strategoTerms.fromStratego(sTerm);
transferFunctions = TFFileInfo.match().match(term, PersistentUnifier.Immutable.of()).orElseThrow(() -> new ParseException("Parse error on reading the transfer function file"));
} catch (ParseError | ParseException | IOException e) {
logger.error("Could not read transfer functions file for {}", component);
return Optional.empty();
}
logger.debug("Caching flowspec transfer functions for language {}", component);
flowSpecTransferFunctionCache.put(component, transferFunctions);
return Optional.of(transferFunctions);
}
use of org.spoofax.terms.ParseError in project spoofax by metaborg.
the class LanguageComponentFactory method request.
private IComponentCreationConfigRequest request(FileObject root) throws MetaborgException {
final Collection<String> errors = Lists.newLinkedList();
final Collection<Throwable> exceptions = Lists.newLinkedList();
final ConfigRequest<ILanguageComponentConfig> configRequest = componentConfigService.get(root);
if (!configRequest.valid()) {
for (IMessage message : configRequest.errors()) {
errors.add(message.message());
final Throwable exception = message.exception();
if (exception != null) {
exceptions.add(exception);
}
}
}
final ILanguageComponentConfig config = configRequest.config();
if (config == null) {
final String message = logger.format("Cannot retrieve language component configuration at {}", root);
errors.add(message);
return new ComponentFactoryRequest(root, errors, exceptions);
}
final IStrategoAppl esvTerm;
try {
final FileObject esvFile = root.resolveFile("target/metaborg/editor.esv.af");
if (!esvFile.exists()) {
esvTerm = null;
} else {
esvTerm = esvTerm(root, esvFile);
}
} catch (ParseError | IOException | MetaborgException e) {
exceptions.add(e);
return new ComponentFactoryRequest(root, errors, exceptions);
}
SyntaxFacet syntaxFacet = null;
StrategoRuntimeFacet strategoRuntimeFacet = null;
if (esvTerm != null) {
try {
syntaxFacet = SyntaxFacetFromESV.create(esvTerm, root);
if (syntaxFacet != null) {
Iterables.addAll(errors, syntaxFacet.available());
}
} catch (FileSystemException e) {
exceptions.add(e);
}
try {
strategoRuntimeFacet = StrategoRuntimeFacetFromESV.create(esvTerm, root);
if (strategoRuntimeFacet != null) {
Iterables.addAll(errors, strategoRuntimeFacet.available(resourceService));
}
} catch (IOException e) {
exceptions.add(e);
}
}
final ComponentFactoryRequest request;
if (errors.isEmpty() && exceptions.isEmpty()) {
request = new ComponentFactoryRequest(root, config, esvTerm, syntaxFacet, strategoRuntimeFacet);
} else {
request = new ComponentFactoryRequest(root, errors, exceptions);
}
return request;
}
Aggregations