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