X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmeta%2FMetaGrammarBindings.java;h=667b6a6a7e0ca29e76390e0912199f5e969ef065;hp=742918be276d99ebe85ae3157ceaf93f423e57af;hb=52a3b053b79694ac0b714c4853c870cfe33a1ed1;hpb=1e8506b9723a432c2c010b0f91b17cd0b68f6f68 diff --git a/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java b/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java index 742918b..667b6a6 100644 --- a/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java +++ b/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java @@ -10,11 +10,13 @@ import java.lang.reflect.*; import java.io.*; /** The java classes typically used to represent a parsed grammar AST; each inner class is a type of AST node. */ -public class MetaGrammarBindings { +public class MetaGrammarBindings extends AnnotationGrammarBindings { + + public MetaGrammarBindings() { super(MetaGrammarBindings.class); } // FIXME ugly ugly ugly scary dangerous public static String prefix = ""; - + /** A grammar (a set of nonterminals) */ public static class GrammarNode extends HashMap implements NonTerminalSource { public NonTerminalNode[] getNonTerminals() { @@ -49,7 +51,7 @@ public class MetaGrammarBindings { for(NonTerminalNode nt : values()) ret += nt + ", "; return ret + " ]"; } - public Union build(String s, GrammarBindingResolver rm) { + public Union build(String s, Grammar.Bindings rm) { Context cx = new Context(this,rm); Union u = null; for(MetaGrammarBindings.NonTerminalNode nt : values()) { @@ -87,9 +89,12 @@ public class MetaGrammarBindings { public static @bind.as("#import") GrammarNode poundimport(String fileName, String as) { if (as==null) as = ""; + else if ("".equals(as)) { } + else as = as +"."; + System.err.println("#import " + fileName + " as " + as); try { - Tree t = new CharParser(MetaGrammar.make()).parse(new FileInputStream(fileName)).expand1(); + Tree t = new CharParser(MetaGrammar.newInstance()).parse(new FileInputStream("tests/"+fileName)).expand1(); Tree.TreeFunctor red = (Tree.TreeFunctor)t.head(); String oldprefix = prefix; prefix = as; @@ -151,10 +156,11 @@ public class MetaGrammarBindings { } } } - public static @bind.as("=") NonTerminalNode go(@bind.arg String name, @bind.arg Seq[][] sequences) { return new NonTerminalNode(name, sequences, true); } + + public static @bind.as("=") NonTerminalNode go(@bind.arg String name, @bind.arg Seq[][] sequences) { + return new NonTerminalNode(name, sequences, true); } public static @bind.as("=") NonTerminalNode go(@bind.arg String name, @bind.arg String sep, @bind.arg Seq[][] sequences) { - return new NonTerminalNode(name, sequences, true, sep); - } + return new NonTerminalNode(name, sequences, true, sep); } public static class AnonUnionNode extends UnionNode { public @bind.as("(") AnonUnionNode(Seq[][] sequences) { @@ -174,31 +180,6 @@ public class MetaGrammarBindings { public char last; } - public static abstract class ElementNode { - public String getLabel() { return null; } - public String getOwnerTag() { return null; } - public boolean drop() { return false; } - public abstract Element build(Context cx, NonTerminalNode cnt); - } - - public static class Drop extends ElementNode { - public ElementNode e; - public Drop(ElementNode e) { this.e = e; } - public String getLabel() { return null; } - public boolean drop() { return true; } - public String getOwnerTag() { return e.getOwnerTag(); } - public Element build(Context cx, NonTerminalNode cnt) { return e.build(cx, cnt); } - } - - public static class Label extends ElementNode { - public String label; - public ElementNode e; - public Label(String label, ElementNode e) { this.e = e; this.label = label; } - public String getLabel() { return label; } - public String getOwnerTag() { return e.getOwnerTag(); } - public Element build(Context cx, NonTerminalNode cnt) { return e.build(cx, cnt); } - } - public static /*abstract*/ class Seq { HashSet and = new HashSet(); HashSet not = new HashSet(); @@ -214,14 +195,14 @@ public class MetaGrammarBindings { } public Seq(ElementNode e) { this(new ElementNode[] { e }); } public Seq(ElementNode[] elements) { this.elements = elements; } - public Seq tag(String tag) { this.tag = tag; return this; } + public Seq tag(String tag) { this.tag = prefix+tag; return this; } public Seq follow(ElementNode follow) { this.follow = follow; return this; } public Seq dup() { Seq ret = new Seq(elements); ret.and.addAll(and); ret.not.addAll(not); ret.follow = follow; - ret.tag = tag; + ret.tag = prefix+tag; return ret; } public Seq and(Seq s) { and.add(s); s.lame = true; return this; } @@ -246,7 +227,7 @@ public class MetaGrammarBindings { } public Sequence build0(Context cx, boolean lame, NonTerminalNode cnt) { boolean dropAll = lame; - if (tag!=null && "()".equals(tag)) dropAll = true; + if (tag!=null && tag.endsWith("()")) dropAll = true; boolean[] drops = new boolean[elements.length]; Element[] els = new Element[elements.length]; for(int i=0; i map = new HashMap(); public GrammarNode grammar; public String cnt = null; - public GrammarBindingResolver rm; - public Context(GrammarNode g, GrammarBindingResolver rm) { + public Grammar.Bindings rm; + public Context(GrammarNode g, Grammar.Bindings rm) { this.grammar = g; this.rm = rm; } @@ -422,7 +394,7 @@ public class MetaGrammarBindings { } return ret; } - public Context(Tree t, GrammarBindingResolver rm) { + public Context(Tree t, Grammar.Bindings rm) { this.rm = rm; Tree.TreeFunctor red = (Tree.TreeFunctor)t.head(); this.grammar = (GrammarNode)red.invoke(t); @@ -449,4 +421,39 @@ public class MetaGrammarBindings { } + public static class Constant extends ElementNode { + Element constant; + public Constant(Element constant) { this.constant = constant; } + public Element build(Context cx, NonTerminalNode cnt) { return constant; } + } + public abstract static class PostProcess extends ElementNode { + ElementNode e; + public PostProcess(ElementNode e) { this.e = e; } + public Element build(Context cx, NonTerminalNode cnt) { return postProcess(e.build(cx, cnt)); } + public abstract Element postProcess(Element e); + } + public static abstract class ElementNode { + public String getLabel() { return null; } + public String getOwnerTag() { return null; } + public boolean drop() { return false; } + public abstract Element build(Context cx, NonTerminalNode cnt); + } + + public static class Drop extends ElementNode { + public ElementNode e; + public Drop(ElementNode e) { this.e = e; } + public String getLabel() { return null; } + public boolean drop() { return true; } + public String getOwnerTag() { return e.getOwnerTag(); } + public Element build(Context cx, NonTerminalNode cnt) { return e.build(cx, cnt); } + } + + public static class Label extends ElementNode { + public String label; + public ElementNode e; + public Label(String label, ElementNode e) { this.e = e; this.label = label; } + public String getLabel() { return label; } + public String getOwnerTag() { return e.getOwnerTag(); } + public Element build(Context cx, NonTerminalNode cnt) { return e.build(cx, cnt); } + } }