public static Union epsilon = new Union("()");
static { epsilon.add(Sequence.empty); }
- // MetaGrammar //////////////////////////////////////////////////////////////////////////////
+ ////////////////////////////////////////////////////////////////////////////////
- public Object _star_(Element r) { return rep(r, null, false, true); }
- public Element epsilon(Object o, Object b) { return epsilon; }
- public Element _rightparen_(Object e) { return SELF; }
+ private Union g;
+ private HashMap<String,Union> nt;
+ private int anon = 0;
+ private String startSymbol;
+
+ public MetaGrammar() { this("s"); }
+ public MetaGrammar(String s) { done(s); }
+ public Union done() { return done("s"); }
+ public Union done(String str) {
+ Union ret = g;
+ g = new Union(str);
+ startSymbol = str;
+ nt = new HashMap<String,Union>();
+ dropAll = new HashSet<Element>();
+ nt.put(str, g);
+ return ret;
+ }
+
+
+ // MetaGrammar //////////////////////////////////////////////////////////////////////////////
public PreSequence _amp_(PreSequence p, Object[] o) { return p.and(new PreSequence(o, null, true).buildSequence(null, true, false)); }
public PreSequence _amp__tilde_(PreSequence p, Object[] o) { return p.not(new PreSequence(o, null, true).buildSequence(null, true, false)); }
-
- public Element _bang_(Element r) { return r; }
- public Object care(String s) { return new MyLift(s); }
- public Object _caret_(String s) { return new MyLift(s); }
- public Object _leftparen__rightparen_() { return epsilon; }
-
+ public Object _star_(Element r) { return Repeat.many0(r); }
+ public Element epsilon(Object o, Object b) { return epsilon; }
+ public Element _rightparen_(Object e) { return SELF; }
+ public Element _bang_(Element r) { return r; }
+ public Object _caret_(String s) { return new MyLift(s); }
+ public Object _leftparen__rightparen_() { return epsilon; }
public Union nonTerminal(String s) { return nonTerminal(s, null, false, false); }
public Union _colon__colon__equals_(String s, PreSequence[][] p) { return nonTerminal(s, p, false, false); }
public Union _bang__colon__colon__equals_(String s, PreSequence[][] p) { return nonTerminal(s, p, false, true); }
return n;
}
-
public Object _backslash__leftbrace_(String s) { return SELF; }
public Object _leftbrace_(String s) { return SELF; }
- public Object _plus_(final Element r) { return rep(r, null, false, false); }
+ public Object _plus_(final Element r) { return Repeat.many1(r); }
+
+ public PreSequence rewrite(Object[] o) { return new PreSequence(o, null); }
+ public PreSequence _equals__greater_(Object[] o, String s) { return new PreSequence(o, s); }
+ public Object[] seq(Object[] o) { return o; }
public Object[] _slash_(Object[] o, Object sep) {
if (o.length <= 1) return o;
Object[] ret = new Object[o.length * 2 - 1];
}
return ret;
}
- public Object _plus__slash_(final Element r, Object s) { return rep(r, (Element)s, false, false); }
- public Object _star__slash_(final Element r, Object s) { return rep(r, (Element)s, false, true); }
- public Object _star__star_(final Element r) { return rep(r, null, true, true); }
- public Object _plus__plus_(final Element r) { return rep(r, null, true, false); }
+
+ public Object _plus__slash_(final Element r, Element s) { return Repeat.many1(r, s); }
+ public Object _star__slash_(final Element r, Element s) { return Repeat.many0(r, s); }
+ public Object _star__star_(final Element r) { return Repeat.maximal(Repeat.many0(r)); }
+ public Object _plus__plus_(final Element r) { return Repeat.maximal(Repeat.many1(r)); }
public Element _question_(final Element r) { return Repeat.maybe(r); }
public MetaGrammar gram(Object o, MetaGrammar g, Object o2) { return g; }
public MetaGrammar grammar(Object[] o) { return this; }
public char _backslash_r() { return '\r'; }
public Object literal(String s) { return new MyDrop(CharToken.string(s)); }
public Range _minus_(char a, char b) { return new Range(a, b); }
- public Element _leftbracket_ (Range[] rr) { return ranges(null, rr); }
- public Element _leftbracket__tilde_(Range[] rr) { return ranges("~", rr); }
public Range range(char a) { return new Range(a, a); }
- public Element ranges(Object o, Range[] rr) {
- Range.Set ret = !"~".equals(o+"") ? new Range.Set() : new Range.Set(new Range(true, true));
+ public Element _leftbracket_ (Range[] rr) { return ranges(true, rr); }
+ public Element _leftbracket__tilde_(Range[] rr) { return ranges(false, rr); }
+ public Element ranges(boolean positive, Range[] rr) {
+ Range.Set ret = positive ? new Range.Set() : new Range.Set(new Range(true, true));
if (rr != null)
for(Range r : rr)
- if (!"~".equals(o+"")) ret.add(r);
- else ret.remove(r);
+ if (positive) ret.add(r);
+ else ret.remove(r);
return CharToken.set(ret);
}
-
- ////////////////////////////////////////////////////////////////////////////////
-
- private Union g;
- private HashMap<String,Union> nt;
- private int anon = 0;
- private Element dws;
- private String startSymbol;
-
- public MetaGrammar() { this("s"); }
- public MetaGrammar(String s) { done(s); }
- public Union done() { return done("s"); }
- public Union done(String str) {
- Union ret = g;
- g = new Union(str);
- startSymbol = str;
- nt = new HashMap<String,Union>();
- dropAll = new HashSet<Element>();
- nt.put(str, g);
- this.dws = Repeat.maximal(Repeat.many0(nonTerminal("w")));
- return ret;
- }
-
- public String stringify(String s) { return StringUtil.unescapify(s); }
- public char unescape(char x, char c) { return unescape(c); }
- public char unescape(char c) { return StringUtil.unescapify("\\"+c).charAt(0); }
- public PreSequence sequence(Object[] o) { return new PreSequence(o, null); }
-
- public PreSequence _equals__greater_(Object[] o, String s) { return new PreSequence(o, s); }
- public PreSequence wrap(Object[] o) { return new PreSequence(o, ""); }
- public PreSequence mwrap(Object[] o) { return new PreSequence(o, ""); }
- public PreSequence rewrite(Object[] o) { return new PreSequence(o, null); }
- public PreSequence rewrite(Object[] o, Object o2) {
- Object[] o3 = new Object[o.length + 1];
- System.arraycopy(o, 0, o3, 0, o.length);
- o3[o3.length-1] = o2;
- return rewrite(o3);
- }
-
- public static class PreBrace {
- public final Object[] o;
- public PreBrace(Object[] o) { this.o = o; }
+ public static class MyLift {
+ public final String s;
+ public MyLift(String s) { this.s = s; }
}
-
- public Element rep(Element e, Element sep, boolean maximal, boolean zero) {
- Element ret = zero ? Repeat.many0(e, sep) : Repeat.many1(e, sep);
- return maximal ? Repeat.maximal(ret) : ret;
+ public static class MyDrop {
+ public final Object o;
+ public MyDrop(Object o) { this.o = o; }
}
public static class PreSequence {
public PreSequence(Object[] o, String tag) { this(o, tag, false); }
public PreSequence(Object[] o, String tag, boolean keeper) { this.o = o; this.tag = tag; this.keeper = keeper; }
boolean[] drops = null;
- public Element[] expand(Union u, HashSet<Sequence> set) {
- if (o==null) return new Element[0];
- Element[] o2 = new Element[o.length];
- drops = new boolean[o.length];
- int j = 0;
- for(int i=0; i<o.length; i++) {
- Object oi = o[i];
- if (oi instanceof MyDrop) { oi = ((MyDrop)oi).o; drops[j] = true; }
- if (oi==SELF) o2[j] = u.new Subset("(("+u+"))", set);
- else if (oi instanceof MyLift) { o2[j] = CharToken.string(tag = ((MyLift)oi).s); drops[j] = true; }
- else o2[j] = (Element)oi;
-
- if (dropAll.contains(o2[j])) drops[j] = true;
-
- o2[j] = o2[j];
- j++;
- }
- return o2;
- }
public Union buildUnion() {
Union u = new Union("???");
u.add(buildSequence(u));
for(Sequence s : and) u.add(s);
for(Sequence s : not) u.add(s);
HashSet<Sequence> set = new HashSet<Sequence>();
- Element[] expansion = expand(u, set);
+ Element[] o2 = o==null ? new Element[0] : new Element[o.length];
+ if (o != null) {
+ drops = new boolean[o.length];
+ int j = 0;
+ for(int i=0; i<o.length; i++) {
+ Object oi = o[i];
+ if (oi instanceof MyDrop) { oi = ((MyDrop)oi).o; drops[j] = true; }
+
+ if (oi==SELF) o2[j] = u.new Subset("(("+u+"))", set);
+ else if (oi instanceof MyLift) { o2[j] = CharToken.string(tag = ((MyLift)oi).s); drops[j] = true; }
+ else o2[j] = (Element)oi;
+
+ if (MetaGrammar.dropAll.contains(o2[j])) drops[j] = true;
+ o2[j] = o2[j];
+ j++;
+ }
+ }
+ Element[] expansion = o2;
boolean keeper = this.keeper;
Sequence ret = dropAll || lame || keeper ? Sequence.drop(expansion, and, not, lame) : null;
- if (ret==null && tag!=null) {
- for(int i=0; i<expansion.length; i++) expansion[i] = expansion[i];
- ret = Sequence.rewritingSequence(tag, expansion, drops, and, not);
- }
+ if (ret==null && tag!=null) ret = Sequence.rewritingSequence(tag, expansion, drops, and, not);
if (ret==null) {
int idx = -1;
for(int i=0; i<expansion.length; i++) {
os.close();
}
- public static class MyLift {
- public final String s;
- public MyLift(String s) { this.s = s; }
- }
- public static class MyDrop {
- public final Object o;
- public MyDrop(Object o) { this.o = o; }
- }
-
public static final Tree meta =