}
public static class NonTerminal extends Un {
+ public boolean rep;
public String name = null;
- public @bind NonTerminal(@bind.arg String name,
- @bind.arg Seq[][] sequences) {
+ public String sep = null;
+ public @bind NonTerminal(@bind.arg String name, @bind.arg Seq[][] sequences) { this(name, sequences, false); }
+ public NonTerminal(String name, Seq[][] sequences, boolean rep) { this(name, sequences, rep, null); }
+ public NonTerminal(String name, Seq[][] sequences, boolean rep, String sep) {
this.name = name;
this.sequences = sequences;
+ this.rep = rep;
+ this.sep = sep;
}
public Element build(MetaGrammar.Context cx) { return cx.get(name); }
+ public void build(MetaGrammar.Context cx, Union u) {
+ if (!rep) { super.build(cx, u); return; }
+ HashSet<Sequence> bad2 = new HashSet<Sequence>();
+
+ Union urep = new Union();
+ urep.add(Sequence.empty);
+ urep.add(Sequence.singleton(new Element[] { cx.get(sep), u }, 1));
+
+ for(int i=0; i<sequences.length; i++) {
+ Seq[] group = sequences[i];
+ Union u2 = new Union();
+ if (sequences.length==1) u2 = u;
+ for(int j=0; j<group.length; j++) {
+ Union u3 = new Union();
+ group[j].build(cx, u3, false);
+ Sequence s = Sequence.unwrap(new Element[] { u3, urep },
+ cx.rm.repeatTag(),
+ new boolean[] { false, false });
+ u2.add(s);
+ }
+ if (sequences.length==1) break;
+ Sequence seq = Sequence.singleton(u2);
+ for(Sequence s : bad2) {
+ s.lame = true;
+ seq = seq.not(s);
+ }
+ u.add(seq);
+ bad2.add(Sequence.singleton(u2));
+ }
+ }
+ }
+ public static @bind.as("=") NonTerminal go(@bind.arg String name, @bind.arg Seq[][] sequences) { return new NonTerminal(name, sequences, true); }
+ public static @bind.as("=") NonTerminal go(@bind.arg String name, @bind.arg String sep, @bind.arg Seq[][] sequences) {
+ return new NonTerminal(name, sequences, true, sep);
}
public static class AnonUn extends Un {
El follow;
String tag = null;
boolean lame;
+ public void append(El e) {
+ El[] elements = new El[this.elements.length+1];
+ System.arraycopy(this.elements, 0, elements, 0, this.elements.length);
+ this.elements = elements;
+ elements[elements.length-1] = e;
+ }
public Seq(El e) { this(new El[] { e }); }
public Seq(El[] elements) { this.elements = elements; }
public Seq tag(String tag) { this.tag = tag; return this; }
return ret;
}
public Sequence build0(MetaGrammar.Context cx, boolean lame) {
- boolean unwrap = false;
boolean dropAll = lame;
- if (tag!=null && tag.equals("[]")) unwrap = true;
if (tag!=null && "()".equals(tag)) dropAll = true;
Object[] labels = new Object[elements.length];
boolean[] drops = new boolean[elements.length];
}
Sequence ret = null;
if (dropAll) ret = Sequence.drop(els, false);
- else if (unwrap) ret = Sequence.unwrap(els, cx.rm.repeatTag(), drops);
- else if (tag!=null) {
- ret = cx.rm.resolveTag(tag, cx.cnt, els, labels, drops);
- } else {
+ else {
ret = cx.rm.tryResolveTag(tag, cx.cnt, els, labels, drops);
if (ret == null) {
int idx = -1;
public static @bind.as("{") class XTree extends El {
public @bind.arg Seq body;
public Element build(MetaGrammar.Context cx) {
- throw new Error();
+ Union u = new Union();
+ Sequence s = body.build(cx, u, false);
+ Union u2 = new Union();
+ u2.add(Sequence.singleton(new Element[] {
+ CharRange.leftBrace,
+ cx.get("ws"),
+ u,
+ cx.get("ws"),
+ CharRange.rightBrace
+ }, 2));
+ return u2;
}
}
//public static @bind.as("(") El subexpression(Seq[][] rhs) { return new NonTerminal(rhs); }
- public static @bind.as("Word") String word(String s) { return s; }
- public static @bind.as("Quoted") String quoted(String s) { return s; }
- public static @bind.as("escaped") String c(char c) { return c+""; }
- public static @bind.as("EmptyString") String emptystring() { return ""; }
- public static @bind.as("\n") String retur() { return "\n"; }
- public static @bind.as("\r") String lf() { return "\r"; }
+ public static @bind.as("Word") String word(String s) { return s; }
+ public static @bind.as("Quoted") String quoted(String s) { return s; }
+ public static @bind.as("escaped") String c(char c) { return c+""; }
+ public static @bind.as("EmptyString") String emptystring() { return ""; }
+ public static @bind.as("\n") String retur() { return "\n"; }
+ public static @bind.as("\r") String lf() { return "\r"; }
static Atom infer(Element e) { return infer((Topology<Character>)Atom.toAtom(e)); }
static Atom infer(Topology<Character> t) { return new CharRange(new CharTopology(t)); }