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 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);
17 ////////////////////////////////////////////////////////////////////////////////
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)); }
24 private MetaGrammar() { }
26 public static String string(Iterable<Tree<String>> children) {
28 for(Tree<String> t : children) ret += string(t);
31 public static String string(Tree<String> tree) {
33 if (tree.head()!=null) ret += tree.head();
34 ret += string(tree.children());
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) {
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);
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);
57 public Sequence resolveTag(String s, String nonTerminalName, Element[] els, Object[] labels, boolean [] drops) {
58 return Sequence.rewritingSequence(s, els, labels, drops);
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));
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);
70 public String toString() {
72 for(NonTerminal mnt : this.values()) ret += mnt + "\n";
76 public class NonTerminal {
79 public NonTerminal(Tree<String> tree) {
80 name = string(tree.child(0));
81 rhs = rhs(tree.child(1));
83 public String toString() { return name + " = " + rhs; }
84 public Union build(BuildContext bc) {
85 NonTerminal ont = bc.currentNonTerminal;
86 bc.currentNonTerminal = this;
88 return rhs.build(bc, name);
90 bc.currentNonTerminal = ont;
94 public MetaUnion rhs(Tree<String> t) {
95 return t.numChildren()==1
96 ? new MetaUnion(t.child(0), false)
97 : new MetaUnion(t, true);
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);
105 public Union buildAnon(BuildContext bc) {
107 for(int i=0; i<sequences.length; i++)
108 s += (i>0?"\n "+(prioritized?">":"|")+" ":"")+sequences[i].buildSequence(bc);
111 public Union build(BuildContext bc, String name) {
112 Union u = bc.get(name);
113 if (u != null) return u;
116 HashSet<Sequence> seqs = new HashSet<Sequence>();
117 for(MetaSequence s : sequences) {
118 Sequence seq = s.buildSequence(bc);
122 for(Sequence seqprev : seqs)
123 seq = seq.not(seqprev);
130 public MetaUnion(Tree<String> t, boolean prioritized) {
131 this.prioritized = prioritized;
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);
139 public String toString() {
141 for(int i=0; i<sequences.length; i++) {
143 if (i<sequences.length-1)
144 ret += (prioritized ? "\n > " : "\n | ");
150 public interface MetaSequence {
151 public abstract Sequence buildSequence(BuildContext bc);
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);
161 public class MetaAnd implements MetaSequence {
165 public Sequence buildSequence(BuildContext bc) {
166 Union u = new Union(toString());
167 Sequence ret = left.buildSequence(bc);
168 Sequence rs = right.buildSequence(bc);
170 if (not) ret = ret.not(rs);
171 else ret = ret.and(rs);
174 return Sequence.singleton(u);
176 public MetaAnd(MetaSequence left, MetaSequence right, boolean not) {
181 public String toString() { return left + " &"+(not?"~":"")+" "+right; }
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;
190 public MetaClause separator;
191 public void addNamedClause(String name, MetaClause mc) {
192 labelMap.put(mc, name);
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;
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);
215 bc.currentNonTerminal = old;
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);
224 ret = tryResolveTag(tag, bc.currentNonTerminal==null ? null : bc.currentNonTerminal.name, els, labels, drops);
227 for(int i=0; i<els.length; 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);
235 if (this.followedBy != null)
236 ret.follow = infer(this.followedBy.build(bc));
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);
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 += ")";
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);
261 if ("->".equals(t.head())) {
262 Conjunct ret = makeConjunct(t.child(0));
263 ret.followedBy = makeMetaClause(t.child(1), ret);
266 if ("::".equals(t.head())) {
267 Conjunct ret = makeConjunct(t.child(1));
268 ret.tag = string(t.child(0));
271 if ("ps".equals(t.head())) {
272 return new Conjunct(t.child(0));
274 return new Conjunct(t);
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);
307 throw new Error("unknown: " + t);
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);
316 public class MetaRepeat extends MetaClause {
317 public MetaClause element, separator;
318 public boolean maximal, zero, many;
319 public Element build(BuildContext bc) {
322 ? Sequence.repeat(element.build(bc), zero, many, null, repeatTag())
323 : Sequence.repeat(element.build(bc), zero, many, separator.build(bc), repeatTag()))
325 ? Sequence.repeatMaximal(infer(element.build(bc)), zero, many, repeatTag())
326 : Sequence.repeatMaximal(element.build(bc), zero, many, infer(separator.build(bc)), repeatTag()));
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;
335 public String toString() {
337 ((zero&&!many)?"?":zero?"*":"+")+
338 (!maximal?"":zero?"*":"+")+
339 (separator==null?"":(" /"+separator));
342 public class Epsilon extends MetaClause {
343 public String toString() { return "()"; }
344 public Element build(BuildContext bc) { return Union.epsilon; }
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); }
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);
362 u.add(Sequence.singleton(new Element[] { CharRange.leftBrace,
363 new NonTerminalReference("ws").build(bc),
365 new NonTerminalReference("ws").build(bc),
366 CharRange.rightBrace }
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)));
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\\")+"\""; }
394 public class NonTerminalReference extends MetaClause {
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; }
401 public class SelfReference extends MetaClause {
402 public String toString() { return "(*)"; }
403 public Element build(BuildContext bc) { return new Union("(*)"); /* FIXME */ }
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) {
410 return infer((Topology<Character>)Atom.toAtom(element.build(bc)).complement().minus(CharRange.braces));
416 public static void main(String[] args) throws Exception {
417 if (args.length != 2) {
418 System.err.println("usage: java " + MetaGrammar.class.getName() + " grammarfile.g com.yourdomain.package.ClassName");
421 //StringBuffer sbs = new StringBuffer();
422 //((MetaGrammar)new MetaGrammar().walk(meta)).nt.get("e").toString(sbs);
423 //System.err.println(sbs);
424 String className = args[1].substring(args[1].lastIndexOf('.')+1);
425 String packageName = args[1].substring(0, args[1].lastIndexOf('.'));
426 String fileName = packageName.replace('.', '/') + "/" + className + ".java";
428 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
429 StringBuffer out = new StringBuffer();
431 boolean skip = false;
432 for(String s = br.readLine(); s != null; s = br.readLine()) {
433 if (s.indexOf("DO NOT EDIT STUFF BELOW: IT IS AUTOMATICALLY GENERATED") != -1 && s.indexOf("\"")==-1) skip = true;
434 if (s.indexOf("DO NOT EDIT STUFF ABOVE: IT IS AUTOMATICALLY GENERATED") != -1 && s.indexOf("\"")==-1) break;
435 if (!skip) out.append(s+"\n");
438 out.append("\n // DO NOT EDIT STUFF BELOW: IT IS AUTOMATICALLY GENERATED\n");
439 Tree<String> ts = new CharParser(MetaGrammar.make()).parse(new FileInputStream(args[0])).expand1();
441 //Forest<String> fs = new CharParser(make()).parse(new FileInputStream(args[0]));
442 //System.out.println(fs.expand1());
444 //GraphViz gv = new GraphViz();
446 //FileOutputStream fox = new FileOutputStream("out.dot");
451 out.append("\n // DO NOT EDIT STUFF ABOVE: IT IS AUTOMATICALLY GENERATED\n");
453 for(String s = br.readLine(); s != null; s = br.readLine()) out.append(s+"\n");
456 OutputStream os = new FileOutputStream(fileName);
457 PrintWriter p = new PrintWriter(new OutputStreamWriter(os));
458 p.println(out.toString());