X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FMetaGrammar.java;h=cb67cba9a2b36e3ccf4a64173a85021defd9bf36;hp=eb061d039542c81ca5638f0c83526727ed1f0da4;hb=cb5151d11037144c14809f6516ce5c1f1976ef7f;hpb=590a67918a69bb23f63cef8cbfec46c031cd38d3 diff --git a/src/edu/berkeley/sbp/misc/MetaGrammar.java b/src/edu/berkeley/sbp/misc/MetaGrammar.java index eb061d0..cb67cba 100644 --- a/src/edu/berkeley/sbp/misc/MetaGrammar.java +++ b/src/edu/berkeley/sbp/misc/MetaGrammar.java @@ -25,78 +25,11 @@ public class MetaGrammar extends ReflectiveWalker { public static Union epsilon = new Union("()"); static { epsilon.add(Sequence.empty); } - // MetaGrammar ////////////////////////////////////////////////////////////////////////////// - - public Object _star_(Element r) { return new Rep(r, null, false, true); } - public Union _colon__colon__equals_(String s, PreSequence[][] p) { return nonTerminalZ(s, p); } - public Union _bang__colon__colon__equals_(String s, PreSequence[][] p) { return nonTerminalZ(s, p, true); } - public Union _colon__colon__equals_(boolean q, String s, PreSequence[][] p) { return nonTerminalZ(s, p, q); } - public Element epsilon(Object o, Object b) { return epsilon; } - public Element _rightparen_(Object e) { return SELF; } - - public PreSequence _amp_(PreSequence p, Object[] o) { return p.and(new PreSequence(o, null, true).buildSequence(null, true, false)); } - public PreSequence _amp__tilde_(PreSequence p, Object[] o) { return p.not(new PreSequence(o, null, true).buildSequence(null, true, false)); } - - public Element _bang_(Element r) { return r; } - public Object care(String s) { return new MyLift(s); } - public Object _caret_(String s) { return new MyLift(s); } - public Object _leftparen__rightparen_() { return epsilon; } - - public Object _leftparen_(PreSequence[][] p) { return nonTerminalZ(p); } - public Union nonTerminal(String s) { return nonTerminalX(s, false, false); } - public Union nonTerminalX(String s, boolean synthetic, boolean dropAll) { - Union n = s.equals(startSymbol) ? g : nt.get(s); - if (n == null) nt.put(s, n = new Union(s, synthetic)); - if (dropAll) this.dropAll.add(n); - return n; - } - public Union nonTerminalZ(PreSequence[][] p) { return nonTerminalX("anon"+(anon++), p, false, false); } - public Union nonTerminalZ(String s, PreSequence[][] p) { return nonTerminalX(s, p, false, false); } - public Union nonTerminalZ(String s, PreSequence[][] p, boolean q) { return nonTerminalX(s, p, false, q); } - public Object _backslash__leftbrace_(String s) { return SELF; } - public Object _leftbrace_(String s) { return SELF; } - public Object _plus_(final Element r) { return new Rep(r, null, false, false); } - public Object[] _slash_(Object[] o, Object sep) { - if (o.length <= 1) return o; - Object[] ret = new Object[o.length * 2 - 1]; - for(int i=0; i nt; private int anon = 0; - private Element dws; private String startSymbol; public MetaGrammar() { this("s"); } @@ -109,13 +42,30 @@ public class MetaGrammar extends ReflectiveWalker { nt = new HashMap(); dropAll = new HashSet(); nt.put(str, g); - this.dws = Repeat.maximal(Repeat.many0(nonTerminal("w"))); return ret; } - public Union nonTerminalX(String str, PreSequence[][] s, boolean synthetic, boolean dropAll) { - Union n = nonTerminalX(str, synthetic, dropAll); - if (s == null || s.length==0) { n.add(Sequence.empty); return n; } + + // MetaGrammar ////////////////////////////////////////////////////////////////////////////// + + public PreSequence _amp_(PreSequence p, Object[] o) { return p.and(new PreSequence(o, null, true).buildSequence(null, true, false)); } + public PreSequence _amp__tilde_(PreSequence p, Object[] o) { return p.not(new PreSequence(o, null, true).buildSequence(null, true, false)); } + public Object _star_(Element r) { return Repeat.many0(r); } + public Element epsilon(Object o, Object b) { return epsilon; } + public Element _rightparen_(Object e) { return SELF; } + public Element _bang_(Element r) { return r; } + public Object _caret_(String s) { return new MyLift(s); } + public Object _leftparen__rightparen_() { return epsilon; } + public Union nonTerminal(String s) { return nonTerminal(s, null, false, false); } + public Union _colon__colon__equals_(String s, PreSequence[][] p) { return nonTerminal(s, p, false, false); } + public Union _bang__colon__colon__equals_(String s, PreSequence[][] p) { return nonTerminal(s, p, false, true); } + public Union _colon__colon__equals_(boolean q, String s, PreSequence[][] p) { return nonTerminal(s, p, false, q); } + public Object _leftparen_(PreSequence[][] p) { return nonTerminal("anon"+(anon++), p, false, false); } + public Union nonTerminal(String str, PreSequence[][] s, boolean synthetic, boolean dropAll) { + Union n = str.equals(startSymbol) ? g : nt.get(str); + if (n == null) nt.put(str, n = new Union(str, synthetic)); + if (dropAll) this.dropAll.add(n); + if (s==null) return n; HashSet seqs = new HashSet(); for(int i=0; i set) { - if (o==null) return new Element[0]; - Element[] o2 = new Element[o.length]; - drops = new boolean[o.length]; - int j = 0; - for(int i=0; i set = new HashSet(); - Element[] expansion = expand(u, set); + Element[] o2 = o==null ? new Element[0] : new Element[o.length]; + if (o != null) { + drops = new boolean[o.length]; + int j = 0; + for(int i=0; i