checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / MetaGrammar.java
1 package edu.berkeley.sbp.misc;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.chr.*;
5 import java.util.*;
6 import java.io.*;
7
8 public class MetaGrammar extends StringWalker {
9
10     public static Object repeatTag = null;
11
12     /** an atom which tracks the possible tokenset of some element, provided that element can only match single-token sequences */
13     public static class Infer<T extends Input> extends Atom<T> {
14         private final Element e;
15         public Infer(Element e) { this.e = e; }
16         public Topology<T> top() { return (Topology<T>)toAtom(e); }
17         public String toString() { return e.toString(); }
18     }
19
20     public static Atom infer(Element e) { return new CharRange((Topology<Character>)Atom.toAtom(e)); }
21
22     /** an atom which tracks the inverse of some other atom */
23     public static class Invert<T extends Input> extends Atom<T> {
24         private final Atom<T> a;
25         public Invert(Atom<T> a) { this.a = a; }
26         public Topology<T> top() { return a.complement(); }
27         public String toString() { return "~"+a; }
28     }
29
30     public static class Hack<T extends Input> extends Atom<T> {
31         private final Atom<T> a;
32         static final Topology leftright = CharRange.rightBrace.union(CharRange.leftBrace);
33         public Hack(Atom<T> a) { this.a = a; }
34         public Topology<T> top() { return a.minus(leftright); }
35         public String toString() { return a.toString(); }
36     }
37
38     public static Union make() throws Exception { return make(MetaGrammarTree.meta, "s"); }
39     public static Union make(Tree<String> tree, String nt) throws Exception {
40         Meta.MetaGrammarFile mgf = new MetaGrammar.Meta.MetaGrammarFile(tree);
41         BuildContext bc = new BuildContext(mgf);
42         return mgf.get(nt).build(bc);
43     }
44
45     ////////////////////////////////////////////////////////////////////////////////
46
47     private static boolean strings;
48     private static Element  set(Range.Set r) { if (strings) throw new Error(); return CharRange.set(r); }
49     private static Element  string(String s) { return strings ? StringInput.string(s) : CharRange.string(s); }
50
51     private MetaGrammar() { }
52
53     public static String string(Iterable<Tree<String>> children) {
54         String ret = "";
55         for(Tree<String> t : children) ret += string(t);
56         return ret;
57     }
58     public static String string(Tree<String> tree) {
59         String ret = "";
60         if (tree.head()!=null) ret += tree.head();
61         ret += string(tree.children());
62         return ret;
63     }
64
65     public static class BuildContext extends HashMap<String,Union> {
66         private final Meta.MetaGrammarFile mgf;
67         public BuildContext(Meta.MetaGrammarFile mgf) { this.mgf = mgf; }
68         public Union build(String s) {
69             Union ret = get(s);
70             if (ret != null) return ret;
71             Meta.MetaNonterminal mnt = mgf.get(s);
72             if (mnt==null) throw new Error("undeclared nonterminal \""+s+"\"");
73             return mnt.build(this);
74         }
75     }
76
77     public static class Meta {
78         public static class MetaGrammarFile extends HashMap<String,MetaNonterminal> {
79             public MetaGrammarFile(Tree<String> tree) {
80                 if (!tree.head().equals("grammar")) throw new Error();
81                 for(Tree<String> nt : tree.child(0))
82                     add(new MetaNonterminal(nt));
83             }
84             private void add(MetaNonterminal mnt) {
85                 if (this.get(mnt.name)!=null) throw new Error("duplicate definition of nonterminal \""+mnt.name+"\"");
86                 this.put(mnt.name, mnt);
87             }
88             public String toString() {
89                 String ret = "";
90                 for(MetaNonterminal mnt : this.values()) ret += mnt + "\n";
91                 return ret;
92             }
93         }
94         public static class MetaNonterminal {
95             public String    name;
96             public MetaUnion rhs;
97             public MetaNonterminal(Tree<String> tree) {
98                 name = string(tree.child(0));
99                 rhs = rhs(tree.child(1));
100             }
101             public String toString() { return name + " = " + rhs; }
102             public Union build(BuildContext bc) { return rhs.build(bc, name); }
103         }
104         public static MetaUnion rhs(Tree<String> t) {
105             return t.numChildren()==1
106                 ? new MetaUnion(t.child(0), false)
107                 : new MetaUnion(t, true);
108         }
109         public static class MetaUnion implements MetaSequence {
110             public boolean prioritized;
111             public MetaSequence[] sequences;
112             public Sequence buildSequence(BuildContext bc) {
113                 return Sequence.singleton(new Element[] { buildAnon(bc) }, 0);
114             }
115             public Union buildAnon(BuildContext bc) {
116                 String s = "";
117                 for(int i=0; i<sequences.length; i++)
118                     s += (i>0?"\n                "+(prioritized?">":"|")+" ":"")+sequences[i].buildSequence(bc);
119                 return build(bc, s);
120             }
121             public Union build(BuildContext bc, String name) {
122                 Union u = bc.get(name);
123                 if (u != null) return u;
124                 u = new Union(name);
125                 bc.put(name, u);
126                 HashSet<Sequence> seqs = new HashSet<Sequence>();
127                 for(MetaSequence s : sequences) {
128                     Sequence seq = s.buildSequence(bc);
129                     if (seq != null) {
130                         Sequence oseq = seq;
131                         if (prioritized)
132                             for(Sequence seqprev : seqs)
133                                 seq = seq.not(seqprev);
134                         u.add(seq);
135                         seqs.add(seq);
136                     }
137                 }
138                 return u;
139             }
140             public MetaUnion(Tree<String> t, boolean prioritized) {
141                 //System.err.println("metaunion: " + t);
142                 this.prioritized = prioritized;
143                 int i = 0;
144                 this.sequences = new MetaSequence[t.numChildren()];
145                 for(Tree<String> tt : t)
146                     sequences[i++] = prioritized
147                         ? new MetaUnion(tt, false)
148                         : makeMetaSequence(tt);
149             }
150             public String toString() {
151                 String ret = "\n     ";
152                 for(int i=0; i<sequences.length; i++) {
153                     ret += sequences[i];
154                     if (i<sequences.length-1)
155                         ret += (prioritized ? "\n    > " : "\n    | ");
156                 }
157                 return ret;
158             }
159         }
160
161         public interface MetaSequence {
162             public abstract Sequence buildSequence(BuildContext bc);
163         }
164
165         public static MetaSequence makeMetaSequence(Tree<String> t) {
166             if ("psx".equals(t.head())) return MetaConjunct.make(t.child(0));
167             if (t.head().equals("&"))  return new MetaAnd(makeMetaSequence(t.child(0)), MetaConjunct.make(t.child(1)), false);
168             if (t.head().equals("&~")) return new MetaAnd(makeMetaSequence(t.child(0)), MetaConjunct.make(t.child(1)), true);
169             return null;
170         }
171
172         public static class MetaAnd implements MetaSequence {
173             boolean not;
174             MetaSequence left;
175             MetaSequence right;
176             public Sequence buildSequence(BuildContext bc) {
177                 Union u = new Union(toString());
178                 Sequence ret = left.buildSequence(bc);
179                 Sequence rs = right.buildSequence(bc);
180                 rs.lame = true;
181                 if (not) ret = ret.not(rs);
182                 else     ret = ret.and(rs);
183                 u.add(rs);
184                 u.add(ret);
185                 return Sequence.singleton(u);
186             }
187             public MetaAnd(MetaSequence left, MetaSequence right, boolean not) {
188                 this.left = left;
189                 this.right = right;
190                 this.not = not;
191             }
192             public String toString() { return left + " &"+(not?"~":"")+" "+right; }
193         }
194
195         public static class MetaConjunct implements MetaSequence {
196             public boolean negated = false;
197             public MetaClause[] elements;
198             public MetaClause followedBy = null;
199             public String tag;
200             public MetaClause separator;
201             public Sequence buildSequence(BuildContext bc) {
202                 Element[] els = new Element[elements.length + (separator==null?0:(elements.length-1))];
203                 boolean[] drops = new boolean[elements.length + (separator==null?0:(elements.length-1))];
204                 boolean unwrap = false;
205                 boolean dropAll = false;
206                 if (tag!=null && tag.equals("[]")) unwrap = true;
207                 if (tag!=null && "()".equals(tag)) dropAll=true;
208                 for(int i=0; i<elements.length; i++) {
209                     int j = separator==null ? i : i*2;
210                     els[j] = elements[i].build(bc);
211                     drops[j] = elements[i].drop;
212                     if (separator!=null && i<elements.length-1) {
213                         els[j+1] = separator.build(bc);
214                         drops[j+1] = true;
215                     }
216                 }
217                 Sequence ret = null;
218                 if (dropAll)     ret = Sequence.drop(els, false);
219                 else if (unwrap) ret = Sequence.unwrap(els, /*repeatTag FIXME*/null, drops);
220                 else if (tag!=null) ret = Sequence.rewritingSequence(tag, els, new Object[els.length], drops);
221                 else {
222                     int idx = -1;
223                     for(int i=0; i<els.length; i++)
224                         if (!drops[i])
225                             if (idx==-1) idx = i;
226                             else throw new Error("multiple non-dropped elements in sequence: " + Sequence.drop(els,false));
227                     if (idx != -1) ret = Sequence.singleton(els, idx);
228                     else           ret = Sequence.drop(els, false);
229                 }
230                 if (this.followedBy != null) ret.follow = new Hack(infer(this.followedBy.build(bc)));
231                 return ret;
232             }
233             private MetaConjunct(Tree<String> t) {
234                 //System.err.println("MetaConjunct("+t+")");
235                 elements = new MetaClause[t.numChildren()];
236                 int i = 0; for(Tree<String> tt : t)
237                     elements[i++] = MetaClause.make(tt, this);
238             }
239             public String toString() {
240                 String ret = (tag==null ? "" : (tag+":: ")) + (negated ? "~" : "");
241                 if (elements.length > 1) ret += "(";
242                 for(MetaClause mc : elements) ret += (" " + mc + " ");
243                 if (separator != null) ret += " /" + separator;
244                 if (followedBy != null) ret += " -> " + followedBy;
245                 if (elements.length > 1) ret += ")";
246                 return ret;
247             }
248             public static MetaConjunct make(Tree<String> t) {
249                 //System.err.println("MetaConjunct.make("+t+")");
250                 if ("/".equals(t.head())) {
251                     MetaConjunct ret = make(t.child(0));
252                     ret.separator = MetaClause.make(t.child(1), ret);
253                     return ret;
254                 }
255                 if ("->".equals(t.head())) {
256                     MetaConjunct ret = make(t.child(0));
257                     ret.followedBy = MetaClause.make(t.child(1), ret);
258                     return ret;
259                 }
260                 if ("::".equals(t.head())) {
261                     MetaConjunct ret = make(t.child(1));
262                     ret.tag = string(t.child(0));
263                     return ret;
264                 }
265                 if ("ps".equals(t.head())) {
266                     return new MetaConjunct(t.child(0));
267                 }
268                 return new MetaConjunct(t);
269             }
270         }
271
272         public static abstract class MetaClause {
273             public String label = null;
274             public boolean drop = false;
275             public boolean lift = false;
276             public abstract Element build(BuildContext bc);
277             public static MetaClause make(Tree<String> t, MetaConjunct c) {
278                 //System.err.println("MetaClause.make("+t+")");
279                 if (t==null) return new MetaEpsilon();
280                 if (t.head()==null) return new MetaEpsilon();
281                 if (t.head().equals("{")) throw new Error("metatree: " + t);
282                 if (t.head().equals("*")) return new MetaRepeat(make(t.child(0), c), false, null, true, true);
283                 if (t.head().equals("+")) return new MetaRepeat(make(t.child(0), c), false, null, false, true);
284                 if (t.head().equals("?")) return new MetaRepeat(make(t.child(0), c), false, null, true, false);
285                 if (t.head().equals("**")) return new MetaRepeat(make(t.child(0), c), true, null, true, true);
286                 if (t.head().equals("++")) return new MetaRepeat(make(t.child(0), c), true, null, false, true);
287                 if (t.head().equals("*/")) return new MetaRepeat(make(t.child(0), c), false, make(t.child(1), c), true, true);
288                 if (t.head().equals("+/")) return new MetaRepeat(make(t.child(0), c), false, make(t.child(1), c), false, true);
289                 if (t.head().equals("**/")) return new MetaRepeat(make(t.child(0), c), true, make(t.child(1), c), true, true);
290                 if (t.head().equals("++/")) return new MetaRepeat(make(t.child(0), c), true, make(t.child(1), c), false, true);
291                 if (t.head().equals("()")) return new MetaEpsilon();
292                 if (t.head().equals("[")) return new MetaRange(t.child(0));
293                 if (t.head().equals("literal")) return new MetaStringLiteral(t.child(0));
294                 if (t.head().equals("nonTerminal")) return new MetaNonterminalReference(t.child(0));
295                 if (t.head().equals(")")) return new MetaSelfReference();
296                 if (t.head().equals("(")) return new MetaParens(t.child(0));
297                 if (t.head().equals("~")) return new MetaInvert(t.child(0), c);
298                 if (t.head().equals("!")) { MetaClause mc = make(t.child(0), c); mc.drop = true; return mc; }
299                 if (t.head().equals("^")) { c.tag = string(t.child(0)); return new MetaStringLiteral(t.child(0)); }
300                 if (t.head().equals("^^")) throw new Error("carets: " + t);
301                 throw new Error("unknown: " + t);
302             }
303             public static class MetaRepeat extends MetaClause {
304                 public MetaClause element, separator;
305                 public boolean maximal, zero, many;
306                 public Element build(BuildContext bc) {
307                     return !maximal
308                         ? new Repeat(element.build(bc), zero, many, separator==null?null:separator.build(bc), null)
309                         : separator==null
310                         ? new Repeat.Maximal(infer(element.build(bc)), zero, many, null)
311                         : new Repeat.Maximal(element.build(bc), zero, many, infer(separator.build(bc)), null);
312                 }
313                 public MetaRepeat(MetaClause element, boolean maximal, MetaClause separator, boolean zero, boolean many) {
314                     this.separator = separator;
315                     this.element = element;
316                     this.maximal = maximal;
317                     this.zero = zero;
318                     this.many = many;
319                 }
320                 public String toString() {
321                     return element+
322                         ((zero&&!many)?"?":zero?"*":"+")+
323                         (!maximal?"":zero?"*":"+")+
324                         (separator==null?"":(" /"+separator));
325                 }
326             }
327             public static class MetaParens extends MetaClause {
328                 public MetaUnion body;
329                 public MetaParens(Tree<String> t) { this.body = rhs(t); }
330                 public String toString() { return "( " + body + " )"; }
331                 public Element build(BuildContext bc) { return body.buildAnon(bc); }
332             }
333             /*
334             public static class MetaTree extends MetaClause {
335                 public MetaConjunct body;
336                 public MetaTree(Tree<String> t) { this.body = MetaConjunct.make(t); }
337                 public String toString() { return "{ " + body + " }"; }
338                 public Element build(BuildContext bc) {
339                     return new Union("{}");// body.buildSequence();
340                 }
341             }
342             */
343             public static class MetaEpsilon extends MetaClause {
344                 public String toString() { return "()"; }
345                 public Element build(BuildContext bc) { return Union.epsilon; }
346             }
347             public static class MetaRange extends MetaClause {
348                 Range.Set range = new Range.Set();
349                 public String toString() { return range.toString(); }
350                 public Element build(BuildContext bc) { return set(range); }
351                 public MetaRange(Tree<String> t) {
352                     for(Tree<String> tt : t) {
353                         if (tt.head().equals("range")) {
354                             range.add(tt.child(0).head().charAt(0));
355                         } else if (tt.head().equals("-")) {
356                             range.add(new Range(string(tt.child(0)).charAt(0),
357                                                 string(tt.child(1)).charAt(0)));
358                         }
359                     }
360                 }
361             }
362             public static class MetaStringLiteral extends MetaClause {
363                 public String literal;
364                 public Element build(BuildContext bc) { return string(literal); }
365                 public MetaStringLiteral(Tree<String> literal) { this.literal = string(literal); this.drop = true; }
366                 public String toString() { return "\""+StringUtil.escapify(literal, "\"\r\n\\")+"\""; }
367             }
368             public static class MetaNonterminalReference extends MetaClause {
369                 public String name;
370                 public MetaNonterminalReference(Tree<String> name) { this.name = string(name); }
371                 public Element build(BuildContext bc) { return bc.build(name); }
372                 public String toString() { return name; } 
373             }
374             public static class MetaSelfReference extends MetaClause {
375                 public String toString() { return "(*)"; } 
376                 public Element build(BuildContext bc) { return new Union("(*)"); /* FIXME */ }
377             }
378             public static class MetaInvert extends MetaClause {
379                 public MetaClause element;
380                 public MetaInvert(Tree<String> t, MetaConjunct c) { this.element = make(t, c); }
381                 public String toString() { return "~"+element; }
382                 public Element build(BuildContext bc) { return new Hack(new Invert(infer(element.build(bc)))); }
383             }
384         }
385
386     }
387
388     public static void main(String[] args) throws Exception {
389         if (args.length != 2) {
390             System.err.println("usage: java " + MetaGrammar.class.getName() + " grammarfile.g com.yourdomain.package.ClassName");
391             System.exit(-1);
392         }
393         //StringBuffer sbs = new StringBuffer();
394         //((MetaGrammar)new MetaGrammar().walk(meta)).nt.get("e").toString(sbs);
395         //System.err.println(sbs);
396         String className   = args[1].substring(args[1].lastIndexOf('.')+1);
397         String packageName = args[1].substring(0, args[1].lastIndexOf('.'));
398         String fileName    = packageName.replace('.', '/') + "/" + className + ".java";
399
400         BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
401         StringBuffer out = new StringBuffer();
402
403         boolean skip = false;
404         for(String s = br.readLine(); s != null; s = br.readLine()) {
405             if (s.indexOf("DO NOT EDIT STUFF BELOW: IT IS AUTOMATICALLY GENERATED") != -1 && s.indexOf("\"")==-1) skip = true;
406             if (s.indexOf("DO NOT EDIT STUFF ABOVE: IT IS AUTOMATICALLY GENERATED") != -1 && s.indexOf("\"")==-1) break;
407             if (!skip) out.append(s+"\n");
408         }
409
410         out.append("\n        // DO NOT EDIT STUFF BELOW: IT IS AUTOMATICALLY GENERATED\n");
411         Tree<String> ts = new CharParser(MetaGrammar.make()).parse(new FileInputStream(args[0])).expand1();
412
413         //Forest<String> fs = new CharParser(make()).parse(new FileInputStream(args[0]));
414         //System.out.println(fs.expand1());
415
416         //GraphViz gv = new GraphViz();
417         //fs.toGraphViz(gv);
418         //FileOutputStream fox = new FileOutputStream("out.dot");
419         //gv.dump(fox);
420         //fox.close();
421
422         ts.toJava(out);
423         out.append("\n        // DO NOT EDIT STUFF ABOVE: IT IS AUTOMATICALLY GENERATED\n");
424
425         for(String s = br.readLine(); s != null; s = br.readLine()) out.append(s+"\n");
426         br.close();
427
428         OutputStream os = new FileOutputStream(fileName);
429         PrintWriter p = new PrintWriter(new OutputStreamWriter(os));
430         p.println(out.toString());
431         p.flush();
432         os.close();
433     }
434 }