return new GrammarAST(includes, "").buildGrammar(grammarAST, startingNonterminal);
}
- public static Object illegalTag = ""; // this is the tag that should never appear in the non-dropped output FIXME
+ private static Object illegalTag = ""; // this is the tag that should never appear in the non-dropped output FIXME
+
+ // Instance //////////////////////////////////////////////////////////////////////////////
private final String prefix;
private final File[] includes;
}
return Reflection.lub(ret);
}
- public String stringifyChildren(Tree t) {
+ private String stringifyChildren(Tree t) {
StringBuffer sb = new StringBuffer();
for(int i=0; i<t.size(); i++) {
sb.append(t.child(i).head());
}
return sb.toString();
}
- public String unescape(Tree t) {
+ private String unescape(Tree t) {
StringBuffer sb = new StringBuffer();
for(int i=0; i<t.size(); i++)
sb.append(t.child(i).head()+stringifyChildren(t.child(i)));
return sb.toString();
}
- public Object walk(Tree t) {
+ private ElementNode walkElement(Tree t) { return (ElementNode)walk(t); }
+ private String walkString(Tree t) { return (String)walk(t); }
+ private Seq walkSeq(Tree t) { return (Seq)walk(t); }
+ private Object walk(Tree t) {
String head = (String)t.head();
while(head.indexOf('.') > 0)
head = head.substring(head.indexOf('.')+1);
if (head.equals("Grammar")) return new GrammarNode(walkChildren(t));
if (head.equals("(")) return new UnionNode((Seq[][])walkChildren(t.child(0)));
if (head.equals("Word")) return stringifyChildren(t);
- if (head.equals("Elements")) return seq2((ElementNode[])Reflection.rebuild(walkChildren(t), ElementNode[].class));
+ if (head.equals("Elements")) return new Seq((ElementNode[])Reflection.rebuild(walkChildren(t), ElementNode[].class));
if (head.equals("NonTerminalReference")) return new ReferenceNode(stringifyChildren(t.child(0)));
if (head.equals(")")) return new ReferenceNode(stringifyChildren(t.child(0)), true);
if (head.equals("{")) return new BracedNode(walkSeq(t.child(0)));
if (head.equals("\n")) return "\n";
if (head.equals("\r")) return "\r";
if (head.equals("grammar.Grammar")) return walkChildren(t);
- if (head.equals("SubGrammar")) return GrammarBuilder.buildFromAST(t.child(0), "s", includes);
+ if (head.equals("SubGrammar")) return GrammarAST.buildFromAST(t.child(0), "s", includes);
if (head.equals("NonTerminal"))
- return new NonTerminalNode((String)walk(t.child(0)),
+ return new NonTerminalNode(walkString(t.child(0)),
(Seq[][])walkChildren(t.child(1)), false, null, false);
if (head.equals("Colons")) {
- String tag = (String)walk(t.child(0));
+ String tag = walkString(t.child(0));
Seq[][] seqs = (Seq[][])walk(t.child(1));
for(Seq[] seq : seqs)
for(int i=0; i<seq.length; i++)
- seq[i] = tag(tag, seq[i]);
+ seq[i] = seq[i].tag(tag);
return new NonTerminalNode(tag, seqs, false, null, false);
}
if (head.equals("TestCase"))
File file = new File(f.getAbsolutePath()+File.separatorChar+fileName);
if (!file.exists()) continue;
try {
- String newPrefix = t.size()<2 ? "" : ((String)walk(t.child(1))+".");
+ String newPrefix = t.size()<2 ? "" : (walkString(t.child(1))+".");
FileInputStream fis = new FileInputStream(file);
Tree tr = new CharParser(MetaGrammar.newInstance()).parse(fis).expand1();
- return (GrammarNode)new GrammarBuilder(includes, newPrefix).walk(tr);
+ return (GrammarNode)new GrammarAST(includes, newPrefix).walk(tr);
} catch (Exception e) {
throw new RuntimeException("while parsing " + file, e);
}
}
throw new RuntimeException("unknown head: \"" + head + "\" => " + (head.equals("...")));
}
+
// Nodes //////////////////////////////////////////////////////////////////////////////
}
}
- public class UnionNode extends ElementNode {
+ private class UnionNode extends ElementNode {
public Seq[][] sequences;
public String sep = null;
public boolean rep;
+ 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 drop(Context cx) {
+ for(Seq[] seqs : sequences)
+ for(Seq seq : seqs)
+ if (!seq.drop(cx))
+ return false;
+ return true;
+ }
public Atom toAtom(Context cx) {
Atom ret = null;
for(Seq[] ss : sequences)
Union urep = null;
if (rep) {
urep = new Union(null, false);
- urep.add(Sequence.create(new Element[0], cnt.name));
+ 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));
Sequence s = Sequence.create(cnt.name,
new Element[] { u3, urep },
new boolean[] { false, false },
- true);
+ new boolean[] { false, true});
u2.add(s);
}
if (sequences.length==1) break;
}
}
- public class NonTerminalNode extends UnionNode {
+ private class NonTerminalNode extends UnionNode {
public boolean alwaysDrop;
public String name = null;
public boolean drop(Context cx) { return alwaysDrop; }
public Element build(Context cx, NonTerminalNode cnt, boolean dropall) { return cx.get(name); }
}
- public class Seq {
+ private class Seq {
public boolean alwaysDrop = false;
- public boolean drop(Context cx) { return alwaysDrop; }
+ public boolean drop(Context cx) {
+ if (alwaysDrop) return true;
+ if (tag!=null) return false;
+ for(int i=0; i<elements.length; i++)
+ if (!elements[i].drop(cx))
+ return false;
+ return true;
+ }
HashSet<Seq> and = new HashSet<Seq>();
HashSet<Seq> not = new HashSet<Seq>();
ElementNode[] elements;
ElementNode follow;
String tag = null;
public Seq(ElementNode e) { this(new ElementNode[] { e }); }
- public Seq(ElementNode[] elements) {
- this.elements = elements;
- for(int i=0; i<elements.length; i++)
+ public Seq(ElementNode[] elements) { this(elements, true); }
+ public Seq(ElementNode[] el, boolean check) {
+ this.elements = new ElementNode[el.length];
+ System.arraycopy(el, 0, elements, 0, el.length);
+ for(int i=0; i<elements.length; i++) {
if (elements[i]==null)
throw new RuntimeException();
+ elements[i].ownerSeq = this;
+ }
+ // FIXME: this whole mechanism is sketchy
+ if (check)
+ for(int i=0; i<elements.length; i++) {
+ if ((elements[i] instanceof ReferenceNode) && ((ReferenceNode)elements[i]).parenthesized) {
+ ReferenceNode rn = (ReferenceNode)elements[i];
+ ElementNode replace = null;
+ for(int j=0; j<elements.length; j++) {
+ if (!(elements[j] instanceof ReferenceNode)) continue;
+ ReferenceNode rn2 = (ReferenceNode)elements[j];
+ if (rn2.nonTerminal.equals(rn.nonTerminal) && !rn2.parenthesized) {
+ if (replace == null) {
+ replace = new UnionNode(new Seq(rn2).andnot(new Seq(elements, false)));
+ }
+ elements[j] = replace;
+ }
+ }
+ }
+ }
}
public Atom toAtom(Context cx) {
if (elements.length != 1)
public Seq follow(ElementNode follow) { this.follow = follow; return this; }
public Seq and(Seq s) { and.add(s); return this; }
public Seq andnot(Seq s) { not.add(s); return this; }
- public Seq dup() {
- Seq ret = new Seq(elements);
- ret.and.addAll(and);
- ret.not.addAll(not);
- ret.follow = follow;
- ret.tag = tag;
- return ret;
- }
public Seq separate(ElementNode sep) {
ElementNode[] elements = new ElementNode[this.elements.length * 2 - 1];
for(int i=0; i<this.elements.length; i++) {
}
}
- public class ReferenceNode extends ElementNode {
+ private class ReferenceNode extends ElementNode {
public String nonTerminal;
+ public boolean parenthesized;
public ReferenceNode() { }
+ public ReferenceNode(String nonTerminal) { this(nonTerminal, false); }
+ public ReferenceNode(String nonTerminal, boolean parenthesized) {
+ this.nonTerminal = nonTerminal.indexOf('.')==-1 ? (prefix + nonTerminal) : nonTerminal;
+ this.parenthesized = parenthesized;
+ }
public NonTerminalNode resolve(Context cx) {
NonTerminalNode ret = cx.grammar.get(nonTerminal);
if (ret==null) throw new RuntimeException("undefined nonterminal: " + nonTerminal);
return ret;
}
- public ReferenceNode(String nonTerminal) { this.nonTerminal = prefix + nonTerminal; }
- public Atom toAtom(Context cx) { return cx.grammar.get(nonTerminal).toAtom(cx); }
+ public Atom toAtom(Context cx) {
+ ElementNode ret = cx.grammar.get(nonTerminal);
+ if (ret == null) throw new RuntimeException("unknown nonterminal \""+nonTerminal+"\"");
+ return ret.toAtom(cx);
+ }
public boolean drop(Context cx) { return resolve(cx).drop(cx); }
public Element build(Context cx, NonTerminalNode cnt, boolean dropall) {
- if (!this.nonTerminal.startsWith(prefix)) nonTerminal = prefix + nonTerminal;
+ /*
+ if (!this.nonTerminal.startsWith(prefix))
+ nonTerminal = prefix + nonTerminal;
+ */
Element ret = cx.get(nonTerminal);
if (ret == null) throw new RuntimeException("unknown nonterminal \""+nonTerminal+"\"");
return ret;
}
}
- public class LiteralNode extends ElementNode {
+ private class LiteralNode extends ElementNode {
private String string;
private final String thePrefix = prefix;
private boolean caret;
public boolean drop(Context cx) { return true; }
public Atom toAtom(Context cx) {
if (string.length()!=1) return super.toAtom(cx);
- edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
+ Range.Set set = new Range.Set();
set.add(string.charAt(0), string.charAt(0));
return CharAtom.set(set);
}
public Element build(Context cx, NonTerminalNode cnt, boolean dropall) { return CharAtom.string(string); }
}
- public class AtomNode extends ElementNode {
- AtomNodeRange[] ranges;
- public AtomNode() { this(new AtomNodeRange[0]); }
- public AtomNode(AtomNodeRange[] ranges) { this.ranges = ranges; }
- public AtomNode(AtomNodeRange range) { this.ranges = new AtomNodeRange[] { range }; }
+ private class AtomNode extends ElementNode {
+ char[][] ranges;
+ public AtomNode() { this(new char[0][]); }
+ public AtomNode(char[][] ranges) { this.ranges = ranges; }
+ public AtomNode(char[] range) { this.ranges = new char[][] { range }; }
public Element build(Context cx, NonTerminalNode cnt, boolean dropall) { return toAtom(cx); }
public Atom toAtom(Context cx) {
- edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
- for(AtomNodeRange r : ranges) set.add(r.first, r.last);
+ Range.Set set = new Range.Set();
+ for(char[] r : ranges) set.add(r[0], r[1]);
return CharAtom.set(set);
}
}
- public class AtomNodeRange {
- public char first;
- public char last;
- public AtomNodeRange(char only) { first = only; last = only; }
- public AtomNodeRange(char first, char last) { this.first = first; this.last = last; }
- }
- public class RepeatNode extends ElementNode {
+ private class RepeatNode extends ElementNode {
public ElementNode e, sep;
public final boolean zero, many, max;
public RepeatNode(ElementNode e, ElementNode sep, boolean zero, boolean many, boolean max) {
}
}
- public abstract class ElementNode {
+ private abstract class ElementNode {
public boolean lifted = false;
+ public Seq ownerSeq = null;
public String getOwnerTag() { return null; }
+ public ElementNode lifted() { this.lifted = true; return this; }
public boolean drop(Context cx) { return false; }
public Atom toAtom(Context cx) { throw new Error("can't convert a " + this.getClass().getName() + " to an atom: " + this); }
public abstract Element build(Context cx, NonTerminalNode cnt, boolean dropall);
}
- public abstract class ElementNodeWrapper extends ElementNode {
+ private abstract class ElementNodeWrapper extends ElementNode {
protected ElementNode _e;
public ElementNodeWrapper(ElementNode e) { this._e = e; }
public String getOwnerTag() { return _e.getOwnerTag(); }
public Element build(Context cx, NonTerminalNode cnt, boolean dropall) { return _e.build(cx, cnt, dropall); }
}
- public class TildeNode extends ElementNodeWrapper {
+ private class TildeNode extends ElementNodeWrapper {
public TildeNode(ElementNode e) { super(e); }
public Atom toAtom(Context cx) { return (Atom)((Topology<Character>)_e.toAtom(cx).complement()); }
public Element build(Context cx, NonTerminalNode cnt, boolean dropall) { return toAtom(cx); }
}
- public class DropNode extends ElementNodeWrapper {
+ private class DropNode extends ElementNodeWrapper {
public DropNode(ElementNode e) { super(e); }
public boolean drop(Context cx) { return true; }
}
- public Seq and2(Seq s, Seq a) { a.alwaysDrop = true; return s.and(a); }
- public Seq andnot2(Seq s, Seq a) { a.alwaysDrop = true; return s.andnot(a); }
- public Seq arrow(Seq s, ElementNode e) { return s.follow(e); }
- public Seq tag(String tagname, Seq s) { return s.tag(tagname); }
- public Seq seq(ElementNode[] elements) { return new Seq(elements); }
- public Seq seq2(ElementNode[] elements) { return new Seq(elements); }
- public ElementNode plusmax(final ElementNode e) { return new RepeatNode(e, null, false, true, true); }
- public ElementNode plus(final ElementNode e) { return new RepeatNode(e, null, false, true, false); }
- public ElementNode plusmaxfollow(final ElementNode e, final ElementNode sep) { return new RepeatNode(e, sep, false, true, true); }
- public ElementNode plusfollow(final ElementNode e, final ElementNode sep) { return new RepeatNode(e, sep, false, true, false); }
- public ElementNode starmax(final ElementNode e) { return new RepeatNode(e, null, true, true, true); }
- public ElementNode star(final ElementNode e) { return new RepeatNode(e, null, true, true, false); }
- public ElementNode starmaxfollow(final ElementNode e, final ElementNode sep) { return new RepeatNode(e, sep, true, true, true); }
- public ElementNode starfollow(final ElementNode e, final ElementNode sep) { return new RepeatNode(e, sep, true, true, false); }
- public ElementNode question(final ElementNode e) { return new RepeatNode(e, null, true, false, false); }
+ // FIXME: doesn't this require a tag?
+ private class BracedNode extends ElementNode {
+ public Seq body;
+ public BracedNode(Seq seq) { this.body = seq; }
+ public Element build(Context cx, NonTerminalNode cnt, boolean dropall) {
+ Union u = new Union(null, false);
+ Sequence s = body.build(cx, u, null, dropall);
+ Union u2 = new Union(null, false);
+ u2.add(Sequence.create(new Element[] {
+ CharAtom.leftBrace,
+ u,
+ CharAtom.rightBrace
+ }, 1));
+ return u2;
+ }
+ }
+
+ public Seq and2(Seq s, Seq a) { a.alwaysDrop = true; return s.and(a); }
+ public Seq andnot2(Seq s, Seq a) { a.alwaysDrop = true; return s.andnot(a); }
//////////////////////////////////////////////////////////////////////////////