1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.misc.*;
5 import edu.berkeley.sbp.util.*;
6 import edu.berkeley.sbp.meta.*;
7 import edu.berkeley.sbp.chr.*;
10 public class HaskellHelper {
11 public static boolean isNull(Object o) { return o==null; }
13 private static CharParser parser = null;
15 synchronized(HaskellHelper.class) {
18 // FIXME: bundle this into the jarfile
19 InputStream grammarFile = HaskellHelper.class.getClassLoader().getResourceAsStream("wix.g");
20 Tree<String> res = new CharParser(GrammarAST.getMetaGrammar())
21 .parse(grammarFile).expand1();
22 Union grammar = GrammarAST.buildFromAST(res, "s", new GrammarAST.ImportResolver() {
23 public InputStream getImportStream(String filename) {
24 return this.getClass().getClassLoader().getResourceAsStream(filename);
27 parser = new CharParser(grammar);
28 } catch (Exception e) {
29 throw new RuntimeException(e);
35 public static Tree help(String targetFile) throws Throwable {
38 Reader r = new InputStreamReader(new FileInputStream(targetFile));
39 Input input = new CharInput(new IndentingReader(r, CharAtom.left, CharAtom.right));
40 ret = parser.parse(input).expand1();
41 } catch (Throwable e) {
45 if (ret==null) throw new NullPointerException("CharParser returned null");
49 public static void main(String[] argv) throws Throwable {
50 if (argv.length != 2) {
51 System.out.println("usage: java -jar wix.jar [-v] <indir> <outdir>");
52 // FIXME: implement this
53 System.out.println(" | java -jar wix.jar [-v] <infile>.wix");
54 System.out.println("");
55 // FIXME: implement these
56 System.out.println(" -v print text as it is parsed (sbp.verbose=true)");
57 System.out.println(" -vv like -v, but also dump parse tree");
58 System.out.println(" -vvv like -vv, but also dump wix tree");
62 File indir = new File(argv[0]);
63 File outdir = new File(argv[1]);
64 process(indir, "", outdir);
67 private static void process(File indir, String suffix, File outdir) throws Throwable {
68 File f = new File(indir.getAbsolutePath()+suffix);
69 if (!f.exists()) return;
70 if (f.isDirectory()) {
71 for (String s : f.list())
72 process(indir, suffix + File.separatorChar + s, outdir);
75 if (f.getPath().endsWith(".wix")) {
77 String out = "== " + suffix + " ";
78 while(out.length() < 75) out+="=";
79 System.out.println(ANSI.yellow(out));
80 Class.forName("Main").
81 getMethod("main", new Class[] { String[].class }).
82 invoke(null, new Object[] { new String[] { f.getAbsolutePath() } });
83 String outPath = outdir.getAbsolutePath()+suffix;
84 outPath = outPath.substring(0, outPath.length()-".wix".length())+".html";
85 new File(new File(outPath).getParent()).mkdirs();
86 PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outPath+"+")));
90 File dest = new File(outPath);
93 Process p = Runtime.getRuntime().exec(new String[] {
96 dest.getAbsolutePath(),
97 new File(outPath+"+").getAbsolutePath()
99 BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
102 for(String s = br.readLine(); s != null; s = br.readLine()) {
103 if (s.startsWith("+")) System.out.println(ANSI.green(s));
104 else if (s.startsWith("-")) System.out.println(ANSI.red(s));
105 /*else System.out.println(ANSI.blue(s));*/
108 } catch (Exception e) {
112 new File(outPath+"+").renameTo(dest);
116 public static Object ret;
117 public static void putBack(String o) { ret = o; }