1 package edu.berkeley.sbp.misc;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.chr.*;
8 public class MetaGrammar extends StringWalker {
10 public static Object repeatTag = null;
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);
19 ////////////////////////////////////////////////////////////////////////////////
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)); }
26 private MetaGrammar() { }
28 public static String string(Iterable<Tree<String>> children) {
30 for(Tree<String> t : children) ret += string(t);
33 public static String string(Tree<String> tree) {
35 if (tree.head()!=null) ret += tree.head();
36 ret += string(tree.children());
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) {
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);
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));
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);
63 public String toString() {
65 for(MetaNonterminal mnt : this.values()) ret += mnt + "\n";
69 public class MetaNonterminal {
72 public MetaNonterminal(Tree<String> tree) {
73 name = string(tree.child(0));
74 rhs = rhs(tree.child(1));
76 public String toString() { return name + " = " + rhs; }
77 public Union build(BuildContext bc) { return rhs.build(bc, name); }
79 public MetaUnion rhs(Tree<String> t) {
80 return t.numChildren()==1
81 ? new MetaUnion(t.child(0), false)
82 : new MetaUnion(t, true);
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);
90 public Union buildAnon(BuildContext bc) {
92 for(int i=0; i<sequences.length; i++)
93 s += (i>0?"\n "+(prioritized?">":"|")+" ":"")+sequences[i].buildSequence(bc);
96 public Union build(BuildContext bc, String name) {
97 Union u = bc.get(name);
98 if (u != null) return u;
101 HashSet<Sequence> seqs = new HashSet<Sequence>();
102 for(MetaSequence s : sequences) {
103 Sequence seq = s.buildSequence(bc);
107 for(Sequence seqprev : seqs)
108 seq = seq.not(seqprev);
115 public MetaUnion(Tree<String> t, boolean prioritized) {
116 this.prioritized = prioritized;
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);
124 public String toString() {
126 for(int i=0; i<sequences.length; i++) {
128 if (i<sequences.length-1)
129 ret += (prioritized ? "\n > " : "\n | ");
135 public interface MetaSequence {
136 public abstract Sequence buildSequence(BuildContext bc);
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);
146 public class MetaAnd implements MetaSequence {
150 public Sequence buildSequence(BuildContext bc) {
151 Union u = new Union(toString());
152 Sequence ret = left.buildSequence(bc);
153 Sequence rs = right.buildSequence(bc);
155 if (not) ret = ret.not(rs);
156 else ret = ret.and(rs);
159 return Sequence.singleton(u);
161 public MetaAnd(MetaSequence left, MetaSequence right, boolean not) {
166 public String toString() { return left + " &"+(not?"~":"")+" "+right; }
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;
175 public MetaClause separator;
176 public void addNamedClause(String name, MetaClause mc) {
177 labelMap.put(mc, name);
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);
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);
204 for(int i=0; i<els.length; 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);
211 if (this.followedBy != null)
212 ret.follow = infer(this.followedBy.build(bc));
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);
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 += ")";
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);
237 if ("->".equals(t.head())) {
238 MetaConjunct ret = makeMetaConjunct(t.child(0));
239 ret.followedBy = makeMetaClause(t.child(1), ret);
242 if ("::".equals(t.head())) {
243 MetaConjunct ret = makeMetaConjunct(t.child(1));
244 ret.tag = string(t.child(0));
247 if ("ps".equals(t.head())) {
248 return new MetaConjunct(t.child(0));
250 return new MetaConjunct(t);
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);
283 throw new Error("unknown: " + t);
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);
292 public class MetaRepeat extends MetaClause {
293 public MetaClause element, separator;
294 public boolean maximal, zero, many;
295 public Element build(BuildContext bc) {
298 ? Sequence.repeat(element.build(bc), zero, many, null, null)
299 : Sequence.repeat(element.build(bc), zero, many, separator.build(bc), null))
301 ? Sequence.repeatMaximal(infer(element.build(bc)), zero, many, null)
302 : Sequence.repeatMaximal(element.build(bc), zero, many, infer(separator.build(bc)), null));
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;
311 public String toString() {
313 ((zero&&!many)?"?":zero?"*":"+")+
314 (!maximal?"":zero?"*":"+")+
315 (separator==null?"":(" /"+separator));
318 public class MetaEpsilon extends MetaClause {
319 public String toString() { return "()"; }
320 public Element build(BuildContext bc) { return Union.epsilon; }
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); }
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();
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)));
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\\")+"\""; }
359 public class MetaNonterminalReference extends MetaClause {
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; }
365 public class MetaSelfReference extends MetaClause {
366 public String toString() { return "(*)"; }
367 public Element build(BuildContext bc) { return new Union("(*)"); /* FIXME */ }
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()); }
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");
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";
390 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
391 StringBuffer out = new StringBuffer();
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");
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();
403 //Forest<String> fs = new CharParser(make()).parse(new FileInputStream(args[0]));
404 //System.out.println(fs.expand1());
406 //GraphViz gv = new GraphViz();
408 //FileOutputStream fox = new FileOutputStream("out.dot");
413 out.append("\n // DO NOT EDIT STUFF ABOVE: IT IS AUTOMATICALLY GENERATED\n");
415 for(String s = br.readLine(); s != null; s = br.readLine()) out.append(s+"\n");
418 OutputStream os = new FileOutputStream(fileName);
419 PrintWriter p = new PrintWriter(new OutputStreamWriter(os));
420 p.println(out.toString());