1 package edu.berkeley.sbp.meta;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.chr.*;
5 import edu.berkeley.sbp.misc.*;
6 import edu.berkeley.sbp.bind.*;
8 import java.lang.annotation.*;
9 import java.lang.reflect.*;
12 public class MetaGrammarBindings {
13 public static @bind class Grammar {
14 public NonTerminal get(String s) {
15 for(NonTerminal nt : nonterminals)
16 if (nt.name.equals(s)) return nt;
19 public @bind.arg NonTerminal[] nonterminals;
20 public String toString() {
22 for(NonTerminal nt : nonterminals) ret += nt + ", ";
26 public abstract static class Un extends El {
27 public Seq[][] sequences;
28 public void build(MetaGrammar.Context cx, Union u) {
29 HashSet<Sequence> bad2 = new HashSet<Sequence>();
30 for(int i=0; i<sequences.length; i++) {
31 Seq[] group = sequences[i];
32 Union u2 = new Union();
33 if (sequences.length==1) u2 = u;
34 for(int j=0; j<group.length; j++) {
35 group[j].build(cx, u2, false);
37 if (sequences.length==1) break;
38 Sequence seq = Sequence.singleton(u2);
39 for(Sequence s : bad2) {
44 bad2.add(Sequence.singleton(u2));
48 public static class NonTerminal extends Un {
49 public String name = null;
50 public @bind NonTerminal(@bind.arg("Word") String name,
51 @bind.arg("RHS") Seq[][] sequences) {
53 this.sequences = sequences;
55 public Element build(MetaGrammar.Context cx) { return cx.get(name); }
58 public static class AnonUn extends Un {
59 public @bind.as("(") AnonUn(Seq[][] sequences) {
60 this.sequences = sequences;
62 public Element build(MetaGrammar.Context cx) {
63 Union ret = new Union();
69 //public static @bind.as void range(char c) { }
70 public static class Range {
71 public @bind.as("range") Range(char only) { first = only; last = only; }
72 public @bind.as("-") Range(char first, char last) { this.first = first; this.last = last; }
76 public static abstract class El {
77 public String getLabel() { return null; }
78 public String getOwnerTag() { return null; }
79 public boolean drop() { return false; }
80 public abstract Element build(MetaGrammar.Context cx);
82 public static class Drop extends El {
84 public Drop(El e) { this.e = e; }
85 public String getLabel() { return null; }
86 public boolean drop() { return true; }
87 public String getOwnerTag() { return e.getOwnerTag(); }
88 public Element build(MetaGrammar.Context cx) { return e.build(cx); }
90 public static class Label extends El {
93 public Label(String label, El e) { this.e = e; this.label = label; }
94 public String getLabel() { return label; }
95 public String getOwnerTag() { return e.getOwnerTag(); }
96 public Element build(MetaGrammar.Context cx) { return e.build(cx); }
98 public static /*abstract*/ class Seq {
99 HashSet<Seq> and = new HashSet<Seq>();
100 HashSet<Seq> not = new HashSet<Seq>();
105 public Seq(El e) { this(new El[] { e }); }
106 public Seq(El[] elements) { this.elements = elements; }
107 public Seq tag(String tag) { this.tag = tag; return this; }
108 public Seq follow(El follow) { this.follow = follow; return this; }
110 Seq ret = new Seq(elements);
117 public Seq and(Seq s) { and.add(s); s.lame = true; return this; }
118 public Seq andnot(Seq s) { not.add(s); s.lame = true; return this; }
119 public Seq separate(El sep) {
120 El[] elements = new El[this.elements.length * 2 - 1];
121 for(int i=0; i<this.elements.length; i++) {
122 elements[i*2] = this.elements[i];
123 if (i<this.elements.length-1)
124 elements[i*2+1] = new Drop(sep);
126 this.elements = elements;
129 public Sequence build(MetaGrammar.Context cx, Union u, boolean lame) {
130 Sequence ret = build0(cx, lame || this.lame);
131 for(Seq s : and) { Sequence dork = s.build(cx, u, true); ret = ret.and(dork); }
132 for(Seq s : not) { Sequence dork = s.build(cx, u, true); ret = ret.not(dork); }
137 public Sequence build0(MetaGrammar.Context cx, boolean lame) {
138 boolean unwrap = false;
139 boolean dropAll = lame;
140 if (tag!=null && tag.equals("[]")) unwrap = true;
141 if (tag!=null && "()".equals(tag)) dropAll = true;
142 Object[] labels = new Object[elements.length];
143 boolean[] drops = new boolean[elements.length];
144 Element[] els = new Element[elements.length];
145 for(int i=0; i<elements.length; i++) {
146 labels[i] = elements[i].getLabel();
147 drops[i] = elements[i].drop();
148 els[i] = elements[i].build(cx);
149 if (elements[i].getOwnerTag() != null)
150 tag = elements[i].getOwnerTag();
153 if (dropAll) ret = Sequence.drop(els, false);
154 else if (unwrap) ret = Sequence.unwrap(els, cx.rm.repeatTag(), drops);
155 else if (tag!=null) {
156 ret = cx.rm.resolveTag(tag, cx.cnt, els, labels, drops);
158 ret = cx.rm.tryResolveTag(tag, cx.cnt, els, labels, drops);
161 for(int i=0; i<els.length; i++)
163 if (idx==-1) idx = i;
164 else throw new Error("multiple non-dropped elements in sequence: " + Sequence.drop(els,false));
165 if (idx != -1) ret = Sequence.singleton(els, idx);
166 else ret = Sequence.drop(els, false);
169 if (this.follow != null)
170 ret.follow = infer(this.follow.build(cx));
171 ret.lame = this.lame;
175 public static @bind.as("&") Seq and(Seq s, El[] elements) { return s.and(seq(elements)); }
176 public static @bind.as("&~") Seq andnot(Seq s, El[] elements) { return s.andnot(seq(elements)); }
177 public static @bind.as("->") Seq arrow(Seq s, El e) { return s.follow(e); }
178 public static @bind.as("::") Seq tag(String tagname, Seq s) { return s.tag(tagname); }
179 public static @bind.as("/") Seq slash(Seq s, El e) { return s.separate(e); }
181 public static @bind.as("ps") Seq seq(El[] elements) { return new Seq(elements); }
182 public static @bind.as Seq psx(Seq s) { return s; }
183 public static @bind.as(":") El colon(String s, El e) { return new Label(s, e); }
184 public static @bind.as(")") void close(String foo) { throw new Error("not supported"); }
185 public static @bind.as("()") El epsilon() { return new Constant(Union.epsilon); }
187 public static @bind.as("nonTerminal") class NonTerminalReference extends El {
188 public @bind.arg String nonTerminal;
189 public Element build(MetaGrammar.Context cx) {
190 return cx.get(nonTerminal);
194 public static class StringLiteral extends Constant {
195 public @bind.as("literal") StringLiteral(String string) { super(CharRange.string(string)); }
196 public boolean drop() { return true; }
199 public static class CharClass extends El {
201 public @bind.as("[") CharClass(Range[] ranges) { this.ranges = ranges; }
202 public Element build(MetaGrammar.Context cx) {
203 edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
204 for(Range r : ranges)
205 set.add(r.first, r.last);
206 return CharRange.set(set);
210 public static @bind.as("{") class XTree extends El {
211 public @bind.arg Seq body;
212 public Element build(MetaGrammar.Context cx) {
217 public static class Rep extends El {
219 public boolean zero, many, max;
220 public Rep(El e, El sep, boolean zero, boolean many, boolean max) {
221 this.e = e; this.sep = sep; this.zero = zero; this.many = many; this.max = max;}
222 public Element build(MetaGrammar.Context cx) {
224 ? Sequence.repeat(e.build(cx), zero, many, sep==null ? null : sep.build(cx), cx.rm.repeatTag())
226 ? Sequence.repeatMaximal(infer(e.build(cx)), zero, many, cx.rm.repeatTag())
227 : Sequence.repeatMaximal(e.build(cx), zero, many, infer(sep.build(cx)), cx.rm.repeatTag());
230 public static class Constant extends El {
232 public Constant(Element constant) { this.constant = constant; }
233 public Element build(MetaGrammar.Context cx) { return constant; }
235 public abstract static class PostProcess extends El {
237 public PostProcess(El e) { this.e = e; }
238 public Element build(MetaGrammar.Context cx) { return postProcess(e.build(cx)); }
239 public abstract Element postProcess(Element e);
242 // FIXME: it would be nice if we could hoist this into "Rep"
243 public static @bind.as("++") El plusmax(final El e) { return new Rep(e, null, false, true, true); }
244 public static @bind.as("+") El plus(final El e) { return new Rep(e, null, false, true, false); }
245 public static @bind.as("++/") El plusmaxfollow(final El e, final El sep) { return new Rep(e, sep, false, true, true); }
246 public static @bind.as("+/") El plusfollow(final El e, final El sep) { return new Rep(e, sep, false, true, false); }
247 public static @bind.as("**") El starmax(final El e) { return new Rep(e, null, true, true, true); }
248 public static @bind.as("*") El star(final El e) { return new Rep(e, null, true, true, false); }
249 public static @bind.as("**/") El starmaxfollow(final El e, final El sep) { return new Rep(e, sep, true, true, true); }
250 public static @bind.as("*/") El starfollow(final El e, final El sep) { return new Rep(e, sep, true, true, false); }
251 public static @bind.as("?") El question(final El e) { return new Rep(e, null, true, true, false); }
253 public static @bind.as("!") El bang(final El e) { return new Drop(e); }
255 public static @bind.as("^") El caret(final String s) {
256 return new Drop(new Constant(CharRange.string(s)) {
257 public String getOwnerTag() { return s; }
261 public static @bind.as("~") El tilde(final El e) {
262 return new PostProcess(e) {
263 public Element postProcess(Element e) {
264 return infer((Topology<Character>)Atom.toAtom(e).complement());
267 public static @bind.as("^^") void doublecaret(final El e) { throw new Error("not implemented"); }
269 //public static @bind.as("(") El subexpression(Seq[][] rhs) { return new NonTerminal(rhs); }
271 public static @bind.as("Word") String word(String s) { return s; }
272 public static @bind.as("Quoted") String quoted(String s) { return s; }
273 public static @bind.as("escaped") String c(char c) { return c+""; }
274 public static @bind.as("\"\"") String emptystring() { return ""; }
275 public static @bind.as("\n") String retur() { return "\n"; }
276 public static @bind.as("\r") String lf() { return "\r"; }
278 static Atom infer(Element e) { return infer((Topology<Character>)Atom.toAtom(e)); }
279 static Atom infer(Topology<Character> t) { return new CharRange(new CharTopology(t)); }