From: adam Date: Wed, 28 Jan 2009 22:09:32 +0000 (-0800) Subject: add Helper.java X-Git-Url: http://git.megacz.com/?p=wix.git;a=commitdiff_plain;h=14761ee6aa4e73c218a6fdea51e783b59af46e29;hp=c01e456a310652c5db54bd58f270b2d1f9ad6f6f add Helper.java darcs-hash:20090128220932-5007d-e85f49e1b940643e1c903d2cce42edacc9a43bcc.gz --- diff --git a/src/Helper.java b/src/Helper.java new file mode 100644 index 0000000..db2f820 --- /dev/null +++ b/src/Helper.java @@ -0,0 +1,51 @@ +// 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 Helper { + private static CharParser parser = null; + private static Object ret; + public static void putBack(String o) { ret = o; } + public static Object getResult() { return ret; } + static { + synchronized(Helper.class) { + if (parser == null) { + try { + File grammarFile = new File("src/wix.g"); + Tree res = new CharParser(GrammarAST.getMetaGrammar()) + .parse(new FileInputStream(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 boolean isNull(Object o) { return o==null; } + 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; + } +} \ No newline at end of file