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 extends AnnotationGrammarBindings {
14
15     public MetaGrammarBindings() { super(MetaGrammarBindings.class); }
16
17     // FIXME ugly ugly ugly scary dangerous
18     public static String prefix = "";
19     
20     /** A grammar (a set of nonterminals) */
21     public static class GrammarNode extends HashMap<String,NonTerminalNode> {
22         public NonTerminalNode[] getNonTerminals() {
23             return (NonTerminalNode[])values().toArray(new NonTerminalNode[0]);
24         }
25         public GrammarNode(NonTerminalNode[] nonterminals) {
26             for(NonTerminalNode nt : nonterminals) {
27                 if (nt==null) continue;
28                 if (this.get(nt.name)!=null)
29                     throw new RuntimeException("duplicate definition of nonterminal \""+nt.name+"\"");
30                 this.put(nt.name, nt);
31             }
32         }
33         public @bind.as("Grammar") GrammarNode(Object[] nt) { add(nt); }
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 GrammarNode) add(((GrammarNode)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, Grammar.Bindings 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 Atom toAtom(Context cx) {
68             Atom ret = null;
69             for(Seq[] ss : sequences)
70                 for(Seq s : ss)
71                     ret = ret==null ? s.toAtom(cx) : infer(ret.union(s.toAtom(cx)));
72             return ret;
73         }
74         public void build(Context cx, Union u, NonTerminalNode cnt) {
75             HashSet<Sequence> bad2 = new HashSet<Sequence>();
76             for(int i=0; i<sequences.length; i++) {
77                 Seq[] group = sequences[i];
78                 Union u2 = new Union(null, false);
79                 if (sequences.length==1) u2 = u;
80                 for(int j=0; j<group.length; j++) {
81                     group[j].build(cx, u2, false, cnt);
82                 }
83                 if (sequences.length==1) break;
84                 Sequence seq = Sequence.singleton(u2);
85                 for(Sequence s : bad2) {
86                     s.lame = true;
87                     seq = seq.not(s);
88                 }
89                 u.add(seq);
90                 bad2.add(Sequence.singleton(u2));
91             }
92         }
93     }
94
95     public static @bind.as("#import") GrammarNode poundimport(String fileName, String as) {
96         if (as==null) as = "";
97         else if ("".equals(as)) { }
98         else as = as +".";
99
100         try {
101             Tree t = new CharParser(MetaGrammar.newInstance()).parse(new FileInputStream("tests/"+fileName)).expand1();
102             TreeFunctor<Object,Object> red = (TreeFunctor<Object,Object>)t.head();
103             String oldprefix = prefix;
104             prefix = as;
105             GrammarNode gn = (GrammarNode)red.invoke(t);
106             prefix = oldprefix;
107             return gn;
108         } catch (Exception e) {
109             e.printStackTrace();
110             throw new RuntimeException(e);
111         }
112     }
113
114     public static class NonTerminalNode extends UnionNode {
115         public boolean rep;
116         public String  name = null;
117         public String sep = null;
118         public NonTerminalNode[] getNonTerminals() { return new NonTerminalNode[] { this }; }
119         public @bind.as("NonTerminal") NonTerminalNode(@bind.arg String name, @bind.arg Seq[][] sequences) {
120             this(name, sequences, false); }
121         public NonTerminalNode(String name, Seq[][] sequences, boolean rep) { this(name, sequences, rep, null); }
122         public NonTerminalNode(String name, Seq[][] sequences, boolean rep, String sep) {
123             this.name = prefix + name;
124             this.sequences = sequences;
125             this.rep = rep;
126             this.sep = sep==null?null:(prefix + sep);
127         }
128         public Element build(Context cx, NonTerminalNode cnt) { return cx.get(name); }
129         public void build(Context cx, Union u, NonTerminalNode cnt) {
130             if (!rep) { super.build(cx, u, this); return; }
131             HashSet<Sequence> bad2 = new HashSet<Sequence>();
132
133             Union urep = new Union(null, false);
134             urep.add(Sequence.empty);
135             if (sep != null)
136                 urep.add(Sequence.singleton(new Element[] { cx.get(sep), u }, 1));
137             else
138                 urep.add(Sequence.singleton(new Element[] { u }, 0));
139
140             for(int i=0; i<sequences.length; i++) {
141                 Seq[] group = sequences[i];
142                 Union u2 = new Union(null, false);
143                 if (sequences.length==1) u2 = u;
144                 for(int j=0; j<group.length; j++) {
145                     Union u3 = new Union(null, false);
146                     group[j].build(cx, u3, false, this);
147                     Sequence s = Sequence.unwrap(new Element[] { u3, urep },
148                                                  cx.rm.repeatTag(),
149                                                  new boolean[] { false, false });
150                     u2.add(s);
151                 }
152                 if (sequences.length==1) break;
153                 Sequence seq = Sequence.singleton(u2);
154                 for(Sequence s : bad2) {
155                     s.lame = true;
156                     seq = seq.not(s);
157                 }
158                 u.add(seq);
159                 bad2.add(Sequence.singleton(u2));
160             }
161         }
162     }
163
164     public static @bind.as("=") NonTerminalNode go(@bind.arg String name, @bind.arg Seq[][] sequences) {
165         return new NonTerminalNode(name, sequences, true); }
166     public static @bind.as("=") NonTerminalNode go(@bind.arg String name, @bind.arg String sep, @bind.arg Seq[][] sequences) {
167         return new NonTerminalNode(name, sequences, true, sep); }
168
169     public static class AnonUnionNode extends UnionNode {
170         public @bind.as("(") AnonUnionNode(Seq[][] sequences) {
171             this.sequences = sequences;
172         }
173         public Element build(Context cx, NonTerminalNode cnt) {
174             Union ret = new Union(null, false);
175             build(cx, ret, cnt);
176             return ret;
177         }
178     }
179
180     public static class Range {
181         public @bind Range(char only) { first = only; last = only; }
182         public @bind Range(char first, char last) { this.first = first; this.last = last; }
183         public char first;
184         public char last;
185     }
186
187     public static /*abstract*/ class Seq {
188         HashSet<Seq> and = new HashSet<Seq>();
189         HashSet<Seq> not = new HashSet<Seq>();
190         ElementNode[] elements;
191         ElementNode follow;
192         String tag = null;
193         boolean lame;
194         public void append(ElementNode e) {
195             ElementNode[] elements = new ElementNode[this.elements.length+1];
196             System.arraycopy(this.elements, 0, elements, 0, this.elements.length);
197             this.elements = elements;
198             elements[elements.length-1] = e;
199         }
200         public Seq(ElementNode e) { this(new ElementNode[] { e }); }
201         public Seq(ElementNode[] elements) { this.elements = elements; }
202         public Atom toAtom(Context cx) {
203             if (elements.length != 1) throw new Error("FIXME");
204             return elements[0].toAtom(cx);
205         }
206         public Seq tag(String tag) { this.tag = prefix+tag; return this; }
207         public Seq follow(ElementNode follow) {
208             this.follow = follow;
209             return this;
210         }
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 = prefix+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 && tag.endsWith("()")) 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                 Production prod = new Production(tag, (cnt==null?null:cnt.name), els, drops);
254                 ret = cx.rm.createSequence(prod);
255                 if (ret == null) {
256                     int idx = -1;
257                     for(int i=0; i<els.length; i++)
258                         if (!drops[i])
259                             if (idx==-1) idx = i;
260                             else throw new Error("multiple non-dropped elements in sequence: " + Sequence.drop(els, false));
261                     if (idx != -1) ret = Sequence.singleton(els, idx);
262                     else           ret = Sequence.drop(els, false);
263                 }
264             }
265             if (this.follow != null)
266                 ret.follow = this.follow.toAtom(cx);
267             ret.lame = this.lame;
268             return ret;
269         }
270     }
271     public static @bind.as("&")   Seq  and2(Seq s,        Seq a)   { return s.and(a); }
272     public static @bind.as("&~")  Seq  andnot2(Seq s,     Seq a)   { return s.andnot(a); }
273     public static @bind.as("->")  Seq  arrow(Seq s, ElementNode e) { return s.follow(e); }
274     public static @bind.as("::")  Seq  tag(String tagname, Seq s)  { return s.tag(tagname); }
275     public static @bind.as("/")   Seq  slash(Seq s, ElementNode e) { return s.separate(e); }
276
277     public static Seq  seq(ElementNode[] elements)               { return new Seq(elements); }
278     public static @bind.as("Elements")  Seq  seq2(ElementNode[] elements)               { return new Seq(elements); }
279     public static @bind.as        Seq  psx(Seq s)                        { return s; }
280     public static @bind.as(":")   ElementNode   colon(String s, ElementNode e)             { return new Label(s, e); }
281     public static @bind.as(")")   void close(String foo)                 { throw new Error("not supported"); }
282     public static @bind.as("()")  ElementNode   epsilon()                         { return new Constant(epsilon); }
283
284     private static Union epsilon = new Union("()");
285     static { epsilon.add(Sequence.empty); }
286
287     public static class NonTerminalReferenceNode extends ElementNode {
288         public String nonTerminal;
289         public NonTerminalReferenceNode() { }
290         public @bind.as("NonTerminalReference") NonTerminalReferenceNode(String nonTerminal) {
291             this.nonTerminal = prefix + nonTerminal;
292         }
293         public Atom toAtom(Context cx) {
294             return cx.grammar.get(nonTerminal).toAtom(cx);
295         }
296         public Element build(Context cx, NonTerminalNode cnt) {
297             if (!this.nonTerminal.startsWith(prefix)) nonTerminal = prefix + nonTerminal;
298             Element ret = cx.get(nonTerminal);
299             if (ret == null) throw new RuntimeException("unknown nonterminal \""+nonTerminal+"\"");
300             return ret;
301         }
302     }
303
304     public static class Literal extends Constant {
305         private String string;
306         public @bind Literal(@bind.arg String string) {
307             super(CharAtom.string(string));
308             this.string = string;
309         }
310         public boolean drop() { return true; }
311         public Atom toAtom(Context cx) {
312             if (string.length()!=1) return super.toAtom(cx);
313             edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
314             set.add(string.charAt(0), string.charAt(0));
315             return CharAtom.set(set);
316         }
317     }
318
319     public static                     class CharClass            extends ElementNode {
320         Range[] ranges;
321         public @bind.as("[") CharClass(Range[] ranges) { this.ranges = ranges; }
322         public Atom toAtom(Context cx) {
323             edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
324             for(Range r : ranges)
325                 set.add(r.first, r.last);
326             return CharAtom.set(set);
327         }
328         public Element build(Context cx, NonTerminalNode cnt) {
329             edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
330             for(Range r : ranges)
331                 set.add(r.first, r.last);
332             return CharAtom.set(set);
333         }
334     }
335
336     public static @bind.as("{")           class XTree                 extends ElementNode {
337         public @bind.arg Seq body;
338         public Element build(Context cx, NonTerminalNode cnt) {
339             Union u = new Union(null, false);
340             Sequence s = body.build(cx, u, false, null);
341             Union u2 = new Union(null, false);
342             u2.add(Sequence.singleton(new Element[] {
343                 CharAtom.leftBrace,
344                 cx.get("ws"),
345                 u,
346                 cx.get("ws"),
347                 CharAtom.rightBrace
348             }, 2));
349             return u2;
350         }
351     }
352
353     public static class Rep extends ElementNode {
354         public ElementNode e, sep;
355         public boolean zero, many, max;
356         public Rep(ElementNode e, ElementNode sep, boolean zero, boolean many, boolean max) {
357             this.e = e; this.sep = sep; this.zero = zero; this.many = many; this.max = max;}
358         public Atom toAtom(Context cx) {
359             if (sep != null) return super.toAtom(cx);
360             return e.toAtom(cx);
361         }
362         public Element build(Context cx, NonTerminalNode cnt) {
363             return (!max)
364                 ? Sequence.repeat(e.build(cx, null), zero, many, sep==null ? null : sep.build(cx, null), cx.rm.repeatTag())
365                 : sep==null
366                 ? Sequence.repeatMaximal(e.toAtom(cx), zero, many, cx.rm.repeatTag())
367                 : Sequence.repeatMaximal(e.build(cx, null), zero, many, sep.toAtom(cx), cx.rm.repeatTag());
368         }
369     }
370
371     // FIXME: it would be nice if we could hoist this into "Rep"
372     public static @bind.as("++")  ElementNode plusmax(final ElementNode e)                     
373     { return new Rep(e, null, false, true, true); }
374     public static @bind.as("+")   ElementNode plus(final ElementNode e)                        
375     { return new Rep(e, null, false, true, false); }
376     public static @bind.as("++/") ElementNode plusmaxfollow(final ElementNode e, final ElementNode sep) 
377     { return new Rep(e, sep,  false, true, true); }
378     public static @bind.as("+/")  ElementNode plusfollow(final ElementNode e, final ElementNode sep)    
379     { return new Rep(e, sep,  false, true, false); }
380     public static @bind.as("**")  ElementNode starmax(final ElementNode e)                     
381     { return new Rep(e, null, true,  true, true); }
382     public static @bind.as("*")   ElementNode star(final ElementNode e)                        
383     { return new Rep(e, null, true,  true, false); }
384     public static @bind.as("**/") ElementNode starmaxfollow(final ElementNode e, final ElementNode sep) 
385     { return new Rep(e, sep,  true,  true, true); }
386     public static @bind.as("*/")  ElementNode starfollow(final ElementNode e, final ElementNode sep)    
387     { return new Rep(e, sep,  true,  true, false); }
388     public static @bind.as("?")   ElementNode question(final ElementNode e)                    
389     { return new Rep(e, null, true,  true, false); }
390     public static @bind.as("!")   ElementNode bang(final ElementNode e)                        
391     { return new Drop(e); }
392
393     public static @bind.as("^")   ElementNode caret(final String s) {
394         final String thePrefix = prefix;
395         return new Constant(CharAtom.string(s)) {
396                 public String getOwnerTag() { return thePrefix+s; }
397                 public boolean drop() { return true; }
398             };
399     }
400
401     public static @bind.as("~")   ElementNode tilde(final ElementNode e) {
402         return new ElementNodeWrapper(e) {
403                 public Atom toAtom(Context cx) {
404                     return infer((Topology<Character>)e.toAtom(cx).complement().minus(CharAtom.braces));
405                 }
406                 public Element build(Context cx, NonTerminalNode cnt) {
407                     return infer((Topology<Character>)e.toAtom(cx).complement().minus(CharAtom.braces));
408                 } }; }
409
410     public static @bind.as("Word")        String word(String s) { return s; }
411     public static @bind.as("Quoted")      String quoted(String s) { return s; }
412     public static @bind.as("escaped")     String c(char c) { return c+""; }
413     public static @bind.as("EmptyString") String emptystring() { return ""; }
414     public static @bind.as("\n")          String retur() { return "\n"; }
415     public static @bind.as("\r")          String lf() { return "\r"; }
416
417     //static Atom infer(Element e)  { return infer((Topology<Character>)Atom.toAtom(e)); }
418     static Atom infer(Topology<Character> t) { return new CharAtom(new CharTopology(t)); }
419
420     public static class Context {
421         public HashMap<String,Union> map = new HashMap<String,Union>();
422         public GrammarNode grammar;
423         public String cnt = null;
424         public Grammar.Bindings rm;
425         public Context(GrammarNode g, Grammar.Bindings rm) {
426             this.grammar = g;
427             this.rm = rm;
428         }
429         public Union build() {
430             Union ret = null;
431             for(NonTerminalNode nt : grammar.values()) {
432                 Union u = get(nt.name);
433                 if ("s".equals(nt.name))
434                     ret = u;
435             }
436             return ret;
437         }
438         public Context(Tree t, Grammar.Bindings rm) {
439             this.rm = rm;
440             TreeFunctor<Object,Object> red = (TreeFunctor<Object,Object>)t.head();
441             this.grammar = (GrammarNode)red.invoke(t);
442         }
443         public Union peek(String name) { return map.get(name); }
444         public void  put(String name, Union u) { map.put(name, u); }
445         public Union get(String name) {
446             Union ret = map.get(name);
447             if (ret != null) return ret;
448             ret = new Union(name);
449             map.put(name, ret);
450             NonTerminalNode nt = grammar.get(name);
451             if (nt==null) {
452                 throw new Error("warning could not find " + name);
453             } else {
454                 String old = cnt;
455                 cnt = name;
456                 nt.build(this, ret, nt);
457                 cnt = old;
458             }
459             return ret;
460         }
461
462     }
463
464     public static abstract class ElementNode {
465         public String getLabel() { return null; }
466         public String getOwnerTag() { return null; }
467         public boolean drop() { return false; }
468         public Atom toAtom(Context cx) { throw new Error("can't convert a " + this.getClass().getName() + " to an atom"); }
469         public abstract Element build(Context cx, NonTerminalNode cnt);
470     }
471
472     public static abstract class ElementNodeWrapper extends ElementNode {
473         protected ElementNode _e;
474         public ElementNodeWrapper(ElementNode e) { this._e = e; }
475         public String getLabel() { return _e.getLabel(); }
476         public String getOwnerTag() { return _e.getOwnerTag(); }
477         public boolean drop() { return _e.drop(); }
478         public Atom toAtom(Context cx) { return _e.toAtom(cx); }
479         public Element build(Context cx, NonTerminalNode cnt) { return _e.build(cx, cnt); }
480     }
481
482     public static class Constant extends ElementNode {
483         Element constant;
484         public Constant(Element constant) { this.constant = constant; }
485         public Element build(Context cx, NonTerminalNode cnt) { return constant; }
486         public Atom toAtom(Context cx) {
487             if (constant instanceof Atom) return ((Atom)constant);
488             return super.toAtom(cx);
489         }
490     }
491
492     public abstract static class PostProcess extends ElementNodeWrapper {
493         public PostProcess(ElementNode e) { super(e); }
494         public Element build(Context cx, NonTerminalNode cnt) { return postProcess(_e.build(cx, cnt)); }
495         public abstract Element postProcess(Element e);
496     }
497
498     public static class Drop extends ElementNodeWrapper {
499         public Drop(ElementNode e) { super(e); }
500         public boolean drop() { return true; }
501     }
502
503     public static class Label extends ElementNodeWrapper {
504         public String label;
505         public Label(String label, ElementNode e) { super(e); this.label = label; }
506         public String getLabel() { return label; }
507     }
508
509     static class Invert extends Atom {
510         private final Atom a;
511         public Invert(Atom a) { this.a = a; }
512         public Topology top() { return a.complement(); }
513         public String toString() { return "~"+a; }
514     }
515 }