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 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())
25 parser = new CharParser(grammar);
26 } catch (Exception e) {
27 throw new RuntimeException(e);
33 public static Tree help(String targetFile) throws Throwable {
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) {
43 if (ret==null) throw new NullPointerException("CharParser returned null");
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");
60 File indir = new File(argv[0]);
61 File outdir = new File(argv[1]);
62 process(indir, "", outdir);
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);
73 if (f.getPath().endsWith(".wix")) {
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+"+")));
88 File dest = new File(outPath);
91 Process p = Runtime.getRuntime().exec(new String[] {
94 dest.getAbsolutePath(),
95 new File(outPath+"+").getAbsolutePath()
97 BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
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));*/
106 } catch (Exception e) {
110 new File(outPath+"+").renameTo(dest);
114 public static Object ret;
115 public static void putBack(String o) { ret = o; }