X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FDemo.java;h=2846d7c841e17ae2fbc59817307c6874c665c66a;hp=638bf319f2e678592cb0a4e4c532767d73679f34;hb=16460a4ebf976fddbcb4dd79f1fa0b010d61602b;hpb=0c29e5bd879906ec108db3216c0f4979ed8315fb diff --git a/src/edu/berkeley/sbp/misc/Demo.java b/src/edu/berkeley/sbp/misc/Demo.java index 638bf31..2846d7c 100644 --- a/src/edu/berkeley/sbp/misc/Demo.java +++ b/src/edu/berkeley/sbp/misc/Demo.java @@ -1,486 +1,58 @@ +// Copyright 2006 all rights reserved; see LICENSE file for BSD-style license + package edu.berkeley.sbp.misc; -import edu.berkeley.sbp.util.*; + import edu.berkeley.sbp.*; import edu.berkeley.sbp.chr.*; +import edu.berkeley.sbp.misc.*; +import edu.berkeley.sbp.meta.*; +import edu.berkeley.sbp.util.*; import java.util.*; -import java.lang.annotation.*; -import java.lang.reflect.*; import java.io.*; public class Demo { - public static void main(String[] s) throws Exception { - Tree res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1(); - - MetaGrammar.Meta m = - new ReflectiveMeta(MG.class, - new Class[] { - MG.Grammar.class, - MG.NonTerminal.class, - MG.Range.class, - MG.El.class, - MG.Seq.class, - MG.NonTerminalReference.class, - MG.StringLiteral.class, - MG.Epsilon.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(t.toPrettyString()); - Reducer red = (Reducer)t.head(); - System.out.println(red.reduce(t)); - } - - public static class ReflectiveMeta extends MetaGrammar.Meta { - private final Class _cl; - private final Class[] _inner; - public ReflectiveMeta(Class c, Class[] inner) { - 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) { - if (t==null) return false; - if (t.value().equals(s)) return true; - if (c != null && t.equals("") && c.getSimpleName().equals(s)) return true; - return false; - } - private boolean match(nonterminal 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; - return false; - } - 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(nonterminal.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(nonterminal.class), c, s)) return true; - return false; - } - public Object repeatTag() { - return new Reducer() { - public String toString() { return "[**]"; } - public Object reduce(Tree t) { - Object[] ret = new Object[t.numChildren()]; - for(int i=0; i name.lastIndexOf('.')) { - len--; - ofs++; - } - */ - String[] names = new String[len]; - arg[] argtags = new arg[len]; - for(int i=0; i" + public static void main(String[] s) throws Exception { - public static @tag void range(char c) { } - public static class Range extends El { - public @tag("range") Range(char only) { first = only; last = only; } - public @tag("-") Range(char first, char last) { this.first = first; this.last = last; } - public char first; - public char last; - } - public static class El { } - public static /*abstract*/ @nonterminal("Sequence") class Seq extends El { } - public static @tag("&") Seq and(Sequence s, El[] elements) { return new Seq(); } - public static @tag("&~") Seq andnot(Sequence s, El[] elements) { return new Seq(); } - public static @tag Seq ps(El[] elements) { return new Seq(); } - public static @tag Seq psx(Seq s) { return s; } - public static @tag("::") Seq tag(String tagname, Seq seq) { return new Seq(); } - public static @tag(":") void colon(String s, El e) { } - public static @tag(")") void close(String foo) { } - public static @tag("/") Seq slash(Seq s, El e) { return new Seq(); } - public static @tag("->") Seq arrow(Seq s, El e) { return new Seq(); } + Parser metaGrammarParser = new CharParser(MetaGrammar.newInstance()); + Tree parsedGrammar = metaGrammarParser.parse(new CharInput(new FileInputStream(s[0]))).expand1(); + Grammar.Bindings gbr = new AnnotationGrammarBindings(Math.class); + Union mathGrammar = Grammar.create(parsedGrammar, "Expr", gbr); + Parser mathParser = new CharParser(mathGrammar); - public static @tag("nonTerminal") class NonTerminalReference extends El { public @arg String nonTerminal; } - public static @tag("literal") class StringLiteral extends El { public @arg String string; } - public static @tag("()") class Epsilon extends El { } - public static @tag("{") class Tree extends El { public @arg Seq body; } - public static class CharClass extends El { public @tag("[") CharClass(Range[] ranges) { } } + System.out.println("about to parse: \""+s[1]+"\""); + Tree tree = mathParser.parse(new CharInput(new StringReader(s[1]))).expand1(); - public static @tag("++") void plusmax(El e) { } - public static @tag("+") void plus(El e) { } - public static @tag("++/") void plusmaxfollow(El e, El sep) { } - public static @tag("+/") void plusfollow(El e, El sep) { } - public static @tag("**") void starmax(El e) { } - public static @tag("*") void star(El e) { } - public static @tag("**/") void starmaxfollow(El e, El sep) { } - public static @tag("*/") void starfollow(El e, El sep) { } - public static @tag("!") void bang(El e) { } - public static @tag("?") void question(El e) { } - public static @tag("^") void caret(String s) { } - public static @tag("~") void tilde(El e) { } - public static @tag("^^") void doublecaret(El e) { } - public static @tag("(") void subexpression(Seq[][] rhs) { } + // below is ugly voodoo which will go away very soon. ignore it. + TreeFunctor tf = (TreeFunctor)tree.head(); + Math.Expr e = (Math.Expr)tf.invoke(tree); + // above is ugly voodoo which will go away very soon. ignore it. - public static @nonterminal("Word") String word(String s) { return null; } - public static @nonterminal("Quoted") String quoted(String s) { return null; } - public static @nonterminal("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"; } + System.out.println("done!"); + System.out.println(Reflection.show(e)); } - + */ }