add Helper.java
authoradam <adam@megacz.com>
Wed, 28 Jan 2009 22:09:32 +0000 (14:09 -0800)
committeradam <adam@megacz.com>
Wed, 28 Jan 2009 22:09:32 +0000 (14:09 -0800)
darcs-hash:20090128220932-5007d-e85f49e1b940643e1c903d2cce42edacc9a43bcc.gz

src/Helper.java [new file with mode: 0644]

diff --git a/src/Helper.java b/src/Helper.java
new file mode 100644 (file)
index 0000000..db2f820
--- /dev/null
@@ -0,0 +1,51 @@
+// Copyright 2008 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+
+import edu.berkeley.sbp.*;
+import edu.berkeley.sbp.misc.*;
+import edu.berkeley.sbp.util.*;
+import edu.berkeley.sbp.meta.*;
+import edu.berkeley.sbp.chr.*;
+import java.io.*;
+
+public class Helper {
+    private static CharParser parser = null;
+    private static Object ret;
+    public static void putBack(String o) { ret = o; }
+    public static Object getResult() { return ret; }
+    static {
+        synchronized(Helper.class) {
+            if (parser == null) {
+                try {
+                    File grammarFile = new File("src/wix.g");
+                    Tree<String> res = new CharParser(GrammarAST.getMetaGrammar())
+                        .parse(new FileInputStream(grammarFile)).expand1();
+                    Union grammar = GrammarAST.buildFromAST(res, "s", new GrammarAST.ImportResolver() {
+                            public InputStream getImportStream(String filename) {
+                                return this.getClass().getClassLoader().getResourceAsStream(filename);
+                            }
+                        });
+                    parser = new CharParser(grammar);
+                } catch (Exception e) {
+                    throw new RuntimeException(e);
+                }
+            }
+        }
+    }
+
+    public static boolean isNull(Object o) { return o==null; }
+    public static Tree help(String targetFile) throws Throwable {
+        Tree ret = null;
+        try {
+            Reader r = new InputStreamReader(new FileInputStream(targetFile));
+            Input input = new CharInput(new IndentingReader(r, CharAtom.left, CharAtom.right));
+            ret = parser.parse(input).expand1();
+        } catch (Throwable e) {
+            e.printStackTrace();
+            throw e;
+        }
+        if (ret==null) throw new NullPointerException("CharParser returned null");
+        return ret;
+    }
+}
\ No newline at end of file