From bb4357ee026741cacecdfbfdcdd2d44699306536 Mon Sep 17 00:00:00 2001 From: adam Date: Sun, 2 Jul 2006 04:28:13 -0400 Subject: [PATCH 1/1] checkpoint darcs-hash:20060702082813-5007d-6ff68cfe1836f568dfb921d43f0a67061814f36f.gz --- Makefile | 2 +- src/edu/berkeley/sbp/misc/Demo.java | 78 +++++++------- src/edu/berkeley/sbp/misc/MetaGrammar.java | 50 ++++++--- src/edu/berkeley/sbp/tib/TibDoc.java | 152 ++++++++++++++++++++-------- tests/tibdoc.g | 16 +-- 5 files changed, 197 insertions(+), 101 deletions(-) diff --git a/Makefile b/Makefile index c9da038..181de0b 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ java = java tibdoc: edu.berkeley.sbp.jar $(java) -cp $< edu.berkeley.sbp.tib.TibDoc \ tests/tibdoc.g \ - tests/input.tibdoc + tests/tib.in demo: edu.berkeley.sbp.jar $(java) -cp $< edu.berkeley.sbp.misc.Demo \ diff --git a/src/edu/berkeley/sbp/misc/Demo.java b/src/edu/berkeley/sbp/misc/Demo.java index 845b1cb..c567edd 100644 --- a/src/edu/berkeley/sbp/misc/Demo.java +++ b/src/edu/berkeley/sbp/misc/Demo.java @@ -8,27 +8,16 @@ import java.lang.reflect.*; import java.io.*; public class Demo { + + public static boolean harsh = false; + public static void main(String[] s) throws Exception { + + ReflectiveMeta m = new ReflectiveMeta(); Tree res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1(); - - ReflectiveMeta m = - new ReflectiveMeta(MG.class, - new Class[] { - MG.Grammar.class, - MG.NonTerminal.class, - MG.AnonUn.class, - MG.Range.class, - MG.El.class, - MG.Seq.class, - MG.NonTerminalReference.class, - MG.StringLiteral.class, - MG.Tree.class, - MG.CharClass.class - }); 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()); @@ -60,6 +49,21 @@ public class Demo { public static class ReflectiveMeta extends MetaGrammar.Meta { private final Class _cl; private final Class[] _inner; + public ReflectiveMeta() { + this(MG.class, + new Class[] { + MG.Grammar.class, + MG.NonTerminal.class, + MG.AnonUn.class, + MG.Range.class, + MG.El.class, + MG.Seq.class, + MG.NonTerminalReference.class, + MG.StringLiteral.class, + MG.Tree.class, + MG.CharClass.class + }); + } public ReflectiveMeta(Class c, Class[] inner) { this._cl = c; this._inner = inner; @@ -108,7 +112,7 @@ public class Demo { } }; } - public Sequence resolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) { + public Sequence tryResolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) { Production p = new Production(tag, nonTerminalName, els, labels, drops); for(Method m : _cl.getMethods()) if (new TargetMethod(m).isCompatible(p)) @@ -120,20 +124,21 @@ public class Demo { for(Class c : _inner) if (new TargetClass(c).isCompatible(p)) return new TargetClass(c).makeSequence(p); - throw new RuntimeException("could not find a Java method/class/ctor matching tag \""+tag+"\", nonterminal \""+nonTerminalName+"\" with " + els.length + " arguments"); + return null; + } + public Sequence resolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) { + Sequence ret = tryResolveTag(tag, nonTerminalName, els, labels, drops); + if (ret != null) return ret; + String message = "could not find a Java method/class/ctor matching tag \""+tag+ + "\", nonterminal \""+nonTerminalName+"\" with " + els.length + " arguments"; + if (harsh) { + throw new RuntimeException(message); + } else { + System.err.println(message); + return Sequence.rewritingSequence(tag, els, labels, drops); + } } } - /* - public static Object makeFlattener(final Method m, final Element[] els, final Object[] labels, final boolean[] drops) { - return new Reducer() { - public Object reduce(Tree t) { - Object[] o = new Object[m.getParameterTypes()]; - int j = 0; - for(int i=0; i " + ret); } else { - int idx = -1; - for(int i=0; i t) { this.body = makeConjunct(t); } + public MetaTree(Conjunct c) { this.body = c; } public String toString() { return "{ " + body + " }"; } public Element build(BuildContext bc) { - return new Union("{}");// body.buildSequence(); + Union u = new Union(); + Union u2 = new Union(); + Sequence seq = body.buildSequence(bc); + u2.add(seq); + u.add(Sequence.singleton(new Element[] { CharRange.leftBrace, + new NonTerminalReference("ws").build(bc), + u2, + new NonTerminalReference("ws").build(bc), + CharRange.rightBrace } + , 2)); + //u.add(seq); + return u; } } - */ + public class MetaRange extends MetaClause { Range.Set range = new Range.Set(); public String toString() { return range.toString(); } @@ -371,6 +394,7 @@ public class MetaGrammar extends StringWalker { public class NonTerminalReference extends MetaClause { public String name; public NonTerminalReference(Tree name) { this.name = string(name); } + public NonTerminalReference(String name) { this.name = name; } public Element build(BuildContext bc) { return bc.build(name); } public String toString() { return name; } } diff --git a/src/edu/berkeley/sbp/tib/TibDoc.java b/src/edu/berkeley/sbp/tib/TibDoc.java index e0414c8..fbe833e 100644 --- a/src/edu/berkeley/sbp/tib/TibDoc.java +++ b/src/edu/berkeley/sbp/tib/TibDoc.java @@ -9,6 +9,7 @@ import edu.berkeley.sbp.util.*; import edu.berkeley.sbp.chr.*; import java.util.*; import java.io.*; +import static edu.berkeley.sbp.misc.Demo.*; public class TibDoc { /* @@ -173,49 +174,6 @@ public class TibDoc { - // Main ////////////////////////////////////////////////////////////////////////////// - - public static void main(String[] s) throws Exception { - try { - System.out.println("parsing " + s[0]); - Tree res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1(); - MetaGrammar gram = new Tib.Grammar(TibDoc.class); - gram = (MetaGrammar)gram.walk(res); - System.out.println("\nparsing " + s[1]); - Forest f = new CharParser(gram.done()).parse(new Tib(new FileInputStream(s[1]))); - System.out.println(); - System.out.println(f.expand1().toPrettyString()); - System.out.println(); - Doc doc = (Doc)new ReflectiveGrammar(TibDoc.class).build(f.expand1()); - System.out.println(doc); - System.out.println(); - System.out.println(); - System.out.println(); - System.out.println(); - StringBuffer sb = new StringBuffer(); - doc.toHTML(new ToHTML.HTML(sb)); - System.out.println(sb); - - FileOutputStream fos = new FileOutputStream("out.html"); - PrintWriter p = new PrintWriter(new OutputStreamWriter(fos)); - p.println(sb); - p.flush(); - p.close(); - - } catch (Ambiguous a) { - FileOutputStream fos = new FileOutputStream("/Users/megacz/Desktop/out.dot"); - PrintWriter p = new PrintWriter(new OutputStreamWriter(fos)); - GraphViz gv = new GraphViz(); - a.ambiguity.toGraphViz(gv); - gv.dump(p); - p.flush(); - p.close(); - a.printStackTrace(); - - } catch (Exception e) { - e.printStackTrace(); - } - } */ /* @@ -363,5 +321,113 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix */ + + // Main ////////////////////////////////////////////////////////////////////////////// + + public static class TD { + + public @nonterminal("Doc") static class Doc { + public @arg("head") Header head; + public @arg("body") Body body; + public String toString() { return "doc:{"+head+","/*+body*/+"}"; } + } + + public @nonterminal("Header") static class Header { + public @arg("attrs") KV[] attrs; + // FIXME: it would be nice to be able to + // void kv(String, String) { ... } imperatively + public String toString() { + StringBuffer ret = new StringBuffer(); + ret.append("<"); + for(KV kv : attrs) ret .append(kv); + ret.append(">"); + return ret.toString(); + } + } + + public @nonterminal("Body") static class Body { + public Object[] sections; + // FIXME: it would be nice to be able to + // void kv(String, String) { ... } imperatively + public String toString() { + StringBuffer ret = new StringBuffer(); + ret.append("<"); + for(Object kv : sections) ret .append(kv); + ret.append(">"); + return ret.toString(); + } + } + + public @nonterminal("kv") static class KV { + public @arg("key") String key; + public @arg("val") Object val; + public String toString() { return "KV["+key+"="+val+"]"; } + } + } + + public static void main(String[] s) throws Exception { + try { + + Demo.ReflectiveMeta m = + new Demo.ReflectiveMeta(TibDoc.TD.class, + new Class[] { + TibDoc.TD.Doc.class, + TibDoc.TD.Header.class, + TibDoc.TD.Body.class, + TibDoc.TD.KV.class + }); + Tree 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 tibgram = mgf.get("s").build(bc); + + System.err.println("parsing " + s[1]); + Tree t = new CharParser(tibgram).parse(new Tib(new FileInputStream(s[1]))).expand1(); + System.out.println("tree:\n" + t.toPrettyString()); + + Reducer red = (Reducer)t.head(); + Object result = red.reduce(t); + System.out.println((TD.Doc)result); + /* + System.out.println("parsing " + s[0]); + Tree res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1(); + MetaGrammar gram = new Tib.Grammar(TibDoc.class); + gram = (MetaGrammar)gram.walk(res); + System.out.println("\nparsing " + s[1]); + Forest f = new CharParser(gram.done()).parse(new Tib(new FileInputStream(s[1]))); + System.out.println(); + System.out.println(f.expand1().toPrettyString()); + System.out.println(); + Doc doc = (Doc)new ReflectiveGrammar(TibDoc.class).build(f.expand1()); + System.out.println(doc); + System.out.println(); + System.out.println(); + System.out.println(); + System.out.println(); + StringBuffer sb = new StringBuffer(); + doc.toHTML(new ToHTML.HTML(sb)); + System.out.println(sb); + + FileOutputStream fos = new FileOutputStream("out.html"); + PrintWriter p = new PrintWriter(new OutputStreamWriter(fos)); + p.println(sb); + p.flush(); + p.close(); + */ + } catch (Ambiguous a) { + FileOutputStream fos = new FileOutputStream("/Users/megacz/Desktop/out.dot"); + PrintWriter p = new PrintWriter(new OutputStreamWriter(fos)); + GraphViz gv = new GraphViz(); + a.ambiguity.toGraphViz(gv); + gv.dump(p); + p.flush(); + p.close(); + a.printStackTrace(); + + } catch (Exception e) { + e.printStackTrace(); + } + } + } diff --git a/tests/tibdoc.g b/tests/tibdoc.g index ec31d30..56a84c6 100644 --- a/tests/tibdoc.g +++ b/tests/tibdoc.g @@ -56,9 +56,9 @@ nw = ~[\r\n\ ] s = Doc -Doc = Doc:: head:{Header} body:Body /ws -Header = Header:: "header" attrs:{ kv */ ws } /ws -Body = Body:: sections:(Section*/ws) +Doc = head:{Header} body:Body /ws +Header = "header" attrs:{ kv */ ws } /ws +Body = Section*/ws Section = { Section:: header:SectionHeader paragraphs:Paragraph* /ws } SectionHeader = "==" SectionHeaderBody "==" SectionHeaderBody = "=" SectionHeaderBody "=" @@ -67,7 +67,7 @@ SectionHeaderBody = "=" SectionHeaderBody "=" sp = " "** blank = !sp "\n" !sp "\n" !ws -kv = kv:: key:word "=" val:text /ws +kv = key:word "=" val:text /ws wp = w++ num = [0-9]++ Paragraph = Blockquote:: { "\"\" " text } @@ -77,8 +77,8 @@ Paragraph = Blockquote:: { "\"\" " text } onums = nums !(". "|") ") any = ~[]* -uli = "* " (!ws text &~ any (oli|uli)) -oli = !("# "|onums) (!ws text &~ any (oli|uli)) +uli = "* " (!ws text &~ any !(oli|uli)) +oli = !("# "|onums) (!ws text &~ any !(oli|uli)) text = Item Itemx = !ws Item @@ -92,7 +92,7 @@ Item = blockquote > "[]":: styled Itemx > "[]":: (Chars:: text:alphanum++) Itemx > "[]":: "\"" text "\"" Itemx - > "[]":: symbol Itemx +// > "[]":: symbol Itemx > "[]":: (Symbol:: sym++) Itemx > "[]":: Paragraph Itemx @@ -118,7 +118,7 @@ block = { text } link = Link:: text:({ text }) "->" href:(url|email) > Link:: text:alphanum++ !ws "->" href:(url|email) -structured = command & "\\" [a-zA-Z0-9]++ block? +structured = command & "\\" !([a-zA-Z0-9]++) block? > glyph > email > url -- 1.7.10.4