X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FMetaGrammar.java;h=49befee1a7c20583f49a2ef78bdc78b06131e005;hp=d03baafadaf2938edb169d81143f19d28e13b3bd;hb=6bee7f519f499a92a92a459fd2c903d85de0759c;hpb=e298db431295ba8870587daceaf6c584c9f07312 diff --git a/src/edu/berkeley/sbp/misc/MetaGrammar.java b/src/edu/berkeley/sbp/misc/MetaGrammar.java index d03baaf..49befee 100644 --- a/src/edu/berkeley/sbp/misc/MetaGrammar.java +++ b/src/edu/berkeley/sbp/misc/MetaGrammar.java @@ -4,7 +4,7 @@ import edu.berkeley.sbp.*; import java.util.*; import java.io.*; -public class MetaGrammar extends ReflectiveWalker { +public class MetaGrammar extends StringWalker { public static Union make() throws Exception { return ((MetaGrammar)new MetaGrammar().walk(meta)).done(); @@ -15,7 +15,7 @@ public class MetaGrammar extends ReflectiveWalker { return ret.toString(); } - private static HashSet dropAll = new HashSet(); + public /*private*/ static HashSet dropAll = new HashSet(); // Statics ////////////////////////////////////////////////////////////////////////////// @@ -51,6 +51,7 @@ public class MetaGrammar extends ReflectiveWalker { // MetaGrammar ////////////////////////////////////////////////////////////////////////////// + public Union nonTerminal(String str) { return nonTerminal(str, null, 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)); @@ -71,41 +72,15 @@ public class MetaGrammar extends ReflectiveWalker { return n; } - public Object _backslash__leftbrace_(String s) { return SELF; } - public Object _leftbrace_(PreSequence p) { - return nonTerminal("braced"+(anon++), - new PreSequence[][] { - new PreSequence[] { - new PreSequence(new Element[] { leftBrace(), - p.buildUnion(), - rightBrace() - }) - } - }, - false, - false); - } - - public PreSequence _equals__greater_(PreSequence p, String tag) { p.tag = tag; return p; } - - public PreSequence ps(Object[] o) { return new PreSequence(o); } - public PreSequence ps2(Object[] o1, String s, Object[] o2) { - if (o1==null) o1 = new Object[0]; - if (o2==null) o2 = new Object[0]; - Object[] o3 = new Object[o1.length + o2.length + 1]; - System.arraycopy(o1, 0, o3, 0, o1.length); - o3[o1.length] = string(s); - System.arraycopy(o2, 0, o3, o1.length+1, o2.length); - PreSequence ret = new PreSequence(o3, s); - ret.drops[o1.length] = o3.length>1; + public String string(Iterable> children) { + String ret = ""; + for(Tree t : children) ret += string(t); return ret; } - public PreSequence _slash_(PreSequence p, Object sep) { return p.sparse(sep); } - public String string(Tree tree) { String ret = ""; if (tree.head()!=null) ret += tree.head(); - for(Tree t : tree.children()) ret += string(t); + ret += string(tree.children()); return ret; } @@ -136,7 +111,24 @@ public class MetaGrammar extends ReflectiveWalker { else if ("literal".equals(head)) { Element ret = string(string(tree.child(0))); dropAll.add(ret); return ret; } else if ("-".equals(head)) return new Range(walk(tree, 0).toString().charAt(0), walk(tree,1).toString().charAt(0)); else if ("range".equals(head)) return new Range(walk(tree, 0).toString().charAt(0), walk(tree,0).toString().charAt(0)); - else if ("[".equals(head) || "[~".equals(head)) { + else if ("gram".equals(head)) return walk(tree, 1); + else if ("=>".equals(head)) { PreSequence p = (PreSequence)walk(tree, 0); p.tag = string(tree.child(1)); return p; } + else if ("/".equals(head)) return ((PreSequence)walk(tree, 0)).sparse((Element)walk(tree, 1)); + else if ("ps".equals(head)) return new PreSequence((Element[])walk(tree, 0)); + else if ("ps2".equals(head)) { + Object[] o1 = (Object[])walk(tree, 0); + String s = string(tree.child(1)); + Object[] o2 = (Object[])walk(tree, 2); + if (o1==null) o1 = new Object[0]; + if (o2==null) o2 = new Object[0]; + Object[] o3 = new Object[o1.length + o2.length + 1]; + System.arraycopy(o1, 0, o3, 0, o1.length); + o3[o1.length] = string(s); + System.arraycopy(o2, 0, o3, o1.length+1, o2.length); + PreSequence ret = new PreSequence(o3, s); + ret.drops[o1.length] = o3.length>1; + return ret; + } else if ("[".equals(head) || "[~".equals(head)) { boolean positive = "[".equals(head); Range[] rr = (Range[])walk(tree, 0); Range.Set ret = positive ? new Range.Set() : new Range.Set(new Range(true, true)); @@ -149,7 +141,13 @@ public class MetaGrammar extends ReflectiveWalker { else return super.walk(tree); } - public Object gram(Object o, Object g, Object o2) { return g; } + + public Object walk(String tag, Object[] argo) { + if (argo.length==0) return super.walk(tag, argo); + if (argo==null) return tag; + if (tag==null || "".equals(tag)) return argo; + return super.walk(tag, argo); + } //////////////////////////////////////////////////////////////////////////////