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 /** 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(); }
20 public static Atom infer(Element e) { return new CharRange((Topology<Character>)Atom.toAtom(e)); }
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; }
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(); }
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);
45 ////////////////////////////////////////////////////////////////////////////////
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); }
51 private MetaGrammar() { }
53 public static String string(Iterable<Tree<String>> children) {
55 for(Tree<String> t : children) ret += string(t);
58 public static String string(Tree<String> tree) {
60 if (tree.head()!=null) ret += tree.head();
61 ret += string(tree.children());
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) {
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);
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));
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);
88 public String toString() {
90 for(MetaNonterminal mnt : this.values()) ret += mnt + "\n";
94 public static class MetaNonterminal {
97 public MetaNonterminal(Tree<String> tree) {
98 name = string(tree.child(0));
99 rhs = rhs(tree.child(1));
101 public String toString() { return name + " = " + rhs; }
102 public Union build(BuildContext bc) { return rhs.build(bc, name); }
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);
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);
115 public Union buildAnon(BuildContext bc) {
117 for(int i=0; i<sequences.length; i++)
118 s += (i>0?"\n "+(prioritized?">":"|")+" ":"")+sequences[i].buildSequence(bc);
121 public Union build(BuildContext bc, String name) {
122 Union u = bc.get(name);
123 if (u != null) return u;
126 HashSet<Sequence> seqs = new HashSet<Sequence>();
127 for(MetaSequence s : sequences) {
128 Sequence seq = s.buildSequence(bc);
132 for(Sequence seqprev : seqs)
133 seq = seq.not(seqprev);
140 public MetaUnion(Tree<String> t, boolean prioritized) {
141 //System.err.println("metaunion: " + t);
142 this.prioritized = prioritized;
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);
150 public String toString() {
152 for(int i=0; i<sequences.length; i++) {
154 if (i<sequences.length-1)
155 ret += (prioritized ? "\n > " : "\n | ");
161 public interface MetaSequence {
162 public abstract Sequence buildSequence(BuildContext bc);
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);
172 public static class MetaAnd implements MetaSequence {
176 public Sequence buildSequence(BuildContext bc) {
177 Union u = new Union(toString());
178 Sequence ret = left.buildSequence(bc);
179 Sequence rs = right.buildSequence(bc);
181 if (not) ret = ret.not(rs);
182 else ret = ret.and(rs);
185 return Sequence.singleton(u);
187 public MetaAnd(MetaSequence left, MetaSequence right, boolean not) {
192 public String toString() { return left + " &"+(not?"~":"")+" "+right; }
195 public static class MetaConjunct implements MetaSequence {
196 public boolean negated = false;
197 public MetaClause[] elements;
198 public MetaClause followedBy = null;
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);
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);
223 for(int i=0; i<els.length; 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);
230 if (this.followedBy != null) ret.follow = new Hack(infer(this.followedBy.build(bc)));
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);
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 += ")";
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);
255 if ("->".equals(t.head())) {
256 MetaConjunct ret = make(t.child(0));
257 ret.followedBy = MetaClause.make(t.child(1), ret);
260 if ("::".equals(t.head())) {
261 MetaConjunct ret = make(t.child(1));
262 ret.tag = string(t.child(0));
265 if ("ps".equals(t.head())) {
266 return new MetaConjunct(t.child(0));
268 return new MetaConjunct(t);
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);
303 public static class MetaRepeat extends MetaClause {
304 public MetaClause element, separator;
305 public boolean maximal, zero, many;
306 public Element build(BuildContext bc) {
308 ? new Repeat(element.build(bc), zero, many, separator==null?null:separator.build(bc), 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);
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;
320 public String toString() {
322 ((zero&&!many)?"?":zero?"*":"+")+
323 (!maximal?"":zero?"*":"+")+
324 (separator==null?"":(" /"+separator));
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); }
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();
343 public static class MetaEpsilon extends MetaClause {
344 public String toString() { return "()"; }
345 public Element build(BuildContext bc) { return Union.epsilon; }
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)));
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\\")+"\""; }
368 public static class MetaNonterminalReference extends MetaClause {
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; }
374 public static class MetaSelfReference extends MetaClause {
375 public String toString() { return "(*)"; }
376 public Element build(BuildContext bc) { return new Union("(*)"); /* FIXME */ }
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)))); }
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");
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";
400 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
401 StringBuffer out = new StringBuffer();
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");
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();
413 //Forest<String> fs = new CharParser(make()).parse(new FileInputStream(args[0]));
414 //System.out.println(fs.expand1());
416 //GraphViz gv = new GraphViz();
418 //FileOutputStream fox = new FileOutputStream("out.dot");
423 out.append("\n // DO NOT EDIT STUFF ABOVE: IT IS AUTOMATICALLY GENERATED\n");
425 for(String s = br.readLine(); s != null; s = br.readLine()) out.append(s+"\n");
428 OutputStream os = new FileOutputStream(fileName);
429 PrintWriter p = new PrintWriter(new OutputStreamWriter(os));
430 p.println(out.toString());