use of org.spoofax.terms.io.binary.TermReader in project spoofax by metaborg.
the class LanguageComponentFactory method esvTerm.
private IStrategoAppl esvTerm(FileObject location, FileObject esvFile) throws ParseError, IOException, MetaborgException {
final TermReader reader = new TermReader(termFactoryService.getGeneric().getFactoryWithStorageType(IStrategoTerm.MUTABLE));
final IStrategoTerm term = reader.parseFromStream(esvFile.getContent().getInputStream());
if (term.getTermType() != IStrategoTerm.APPL) {
final String message = logger.format("Cannot discover language at {}, ESV file at {} does not contain a valid ESV term", location, esvFile);
throw new MetaborgException(message);
}
return (IStrategoAppl) term;
}
use of org.spoofax.terms.io.binary.TermReader 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.io.binary.TermReader in project spoofax by metaborg.
the class JSGLR1FileParseTableProvider method parseTable.
@Override
public ParseTable parseTable() throws IOException {
if (parseTable != null) {
return parseTable;
}
resource.refresh();
if (!resource.exists()) {
throw new IOException("Could not load parse table from " + resource + ", file does not exist");
}
try (final InputStream stream = resource.getContent().getInputStream()) {
final TermReader termReader = new TermReader(termFactory);
IStrategoTerm parseTableTerm = termReader.parseFromStream(stream);
FileObject persistedTable = resource.getParent().resolveFile("table.bin");
if (persistedTable.exists()) {
parseTable = new ParseTable(parseTableTerm, termFactory, persistedTable);
} else {
parseTable = new ParseTable(parseTableTerm, termFactory);
}
} catch (Exception e) {
throw new IOException("Could not load parse table from " + resource, e);
}
return parseTable;
}
use of org.spoofax.terms.io.binary.TermReader in project spoofax by metaborg.
the class JSGLR1IncrementalParseTableProvider method parseTable.
@Override
public ParseTable parseTable() throws IOException {
if (parseTable != null) {
return parseTable;
}
resource.refresh();
if (!resource.exists()) {
throw new IOException("Could not load parse table from " + resource + ", file does not exist");
}
try (final InputStream stream = resource.getContent().getInputStream()) {
final TermReader termReader = new TermReader(termFactory);
final IStrategoTerm parseTableTerm = termReader.parseFromStream(stream);
FileObject persistedTable = resource.getParent().resolveFile("table.bin");
if (persistedTable.exists()) {
if (referenceTable != null) {
parseTable = new ParseTable(parseTableTerm, termFactory, persistedTable, referenceTable);
} else {
parseTable = new ParseTable(parseTableTerm, termFactory, persistedTable);
}
} else {
parseTable = new ParseTable(parseTableTerm, termFactory);
}
} catch (Exception e) {
throw new IOException("Could not load parse table from " + resource, e);
}
return parseTable;
}
use of org.spoofax.terms.io.binary.TermReader in project spoofax by metaborg.
the class JSGLR2FileParseTableProvider method parseTable.
@Override
public IParseTable parseTable() throws IOException {
if (parseTable != null) {
return parseTable;
}
resource.refresh();
if (!resource.exists()) {
throw new IOException("Could not load parse table from " + resource + ", file does not exist");
}
try (final InputStream stream = resource.getContent().getInputStream()) {
final TermReader termReader = new TermReader(termFactory);
IStrategoTerm parseTableTerm = termReader.parseFromStream(stream);
FileObject persistedTable = resource.getParent().resolveFile("table.bin");
if (!persistedTable.exists()) {
parseTable = new ParseTableReader(new CharacterClassFactory(true, true), new ActionsFactory(true), new StateFactory()).read(parseTableTerm);
} else {
parseTable = new ParseTableReader(new CharacterClassFactory(true, true), new ActionsFactory(true), new StateFactory()).read(parseTableTerm, persistedTable);
}
} catch (Exception e) {
throw new IOException("Could not load parse table from " + resource, e);
}
return parseTable;
}
Aggregations