_____________________________________________________________________________
Immediately
-- If a top-level rule has labels but no head-tag, like this
- Foo = a:Bar b:Baz
- then infer the name of the rule it belongs to
+- Migrate Demo.java -> MetaGrammar.java
+ - Figure out serialization
-create( $c:{...}, class ) =
- return create($c:{...})
-
-create( h:{...}, class ) =
-
-create( , String)
-
-create( _:{...}, String) = treat as char[]
-create( _:{...}, c[] ) = { create(.,c), create(.,c), ... }
-
-create( $c:{...} ) =
+- Clean up the prioritized-match garbage
+ - evil problems with (x y? z /ws)
- better ambiguity debugging tools / visualization
-
- ParseFailed, GSS, Walk, Parser, Sequence, Forest
-
- - Fix the metagrammar (really?)
- - evil problems with (x y? z /ws)
-
- copyright notices
- documentation
Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
MetaGrammar.Meta.MetaGrammarFile mgf = m.new MetaGrammarFile(res);
MetaGrammar.BuildContext bc = new MetaGrammar.BuildContext(mgf);
+
Union meta = mgf.get("s").build(bc);
- System.err.println("parsing " + s[1]);
Tree t = new CharParser(meta).parse(new FileInputStream(s[1])).expand1();
- System.out.println("tree:\n" + t.toPrettyString());
- Reducer red = (Reducer)t.head();
- MG.Grammar g = (MG.Grammar)red.reduce(t);
- System.out.println(g);
-
- Context cx = new Context(g,m);
- Element u = null;
- for(MG.NonTerminal nt : g.nonterminals) {
- Union el = (Union)cx.get(nt.name);
- StringBuffer st = new StringBuffer();
- el.toString(st);
- System.err.println(st);
- if (nt.name.equals("s")) u = el;
- }
- System.err.println();
+ Union u = Demo.make(t, "s");
+ System.err.println();
System.err.println("== parsing with parsed grammar =================================================================================");
t = new CharParser((Union)u).parse(new FileInputStream(s[1])).expand1();
System.out.println(t.toPrettyString());
return o;
}
+ public static Union make(Tree t, String s) {
+ ReflectiveMeta rm = new ReflectiveMeta();
+ Reducer red = (Reducer)t.head();
+ MG.Grammar g = (MG.Grammar)red.reduce(t);
+ Context cx = new Context(g,rm);
+ Union u = null;
+ for(MG.NonTerminal nt : g.nonterminals) {
+ Union el = (Union)cx.get(nt.name);
+ StringBuffer st = new StringBuffer();
+ el.toString(st);
+ System.err.println(st);
+ if (nt.name.equals(s)) u = el;
+ }
+ return u;
+ }
+
public static class MG {
public static @tag("grammar") class Grammar {
public NonTerminal get(String s) {
s = s2;
}
- //MetaGrammar mg0 = new MetaGrammar();
- //mg0.walk(MetaGrammar.meta);
- //System.out.println(mg0);
System.err.println("parsing " + s[0]);
Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
- //System.out.println(mg);
Union meta = MetaGrammar.make(res, "s");
System.err.println("parsing " + s[1]);
SequenceInputStream sis = new SequenceInputStream(new FileInputStream(s[0]), new FileInputStream(s[1]));
}
}
}
- /*
- public static class JavaGrammar extends MetaGrammar {
- public Object convertLabel(String label) { return new ClassMark(label); }
- public Object convertFieldLabel(String label) { return new FieldMark(label); }
-
- private static class FieldMark {
- public final String field;
- public FieldMark(String field) { this.field = field; }
- public String toString() { return "."+field; }
- }
- private static class ClassMark {
- public final String clazz;
- public ClassMark(String clazz) { this.clazz = clazz; }
- public String toString() { return clazz+"$"; }
- }
-
- public static Object build(Tree<Object> t, Class c) throws Exception {
- System.out.println("** build " + c.getName() + " " + t.toPrettyString());
- Object h = t.head();
- if (h instanceof ClassMark) return build2(t, Class.forName(JavaGrammar.class.getName()+"$"+((ClassMark)h).clazz));
- Object o = null;
- if (t.numChildren()==0) o = t.head();
- else if (t.head()==null) return build2(t, c);
- else if (t.head() instanceof FieldMark) {
- return build2(new Tree(null, new Tree[] { t }), c);
- } else {
- throw new Exception("don't know how to cope: " + c.getName() + " <- " + t.toPrettyString());
- }
- return Reflection.rebuild(o, c);
- }
- private static Object build2(Tree<Object> t, Class c) throws Exception {
- System.out.println("** build2 " + c.getName() + " " + t.toPrettyString());
- if (!Reflection.isConcrete(c)) {
- Field f = c.getField("subclasses");
- if (f==null) throw new Exception("don't know how to cope: " + c.getName() + " <- " + t.toPrettyString());
- Class[] subs = (Class[])f.get(null);
- OUTER: for(int i=0; i<subs.length; i++) {
- for(Tree<Object> t2 : t) {
- Object o2 = t2.head();
- System.out.println("checking " + o2 + " in " + subs[i].getName());
- if (o2 instanceof FieldMark) {
- if (subs[i].getField(((FieldMark)o2).field)==null) continue OUTER;
- }
- }
- c = subs[i];
- break;
- }
- }
- Object o = c.newInstance();
- for(Tree<Object> t2 : t) {
- Object o2 = t2.head();
- if (o2 instanceof FieldMark) {
- FieldMark f = (FieldMark)o2;
- Field field = c.getField(ReflectiveWalker.mangle(f.field));
- Object tgt = build(t2.child(0), field.getType());
- System.out.println("setting " + f.field +
- " on a " + o.getClass().getName() +
- " to " + (tgt==null?"null":tgt.getClass().getName()));
- field.set(o, tgt);
- }
- }
- System.out.println("returning a " + o.getClass().getName());
- return o;
- }
- public static Object build(Tree<Object> t) throws Exception {
- Object h = t.head();
- if (h instanceof ClassMark) {
- ClassMark cm = (ClassMark)h;
- Class c = Class.forName(JavaGrammar.class.getName() + "$" + ReflectiveWalker.mangle(cm.clazz));
- return build2(t, c);
- }
-
- return h==null ? null : h.toString();
- }
-
- public static interface Expr {
- public static Class[] subclasses = new Class[] { _plus_.class, num.class };
- }
- public static class _plus_ implements Expr {
- public Expr left;
- public Expr right;
- public String toString() { return left + "+" + right; }
- }
- public static class num implements Expr {
- public String val;
- public String toString() { return "["+val+"]"; }
- }
-
- }
- */
private static String pad(int i,String s) { return s.length() >= i ? s : pad(i-1,s)+" "; }
}
Doc = head:{Header} body:Body /ws
Header = "header" attrs:{ KeyVal */ ws } /ws
Body = {Section}*/ws
-Section = SectionHeader Paragraphs /ws
+Section = SectionHeader Paragraph* /ws
SectionHeader = "==" SectionHeaderBody "=="
SectionHeaderBody = "=" SectionHeaderBody "="
> !ws alphanum++ !ws
KeyVal = key:word "=" val:text /ws
wp = w++
num = [0-9]++
-Paragraphs = "[]":: { Blockquote:: "\"\" " text } Paragraphs
- > "[]":: { HR:: "---" "-"* } Paragraphs
- > "[]":: { P:: text } Paragraphs
- > ()
+
+Paragraph = Blockquote:: { "\"\" " text }
+ > HR:: { "---" "-"* }
+ > P:: { text }
onums = nums !(". "|") ")
any = ~[]*
text = Item
Itemx = !ws Item
| ()
-Item = blockquote
+Item = "[]":: blockquote Itemx
> "[]":: { UL:: uli+/ws } Itemx
| "[]":: { OL:: oli+/ws } Itemx
> "[]":: pre Itemx
> "[]":: (Symbol:: sym++) Itemx
> "[]":: { Block:: text } Itemx
-blockquote = Blockquote:: "\"\"" text "\"\""
- | Blockquote:: "\"\"" block
+blockquote = Blockquote:: "\"\"" (block | text "\"\"")
pre = Verbatim:: "[verbatim]" { ~[]+ } /ws // FIXME doesn't work