use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class Typesmart method processModule.
private List<String> processModule(IStrategoTerm module) {
if (module == null) {
return Collections.emptyList();
}
assert ((IStrategoAppl) module).getName().equals("Module");
List<String> imports = new ArrayList<>();
IStrategoList decls = (IStrategoList) module.getSubterm(1);
for (IStrategoTerm decl : decls) {
String declName = ((IStrategoAppl) decl).getName();
if (declName.equals("Imports")) {
extractImports(decl.getSubterm(0), imports);
} else if (declName.equals("Signature")) {
processSignature(decl.getSubterm(0));
}
}
return imports;
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class Typesmart method processMainStrategoFile.
private void processMainStrategoFile(File strFile, List<File> strjIncludeDirs) throws IOException {
Set<String> seenImports = new HashSet<>();
Set<File> seenFiles = new HashSet<>();
LinkedList<String> todo = new LinkedList<>();
String basePath = context.baseDir.getAbsolutePath();
IStrategoTerm term = parseStratego(strFile);
todo.addAll(processModule(term));
boolean isRuntimeLibrary = strFile.getAbsolutePath().endsWith("org.metaborg.meta.lib.analysis/trans/runtime_libraries.str");
while (!todo.isEmpty()) {
String next = todo.pop();
if (!isRuntimeLibrary && next.startsWith("runtime/") || !seenImports.add(next)) {
continue;
}
Collection<File> files = findStrFiles(next, strjIncludeDirs);
if (files.isEmpty() && !next.startsWith("lib")) {
logger.warn("Could not extract typesmart info for unresolvable import " + next);
}
for (File file : files) {
if (file.getAbsolutePath().startsWith(basePath) && seenFiles.add(file)) {
// logger.debug("Entering module " + next);
term = parseStratego(file);
todo.addAll(processModule(term));
}
}
}
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class Typesmart method processSignature.
private void processSignature(IStrategoTerm sigDecls) {
for (IStrategoTerm decl : sigDecls) {
String declName = ((IStrategoAppl) decl).getName();
if (!declName.equals("Constructors")) {
continue;
}
next_constr: for (IStrategoTerm constr : decl.getSubterm(0)) {
String kind = ((IStrategoAppl) constr).getName();
String cname;
IStrategoAppl typeTerm;
if (kind.equals("OpDeclInj") || kind.equals("ExtOpDeclInj")) {
cname = "";
typeTerm = (IStrategoAppl) constr.getSubterm(0);
} else {
cname = ((IStrategoString) constr.getSubterm(0)).stringValue();
typeTerm = (IStrategoAppl) constr.getSubterm(1);
}
List<SortType> sortTypes;
if (typeTerm.getName().equals("ConstType")) {
// no constructor arguments
sortTypes = new ArrayList<>(1);
SortType t = extractSortType(typeTerm.getSubterm(0));
if (t == null) {
continue next_constr;
}
sortTypes.add(t);
} else if (typeTerm.getName().equals("FunType")) {
IStrategoTerm[] argTypes = typeTerm.getSubterm(0).getAllSubterms();
sortTypes = new ArrayList<>(argTypes.length + 1);
for (IStrategoTerm argType : argTypes) {
SortType t = extractSortType(argType.getSubterm(0));
if (t == null) {
continue next_constr;
}
sortTypes.add(t);
}
SortType t = extractSortType(typeTerm.getSubterm(1).getSubterm(0));
if (t == null) {
continue next_constr;
}
sortTypes.add(t);
} else {
throw new IllegalArgumentException("Found constructor declaration in unexpected format " + constr);
}
addConstructorSignature(cname, sortTypes);
}
}
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class SpoofaxContext method parse.
@Nullable
public IStrategoTerm parse(File file) throws IOException, ParseException {
final FileObject resource = resourceService.resolve(file);
final ILanguageImpl language = languageIdentifierService.identify(resource);
if (language == null) {
return null;
}
final String text = sourceTextService.text(resource);
final ISpoofaxInputUnit inputUnit = unitService.inputUnit(resource, text, language, null);
final ISpoofaxParseUnit result = syntaxService.parse(inputUnit);
if (!result.valid() || !result.success()) {
return null;
}
final IStrategoTerm term = result.ast();
return term;
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class Sdf2RtgStamper method stampOf.
@Override
public Stamp stampOf(File file) {
if (!FileCommands.exists(file)) {
return new ValueStamp<>(this, null);
}
final IStrategoTerm term;
try {
term = context.parse(file);
} catch (ParseException | IOException e) {
return LastModifiedStamper.instance.stampOf(file);
}
if (term == null) {
return LastModifiedStamper.instance.stampOf(file);
}
final Deliteralize deliteralize = new Deliteralize(context.termFactory(), false);
final IStrategoTerm delit = deliteralize.transform(term);
return new ValueStamp<>(this, delit);
}
Aggregations