From 87a031919defe819658fc5f0ddeb2476193151c4 Mon Sep 17 00:00:00 2001 From: adam Date: Tue, 4 Jul 2006 22:17:48 -0400 Subject: [PATCH] checkpoint darcs-hash:20060705021748-5007d-1c5306807bb5c2d6c0296867a3b399b4d4bb8066.gz --- src/edu/berkeley/sbp/misc/Demo.java | 108 ++++++++++++------------ src/edu/berkeley/sbp/misc/RegressionTests.java | 12 +-- src/edu/berkeley/sbp/tib/TibDoc.java | 72 ++++++++-------- 3 files changed, 97 insertions(+), 95 deletions(-) diff --git a/src/edu/berkeley/sbp/misc/Demo.java b/src/edu/berkeley/sbp/misc/Demo.java index c9c18f3..82741d6 100644 --- a/src/edu/berkeley/sbp/misc/Demo.java +++ b/src/edu/berkeley/sbp/misc/Demo.java @@ -64,8 +64,8 @@ public class Demo { this._cl = c; this._inner = inner; } - private boolean match(Method m, String s) { return match(m.getAnnotation(tag.class), null, s); } - private boolean match(tag t, Class c, String s) { + private boolean match(Method m, String s) { return match(m.getAnnotation(bind.as.class), null, s); } + private boolean match(bind.as t, Class c, String s) { if (t==null) return false; if (t.value().equals(s)) return true; if (c != null && t.equals("") && c.getSimpleName().equals(s)) return true; @@ -80,14 +80,14 @@ public class Demo { } */ private boolean match(Class c, String s, String nonTerminalName) { - if (match((tag)c.getAnnotation(tag.class), c, s)) return true; - //if (match((nonterminal)c.getAnnotation(tag.class), c, nonTerminalName)) return true; + if (match((bind.as)c.getAnnotation(bind.as.class), c, s)) return true; + //if (match((nonterminal)c.getAnnotation(bind.as.class), c, nonTerminalName)) return true; return false; } public boolean match(Constructor con, String s, String nonTerminalName) { Class c = con.getDeclaringClass(); - if (match((tag)con.getAnnotation(tag.class), null, s)) return true; - //if (match((nonterminal)con.getAnnotation(tag.class), c, s)) return true; + if (match((bind.as)con.getAnnotation(bind.as.class), null, s)) return true; + //if (match((nonterminal)con.getAnnotation(bind.as.class), c, s)) return true; return false; } public Object repeatTag() { @@ -140,7 +140,9 @@ public class Demo { * If value() is undefined, then the class/constructor/method * name is used. */ - @Retention(RetentionPolicy.RUNTIME) public static @interface tag { String value() default ""; } + public static class bind { + @Retention(RetentionPolicy.RUNTIME) public static @interface as { String value() default ""; } + } /** * If any parameter to a method or field in a class has a named @@ -189,21 +191,21 @@ public class Demo { public Target(Reflection.Bindable b) { this._bindable = b; } public String getName() { return _bindable.getSimpleName(); } - public tag getTag() { return (tag)_bindable.getAnnotation(tag.class); } - //public nonterminal getNonTerminal() { return (nonterminal)_bindable.getAnnotation(tag.class); } + public bind.as getBindAs() { return (bind.as)_bindable.getAnnotation(bind.as.class); } + //public nonterminal getNonTerminal() { return (nonterminal)_bindable.getAnnotation(bind.as.class); } public String toString() { return _bindable.getSimpleName(); } public Object plant(Object[] fields) { return _bindable.impose(fields); } public boolean isRaw() { return _bindable.isAnnotationPresent(raw.class); } public Object invokeRaw(Iterable> t) { return _bindable.impose(new Object[] { t }); } public boolean isCompatible(Production p) { - tag t = getTag(); + bind.as t = getBindAs(); if (t != null && (t.value().equals(p.tag) || (t.value().equals("") && getName().equals(p.tag)))) return buildSequence(p)!=null; - tag n = getTag(); + bind.as n = getBindAs(); if (n != null && (n.value().equals(p.nonTerminal) || (n.value().equals("") && getName().equals(p.nonTerminal)))) @@ -364,7 +366,7 @@ public class Demo { } public static class MG { - public static @tag("grammar") class Grammar { + public static @bind.as("grammar") class Grammar { public NonTerminal get(String s) { for(NonTerminal nt : nonterminals) if (nt.name.equals(s)) @@ -402,7 +404,7 @@ public class Demo { } public static class NonTerminal extends Un { public String name = null; - public @tag("=") NonTerminal(@arg("Word") String name, + public @bind.as("=") NonTerminal(@arg("Word") String name, @arg("RHS") Seq[][] sequences) { this.name = name; this.sequences = sequences; @@ -411,7 +413,7 @@ public class Demo { } public static class AnonUn extends Un { - public @tag("(") AnonUn(Seq[][] sequences) { + public @bind.as("(") AnonUn(Seq[][] sequences) { this.sequences = sequences; } public Element build(Context cx) { @@ -421,10 +423,10 @@ public class Demo { } } - //public static @tag void range(char c) { } + //public static @bind.as void range(char c) { } public static class Range { - public @tag("range") Range(char only) { first = only; last = only; } - public @tag("-") Range(char first, char last) { this.first = first; this.last = last; } + public @bind.as("range") Range(char only) { first = only; last = only; } + public @bind.as("-") Range(char first, char last) { this.first = first; this.last = last; } public char first; public char last; } @@ -524,19 +526,19 @@ public class Demo { return ret; } } - public static @tag("&") Seq and(Seq s, El[] elements) { return s.and(seq(elements)); } - public static @tag("&~") Seq andnot(Seq s, El[] elements) { return s.andnot(seq(elements)); } - public static @tag("->") Seq arrow(Seq s, El e) { return s.follow(e); } - public static @tag("::") Seq tag(String tagname, Seq s) { return s.tag(tagname); } - public static @tag("/") Seq slash(Seq s, El e) { return s.separate(e); } + public static @bind.as("&") Seq and(Seq s, El[] elements) { return s.and(seq(elements)); } + public static @bind.as("&~") Seq andnot(Seq s, El[] elements) { return s.andnot(seq(elements)); } + public static @bind.as("->") Seq arrow(Seq s, El e) { return s.follow(e); } + public static @bind.as("::") Seq tag(String tagname, Seq s) { return s.tag(tagname); } + public static @bind.as("/") Seq slash(Seq s, El e) { return s.separate(e); } - public static @tag("ps") Seq seq(El[] elements) { return new Seq(elements); } - public static @tag Seq psx(Seq s) { return s; } - public static @tag(":") El colon(String s, El e) { return new Label(s, e); } - public static @tag(")") void close(String foo) { throw new Error("not supported"); } - public static @tag("()") El epsilon() { return new Constant(Union.epsilon); } + public static @bind.as("ps") Seq seq(El[] elements) { return new Seq(elements); } + public static @bind.as Seq psx(Seq s) { return s; } + public static @bind.as(":") El colon(String s, El e) { return new Label(s, e); } + public static @bind.as(")") void close(String foo) { throw new Error("not supported"); } + public static @bind.as("()") El epsilon() { return new Constant(Union.epsilon); } - public static @tag("nonTerminal") class NonTerminalReference extends El { + public static @bind.as("nonTerminal") class NonTerminalReference extends El { public @arg String nonTerminal; public Element build(Context cx) { return cx.get(nonTerminal); @@ -544,13 +546,13 @@ public class Demo { } public static class StringLiteral extends Constant { - public @tag("literal") StringLiteral(String string) { super(CharRange.string(string)); } + public @bind.as("literal") StringLiteral(String string) { super(CharRange.string(string)); } public boolean drop() { return true; } } public static class CharClass extends El { Range[] ranges; - public @tag("[") CharClass(Range[] ranges) { this.ranges = ranges; } + public @bind.as("[") CharClass(Range[] ranges) { this.ranges = ranges; } public Element build(Context cx) { edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set(); for(Range r : ranges) @@ -559,7 +561,7 @@ public class Demo { } } - public static @tag("{") class XTree extends El { + public static @bind.as("{") class XTree extends El { public @arg Seq body; public Element build(Context cx) { throw new Error(); @@ -592,40 +594,40 @@ public class Demo { } // FIXME: it would be nice if we could hoist this into "Rep" - public static @tag("++") El plusmax(final El e) { return new Rep(e, null, false, true, true); } - public static @tag("+") El plus(final El e) { return new Rep(e, null, false, true, false); } - public static @tag("++/") El plusmaxfollow(final El e, final El sep) { return new Rep(e, sep, false, true, true); } - public static @tag("+/") El plusfollow(final El e, final El sep) { return new Rep(e, sep, false, true, false); } - public static @tag("**") El starmax(final El e) { return new Rep(e, null, true, true, true); } - public static @tag("*") El star(final El e) { return new Rep(e, null, true, true, false); } - public static @tag("**/") El starmaxfollow(final El e, final El sep) { return new Rep(e, sep, true, true, true); } - public static @tag("*/") El starfollow(final El e, final El sep) { return new Rep(e, sep, true, true, false); } - public static @tag("?") El question(final El e) { return new Rep(e, null, true, true, false); } - - public static @tag("!") El bang(final El e) { return new Drop(e); } - - public static @tag("^") El caret(final String s) { + public static @bind.as("++") El plusmax(final El e) { return new Rep(e, null, false, true, true); } + public static @bind.as("+") El plus(final El e) { return new Rep(e, null, false, true, false); } + public static @bind.as("++/") El plusmaxfollow(final El e, final El sep) { return new Rep(e, sep, false, true, true); } + public static @bind.as("+/") El plusfollow(final El e, final El sep) { return new Rep(e, sep, false, true, false); } + public static @bind.as("**") El starmax(final El e) { return new Rep(e, null, true, true, true); } + public static @bind.as("*") El star(final El e) { return new Rep(e, null, true, true, false); } + public static @bind.as("**/") El starmaxfollow(final El e, final El sep) { return new Rep(e, sep, true, true, true); } + public static @bind.as("*/") El starfollow(final El e, final El sep) { return new Rep(e, sep, true, true, false); } + public static @bind.as("?") El question(final El e) { return new Rep(e, null, true, true, false); } + + public static @bind.as("!") El bang(final El e) { return new Drop(e); } + + public static @bind.as("^") El caret(final String s) { return new Drop(new Constant(CharRange.string(s)) { public String getOwnerTag() { return s; } }); } - public static @tag("~") El tilde(final El e) { + public static @bind.as("~") El tilde(final El e) { return new PostProcess(e) { public Element postProcess(Element e) { return MetaGrammar.infer((Topology)Atom.toAtom(e).complement()); } }; } - public static @tag("^^") void doublecaret(final El e) { throw new Error("not implemented"); } + public static @bind.as("^^") void doublecaret(final El e) { throw new Error("not implemented"); } - //public static @tag("(") El subexpression(Seq[][] rhs) { return new NonTerminal(rhs); } + //public static @bind.as("(") El subexpression(Seq[][] rhs) { return new NonTerminal(rhs); } - public static @tag("Word") String word(String s) { return s; } - public static @tag("Quoted") String quoted(String s) { return s; } - public static @tag("escaped") String c(char c) { return c+""; } - public static @tag("\"\"") String emptystring() { return ""; } - public static @tag("\n") String retur() { return "\n"; } - public static @tag("\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("\"\"") String emptystring() { return ""; } + public static @bind.as("\n") String retur() { return "\n"; } + public static @bind.as("\r") String lf() { return "\r"; } } public static class Context { diff --git a/src/edu/berkeley/sbp/misc/RegressionTests.java b/src/edu/berkeley/sbp/misc/RegressionTests.java index 02a5eee..3255630 100644 --- a/src/edu/berkeley/sbp/misc/RegressionTests.java +++ b/src/edu/berkeley/sbp/misc/RegressionTests.java @@ -103,18 +103,18 @@ public class RegressionTests { }); } public static class TestCaseMakerHelper extends MG { - public static @tag("grammaro") @raw Object grammaro(Iterable t) { + public static @bind.as("grammaro") @raw Object grammaro(Iterable t) { System.out.println("working on " + t); return Demo.make(t.iterator().next(), "s", new ReflectiveMetaPlain()); } - //public static @tag("tca") Object tca(Object[] o) throws IOException { + //public static @bind.as("tca") Object tca(Object[] o) throws IOException { //return new TestCase((String)o[0], (String[])o[1], (Union)o[2], false, false); } - public static @tag("tca") Object tca(String input, String[] output, Union u) throws IOException { + public static @bind.as("tca") Object tca(String input, String[] output, Union u) throws IOException { return new TestCase(input, output, u, false, false); } - public static @tag("tcb") Object tca(String input, Union u) throws IOException { + public static @bind.as("tcb") Object tca(String input, Union u) throws IOException { return new TestCase(input, new String[0], u, false, false); } - public static @tag("ts") TestCase[] go(TestCase[] cases) { return cases; } - public static @tag("o") Object o(Object[] s) { return s; } + public static @bind.as("ts") TestCase[] go(TestCase[] cases) { return cases; } + public static @bind.as("o") Object o(Object[] s) { return s; } } } diff --git a/src/edu/berkeley/sbp/tib/TibDoc.java b/src/edu/berkeley/sbp/tib/TibDoc.java index 2400ea4..572ccbe 100644 --- a/src/edu/berkeley/sbp/tib/TibDoc.java +++ b/src/edu/berkeley/sbp/tib/TibDoc.java @@ -330,34 +330,34 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix public static class TD { - public @tag static class Doc extends Dump { + public @bind.as static class Doc extends Dump { public @arg Header head; public @arg Body body; } - public @tag static class Header extends Dump { + public @bind.as static class Header extends Dump { public @arg KeyVal[] attrs; // FIXME: it would be nice to be able to // void KeyVal(String, String) { ... } imperatively } - public @tag static class Body extends Dump { + public @bind.as static class Body extends Dump { public Section[] sections; } - public @tag("Section") static class Section extends Dump { + public @bind.as("Section") static class Section extends Dump { public String header; public Paragraph[] paragraphs; } - public @tag static class KeyVal extends Dump { + public @bind.as static class KeyVal extends Dump { public @arg String key; public @arg Object val; } public abstract static class Paragraph extends Dump implements ToHTML { } - public @tag("P") static class P extends Paragraph { + public @bind.as("P") static class P extends Paragraph { public Text[] text; public P() { } public P(Text[] text) { this.text = text; } @@ -370,24 +370,24 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix } } - public @tag("HR") static class HR extends Paragraph { + public @bind.as("HR") static class HR extends Paragraph { public void toHTML(HTML h) { h.tag("hr"); } } - public @tag("Blockquote") static class Blockquote extends Paragraph { + public @bind.as("Blockquote") static class Blockquote extends Paragraph { Text[] text; public void toHTML(HTML h) { h.tag("blockquote", new P(text)); } } public abstract static class Text extends Dump implements ToHTML { } - public @tag static class Chars extends Text { + public @bind.as static class Chars extends Text { public String text; public Chars() { } public Chars(String text) { this.text = text; } public void toHTML(HTML h) { h.appendText(" " + text + " "); } public String toString() { return text; } } - public @tag static class Block extends Text { + public @bind.as static class Block extends Text { public Text[] text; public void toHTML(HTML h) { for(Text t : text) t.toHTML(h); } } @@ -402,32 +402,32 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix public String htmlTag() { return null; } public Object[] htmlTagParams() { return null; } } - public static @tag class Verbatim extends Text { public char[] c; public void toHTML(HTML h) { } } - //public @tag class Blockquote extends TextWrap { } - public static @tag class Underline extends TextWrap { public String htmlTag() { return "u"; } } - public static @tag class Footnote extends TextWrap { public String htmlTag() { return "small"; } } - public static @tag class TT extends TextWrap { public String htmlTag() { return "tt"; } } - //public @tag class Citation extends Text { "[" word "]" } - public static @tag class Strikethrough extends TextWrap { public String htmlTag() { return "strikethrough"; } } - public static @tag class Superscript extends TextWrap { public String htmlTag() { return "sup"; } } - public static @tag class Subscript extends TextWrap { public String htmlTag() { return "sub"; } } - public static @tag class Smallcap extends TextWrap { public String htmlTag() { return "sc"; } } - public static @tag class Keyword extends TextWrap { public String htmlTag() { return "sc"; } } - public static @tag class Bold extends TextWrap { public String htmlTag() { return "b"; } } - public static @tag class Italic extends TextWrap { public String htmlTag() { return "i"; } } + public static @bind.as class Verbatim extends Text { public char[] c; public void toHTML(HTML h) { } } + //public @bind.as class Blockquote extends TextWrap { } + public static @bind.as class Underline extends TextWrap { public String htmlTag() { return "u"; } } + public static @bind.as class Footnote extends TextWrap { public String htmlTag() { return "small"; } } + public static @bind.as class TT extends TextWrap { public String htmlTag() { return "tt"; } } + //public @bind.as class Citation extends Text { "[" word "]" } + public static @bind.as class Strikethrough extends TextWrap { public String htmlTag() { return "strikethrough"; } } + public static @bind.as class Superscript extends TextWrap { public String htmlTag() { return "sup"; } } + public static @bind.as class Subscript extends TextWrap { public String htmlTag() { return "sub"; } } + public static @bind.as class Smallcap extends TextWrap { public String htmlTag() { return "sc"; } } + public static @bind.as class Keyword extends TextWrap { public String htmlTag() { return "sc"; } } + public static @bind.as class Bold extends TextWrap { public String htmlTag() { return "b"; } } + public static @bind.as class Italic extends TextWrap { public String htmlTag() { return "i"; } } public abstract static class Command extends Text { } - public static @tag class Today extends Command { public void toHTML(HTML h) { } } - public static @tag class LineBreak extends Command { public void toHTML(HTML h) { h.tag("br"); } } + public static @bind.as class Today extends Command { public void toHTML(HTML h) { } } + public static @bind.as class LineBreak extends Command { public void toHTML(HTML h) { h.tag("br"); } } public abstract static class Glyph extends Text { } - public static @tag("emdash") class Emdash extends Glyph { public void toHTML(HTML h) { h.append("&emdash;"); } } + public static @bind.as("emdash") class Emdash extends Glyph { public void toHTML(HTML h) { h.append("&emdash;"); } } public static class Link extends Text { public Text[] t; public Url u; - public @tag("LinkText") Link(Text[] t, Url u) { this.t = t; this.u = u; } - public @tag("LinkChars") Link(String s, Url u) { this(new Text[] { new Chars(s) }, u); } + public @bind.as("LinkText") Link(Text[] t, Url u) { this.t = t; this.u = u; } + public @bind.as("LinkChars") Link(String s, Url u) { this(new Text[] { new Chars(s) }, u); } public void toHTML(HTML h) { h.tag("a", new Object[] { "href", u==null ? "" : u.toString() }, @@ -438,7 +438,7 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix public static class Host { public String name; public String toString() { return name; } - public @tag("DNS") Host(String[][] parts) { + public @bind.as("DNS") Host(String[][] parts) { name = ""; for(String[] s : parts) { for(String ss : s) @@ -446,7 +446,7 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix name += "."; } } - public @tag("IP") Host(int a, int b, int c, int d) { name = a+"."+b+"."+c+"."+d; } + public @bind.as("IP") Host(int a, int b, int c, int d) { name = a+"."+b+"."+c+"."+d; } } public static class Url extends Text { @@ -456,7 +456,7 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix public String pass; public String port; public String path; - public @tag("URL") Url(String method, String[] login, Host host, String port, String path) { + public @bind.as("URL") Url(String method, String[] login, Host host, String port, String path) { this.method = method; this.user = login==null ? null : login.length >= 1 ? login[0] : null; this.pass = login==null ? null : login.length >= 2 ? login[1] : null; @@ -464,16 +464,16 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix this.port = port; this.path = path; } - public @tag("Mailto") Url(String email) { this("mailto", null, null, "25", email); } + public @bind.as("Mailto") Url(String email) { this("mailto", null, null, "25", email); } public void toHTML(HTML h) { new Link(toString(), this).toHTML(h); } public String toString() { return method + "://" + host + "/" + path; } } - public static @tag("lf") String lf() { return "\r"; } - public static @tag("cr") String cr() { return "\n"; } - public static @tag("\"\"") String empty() { return ""; } - public static @tag("urlescape") String urlescape(char a, char b) { return ((char)((a-'0') * 16 + (b-'0')))+""; } + public static @bind.as("lf") String lf() { return "\r"; } + public static @bind.as("cr") String cr() { return "\n"; } + public static @bind.as("\"\"") String empty() { return ""; } + public static @bind.as("urlescape") String urlescape(char a, char b) { return ((char)((a-'0') * 16 + (b-'0')))+""; } } public static void main(String[] s) throws Exception { -- 1.7.10.4