cae7f13b6f62afb3f74872f2d831a176e183dd3d
[sbp.git] / src / edu / berkeley / sbp / meta / MetaGrammarBindings.java
1 package edu.berkeley.sbp.meta;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.chr.*;
5 import edu.berkeley.sbp.misc.*;
6 import edu.berkeley.sbp.bind.*;
7 import java.util.*;
8 import java.lang.annotation.*;
9 import java.lang.reflect.*;
10 import java.io.*;
11
12 /** The java classes typically used to represent a parsed grammar AST; each inner class is a type of AST node. */
13 public class MetaGrammarBindings {
14
15     /** A grammar (a set of nonterminals) */
16     public static class Grammar extends HashMap<String,NonTerminal> {
17         public @bind Grammar(NonTerminal[] nonterminals) {
18             for(NonTerminal nt : nonterminals) this.put(nt.name, nt); }
19         public String toString() {
20             String ret = "[ ";
21             for(NonTerminal nt : values()) ret += nt + ", ";
22             return ret + " ]";
23         }
24     }
25
26     public abstract static class UnionNode extends ElementNode {
27         public Seq[][] sequences;
28         public void build(MetaGrammar.Context cx, Union u) {
29             HashSet<Sequence> bad2 = new HashSet<Sequence>();
30             for(int i=0; i<sequences.length; i++) {
31                 Seq[] group = sequences[i];
32                 Union u2 = new Union();
33                 if (sequences.length==1) u2 = u;
34                 for(int j=0; j<group.length; j++) {
35                     group[j].build(cx, u2, false);
36                 }
37                 if (sequences.length==1) break;
38                 Sequence seq = Sequence.singleton(u2);
39                 for(Sequence s : bad2) {
40                     s.lame = true;
41                     seq = seq.not(s);
42                 }
43                 u.add(seq);
44                 bad2.add(Sequence.singleton(u2));
45             }
46         }
47     }
48
49     public static class NonTerminal extends UnionNode {
50         public boolean rep;
51         public String  name = null;
52         public String sep = null;
53         public @bind NonTerminal(@bind.arg String name, @bind.arg Seq[][] sequences) { this(name, sequences, false); }
54         public NonTerminal(String name, Seq[][] sequences, boolean rep) { this(name, sequences, rep, null); }
55         public NonTerminal(String name, Seq[][] sequences, boolean rep, String sep) {
56             this.name = name;
57             this.sequences = sequences;
58             this.rep = rep;
59             this.sep = sep;
60         }
61         public Element build(MetaGrammar.Context cx) { return cx.get(name); }
62         public void build(MetaGrammar.Context cx, Union u) {
63             if (!rep) { super.build(cx, u); return; }
64             HashSet<Sequence> bad2 = new HashSet<Sequence>();
65
66             Union urep = new Union();
67             urep.add(Sequence.empty);
68             urep.add(Sequence.singleton(new Element[] { cx.get(sep), u }, 1));
69
70             for(int i=0; i<sequences.length; i++) {
71                 Seq[] group = sequences[i];
72                 Union u2 = new Union();
73                 if (sequences.length==1) u2 = u;
74                 for(int j=0; j<group.length; j++) {
75                     Union u3 = new Union();
76                     group[j].build(cx, u3, false);
77                     Sequence s = Sequence.unwrap(new Element[] { u3, urep },
78                                                  cx.rm.repeatTag(),
79                                                  new boolean[] { false, false });
80                     u2.add(s);
81                 }
82                 if (sequences.length==1) break;
83                 Sequence seq = Sequence.singleton(u2);
84                 for(Sequence s : bad2) {
85                     s.lame = true;
86                     seq = seq.not(s);
87                 }
88                 u.add(seq);
89                 bad2.add(Sequence.singleton(u2));
90             }
91         }
92     }
93     public static @bind.as("=") NonTerminal go(@bind.arg String name, @bind.arg Seq[][] sequences) { return new NonTerminal(name, sequences, true); }
94     public static @bind.as("=") NonTerminal go(@bind.arg String name, @bind.arg String sep, @bind.arg Seq[][] sequences) {
95         return new NonTerminal(name, sequences, true, sep);
96     }
97
98     public static class AnonUnionNode extends UnionNode {
99         public @bind.as("(") AnonUnionNode(Seq[][] sequences) {
100             this.sequences = sequences;
101         }
102         public Element build(MetaGrammar.Context cx) {
103             Union ret = new Union();
104             build(cx, ret);
105             return ret;
106         }
107     }
108
109     public static class Range {
110         public @bind Range(char only) { first = only; last = only; }
111         public @bind Range(char first, char last) { this.first = first; this.last = last; }
112         public char first;
113         public char last;
114     }
115     public static abstract class ElementNode {
116         public String getLabel() { return null; }
117         public String getOwnerTag() { return null; }
118         public boolean drop() { return false; }
119         public abstract Element build(MetaGrammar.Context cx);
120     }
121     public static class Drop extends ElementNode {
122         public ElementNode e;
123         public Drop(ElementNode e) { this.e = e; }
124         public String getLabel() { return null; }
125         public boolean drop() { return true; }
126         public String getOwnerTag() { return e.getOwnerTag(); }
127         public Element build(MetaGrammar.Context cx) { return e.build(cx); }
128     }
129     public static class Label extends ElementNode {
130         public String label;
131         public ElementNode e;
132         public Label(String label, ElementNode e) { this.e = e; this.label = label; }
133         public String getLabel() { return label; }
134         public String getOwnerTag() { return e.getOwnerTag(); }
135         public Element build(MetaGrammar.Context cx) { return e.build(cx); }
136     }
137     public static /*abstract*/ class Seq {
138         HashSet<Seq> and = new HashSet<Seq>();
139         HashSet<Seq> not = new HashSet<Seq>();
140         ElementNode[] elements;
141         ElementNode follow;
142         String tag = null;
143         boolean lame;
144         public void append(ElementNode e) {
145             ElementNode[] elements = new ElementNode[this.elements.length+1];
146             System.arraycopy(this.elements, 0, elements, 0, this.elements.length);
147             this.elements = elements;
148             elements[elements.length-1] = e;
149         }
150         public Seq(ElementNode e) { this(new ElementNode[] { e }); }
151         public Seq(ElementNode[] elements) { this.elements = elements; }
152         public Seq tag(String tag) { this.tag = tag; return this; }
153         public Seq follow(ElementNode follow) { this.follow = follow; return this; }
154         public Seq dup() {
155             Seq ret = new Seq(elements);
156             ret.and.addAll(and);
157             ret.not.addAll(not);
158             ret.follow = follow;
159             ret.tag = tag;
160             return ret;
161         }
162         public Seq and(Seq s) { and.add(s); s.lame = true; return this; }
163         public Seq andnot(Seq s) { not.add(s); s.lame = true; return this; }
164         public Seq separate(ElementNode sep) {
165             ElementNode[] elements = new ElementNode[this.elements.length * 2 - 1];
166             for(int i=0; i<this.elements.length; i++) {
167                 elements[i*2]   = this.elements[i];
168                 if (i<this.elements.length-1)
169                     elements[i*2+1] = new Drop(sep);
170             }
171             this.elements = elements;
172             return this;
173         }
174         public Sequence build(MetaGrammar.Context cx, Union u, boolean lame) {
175             Sequence ret = build0(cx, lame || this.lame);
176             for(Seq s : and) { Sequence dork = s.build(cx, u, true); ret = ret.and(dork); }
177             for(Seq s : not) { Sequence dork = s.build(cx, u, true); ret = ret.not(dork); }
178             u.add(ret);
179             ret.lame = lame;
180             return ret;
181         }
182         public Sequence build0(MetaGrammar.Context cx, boolean lame) {
183             boolean dropAll = lame;
184             if (tag!=null && "()".equals(tag)) dropAll = true;
185             Object[] labels = new Object[elements.length];
186             boolean[] drops = new boolean[elements.length];
187             Element[] els = new Element[elements.length];
188             for(int i=0; i<elements.length; i++) {
189                 labels[i] = elements[i].getLabel();
190                 drops[i]  = elements[i].drop();
191                 els[i] = elements[i].build(cx);
192                 if (elements[i].getOwnerTag() != null)
193                     tag = elements[i].getOwnerTag();
194             }
195             Sequence ret = null;
196             if (dropAll)     ret = Sequence.drop(els, false);
197             else {
198                 ret = cx.rm.tryResolveTag(tag, cx.cnt, els, labels, drops);
199                 if (ret == null) {
200                     int idx = -1;
201                     for(int i=0; i<els.length; i++)
202                         if (!drops[i])
203                             if (idx==-1) idx = i;
204                             else throw new Error("multiple non-dropped elements in sequence: " + Sequence.drop(els,false));
205                     if (idx != -1) ret = Sequence.singleton(els, idx);
206                     else           ret = Sequence.drop(els, false);
207                 }
208             }
209             if (this.follow != null)
210                 ret.follow = infer(this.follow.build(cx));
211             ret.lame = this.lame;
212             return ret;
213         }
214     }
215     public static @bind.as("&")   Seq  and2(Seq s,        Seq a) { return s.and(a); }
216     public static @bind.as("&~")  Seq  andnot2(Seq s,     Seq a) { return s.andnot(a); }
217     public static @bind.as("->")  Seq  arrow(Seq s, ElementNode e)                { return s.follow(e); }
218     public static @bind.as("::")  Seq  tag(String tagname, Seq s)        { return s.tag(tagname); }
219     public static @bind.as("/")   Seq  slash(Seq s, ElementNode e)                { return s.separate(e); }
220
221     public static Seq  seq(ElementNode[] elements)               { return new Seq(elements); }
222     public static @bind.as("Elements")  Seq  seq2(ElementNode[] elements)               { return new Seq(elements); }
223     public static @bind.as        Seq  psx(Seq s)                        { return s; }
224     public static @bind.as(":")   ElementNode   colon(String s, ElementNode e)             { return new Label(s, e); }
225     public static @bind.as(")")   void close(String foo)                 { throw new Error("not supported"); }
226     public static @bind.as("()")  ElementNode   epsilon()                         { return new Constant(Union.epsilon); }
227
228     public static @bind class NonTerminalReference extends ElementNode {
229         public @bind.arg String nonTerminal;
230         public Element build(MetaGrammar.Context cx) { return cx.get(nonTerminal); }
231     }
232
233     public static class Literal extends Constant {
234         public @bind Literal(@bind.arg String string) { super(CharRange.string(string)); }
235         public boolean drop() { return true; }
236     }
237
238     public static                     class CharClass            extends ElementNode {
239         Range[] ranges;
240         public @bind.as("[") CharClass(Range[] ranges) { this.ranges = ranges; }
241         public Element build(MetaGrammar.Context cx) {
242             edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
243             for(Range r : ranges)
244                 set.add(r.first, r.last);
245             return CharRange.set(set);
246         }
247     }
248
249     public static @bind.as("{")           class XTree                 extends ElementNode {
250         public @bind.arg Seq body;
251         public Element build(MetaGrammar.Context cx) {
252             Union u = new Union();
253             Sequence s = body.build(cx, u, false);
254             Union u2 = new Union();
255             u2.add(Sequence.singleton(new Element[] {
256                 CharRange.leftBrace,
257                 cx.get("ws"),
258                 u,
259                 cx.get("ws"),
260                 CharRange.rightBrace
261             }, 2));
262             return u2;
263         }
264     }
265
266     public static class Rep extends ElementNode {
267         public ElementNode e, sep;
268         public boolean zero, many, max;
269         public Rep(ElementNode e, ElementNode sep, boolean zero, boolean many, boolean max) {
270             this.e = e; this.sep = sep; this.zero = zero; this.many = many; this.max = max;}
271         public Element build(MetaGrammar.Context cx) {
272             return (!max)
273                 ? Sequence.repeat(e.build(cx),        zero, many, sep==null ? null : sep.build(cx), cx.rm.repeatTag())
274                 : sep==null
275                 ? Sequence.repeatMaximal(infer(e.build(cx)), zero, many,                                   cx.rm.repeatTag())
276                 : Sequence.repeatMaximal(e.build(cx),                    zero, many, infer(sep.build(cx)), cx.rm.repeatTag());
277         }
278     }
279     public static class Constant extends ElementNode {
280         Element constant;
281         public Constant(Element constant) { this.constant = constant; }
282         public Element build(MetaGrammar.Context cx) { return constant; }
283     }
284     public abstract static class PostProcess extends ElementNode {
285         ElementNode e;
286         public PostProcess(ElementNode e) { this.e = e; }
287         public Element build(MetaGrammar.Context cx) { return postProcess(e.build(cx)); }
288         public abstract Element postProcess(Element e);
289     }
290
291     // FIXME: it would be nice if we could hoist this into "Rep"
292     public static @bind.as("++")  ElementNode plusmax(final ElementNode e)                     { return new Rep(e, null, false, true, true); }
293     public static @bind.as("+")   ElementNode plus(final ElementNode e)                        { return new Rep(e, null, false, true, false); }
294     public static @bind.as("++/") ElementNode plusmaxfollow(final ElementNode e, final ElementNode sep) { return new Rep(e, sep,  false, true, true); }
295     public static @bind.as("+/")  ElementNode plusfollow(final ElementNode e, final ElementNode sep)    { return new Rep(e, sep,  false, true, false); }
296     public static @bind.as("**")  ElementNode starmax(final ElementNode e)                     { return new Rep(e, null, true,  true, true); }
297     public static @bind.as("*")   ElementNode star(final ElementNode e)                        { return new Rep(e, null, true,  true, false); }
298     public static @bind.as("**/") ElementNode starmaxfollow(final ElementNode e, final ElementNode sep) { return new Rep(e, sep,  true,  true, true); }
299     public static @bind.as("*/")  ElementNode starfollow(final ElementNode e, final ElementNode sep)    { return new Rep(e, sep,  true,  true, false); }
300     public static @bind.as("?")   ElementNode question(final ElementNode e)                    { return new Rep(e, null, true,  true, false); }
301
302     public static @bind.as("!")   ElementNode bang(final ElementNode e)                        { return new Drop(e); }
303
304     public static @bind.as("^")   ElementNode caret(final String s) {
305         return new Drop(new Constant(CharRange.string(s)) {
306                 public String getOwnerTag() { return s; }
307             });
308     }
309
310     public static @bind.as("~")   ElementNode tilde(final ElementNode e) {
311         return new PostProcess(e) {
312                 public Element postProcess(Element e) {
313                     return infer((Topology<Character>)Atom.toAtom(e).complement()); 
314                 } }; }
315
316     public static @bind.as("^^")  void doublecaret(final ElementNode e)                 { throw new Error("not implemented"); }
317
318     //public static @bind.as("(")   ElementNode subexpression(Seq[][] rhs)                { return new NonTerminal(rhs); }
319
320     public static @bind.as("Word")        String word(String s) { return s; }
321     public static @bind.as("Quoted")      String quoted(String s) { return s; }
322     public static @bind.as("escaped")     String c(char c) { return c+""; }
323     public static @bind.as("EmptyString") String emptystring() { return ""; }
324     public static @bind.as("\n")          String retur() { return "\n"; }
325     public static @bind.as("\r")          String lf() { return "\r"; }
326
327     static Atom infer(Element e)  { return infer((Topology<Character>)Atom.toAtom(e)); }
328     static Atom infer(Topology<Character> t) { return new CharRange(new CharTopology(t)); }
329 }