/** * Yields a parse tree (parsetree or appl) to a string builder. * * @author Martin Bravenboer */ public void yield(ATerm parsetree, StringBuilder builder) { ATermAppl appl = assertAppl(parsetree); if (appl.getAFun() == parsetreeFun) { appl = assertAppl(appl.getArgument(PARSE_TREE)); } yieldAppl(appl, builder); }
private static ATermAppl assertAppl(ATerm t, AFun fun) { ATermAppl result = assertAppl(t); if (result.getAFun() != fun) { throw new IllegalArgumentException( "Expected application of function " + fun + ": " + result.getAFun()); } return result; }
/** Private helper for the yield method. */ private void yieldAppl(ATermAppl appl, StringBuilder builder) { for (ATerm t : (ATermList) appl.getArgument(APPL_ARGS)) { if (t instanceof ATermAppl) { ATermAppl arg = (ATermAppl) t; if (arg.getAFun() == applFun) { yieldAppl(arg, builder); } else { throw new IllegalArgumentException("Don't know how to yield " + arg); } } else if (t instanceof ATermInt) { ATermInt arg = (ATermInt) t; builder.append((char) arg.getInt()); } else { throw new IllegalArgumentException("Don't know how to yield " + t); } } }
@Override public Visitable visitAppl(ATermAppl arg) throws VisitFailure { AFun fun = (AFun) arg.getAFun().accept(this); ATerm[] arguments = arg.getArgumentArray(); if (fun != arg.getAFun()) { arg = arg.getFactory().makeAppl(fun, arguments); } for (int i = 0; i < arguments.length; i++) { Visitable v = arguments[i].accept(this); if (v != arguments[i]) { arg = (ATermAppl) arg.setChildAt(i, v); } } return arg; }
/** * Given a production or application returns the constructor name attached to the production, or * null if there is no constructor. * * @author Martin Bravenboer * @author Lennart Kats */ public String getConstructor(ATerm arg) { ATermAppl appl = assertAppl(arg, applFun); ATermAppl prod; if (appl.getAFun() == prodFun) { prod = appl; } else if (appl.getAFun() == applFun) { prod = assertAppl(appl.getArgument(APPL_PROD), prodFun); } else { throw new IllegalArgumentException("Expected prod or appl: " + arg); } ATermAppl attrs = assertAppl(prod.getArgument(PROD_ATTRS)); if (attrs.getAFun() == noattrsFun) { return null; } else { for (ATerm attr : (ATermList) attrs.getChildAt(ATTRS_LIST)) { if (attr instanceof ATermAppl) { ATermAppl namedAttr = (ATermAppl) attr; if (namedAttr.getAFun() == termFun) { namedAttr = (ATermAppl) namedAttr.getArgument(TERM_CONS); if (namedAttr.getAFun() == consFun) { namedAttr = (ATermAppl) namedAttr.getArgument(CONS_NAME); return namedAttr.getName(); } } } } } return null; }