public abstract String[] getArgNames();
public abstract Class[] getArgTypes();
- public static Bindable create(Object o) {
- if (o instanceof Class) return new BindableClass((Class)o);
- if (o instanceof Method) return new BindableMethod((Method)o);
- if (o instanceof Constructor) return new BindableConstructor((Constructor)o);
- return null;
- }
-
public Binding createBinding() { return new SimpleBinding(); }
public Binding createBinding(final int[] map) { return new SimpleBinding(map); }
public Binding createBinding(final int[] map, Object prepend) { return new SimpleBinding(map, prepend); }
- public class SimpleBinding implements Binding, ToJava {
- private int[] map = null;
- private Object prepend = null;
- public SimpleBinding() { }
- public SimpleBinding(int[] map) { this.map = map; }
- public SimpleBinding(int[] map, Object prepend) { this.map = map; this.prepend = prepend; }
-
- public Object invoke(Object[] o) {
- if (map==null) return impose(o);
- int max = 0;
- for(int i=0; i<map.length; i++) max = Math.max(map[i], max);
- Object[] o2 = new Object[max+1];
- for(int i=0; i<o.length; i++) o2[map[i]+(prepend==null?0:1)] = o[i];
- if (prepend != null) o2[0] = prepend;
- return impose(o2);
- }
- public void toJava(StringBuffer sb) {
- Bindable.this.toJava(sb);
- sb.append(".createBinding(");
- if (map != null) {
- sb.append("new int[] {");
- for(int i=0; i<map.length; i++) {
- sb.append(i);
- if (i<map.length-1) sb.append(",");
- }
- sb.append("}");
- }
- sb.append(")");
- }
+ public static Bindable create(Object o) {
+ if (o instanceof Class) return new BindableClass((Class)o);
+ if (o instanceof Method) return new BindableMethod((Method)o);
+ if (o instanceof Constructor) return new BindableConstructor((Constructor)o);
+ return null;
}
+ // Subclasses //////////////////////////////////////////////////////////////////////////////
private static class BindableMethod extends Bindable {
private final Method _method;
}
+ // Helpers //////////////////////////////////////////////////////////////////////////////
+
private static void appendClassArray(StringBuffer sb, Class[] c) {
sb.append("new Class[] {");
for(int i=0; i<c.length; i++) {
if (!c.isArray()) return c.getName().replace('$','.');
return makeClass(c.getComponentType())+"[]";
}
+
+
+ // Creating Bindings //////////////////////////////////////////////////////////////////////////////
+
+ private class SimpleBinding implements Binding, ToJava {
+ private int[] map = null;
+ private Object prepend = null;
+ public SimpleBinding() { }
+ public SimpleBinding(int[] map) { this.map = map; }
+ public SimpleBinding(int[] map, Object prepend) { this.map = map; this.prepend = prepend; }
+
+ public Object invoke(Object[] o) {
+ if (map==null) return impose(o);
+ int max = 0;
+ for(int i=0; i<map.length; i++) max = Math.max(map[i], max);
+ Object[] o2 = new Object[max+1];
+ for(int i=0; i<o.length; i++) o2[map[i]+(prepend==null?0:1)] = o[i];
+ if (prepend != null) o2[0] = prepend;
+ return impose(o2);
+ }
+
+ public void toJava(StringBuffer sb) {
+ Bindable.this.toJava(sb);
+ sb.append(".createBinding(");
+ if (map != null) {
+ sb.append("new int[] {");
+ for(int i=0; i<map.length; i++) {
+ sb.append(i);
+ if (i<map.length-1) sb.append(",");
+ }
+ sb.append("}");
+ }
+ sb.append(")");
+ }
+ }
+
}
this.name = prefix + name;
this.sequences = sequences;
this.rep = rep;
- this.sep = prefix + sep;
+ this.sep = sep==null?null:(prefix + sep);
}
public Element build(Context cx, NonTerminalNode cnt) { return cx.get(name); }
public void build(Context cx, Union u, NonTerminalNode cnt) {
Union urep = new Union();
urep.add(Sequence.empty);
- urep.add(Sequence.singleton(new Element[] { cx.get(sep), u }, 1));
+ if (sep != null)
+ urep.add(Sequence.singleton(new Element[] { cx.get(sep), u }, 1));
+ else
+ urep.add(Sequence.singleton(new Element[] { u }, 0));
for(int i=0; i<sequences.length; i++) {
Seq[] group = sequences[i];
this.e = e; this.sep = sep; this.zero = zero; this.many = many; this.max = max;}
public Element build(Context cx, NonTerminalNode cnt) {
return (!max)
- ? Sequence.repeat(e.build(cx, null), zero, many, sep==null ? null : sep.build(cx, null), cx.rm.repeatTag())
+ ? Sequence.repeat(e.build(cx, null), zero, many, sep==null ? null : sep.build(cx, null), cx.rm.repeatTag())
: sep==null
- ? Sequence.repeatMaximal(infer(e.build(cx, null)), zero, many, cx.rm.repeatTag())
- : Sequence.repeatMaximal(e.build(cx, null), zero, many, infer(sep.build(cx, null)), cx.rm.repeatTag());
+ ? Sequence.repeatMaximal(infer(e.build(cx, null)), zero, many, cx.rm.repeatTag())
+ : Sequence.repeatMaximal(e.build(cx, null), zero, many, infer(sep.build(cx, null)), cx.rm.repeatTag());
}
}
map.put(name, ret);
NonTerminalNode nt = grammar.get(name);
if (nt==null) {
- //System.err.println("*** warning could not find " + name);
throw new Error("warning could not find " + name);
} else {
String old = cnt;
public P() { }
public P(Text[] text) { this.text = text; }
public P(String string) { this.text = new Text[] { new Chars(string) }; }
- public void toHTML(HTML h) { if (text != null) for (Text t : text) if (t != null) t.toHTML(h); }
+ public void toHTML(HTML h) { if (text != null) h.append(text); }
public String toString() {
StringBuffer sb = new StringBuffer();
ToHTML.HTML h = new ToHTML.HTML(sb);
public @bind.arg String password;
}
- public static @bind.as("Euro") Object euro() { return null; }
public static @bind.as("Citation") Object cite(Object o) { return new Chars("*cite*"); }
public static @bind.as("Symbol") Object sym(Object o) { return null; }
}
public abstract static class Text extends Dump implements ToHTML { }
+
+ public @bind static class Space extends Chars {
+ public Space() { super(" "); }
+ }
public @bind 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 void toHTML(HTML h) { h.appendText(text); }
public String toString() { return text; }
}
public @bind static class Block extends Text {
public Text[] text;
- public void toHTML(HTML h) {
- for(Text t : text)
- if (t != null)
- t.toHTML(h);
- }
+ public void toHTML(HTML h) { h.append(text); }
}
public static class TextWrap extends Text {
public @bind.arg Text[] text;
if (htmlTag()!=null) {
h.openTag(htmlTag(), htmlTagParams());
}
- for(Text t : text) t.toHTML(h);
+ h.append(text);
if (htmlTag()!=null) {
h.closeTag(htmlTag());
}
public String htmlTag() { return null; }
public Object[] htmlTagParams() { return null; }
}
- public static @bind class Verbatim extends Text {
+
+ public static @bind class Verbatim extends Text {
public @bind.arg String s;
public void toHTML(HTML h) {
h.openTag("div", new Object[] { "class", "terminal" });
h.closeTag("div");
}
}
+
//public @bind class Blockquote extends TextWrap { }
public static @bind class Underline extends TextWrap { public String htmlTag() { return "u"; } }
public static @bind class Footnote extends TextWrap { public String htmlTag() { return "small"; } }
public static @bind class Today extends Command { public void toHTML(HTML h) { } }
public static @bind class LineBreak extends Command { public void toHTML(HTML h) { h.tag("br"); } }
- public static abstract class Glyph extends Text implements ToHTML { }
- public static @bind.as("emdash") class Emdash extends Glyph { public void toHTML(HTML h) { h.appendLiterally("—"); } }
- public static @bind.as("ellipses") class Ellipses extends Glyph { public void toHTML(HTML h) { h.appendLiterally("…"); } }
- public static @bind.as("r") class RegTm extends Glyph { public void toHTML(HTML h) { h.appendLiterally("®"); } }
- public static @bind.as("c") class Copyright extends Glyph { public void toHTML(HTML h) { h.appendLiterally("©"); } }
- public static @bind.as("tm") class Tm extends Glyph { public void toHTML(HTML h) { h.appendLiterally("&;"); } }
- public static @bind.as("euro") class Euro extends Glyph { public void toHTML(HTML h) { h.appendLiterally("€"); } }
+ public static @bind.as("emdash") Entity emdash() { return new Entity("mdash"); }
+ public static @bind.as("ellipses") Entity ellipses() { return new Entity("#8230"); }
+ public static @bind.as("r") Entity r() { return new Entity("#xAE"); }
+ public static @bind.as("c") Entity c() { return new Entity("#xA9"); }
+ public static @bind.as("tm") Entity tm() { return new Entity(""); }
+ public static @bind.as("euro") Entity euro() { return new Entity("euro"); }
- public static @bind.as("#") Text comment() { return new Chars(""); }
public static class Entity extends Text implements ToHTML {
- public int code;
public String name;
- public Entity(String name, int code) { this.name = name; this.code = code; }
+ public String code;
+ public Entity(String code) { this.name = ""; this.code = code; }
+ public Entity(String name, int code) { this.code = "#"+code; this.name = name; }
public Entity(String name, String abbrev, int code) { this(name, code); }
- public void toHTML(HTML h) {
- h.appendLiterally("&#x"+Integer.toString(code, 16)+";");
- }
+ public void toHTML(HTML h) { h.appendLiterally("&"+code+";"); }
}
public static Entity[] entities = new Entity[] {
return null;
}
+ public static @bind class Quotes extends Text {
+ public Text[] text;
+ public void toHTML(HTML h) {
+ h.append("\"");
+ h.append(text);
+ h.append("\"");
+ }
+ }
+
public static class Link extends Text {
public Text[] t;
public Url u;
- public Input.Region region;
- public @bind.as("link") Link(Input.Region region, @bind.arg Text[] t, @bind.arg Url u) { this.region = region; this.t = t; this.u = u; }
- public Link(String s, Url u) { this(null,new Text[] { new Chars(s) }, u); }
+ public @bind.as("link") Link(@bind.arg Text[] t, @bind.arg Url u) { this.t = t; this.u = u; }
+ public 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() },
- new P(/*t*/region+""));
+ new P(t));
}
}
public String pass;
public String port;
public String path;
- public @bind.as("URL") Url(String method, String[] login, Host host, String port, String path) {
+ public String ref;
+ public @bind.as("URL") Url(String method, String[] login, Host host, String port, String path, String ref) {
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;
this.host = host;
this.port = port;
this.path = path;
+ this.ref = ref;
}
public void toHTML(HTML h) { new Link(toString(), this).toHTML(h); }
public String toString() {
- return method + "://" + host + "/" + path;
+ return method + "://" + host + "/" + path + (ref==null ? "" : ("#"+ref));
}
}
public static class Email extends Url {
public @bind.as("Mailto") Email(String email) {
- super("mailto", null, new Host(email.substring(email.indexOf('@'))), "25", email.substring(email.indexOf('@')));
+ super("mailto",
+ null,
+ new Host(email.substring(email.indexOf('@'))),
+ "25",
+ email.substring(email.indexOf('@')),
+ null
+ );
}
public @bind.as("email") Email(String user, Host host) {
- super("mailto", null, host, "25", user);
+ super("mailto", null, host, "25", user, null);
}
public void toHTML(HTML h) {
h.tag("a",
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") char urlescape(char a, char b) { return ((char)((a-'0') * 16 + (b-'0'))); }
+ public static @bind.as("urlescape") char urlescape(char a, char b) { return ((char)((a-'0') * 16 + (b-'0'))); }
+ public static @bind String urlpath(String[] s) {
+ StringBuffer sb = new StringBuffer();
+ for(String st : s) sb.append(st);
+ return sb.toString();
+ }
}
public static void main(String[] s) throws Exception {
- try {
+ try {
Tree<String> res = new CharParser(MetaGrammar.newInstance()).parse(new FileInputStream(s[0])).expand1();
AnnotationGrammarBindings resolver = new AnnotationGrammarBindings(TD.class);
p.println(sb);
p.flush();
p.close();
-
-
-
- /*
- System.out.println("parsing " + s[0]);
- Tree<String> 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();
-
-
- 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));