X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FMetaGrammar.java;h=900ee3f497112e11fe94c2c1f5027bb154c53c66;hp=26f8ae1712cdd015aad3a2904ded7d712f3b23eb;hb=fc28000b13f531d55e0509a596ce5ad96c63bbf2;hpb=d36f4e4e82c44906ed45f8184e231779bdd61232 diff --git a/src/edu/berkeley/sbp/misc/MetaGrammar.java b/src/edu/berkeley/sbp/misc/MetaGrammar.java index 26f8ae1..900ee3f 100644 --- a/src/edu/berkeley/sbp/misc/MetaGrammar.java +++ b/src/edu/berkeley/sbp/misc/MetaGrammar.java @@ -25,34 +25,71 @@ public class MetaGrammar extends ReflectiveWalker { public static Union epsilon = new Union("()"); static { epsilon.add(Sequence.empty); } + //////////////////////////////////////////////////////////////////////////////// + + private Union g; + private HashMap nt; + private int anon = 0; + private Element dws; + private String startSymbol; + + public MetaGrammar() { this("s"); } + public MetaGrammar(String s) { done(s); } + public Union done() { return done("s"); } + public Union done(String str) { + Union ret = g; + g = new Union(str); + startSymbol = str; + nt = new HashMap(); + dropAll = new HashSet(); + nt.put(str, g); + this.dws = Repeat.maximal(Repeat.many0(nonTerminal("w"))); + return ret; + } // MetaGrammar ////////////////////////////////////////////////////////////////////////////// - public Object _star_(Element r) { return new Rep(r, null, false, true); } + public PreSequence _equals__greater_(Object[] o, String s) { return new PreSequence(o, s); } + 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 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 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 Union nonTerminal(String s) { return nonTerminal(s, false, false); } - public Union nonTerminal(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 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 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 temp = new HashSet(); + for(PreSequence pre : s[i]) { + pre.not.addAll(seqs); + Sequence seq = pre.buildSequence(n, false, dropAll); + temp.add(seq); + n.add(seq); + } + seqs.addAll(temp); + } + return n; + } + + 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 _plus_(final Element r) { return Repeat.many1(r); } public Object[] _slash_(Object[] o, Object sep) { if (o.length <= 1) return o; Object[] ret = new Object[o.length * 2 - 1]; @@ -63,10 +100,11 @@ public class MetaGrammar extends ReflectiveWalker { } return ret; } - public Object _plus__slash_(final Element r, Object s) { return new Rep(r, (Element)s, false, false); } - public Object _star__slash_(final Element r, Object s) { return new Rep(r, (Element)s, false, true); } - public Object _star__star_(final Element r) { return new Rep(r, null, true, true); } - public Object _plus__plus_(final Element r) { return new Rep(r, null, true, false); } + + public Object _plus__slash_(final Element r, Element s) { return Repeat.many1(r, s); } + public Object _star__slash_(final Element r, Element s) { return Repeat.many0(r, s); } + public Object _star__star_(final Element r) { return Repeat.maximal(Repeat.many0(r)); } + public Object _plus__plus_(final Element r) { return Repeat.maximal(Repeat.many1(r)); } public Element _question_(final Element r) { return Repeat.maybe(r); } public MetaGrammar gram(Object o, MetaGrammar g, Object o2) { return g; } public MetaGrammar grammar(Object[] o) { return this; } @@ -87,79 +125,16 @@ public class MetaGrammar extends ReflectiveWalker { return CharToken.set(ret); } - - //////////////////////////////////////////////////////////////////////////////// - - private Union g; - private HashMap nt; - private int anon = 0; - private Element dws; - private String startSymbol; - - public MetaGrammar() { this("s"); } - public MetaGrammar(String s) { done(s); } - public Union done() { return done("s"); } - public Union done(String str) { - Union ret = g; - g = new Union(str); - startSymbol = str; - nt = new HashMap(); - dropAll = new HashSet(); - nt.put(str, g); - this.dws = Repeat.maximal(Repeat.many0(nonTerminal("w"))); - return ret; - } - - public Union nonTerminal(String str, PreSequence[][] s, boolean synthetic, boolean dropAll) { - Union n = nonTerminal(str, synthetic, dropAll); - if (s == null || s.length==0) { n.add(Sequence.empty); return n; } - HashSet seqs = new HashSet(); - for(int i=0; i temp = new HashSet(); - for(PreSequence pre : s[i]) { - pre.not.addAll(seqs); - Sequence seq = pre.buildSequence(n, false, dropAll); - temp.add(seq); - n.add(seq); - } - seqs.addAll(temp); - } - return n; - } - - public String stringify(String s) { return StringUtil.unescapify(s); } - public char unescape(char x, char c) { return unescape(c); } - public char unescape(char c) { return StringUtil.unescapify("\\"+c).charAt(0); } - public PreSequence sequence(Object[] o) { return new PreSequence(o, null); } - - public PreSequence _equals__greater_(Object[] o, String s) { return new PreSequence(o, s); } - public PreSequence wrap(Object[] o) { return new PreSequence(o, ""); } - public PreSequence mwrap(Object[] o) { return new PreSequence(o, ""); } + public PreSequence wrap(Object[] o) { return new PreSequence(o, ""); } public PreSequence rewrite(Object[] o) { return new PreSequence(o, null); } - public PreSequence rewrite(Object[] o, Object o2) { - Object[] o3 = new Object[o.length + 1]; - System.arraycopy(o, 0, o3, 0, o.length); - o3[o3.length-1] = o2; - return rewrite(o3); - } - - public static class PreBrace { - public final Object[] o; - public PreBrace(Object[] o) { this.o = o; } - } - public static class Rep { - private final Element e; - private final Element s; - private final boolean maximal; - private final boolean zero; - public Rep(Element e, Element s, boolean maximal, boolean zero) { this.e = e; this.s = s; this.zero = zero; this.maximal = maximal;} - public Element build() { - Element sep = s; - Element ret = zero ? Repeat.many0(e, sep) : Repeat.many1(e, sep); - return maximal ? Repeat.maximal(ret) : ret; - } + public static class MyLift { + public final String s; + public MyLift(String s) { this.s = s; } + } + public static class MyDrop { + public final Object o; + public MyDrop(Object o) { this.o = o; } } public static class PreSequence { @@ -182,11 +157,9 @@ public class MetaGrammar extends ReflectiveWalker { for(int i=0; i