X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FMetaGrammar.java;h=972d3a12d8ffd8b862d0b1069decb8b3d3c28a4c;hb=eda544585c2304faa82d249c4744fd5cecbf9211;hp=33b3b1172464dd93588888c80c1473f49ccdaaaf;hpb=45d799349e635f1a99e3974e4504a43d5a7aaf33;p=sbp.git diff --git a/src/edu/berkeley/sbp/misc/MetaGrammar.java b/src/edu/berkeley/sbp/misc/MetaGrammar.java index 33b3b11..972d3a1 100644 --- a/src/edu/berkeley/sbp/misc/MetaGrammar.java +++ b/src/edu/berkeley/sbp/misc/MetaGrammar.java @@ -7,6 +7,22 @@ import java.io.*; public class MetaGrammar extends StringWalker { + /** an atom which tracks the possible tokenset of some element, provided that element can only match single-token sequences */ + static class Infer extends Atom { + private final Element e; + public Infer(Element e) { this.e = e; } + public Topology top() { return (Topology)toAtom(e); } + public String toString() { return e.toString(); } + } + + /** an atom which tracks the inverse of some other atom */ + static class Invert extends Atom { + private final Atom a; + public Invert(Atom a) { this.a = a; } + public Topology top() { return a.complement(); } + public String toString() { return "~"+a; } + } + public static class Hack extends Atom { private final Atom a; static final Topology leftright = CharRange.rightBrace.union(CharRange.leftBrace); @@ -115,14 +131,14 @@ public class MetaGrammar extends StringWalker { if ("\\n".equals(head)) return new Character('\n'); else if ("\\r".equals(head)) return new Character('\r'); else if ("grammar".equals(head)) { for(Tree t : tree.children()) walk(t); return this; } - else if ("*".equals(head)) return Repeat.many0((Element)walk(tree.child(0))); - else if ("+".equals(head)) return Repeat.many1((Element)walk(tree.child(0))); - else if ("+/".equals(head)) return Repeat.many1((Element)walk(tree.child(0)), (Element)walk(tree.child(1))); - else if ("*/".equals(head)) return Repeat.many0((Element)walk(tree.child(0)), (Element)walk(tree.child(1))); - else if ("++/".equals(head)) return Repeat.maximal1((Element)walk(tree.child(0)), (Element)walk(tree.child(1))); - else if ("**".equals(head)) return Repeat.maximal0((Element)walk(tree.child(0))); - else if ("++".equals(head)) return Repeat.maximal1((Element)walk(tree.child(0))); - else if ("?".equals(head)) return Repeat.maybe((Element)walk(tree.child(0))); + else if ("*".equals(head)) return Sequence.many0((Element)walk(tree.child(0)), repeatTag()); + else if ("+".equals(head)) return Sequence.many1((Element)walk(tree.child(0)), repeatTag()); + else if ("+/".equals(head)) return Sequence.many1((Element)walk(tree.child(0)), (Element)walk(tree.child(1)), repeatTag()); + else if ("*/".equals(head)) return Sequence.many0((Element)walk(tree.child(0)), (Element)walk(tree.child(1)), repeatTag()); + else if ("++/".equals(head)) return Sequence.maximal1((Element)walk(tree.child(0)), (Element)walk(tree.child(1)), repeatTag()); + else if ("**".equals(head)) return Sequence.maximal0((Element)walk(tree.child(0)), repeatTag()); + else if ("++".equals(head)) return Sequence.maximal1((Element)walk(tree.child(0)), repeatTag()); + else if ("?".equals(head)) return Sequence.maybe((Element)walk(tree.child(0)), repeatTag()); else if ("!".equals(head)) { Element e = (Element)walk(tree.child(0)); dropAll.add(e); return e; } else if ("&".equals(head)) return ((Sequence)walk(tree,0)).and(sequence(walk(tree, 1), true)); else if ("and".equals(head)) return ((Sequence)walk(tree,0)).and(sequence(walk(tree, 1), true)); @@ -153,7 +169,7 @@ public class MetaGrammar extends StringWalker { else if ("->".equals(head)) { PreSequence p = (PreSequence)walk(tree, 0); p.noFollow = (Element)walk(tree, 1); return p; } else if ("/".equals(head)) return ((PreSequence)walk(tree, 0)).sparse((Element)walk(tree, 1)); else if (" /".equals(head)) return ((PreSequence)walk(tree, 0)).sparse((Element)walk(tree, 1)); - else if ("~".equals(head)) return new Hack(new Atom.Invert(new Atom.Infer((Element)walk(tree, 0)))); + else if ("~".equals(head)) return new Hack(new Invert(new Infer((Element)walk(tree, 0)))); else if ("ps".equals(head)) return new PreSequence((Object[])walk(tree,0), null); else if (":".equals(head)) { String s = string(tree.child(0)); @@ -186,6 +202,7 @@ public class MetaGrammar extends StringWalker { } public Object convertLabel(String label) { return label; } + public Object repeatTag() { return null; } public Object walk(String tag, Object[] argo) { if (argo.length==0) return super.walk(tag, argo); @@ -276,6 +293,7 @@ public class MetaGrammar extends StringWalker { for(int i=0; i