+ /** a node in the AST which is resolved into an Element */
+ private abstract class ElementNode {
+ /** the field name to be used when synthesizing AST classes; null if none suggested */
+ public String getFieldName() { return null; }
+ public boolean isLifted() { return false; }
+ public boolean isDropped(Context cx) { return false; }
+ //public abstract boolean isTagless();
+ public boolean isTagless() { return false; }
+ public void _emitCode(EmitContext cx,
+ StringBuffer fieldDeclarations,
+ StringBuffer walkCode) {
+ throw new RuntimeException("not implemented " + this.getClass().getName());
+ }
+ public final void emitCode(EmitContext cx,
+ StringBuffer fieldDeclarations,
+ StringBuffer walkCode) {
+ if (isDropped(cx)) return;
+ if (isTagless()) {
+ // parse just the literal text, create an int/float/char/string
+ // FIXME: how do we know which one?
+ walkCode.append(" stringify");
+ } else {
+ }
+ _emitCode(cx, fieldDeclarations, walkCode);
+ walkCode.append(" i++;");
+ }
+ public Atom toAtom(BuildContext cx) { throw new Error("can't convert a " + this.getClass().getName() + " to an atom: " + this); }
+ public abstract Element build(BuildContext cx, NonTerminalNode cnt, boolean dropall);
+ }
+
+ /** a union, produced by a ( .. | .. | .. ) construct */
+ private class UnionNode extends ElementNode {
+
+ /** each component of a union is a sequence */
+ public Seq[][] sequences;
+
+ /** if the union is a NonTerminal specified as Foo*=..., this is true */
+ public boolean rep;
+
+ /** if the union is a NonTerminal specified as Foo* /ws=..., then this is "ws" */
+ public String sep = null;
+
+ public UnionNode(Seq seq) { this(new Seq[][] { new Seq[] { seq } }); }
+ public UnionNode(Seq[][] sequences) { this(sequences, false, null); }
+ public UnionNode(Seq[][] sequences, boolean rep, String sep) {
+ this.sequences = sequences;
+ this.rep = rep;
+ this.sep = sep;
+ }
+
+ public boolean isTagless() {
+ for (Seq[] ss : sequences)
+ for (Seq s : ss)
+ if (!s.isTagless()) return false;
+ return true;
+ }
+
+ public String[] getPossibleEmitClasses() {
+ HashSet<String> cl = new HashSet<String> ();
+ for(Seq[] ss : sequences)
+ for(Seq s : ss) {
+ /*
+ String cls = s.getEmitClass();
+ if (cls != null) cl.add(cls);
+ */
+ }
+ return (String[])cl.toArray(new String[0]);
+ }
+
+ public void _emitCode(EmitContext cx,
+ StringBuffer fieldDeclarations,
+ StringBuffer walkCode) {
+ throw new RuntimeException("not implemented " + this.getClass().getName());
+ }
+
+ public String getFieldName() { return null; }
+ public boolean isLifted() { return false; }
+ public boolean isDropped(Context cx) {
+ for(Seq[] seqs : sequences)
+ for(Seq seq : seqs)
+ if (!seq.isDropped(cx))
+ return false;
+ return true;
+ }
+ public Atom toAtom(BuildContext cx) {
+ Atom ret = null;
+ for(Seq[] ss : sequences)
+ for(Seq s : ss)
+ ret = ret==null ? s.toAtom(cx) : (Atom)ret.union(s.toAtom(cx));
+ return ret;
+ }
+
+ public Element build(BuildContext cx, NonTerminalNode cnt, boolean dropall) {
+ return buildIntoPreallocatedUnion(cx, cnt, dropall, new Union(null, false)); }
+ public Element buildIntoPreallocatedUnion(BuildContext cx, NonTerminalNode cnt, boolean dropall, Union u) {
+ Union urep = null;
+ if (rep) {
+ urep = new Union(null, false);
+ urep.add(Sequence.create(cnt.name, new Element[0]));
+ urep.add(sep==null
+ ? Sequence.create(new Element[] { u }, 0)
+ : Sequence.create(new Element[] { cx.get(sep), u }, 1));
+ }
+ HashSet<Sequence> bad2 = new HashSet<Sequence>();
+ for(int i=0; i<sequences.length; i++) {
+ Seq[] group = sequences[i];
+ Union u2 = new Union(null, false);
+ if (sequences.length==1) u2 = u;
+ for(int j=0; j<group.length; j++)
+ if (!rep)
+ group[j].build(cx, u2, cnt, dropall);
+ else {
+ Union u3 = new Union(null, false);
+ group[j].build(cx, u3, cnt, dropall);
+ Sequence s = Sequence.create(cnt.name,
+ new Element[] { u3, urep },
+ new boolean[] { false, false },
+ new boolean[] { false, true});
+ u2.add(s);
+ }
+ if (sequences.length==1) break;
+ Sequence seq = Sequence.create(u2);
+ for(Sequence s : bad2) seq = seq.andnot(s);
+ u.add(seq);
+ bad2.add(Sequence.create(u2));
+ }
+ return u;
+ }
+ }
+