rewrite Haskell parts in Scala
[wix.git] / src / Helper.java
1 // Copyright 2008 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 import edu.berkeley.sbp.*;
6 import edu.berkeley.sbp.misc.*;
7 import edu.berkeley.sbp.util.*;
8 import edu.berkeley.sbp.meta.*;
9 import edu.berkeley.sbp.chr.*;
10 import java.io.*;
11
12 public class Helper {
13     private static CharParser parser = null;
14     private static Object ret;
15     public static void putBack(String o) { ret = o; }
16     public static Object getResult() { return ret; }
17     static {
18         synchronized(Helper.class) {
19             if (parser == null) {
20                 try {
21                     File grammarFile = new File("src/wix.g");
22                     Tree<String> res = new CharParser(GrammarAST.getMetaGrammar())
23                         .parse(new FileInputStream(grammarFile)).expand1();
24                     Union grammar = GrammarAST.buildFromAST(res, "s", new GrammarAST.ImportResolver() {
25                             public InputStream getImportStream(String filename) {
26                                 return this.getClass().getClassLoader().getResourceAsStream(filename);
27                             }
28                         });
29                     parser = new CharParser(grammar);
30                 } catch (Exception e) {
31                     throw new RuntimeException(e);
32                 }
33             }
34         }
35     }
36
37     public static boolean isNull(Object o) { return o==null; }
38     public static Tree help(String targetFile) throws Throwable {
39         Tree ret = null;
40         try {
41             Reader r = new InputStreamReader(new FileInputStream(targetFile));
42             Input input = new CharInput(new IndentingReader(r, CharAtom.left, CharAtom.right));
43             ret = parser.parse(input).expand1();
44         } catch (Throwable e) {
45             e.printStackTrace();
46             throw e;
47         }
48         if (ret==null) throw new NullPointerException("CharParser returned null");
49         return ret;
50     }
51 }