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