X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FMetaGrammar.java;h=49befee1a7c20583f49a2ef78bdc78b06131e005;hp=2751db707fb12e7a8908ae363e2a52b69439ed8b;hb=4c5cadfb5604449d04e2cdaaae7b9a61795c9044;hpb=5fbd1d712d38b88153a824160322050da6c5e184 diff --git a/src/edu/berkeley/sbp/misc/MetaGrammar.java b/src/edu/berkeley/sbp/misc/MetaGrammar.java index 2751db7..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,10 +72,15 @@ public class MetaGrammar extends ReflectiveWalker { return n; } + public String string(Iterable> children) { + String ret = ""; + for(Tree t : children) ret += string(t); + return ret; + } 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; } @@ -122,19 +128,7 @@ public class MetaGrammar extends ReflectiveWalker { PreSequence ret = new PreSequence(o3, s); ret.drops[o1.length] = o3.length>1; return ret; - } else if ("{".equals(head)) - return nonTerminal("braced"+(anon++), - new PreSequence[][] { - new PreSequence[] { - new PreSequence(new Element[] { leftBrace(), - ((PreSequence)walk(tree, 0)).buildUnion(), - rightBrace() - }) - } - }, - false, - false); - else if ("[".equals(head) || "[~".equals(head)) { + } 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)); @@ -148,6 +142,13 @@ public class MetaGrammar extends ReflectiveWalker { } + 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); + } + ////////////////////////////////////////////////////////////////////////////// public class PreSequence {