public static boolean hasType(IStrategoTerm t, int type) { return t.getTermType() == type; }
private static String traceToString(IStrategoList trace) { final StringBuilder sb = new StringBuilder(); sb.append("Stratego trace:"); final int depth = trace.getSubtermCount(); for(int i = 0; i < depth; i++) { final IStrategoTerm t = trace.getSubterm(depth - i - 1); sb.append("\n\t"); sb.append(t.getTermType() == IStrategoTerm.STRING ? Tools.asJavaString(t) : t); } return sb.toString(); }
public static String cons(IStrategoTerm t) { if(t == null || t.getTermType() != APPL) return null; return ((IStrategoAppl) t).getConstructor().getName(); } }
public static IStrategoAppl findTerm(IStrategoTerm term, String constructor) { if(term.getTermType() == IStrategoTerm.APPL && cons(term).equals(constructor)) return (IStrategoAppl) term; IStrategoTerm[] subterms = term.getAllSubterms(); for(int i = subterms.length - 1; i >= 0; i--) { IStrategoAppl result = findTerm(subterms[i], constructor); if(result != null) return result; } return null; }
private static void collectTerms(IStrategoTerm term, String constructor, ArrayList<IStrategoAppl> results) { if(term.getTermType() == IStrategoTerm.APPL && cons(term).equals(constructor)) results.add((IStrategoAppl) term); // TODO: optimize: use TermVisitor, avoid indexed access to long lists for(int i = 0; i < term.getSubtermCount(); i++) { collectTerms(termAt(term, i), constructor, results); } }
private @Nullable Style termStyle(IStrategoTerm term) { final int termType = term.getTermType(); if(termType != IStrategoTerm.APPL && termType != IStrategoTerm.TUPLE && termType != IStrategoTerm.LIST) { // Try to use the parent of terminal nodes, mimicking behavior of old Spoofax/IMP runtime. final IStrategoTerm parentTerm = ParentAttachment.getParent(term); if(parentTerm != null) { final Style style = sortConsStyle(parentTerm); if(style != null) { return style; } } } return sortConsStyle(term); }
@Nullable public static String termContents(IStrategoTerm t) { if(t == null) return null; String result; if(t.getTermType() == STRING) { result = asJavaString(t); } else if(t.getSubtermCount() == 1 && "Values".equals(tryGetName(t))) { return concatTermStrings(Tools.listAt(t, 0)); } else if(t.getTermType() == APPL && t.getSubtermCount() == 1 && termAt(t, 0).getTermType() == STRING) { result = asJavaString(termAt(t, 0)); } else if(t.getTermType() == APPL && t.getSubtermCount() == 1) { return termContents(termAt(t, 0)); } else { return null; } if(result.startsWith("\"") && result.endsWith("\"") && result.length() > 1) result = result.substring(1, result.length() - 1).replace("\\\\", "\""); return result; }
public static @Nullable IStrategoAppl read(PPath file) throws ParseError, IOException { final ITermFactory termFactory = StaticSpoofaxCoreFacade.spoofax().termFactoryService.getGeneric().getFactoryWithStorageType(IStrategoTerm.MUTABLE); final TermReader reader = new TermReader(termFactory); final IStrategoTerm term = reader.parseFromStream(file.inputStream()); if(term.getTermType() != IStrategoTerm.APPL) { return null; } return (IStrategoAppl) term; } }
if(term.getTermType() == IStrategoTerm.APPL) { final String cons = ((IStrategoAppl) term).getConstructor().getName(); if(rules.hasSortConsStyle(massagedSort, cons)) {