checkpoint
[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 GrammarNode extends HashMap<String,NonTerminalNode> implements NonTerminalSource {
17         public NonTerminalNode[] getNonTerminals() {
18             return (NonTerminalNode[])values().toArray(new NonTerminalNode[0]);
19         }
20         public GrammarNode(NonTerminalNode[] nonterminals) {
21             for(NonTerminalNode nt : nonterminals) {
22                 if (nt==null) continue;
23                 if (this.get(nt.name)!=null)
24                     throw new RuntimeException("duplicate definition of nonterminal \""+nt.name+"\"");
25                 this.put(nt.name, nt);
26             }
27         }
28         public @bind.as("Grammar") GrammarNode(Object[] nt) {
29             add(nt);
30         }
31         private void add(Object[] obs) {
32             for(Object o : obs) {
33                 if (o==null) continue;
34                 else if (o instanceof Object[]) add((Object[])o);
35                 else if (o instanceof NonTerminalNode) {
36                     NonTerminalNode nt = (NonTerminalNode)o;
37                     if (this.get(nt.name)!=null)
38                         throw new RuntimeException("duplicate definition of nonterminal \""+nt.name+"\"");
39                     this.put(nt.name, nt);
40                 }
41                 else if (o instanceof NonTerminalSource) add(((NonTerminalSource)o).getNonTerminals());
42             }
43         }
44         public String toString() {
45             String ret = "[ ";
46             for(NonTerminalNode nt : values()) ret += nt + ", ";
47             return ret + " ]";
48         }
49         public Union build(String s, GrammarBindingResolver rm) {
50             Context cx = new Context(this,rm);
51             Union u = null;
52             for(MetaGrammarBindings.NonTerminalNode nt : values()) {
53                 Union el = (Union)cx.get(nt.name);
54                 StringBuffer st = new StringBuffer();
55                 el.toString(st);
56                 if (nt.name.equals(s)) u = el;
57             }
58             return u;
59         }
60     }
61
62     public abstract static class UnionNode extends ElementNode {
63         public Seq[][] sequences;
64         public void build(Context cx, Union u, NonTerminalNode cnt) {
65             HashSet<Sequence> bad2 = new HashSet<Sequence>();
66             for(int i=0; i<sequences.length; i++) {
67                 Seq[] group = sequences[i];
68                 Union u2 = new Union();
69                 if (sequences.length==1) u2 = u;
70                 for(int j=0; j<group.length; j++) {
71                     group[j].build(cx, u2, false, cnt);
72                 }
73                 if (sequences.length==1) break;
74                 Sequence seq = Sequence.singleton(u2);
75                 for(Sequence s : bad2) {
76                     s.lame = true;
77                     seq = seq.not(s);
78                 }
79                 u.add(seq);
80                 bad2.add(Sequence.singleton(u2));
81             }
82         }
83     }
84
85     public static @bind.as("#import") GrammarNode poundimport(String fileName) {
86         System.err.println("#import " + fileName);
87         try {
88             Tree t = new CharParser(MetaGrammar.make()).parse(new FileInputStream(fileName)).expand1();
89             Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
90             return (MetaGrammarBindings.GrammarNode)red.invoke(t);
91         } catch (Exception e) {
92             e.printStackTrace();
93             throw new RuntimeException(e);
94         }
95     }
96
97     public static interface NonTerminalSource {
98         public NonTerminalNode[] getNonTerminals();
99     }
100
101     public static class NonTerminalNode extends UnionNode implements NonTerminalSource {
102         public boolean rep;
103         public String  name = null;
104         public String sep = null;
105         public NonTerminalNode[] getNonTerminals() { return new NonTerminalNode[] { this }; }
106         public @bind.as("NonTerminal") NonTerminalNode(@bind.arg String name, @bind.arg Seq[][] sequences) { this(name, sequences, false); }
107         public NonTerminalNode(String name, Seq[][] sequences, boolean rep) { this(name, sequences, rep, null); }
108         public NonTerminalNode(String name, Seq[][] sequences, boolean rep, String sep) {
109             this.name = name;
110             this.sequences = sequences;
111             this.rep = rep;
112             this.sep = sep;
113         }
114         public Element build(Context cx, NonTerminalNode cnt) { return cx.get(name); }
115         public void build(Context cx, Union u, NonTerminalNode cnt) {
116             if (!rep) { super.build(cx, u, this); return; }
117             HashSet<Sequence> bad2 = new HashSet<Sequence>();
118
119             Union urep = new Union();
120             urep.add(Sequence.empty);
121             urep.add(Sequence.singleton(new Element[] { cx.get(sep), u }, 1));
122
123             for(int i=0; i<sequences.length; i++) {
124                 Seq[] group = sequences[i];
125                 Union u2 = new Union();
126                 if (sequences.length==1) u2 = u;
127                 for(int j=0; j<group.length; j++) {
128                     Union u3 = new Union();
129                     group[j].build(cx, u3, false, this);
130                     Sequence s = Sequence.unwrap(new Element[] { u3, urep },
131                                                  cx.rm.repeatTag(),
132                                                  new boolean[] { false, false });
133                     u2.add(s);
134                 }
135                 if (sequences.length==1) break;
136                 Sequence seq = Sequence.singleton(u2);
137                 for(Sequence s : bad2) {
138                     s.lame = true;
139                     seq = seq.not(s);
140                 }
141                 u.add(seq);
142                 bad2.add(Sequence.singleton(u2));
143             }
144         }
145     }
146     public static @bind.as("=") NonTerminalNode go(@bind.arg String name, @bind.arg Seq[][] sequences) { return new NonTerminalNode(name, sequences, true); }
147     public static @bind.as("=") NonTerminalNode go(@bind.arg String name, @bind.arg String sep, @bind.arg Seq[][] sequences) {
148         return new NonTerminalNode(name, sequences, true, sep);
149     }
150
151     public static class AnonUnionNode extends UnionNode {
152         public @bind.as("(") AnonUnionNode(Seq[][] sequences) {
153             this.sequences = sequences;
154         }
155         public Element build(Context cx, NonTerminalNode cnt) {
156             Union ret = new Union();
157             build(cx, ret, cnt);
158             return ret;
159         }
160     }
161
162     public static class Range {
163         public @bind Range(char only) { first = only; last = only; }
164         public @bind Range(char first, char last) { this.first = first; this.last = last; }
165         public char first;
166         public char last;
167     }
168
169     public static abstract class ElementNode {
170         public String getLabel() { return null; }
171         public String getOwnerTag() { return null; }
172         public boolean drop() { return false; }
173         public abstract Element build(Context cx, NonTerminalNode cnt);
174     }
175
176     public static class Drop extends ElementNode {
177         public ElementNode e;
178         public Drop(ElementNode e) { this.e = e; }
179         public String getLabel() { return null; }
180         public boolean drop() { return true; }
181         public String getOwnerTag() { return e.getOwnerTag(); }
182         public Element build(Context cx, NonTerminalNode cnt) { return e.build(cx, cnt); }
183     }
184
185     public static class Label extends ElementNode {
186         public String label;
187         public ElementNode e;
188         public Label(String label, ElementNode e) { this.e = e; this.label = label; }
189         public String getLabel() { return label; }
190         public String getOwnerTag() { return e.getOwnerTag(); }
191         public Element build(Context cx, NonTerminalNode cnt) { return e.build(cx, cnt); }
192     }
193
194     public static /*abstract*/ class Seq {
195         HashSet<Seq> and = new HashSet<Seq>();
196         HashSet<Seq> not = new HashSet<Seq>();
197         ElementNode[] elements;
198         ElementNode follow;
199         String tag = null;
200         boolean lame;
201         public void append(ElementNode e) {
202             ElementNode[] elements = new ElementNode[this.elements.length+1];
203             System.arraycopy(this.elements, 0, elements, 0, this.elements.length);
204             this.elements = elements;
205             elements[elements.length-1] = e;
206         }
207         public Seq(ElementNode e) { this(new ElementNode[] { e }); }
208         public Seq(ElementNode[] elements) { this.elements = elements; }
209         public Seq tag(String tag) { this.tag = tag; return this; }
210         public Seq follow(ElementNode follow) { this.follow = follow; return this; }
211         public Seq dup() {
212             Seq ret = new Seq(elements);
213             ret.and.addAll(and);
214             ret.not.addAll(not);
215             ret.follow = follow;
216             ret.tag = tag;
217             return ret;
218         }
219         public Seq and(Seq s) { and.add(s); s.lame = true; return this; }
220         public Seq andnot(Seq s) { not.add(s); s.lame = true; return this; }
221         public Seq separate(ElementNode sep) {
222             ElementNode[] elements = new ElementNode[this.elements.length * 2 - 1];
223             for(int i=0; i<this.elements.length; i++) {
224                 elements[i*2]   = this.elements[i];
225                 if (i<this.elements.length-1)
226                     elements[i*2+1] = new Drop(sep);
227             }
228             this.elements = elements;
229             return this;
230         }
231         public Sequence build(Context cx, Union u, boolean lame, NonTerminalNode cnt) {
232             Sequence ret = build0(cx, lame || this.lame, cnt);
233             for(Seq s : and) { Sequence dork = s.build(cx, u, true, cnt); ret = ret.and(dork); }
234             for(Seq s : not) { Sequence dork = s.build(cx, u, true, cnt); ret = ret.not(dork); }
235             u.add(ret);
236             ret.lame = lame;
237             return ret;
238         }
239         public Sequence build0(Context cx, boolean lame, NonTerminalNode cnt) {
240             boolean dropAll = lame;
241             if (tag!=null && "()".equals(tag)) dropAll = true;
242             boolean[] drops = new boolean[elements.length];
243             Element[] els = new Element[elements.length];
244             for(int i=0; i<elements.length; i++) {
245                 drops[i]  = elements[i].drop();
246                 els[i] = elements[i].build(cx, cnt);
247                 if (elements[i].getOwnerTag() != null)
248                     tag = elements[i].getOwnerTag();
249             }
250             Sequence ret = null;
251             if (dropAll)     ret = Sequence.drop(els, false);
252             else {
253                 ret = cx.rm.tryResolveTag(tag, cnt==null?null:cnt.name, els, drops);
254                 if (ret == null) {
255                     int idx = -1;
256                     for(int i=0; i<els.length; i++)
257                         if (!drops[i])
258                             if (idx==-1) idx = i;
259                             else throw new Error("multiple non-dropped elements in sequence: " + Sequence.drop(els,false));
260                     if (idx != -1) ret = Sequence.singleton(els, idx);
261                     else           ret = Sequence.drop(els, false);
262                 }
263             }
264             if (this.follow != null)
265                 ret.follow = infer(this.follow.build(cx, null));
266             ret.lame = this.lame;
267             return ret;
268         }
269     }
270     public static @bind.as("&")   Seq  and2(Seq s,        Seq a) { return s.and(a); }
271     public static @bind.as("&~")  Seq  andnot2(Seq s,     Seq a) { return s.andnot(a); }
272     public static @bind.as("->")  Seq  arrow(Seq s, ElementNode e)                { return s.follow(e); }
273     public static @bind.as("::")  Seq  tag(String tagname, Seq s)        { return s.tag(tagname); }
274     public static @bind.as("/")   Seq  slash(Seq s, ElementNode e)                { return s.separate(e); }
275
276     public static Seq  seq(ElementNode[] elements)               { return new Seq(elements); }
277     public static @bind.as("Elements")  Seq  seq2(ElementNode[] elements)               { return new Seq(elements); }
278     public static @bind.as        Seq  psx(Seq s)                        { return s; }
279     public static @bind.as(":")   ElementNode   colon(String s, ElementNode e)             { return new Label(s, e); }
280     public static @bind.as(")")   void close(String foo)                 { throw new Error("not supported"); }
281     public static @bind.as("()")  ElementNode   epsilon()                         { return new Constant(Union.epsilon); }
282
283     public static @bind.as("NonTerminalReference") class NonTerminalReferenceNode extends ElementNode {
284         public @bind.arg String nonTerminal;
285         public Element build(Context cx, NonTerminalNode cnt) {
286             Element ret = cx.get(nonTerminal);
287             if (ret == null) throw new RuntimeException("unkown nonterminal \""+nonTerminal+"\"");
288             return ret;
289         }
290     }
291
292     public static class Literal extends Constant {
293         public @bind Literal(@bind.arg String string) { super(CharRange.string(string)); }
294         public boolean drop() { return true; }
295     }
296
297     public static                     class CharClass            extends ElementNode {
298         Range[] ranges;
299         public @bind.as("[") CharClass(Range[] ranges) { this.ranges = ranges; }
300         public Element build(Context cx, NonTerminalNode cnt) {
301             edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
302             for(Range r : ranges)
303                 set.add(r.first, r.last);
304             return CharRange.set(set);
305         }
306     }
307
308     public static @bind.as("{")           class XTree                 extends ElementNode {
309         public @bind.arg Seq body;
310         public Element build(Context cx, NonTerminalNode cnt) {
311             Union u = new Union();
312             Sequence s = body.build(cx, u, false, null);
313             Union u2 = new Union();
314             u2.add(Sequence.singleton(new Element[] {
315                 CharRange.leftBrace,
316                 cx.get("ws"),
317                 u,
318                 cx.get("ws"),
319                 CharRange.rightBrace
320             }, 2));
321             return u2;
322         }
323     }
324
325     public static class Rep extends ElementNode {
326         public ElementNode e, sep;
327         public boolean zero, many, max;
328         public Rep(ElementNode e, ElementNode sep, boolean zero, boolean many, boolean max) {
329             this.e = e; this.sep = sep; this.zero = zero; this.many = many; this.max = max;}
330         public Element build(Context cx, NonTerminalNode cnt) {
331             return (!max)
332                 ? Sequence.repeat(e.build(cx, null),        zero, many, sep==null ? null : sep.build(cx, null), cx.rm.repeatTag())
333                 : sep==null
334                 ? Sequence.repeatMaximal(infer(e.build(cx, null)), zero, many,                                   cx.rm.repeatTag())
335                 : Sequence.repeatMaximal(e.build(cx, null),                    zero, many, infer(sep.build(cx, null)), cx.rm.repeatTag());
336         }
337     }
338     public static class Constant extends ElementNode {
339         Element constant;
340         public Constant(Element constant) { this.constant = constant; }
341         public Element build(Context cx, NonTerminalNode cnt) { return constant; }
342     }
343     public abstract static class PostProcess extends ElementNode {
344         ElementNode e;
345         public PostProcess(ElementNode e) { this.e = e; }
346         public Element build(Context cx, NonTerminalNode cnt) { return postProcess(e.build(cx, cnt)); }
347         public abstract Element postProcess(Element e);
348     }
349
350     // FIXME: it would be nice if we could hoist this into "Rep"
351     public static @bind.as("++")  ElementNode plusmax(final ElementNode e)                     { return new Rep(e, null, false, true, true); }
352     public static @bind.as("+")   ElementNode plus(final ElementNode e)                        { return new Rep(e, null, false, true, false); }
353     public static @bind.as("++/") ElementNode plusmaxfollow(final ElementNode e, final ElementNode sep) { return new Rep(e, sep,  false, true, true); }
354     public static @bind.as("+/")  ElementNode plusfollow(final ElementNode e, final ElementNode sep)    { return new Rep(e, sep,  false, true, false); }
355     public static @bind.as("**")  ElementNode starmax(final ElementNode e)                     { return new Rep(e, null, true,  true, true); }
356     public static @bind.as("*")   ElementNode star(final ElementNode e)                        { return new Rep(e, null, true,  true, false); }
357     public static @bind.as("**/") ElementNode starmaxfollow(final ElementNode e, final ElementNode sep) { return new Rep(e, sep,  true,  true, true); }
358     public static @bind.as("*/")  ElementNode starfollow(final ElementNode e, final ElementNode sep)    { return new Rep(e, sep,  true,  true, false); }
359     public static @bind.as("?")   ElementNode question(final ElementNode e)                    { return new Rep(e, null, true,  true, false); }
360
361     public static @bind.as("!")   ElementNode bang(final ElementNode e)                        { return new Drop(e); }
362
363     public static @bind.as("^")   ElementNode caret(final String s) {
364         return new Drop(new Constant(CharRange.string(s)) {
365                 public String getOwnerTag() { return s; }
366             });
367     }
368
369     public static @bind.as("~")   ElementNode tilde(final ElementNode e) {
370         return new PostProcess(e) {
371                 public Element postProcess(Element e) {
372                     return infer((Topology<Character>)Atom.toAtom(e).complement().minus(CharRange.braces));
373                 } }; }
374
375     public static @bind.as("Word")        String word(String s) { return s; }
376     public static @bind.as("Quoted")      String quoted(String s) { return s; }
377     public static @bind.as("escaped")     String c(char c) { return c+""; }
378     public static @bind.as("EmptyString") String emptystring() { return ""; }
379     public static @bind.as("\n")          String retur() { return "\n"; }
380     public static @bind.as("\r")          String lf() { return "\r"; }
381
382     static Atom infer(Element e)  { return infer((Topology<Character>)Atom.toAtom(e)); }
383     static Atom infer(Topology<Character> t) { return new CharRange(new CharTopology(t)); }
384
385     public static class Context {
386         public HashMap<String,Union> map = new HashMap<String,Union>();
387         public GrammarNode grammar;
388         public String cnt = null;
389         public GrammarBindingResolver rm;
390         public Context(GrammarNode g, GrammarBindingResolver rm) {
391             this.grammar = g;
392             this.rm = rm;
393         }
394         public Union build() {
395             Union ret = null;
396             for(NonTerminalNode nt : grammar.values()) {
397                 Union u = get(nt.name);
398                 if ("s".equals(nt.name))
399                     ret = u;
400             }
401             return ret;
402         }
403         public Context(Tree t, GrammarBindingResolver rm) {
404             this.rm = rm;
405             Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
406             this.grammar = (GrammarNode)red.invoke(t);
407         }
408         public Union peek(String name) { return map.get(name); }
409         public void  put(String name, Union u) { map.put(name, u); }
410         public Union get(String name) {
411             Union ret = map.get(name);
412             if (ret != null) return ret;
413             ret = new Union(name);
414             map.put(name, ret);
415             NonTerminalNode nt = grammar.get(name);
416             if (nt==null) {
417                 System.err.println("*** warning could not find " + name);
418             } else {
419                 String old = cnt;
420                 cnt = name;
421                 nt.build(this, ret, nt);
422                 cnt = old;
423             }
424             return ret;
425         }
426
427     }
428
429 }