initial release
[wix.git] / src / HaskellHelper.java
1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
2
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.*;
8 import java.io.*;
9
10 public class HaskellHelper {
11     public static boolean isNull(Object o) { return o==null; }
12
13     private static CharParser parser = null;
14     static {
15         synchronized(HaskellHelper.class) {
16             if (parser == null) {
17                 try {
18                     // FIXME: bundle this into the jarfile
19                     File grammarFile = new File("src/wix.g");
20                     Tree<String> res = new CharParser(MetaGrammar.newInstance())
21                         .parse(new FileInputStream(grammarFile)).expand1();
22                     Union grammar = GrammarAST.buildFromAST(res, "s", new File[] {
23                             new File(grammarFile.getParent())
24                         });
25                     parser = new CharParser(grammar);
26                 } catch (Exception e) {
27                     throw new RuntimeException(e);
28                 }
29             }
30         }
31     }
32
33     public static Tree help(String targetFile) throws Throwable {
34         Tree ret = null;
35         try {
36             Reader r = new InputStreamReader(new FileInputStream(targetFile));
37             Input input = new CharInput(new IndentingReader(r, CharAtom.left, CharAtom.right));
38             ret = parser.parse(input).expand1();
39         } catch (Throwable e) {
40             e.printStackTrace();
41             throw e;
42         }
43         if (ret==null) throw new NullPointerException("CharParser returned null");
44         return ret;
45     }
46
47     public static void main(String[] argv) throws Throwable {
48         if (argv.length != 2) {
49             System.out.println("usage: java -jar wix.jar [-v] <indir> <outdir>");
50             // FIXME: implement this
51             System.out.println("     | java -jar wix.jar [-v] <infile>.wix");
52             System.out.println("");
53             // FIXME: implement these
54             System.out.println("   -v   print text as it is parsed (sbp.verbose=true)");
55             System.out.println("   -vv  like -v, but also dump parse tree");
56             System.out.println("   -vvv like -vv, but also dump wix tree");
57             System.exit(-1);
58             return;
59         }
60         File indir = new File(argv[0]);
61         File outdir = new File(argv[1]);
62         process(indir, "", outdir);
63     }
64
65     private static void process(File indir, String suffix, File outdir) throws Throwable {
66         File f = new File(indir.getAbsolutePath()+suffix);
67         if (!f.exists()) return;
68         if (f.isDirectory()) {
69             for (String s : f.list())
70                 process(indir, suffix + File.separatorChar + s, outdir);
71             return;
72         }
73         if (f.getPath().endsWith(".wix")) {
74             System.out.println();
75             String out = "== " + suffix + " ";
76             while(out.length() < 75) out+="=";
77             System.out.println(ANSI.yellow(out));
78             Class.forName("Main").
79                 getMethod("main", new Class[] { String[].class }).
80                 invoke(null, new Object[] { new String[] { f.getAbsolutePath() } });
81             String outPath = outdir.getAbsolutePath()+suffix;
82             outPath = outPath.substring(0, outPath.length()-".wix".length())+".html";
83             new File(new File(outPath).getParent()).mkdirs();
84             PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outPath+"+")));
85             pw.println(ret);
86             pw.flush();
87             pw.close();
88             File dest = new File(outPath);
89             if (dest.exists()) {
90                 try {
91                     Process p = Runtime.getRuntime().exec(new String[] {
92                             "diff",
93                             "-Bub",
94                             dest.getAbsolutePath(),
95                             new File(outPath+"+").getAbsolutePath()
96                         });
97                     BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
98                     br.readLine();
99                     br.readLine();
100                     for(String s = br.readLine(); s != null; s = br.readLine()) {
101                         if      (s.startsWith("+")) System.out.println(ANSI.green(s));
102                         else if (s.startsWith("-")) System.out.println(ANSI.red(s));
103                         /*else System.out.println(ANSI.blue(s));*/
104                     }
105                     p.waitFor();
106                 } catch (Exception e) {
107                     e.printStackTrace();
108                 }
109             }
110             new File(outPath+"+").renameTo(dest);
111         }
112     }
113
114     public static Object ret;
115     public static void putBack(String o) { ret = o; }
116 }