@Override public void run() { for (File file : DirList.listDirectory(m_inputDir)) { PBFile window = null; try { window = WindowFactory.getInstance().createWindow(file); } catch (IOException e) { e.printStackTrace(); } catch (RecognitionException e) { e.printStackTrace(); } if (window != null) buffer.add(window); long now = QDateTime.currentMSecsSinceEpoch(); if (now - lastEmitted > 200 && !buffer.isEmpty()) { // push the queue every 200ms newDataVectorReady.emit(buffer); buffer = new Vector<PBFile>(); lastEmitted = now; } } if (!buffer.isEmpty()) { newDataVectorReady.emit(buffer); } }
public void testErrors(String[] pairs, boolean printTree) { for (int i = 0; i < pairs.length; i += 2) { String input = pairs[i]; String expect = pairs[i + 1]; ErrorQueue equeue = new ErrorQueue(); Grammar g = null; try { String[] lines = input.split("\n"); String fileName = getFilenameFromFirstLineOfGrammar(lines[0]); g = new Grammar(fileName, input, equeue); } catch (org.antlr.runtime.RecognitionException re) { re.printStackTrace(System.err); } String actual = equeue.toString(g.tool); System.err.println(actual); String msg = input; msg = msg.replaceAll("\n", "\\\\n"); msg = msg.replaceAll("\r", "\\\\r"); msg = msg.replaceAll("\t", "\\\\t"); // ignore error number expect = stripErrorNum(expect); actual = stripErrorNum(actual); assertEquals("error in: " + msg, expect, actual); } }
/** * Test method for {@link * org.drools.rule.builder.dialect.java.JavaExprAnalyzer#analyzeBlock(java.lang.String, * java.util.Set[])}. */ @Test public void testAnalyzeBlock() { JavaExprAnalyzer analyzer = new JavaExprAnalyzer(); String codeBlock = "int x;\n" + "Cheese cheese = new Cheese();\n" + "for( Iterator it = list.iterator(); it.hasNext(); ) {\n" + " int shouldNotBeIncluded = 1;\n" + "}\n" + "{\n" + " String anotherNonTopLevelVar = \"test\";\n" + "}\n" + "double thisIsAGoodVar = 0;\n" + "method();\n"; try { JavaAnalysisResult analysis = analyzer.analyzeBlock( codeBlock, new BoundIdentifiers( new HashMap<String, Class<?>>(), new HashMap<String, Class<?>>())); Set<String> vars = analysis.getLocalVariables(); assertEquals(3, vars.size()); assertTrue(vars.contains("x")); assertTrue(vars.contains("cheese")); assertTrue(vars.contains("thisIsAGoodVar")); } catch (RecognitionException e) { e.printStackTrace(); fail("Not supposed to raise exception: " + e.getMessage()); } }
public boolean compile(String str, VMData data) { file = str; // FIXME: int result = 0; // ANTLRStringStream input = new ANTLRStringStream(str); StackBasicLexer lexer = new StackBasicLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); StackBasicParser parser = new StackBasicParser(tokens); // Share data here parser.driver = this; try { parser.compilationUnit(); } catch (RecognitionException e) { e.printStackTrace(); result = -1; } finally { parser.driver = null; } if (result != 0) { return false; } while (!state_stack.empty()) { State state = state_stack.peek(); switch (state.getState()) { case State.STATE_IF: error("if does not match endif"); break; case State.STATE_FOR: error("for does not match next"); state.setStart(null); state.setEnd(null); state.setStep(null); break; case State.STATE_WHILE: error("while does not match wend"); break; } state_stack.pop(); } // FIXME: VMCode code = statement.get(statement.size() - 1); if (code.getOp() != VMCode.VM_HALT) { OpHalt(); } int code_size = LabelSetting(); // FIXME:CraeteData // from statements to data (dump to byte array) CreateData(data, code_size); return error_count == 0; }
public static void main(String args[]) throws Exception { exprLexer lex = new exprLexer( new ANTLRFileStream("/Users/dannluciano/Sources/MyLanguage/input.txt", "UTF8")); CommonTokenStream tokens = new CommonTokenStream(lex); exprParser g = new exprParser(tokens, 49100, null); try { g.program(); } catch (RecognitionException e) { e.printStackTrace(); } }
public static void parse(String filename) throws Exception { DotAlphaLexer lex = new DotAlphaLexer(new ANTLRFileStream(filename)); CommonTokenStream tokens = new CommonTokenStream(lex); DotAlphaParser parser = new DotAlphaParser(tokens); try { parser.dotAlpha(); } catch (RecognitionException e) { e.printStackTrace(); } actions = parser.getActions(); alphas = parser.getAlphas(); }
public static void main(String args[]) throws Exception { ChronosLexer lex = new ChronosLexer( new ANTLRFileStream( "/Users/shannonlee/PLT_Team20/SHANNONTEST/__Test___input.txt", "UTF8")); CommonTokenStream tokens = new CommonTokenStream(lex); ChronosParser g = new ChronosParser(tokens, 49100, null); try { g.program(); } catch (RecognitionException e) { e.printStackTrace(); } }
private static CommonTree runParser(CharStream charStream) throws Exception { FilterLexer lex = new FilterLexer(charStream); CommonTokenStream tokens = new CommonTokenStream(lex); FilterParser g = new FilterParser(tokens); try { filter_return fr = g.filter(); CommonTree tree = (CommonTree) fr.getTree(); return tree; } catch (RecognitionException e) { e.printStackTrace(); return null; } }
// xxx: Wrong! Should use TreeGrammer and not to populate customizer with custom nodes // Should be rewritten but I have no time for this public List<JavaVMOption<?>> parse() { Set<JavaVMOption<?>> result = new HashSet<JavaVMOption<?>>(); try { vmOptions_return options_return = vmOptions(); CommonTree root = options_return.tree; if (root instanceof JavaVMOption<?>) { result.add((JavaVMOption<?>) root); } else if (root != null) { result.addAll(root.getChildren()); } } catch (RecognitionException e) { e.printStackTrace(); } result.addAll(getAllOptions()); return new LinkedList<JavaVMOption<?>>(result); }
protected Atom stringToAtom(String a) { ANTLRStringStream stream = new ANTLRStringStream(a); JVM vm = JVM.getVM(); LTLSpec_SymbolicAtom p = new LTLSpec_SymbolicAtom( new CommonTokenStream(new LTLSpecLexer(stream)), SymbolicLTLListener.invokedMethodName(vm), true); try { return p.atom(); } catch (RecognitionException e1) { e1.printStackTrace(); assert false : "Can not get atom from literal " + a; } return null; }
private FacebookObject fetchData(URL url) { FacebookObject pVal = null; try { ANTLRInputStream in = new ANTLRInputStream(url.openStream()); FacebookGraphLexer lexer = new FacebookGraphLexer(in); CommonTokenStream tokens = new CommonTokenStream(lexer); FacebookGraphParser parser = new FacebookGraphParser(tokens); pVal = parser.start(); } catch (IOException e) { e.printStackTrace(); } catch (RecognitionException e) { e.printStackTrace(); } return pVal; }
@Test public void testTableBodyMixedUid() { try { MetaData data = UidParser.parse("{row:3, column -> bad} as Search"); assertNotNull(data); assertEquals("Search", data.getId()); assertTrue(data instanceof TableBodyMetaData); TableBodyMetaData tbmd = (TableBodyMetaData) data; assertEquals("1", tbmd.getTbody().getValue()); assertEquals(IndexType.VAL, tbmd.getTbody().getType()); assertEquals("3", tbmd.getRow().getValue()); assertEquals(IndexType.VAL, tbmd.getRow().getType()); assertEquals("bad", tbmd.getColumn().getValue()); assertEquals(IndexType.REF, tbmd.getColumn().getType()); } catch (RecognitionException e) { e.printStackTrace(); fail(e.getMessage()); } }
public static void generate(File input, String pack) { System.out.println("Processing " + input); FileReader reader = null; FileWriter writer = null; try { // 1. Basic parsing step; building AST. reader = new FileReader(input); ANTLRv3Lexer lexer = new ANTLRv3Lexer(new ANTLRReaderStream(reader)); CommonTokenStream tokens = new CommonTokenStream(lexer); ANTLRv3Parser parser = new ANTLRv3Parser(tokens); ANTLRv3Parser.grammarDef_return r = parser.grammarDef(); CommonTree t = (CommonTree) r.getTree(); // System.out.println(t.toStringTree()); // 2. Verifying the AST. CommonTreeNodeStream nodes = new CommonTreeNodeStream(t); ANTLRv3Tree walker = new ANTLRv3Tree(nodes); walker.grammarDef(); // 3. Code generation step. Reader templatesIn = new InputStreamReader( ANTLRToFilter.class.getResourceAsStream( "/koopa/trees/antlr/filter/generator/filter.stg")); StringTemplateGroup templates = new StringTemplateGroup(templatesIn, DefaultTemplateLexer.class); nodes = new CommonTreeNodeStream(t); ANTLRv3TreeFilter filterMaker = new ANTLRv3TreeFilter(nodes); filterMaker.setTemplateLib(templates); String code = filterMaker.grammarDef(pack).toString(); // 4. Saving the result. String name = input.getName().replace(".g", "Filter.java"); // System.out.println(name); File output = new File(input.getParentFile(), name); System.out.println("Writing output to " + output); writer = new FileWriter(output); writer.append(code); writer.close(); System.out.println("Code generation complete."); } catch (IOException e) { e.printStackTrace(); System.exit(-1); } catch (RecognitionException e) { e.printStackTrace(); System.exit(-1); } catch (UnsupportedSyntaxException e) { e.printStackTrace(); System.exit(-1); } finally { try { if (reader != null) reader.close(); } catch (IOException e) { e.printStackTrace(); } try { if (writer != null) writer.close(); } catch (IOException e) { e.printStackTrace(); } } }