// 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 HaskellHelper { public static boolean isNull(Object o) { return o==null; } private static CharParser parser = null; static { synchronized(HaskellHelper.class) { if (parser == null) { try { // FIXME: bundle this into the jarfile InputStream grammarFile = HaskellHelper.class.getClassLoader().getResourceAsStream("wix.g"); Tree res = new CharParser(GrammarAST.getMetaGrammar()) .parse(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 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; } public static void main(String[] argv) throws Throwable { if (argv.length != 2) { System.out.println("usage: java -jar wix.jar [-v] "); // FIXME: implement this System.out.println(" | java -jar wix.jar [-v] .wix"); System.out.println(""); // FIXME: implement these System.out.println(" -v print text as it is parsed (sbp.verbose=true)"); System.out.println(" -vv like -v, but also dump parse tree"); System.out.println(" -vvv like -vv, but also dump wix tree"); System.exit(-1); return; } File indir = new File(argv[0]); File outdir = new File(argv[1]); if (!indir.isDirectory()) { process(new File(indir.getParent()), indir.getName(), outdir); } else { process(indir, "", outdir); } } private static void process(File indir, String suffix, File outdir) throws Throwable { File f = new File(indir.getAbsolutePath()+File.separatorChar+suffix); //System.out.println(f+" "+indir + " " + suffix + " " + outdir); if (!f.exists()) return; if (f.isDirectory()) { for (String s : f.list()) process(indir, suffix + File.separatorChar + s, outdir); return; } if (f.getPath().endsWith(".wix")) { String out = "== " + suffix + " "; while(out.length() < 75) out+="="; System.out.println(ANSI.yellow(out)); //System.out.println(); String outPath = outdir.getAbsolutePath()+File.separatorChar+suffix; outPath = outPath.substring(0, outPath.length()-".wix".length())+".html"; if (new File(outPath).exists() && new File(outPath).lastModified() > f.lastModified()) return; Class.forName("Main"). getMethod("main", new Class[] { String[].class }). invoke(null, new Object[] { new String[] { f.getAbsolutePath() } }); new File(new File(outPath).getParent()).mkdirs(); PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outPath+"+"))); pw.println(ret); pw.flush(); pw.close(); File dest = new File(outPath); if (dest.exists()) { try { Process p = Runtime.getRuntime().exec(new String[] { "diff", "-Bub", dest.getAbsolutePath(), new File(outPath+"+").getAbsolutePath() }); BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); br.readLine(); br.readLine(); for(String s = br.readLine(); s != null; s = br.readLine()) { if (s.startsWith("+")) System.out.println(ANSI.green(s)); else if (s.startsWith("-")) System.out.println(ANSI.red(s)); /*else System.out.println(ANSI.blue(s));*/ } p.waitFor(); } catch (Exception e) { e.printStackTrace(); } } new File(outPath+"+").renameTo(dest); if (dest.lastModified() <= f.lastModified()) dest.setLastModified(f.lastModified()+1); } } public static Object ret; public static void putBack(String o) { ret = o; } }