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