public static List<ExecutionPlan> parse(String source) throws SiddhiParserException { try { SiddhiQLGrammarLexer lexer = new SiddhiQLGrammarLexer(); lexer.setCharStream(new ANTLRStringStream(source)); CommonTokenStream tokens = new CommonTokenStream(lexer); SiddhiQLGrammarParser parser = new SiddhiQLGrammarParser(tokens); SiddhiQLGrammarParser.executionPlan_return r = parser.executionPlan(); CommonTree t = (CommonTree) r.getTree(); CommonTreeNodeStream nodes = new CommonTreeNodeStream(t); nodes.setTokenStream(tokens); SiddhiQLGrammarWalker walker = new SiddhiQLGrammarWalker(nodes); return walker.executionPlan(); } catch (Throwable e) { throw new SiddhiParserException(e.getMessage(), e); } }
public static StreamDefinition parseStreamDefinition(String source) throws SiddhiParserException { try { SiddhiQLGrammarLexer lexer = new SiddhiQLGrammarLexer(); lexer.setCharStream(new ANTLRStringStream(source)); CommonTokenStream tokens = new CommonTokenStream(lexer); SiddhiQLGrammarParser parser = new SiddhiQLGrammarParser(tokens); SiddhiQLGrammarParser.definitionStreamFinal_return r = parser.definitionStreamFinal(); CommonTree t = (CommonTree) r.getTree(); CommonTreeNodeStream nodes = new CommonTreeNodeStream(t); nodes.setTokenStream(tokens); SiddhiQLGrammarWalker walker = new SiddhiQLGrammarWalker(nodes); return walker.definitionStreamFinal(); } catch (Throwable e) { throw new SiddhiParserException(e.getMessage(), e); } }
public static TableDefinition parseTableDefinition(String source) throws SiddhiParserException { try { SiddhiQLGrammarLexer lexer = new SiddhiQLGrammarLexer(); lexer.setCharStream(new ANTLRStringStream(source)); CommonTokenStream tokens = new CommonTokenStream(lexer); SiddhiQLGrammarParser parser = new SiddhiQLGrammarParser(tokens); SiddhiQLGrammarParser.definitionTableFinal_return r = parser.definitionTableFinal(); CommonTree t = (CommonTree) r.getTree(); CommonTreeNodeStream nodes = new CommonTreeNodeStream(t); nodes.setTokenStream(tokens); SiddhiQLGrammarWalker walker = new SiddhiQLGrammarWalker(nodes); return walker.definitionTableFinal(); } catch (Throwable e) { throw new SiddhiParserException(e.getMessage(), e); } }
public static PartitionDefinition parsePartitionDefinition(String source) throws SiddhiParserException { try { SiddhiQLGrammarLexer lexer = new SiddhiQLGrammarLexer(); lexer.setCharStream(new ANTLRStringStream(source)); CommonTokenStream tokens = new CommonTokenStream(lexer); SiddhiQLGrammarParser parser = new SiddhiQLGrammarParser(tokens); SiddhiQLGrammarParser.definitionPartitionFinal_return r = parser.definitionPartitionFinal(); CommonTree t = (CommonTree) r.getTree(); CommonTreeNodeStream nodes = new CommonTreeNodeStream(t); nodes.setTokenStream(tokens); SiddhiQLGrammarWalker walker = new SiddhiQLGrammarWalker(nodes); return walker.definitionPartitionFinal(); } catch (Throwable e) { throw new SiddhiParserException(e.getMessage(), e); } }
public static Query parseQuery(String source) throws SiddhiParserException { try { SiddhiQLGrammarLexer lexer = new SiddhiQLGrammarLexer(); lexer.setCharStream(new ANTLRStringStream(source)); CommonTokenStream tokens = new CommonTokenStream(lexer); SiddhiQLGrammarParser parser = new SiddhiQLGrammarParser(tokens); SiddhiQLGrammarParser.queryFinal_return r = parser.queryFinal(); CommonTree t = (CommonTree) r.getTree(); CommonTreeNodeStream nodes = new CommonTreeNodeStream(t); nodes.setTokenStream(tokens); SiddhiQLGrammarWalker walker = new SiddhiQLGrammarWalker(nodes); return walker.queryFinal(); } catch (Throwable e) { throw new SiddhiParserException(e.getMessage(), e); } }
private void runTestFor(String script) throws RecognitionException { ReflexLexer lexer = new ReflexLexer(); lexer.setCharStream(new ANTLRStringStream(script)); CommonTokenStream tokens = new CommonTokenStream(lexer); ReflexParser parser = new ReflexParser(tokens); CommonTree tree = (CommonTree) parser.parse().getTree(); /* * DOTTreeGenerator gen = new DOTTreeGenerator(); StringTemplate st = * gen.toDOT(tree); System.out.println(st); */ CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree); ReflexTreeWalker walker = new ReflexTreeWalker(nodes, parser.languageRegistry); HttpLoginApi loginApi = new HttpLoginApi("http://localhost:8665/rapture", new SimpleCredentialsProvider("rapture", "rapture")); loginApi.login(); HttpUserApi userApi = new HttpUserApi(loginApi); ApiVersion version = userApi.getServerApiVersion(); System.out.println("version is " + version.getMajor() + ":" + version.getMinor()); walker.getReflexHandler().setApi(new ScriptClient(loginApi)); ReflexNode returned = walker.walk(); System.out.println(returned == null ? "null" : returned.evaluateWithoutScope(new ReflexPrintingDebugger())); }
protected void runSuspendTestFor(String fileName) throws RecognitionException { ReflexLexer lexer = new ReflexLexer(); lexer.setCharStream(new ANTLRStringStream(getResourceAsString(this, fileName))); CommonTokenStream tokens = new CommonTokenStream(lexer); ReflexParser parser = new ReflexParser(tokens); CommonTree tree = (CommonTree) parser.parse().getTree(); /* * DOTTreeGenerator gen = new DOTTreeGenerator(); StringTemplate st = * gen.toDOT(tree); System.out.println(st); */ CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree); ReflexTreeWalker walker = new ReflexTreeWalker(nodes, parser.languageRegistry); ReflexNode returned = walker.walk(); ReflexValue value = returned.evaluateWithoutScope(new NullDebugger()); while (value.getValue() == ReflexValue.Internal.SUSPEND) { System.out.println("Oooh it suspended..."); System.out.println("Resuming script"); value = returned.evaluateWithResume(new NullDebugger(), returned.getScope()); } System.out.println(value); }
protected String runTestForWithApi(String fileName, ScriptingApi api, IReflexDataHandler dataHandler, Map<String, Object> injectedVars) throws RecognitionException { ReflexLexer lexer = new ReflexLexer(); lexer.setCharStream(new ANTLRStringStream(getResourceAsString(this, fileName))); CommonTokenStream tokens = new CommonTokenStream(lexer); ReflexParser parser = new ReflexParser(tokens); CommonTree tree = (CommonTree) parser.parse().getTree(); CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree); ReflexTreeWalker walker = new ReflexTreeWalker(nodes, parser.languageRegistry); walker.getReflexHandler().setApi(api); walker.getReflexHandler().setDataHandler(dataHandler); if (injectedVars != null && !injectedVars.isEmpty()) { for (Map.Entry<String, Object> kv : injectedVars.entrySet()) { walker.currentScope.assign(kv.getKey(), kv.getValue() == null ? new ReflexNullValue() : new ReflexValue(kv.getValue())); } } ReflexNode returned = walker.walk(); return returned.evaluateWithoutScope(new ReflexPrintingDebugger()).asString(); }
@Test public void test1() throws RecognitionException { CharStream input = new ANTLRStringStream("version(1.1.0)\n" + "minVer(1.1.0)\n" + "[ The Admin API is used to manipulate and access the low level entities in Rapture. Typically the methods in this API\n" + "are only used during significant setup events in a Rapture environment.]\n" + "api(Admin) {\n" + " [This method restores a user that has been deleted]\n" + " @entitle=/admin/main\n" + " @public Boolean restoreUser(String userName);\n" + "}\n" + "[A return value from a native query]\n" + "type RaptureQueryResult(@package=rapture.common) {\n" + " List(JsonContent) rows;\n" + "}\n"); TLexer lexer = new TLexer(input); TokenStream tokenInputStream = new CommonTokenStream(lexer); TParser parser = new TParser(tokenInputStream); hmxdef_return returnVal = parser.hmxdef(); System.out.println("Done " + returnVal.getTree().toStringTree()); TreeNodeStream treeInput = new CommonTreeNodeStream(returnVal.getTree()); TTree walker = new TTree(treeInput); walker.setTemplateLib(TemplateRepo.getApiTemplates("Java")); com.incapture.rapgen.TTree.hmxdef_return walkerResult = walker.hmxdef(); System.out.println("Done, result=" + walkerResult.toString()); }
@Test public void test2() throws RecognitionException { CharStream input = new ANTLRStringStream("version(1.1.0)\n" + "minVer(1.1.0)\n" + "[ The Admin API is used to manipulate and access the low level entities in Rapture. Typically the methods in this API\n" + "are only used during significant setup events in a Rapture environment.]\n" + "api(Admin) {\n" + " [This method restores a user that has been deleted]\n" + " @entitle=/admin/main\n" + " @public Boolean restoreUser(String userName);\n" + "}\n" + "[A return value from a native query]\n" + "type RaptureQueryResult(@package=rapture.common) {\n" + " List(JsonContent) rows;\n" + "}\n"); TLexer lexer = new TLexer(input); TokenStream tokenInputStream = new CommonTokenStream(lexer); TParser parser = new TParser(tokenInputStream); hmxdef_return returnVal = parser.hmxdef(); System.out.println("Done " + returnVal.getTree().toStringTree()); TreeNodeStream treeInput = new CommonTreeNodeStream(returnVal.getTree()); TTree walker = new TTree(treeInput); walker.setTemplateLib(TemplateRepo.getApiTemplates("Java")); apiGen_return walkerResult = walker.apiGen(); System.out.println("Done, result=" + walkerResult.toString()); }
@Test public void test3() throws RecognitionException { CharStream input = new ANTLRStringStream("version(1.1.0)\n" + "minVer(1.1.0)\n" + "[ The Admin API is used to manipulate and access the low level entities in Rapture. Typically the methods in this API\n" + "are only used during significant setup events in a Rapture environment.]\n" + "api(Admin) {\n" + " [This method restores a user that has been deleted]\n" + " @entitle=/admin/main\n" + " @public Boolean restoreUser(String userName);\n" + "}\n" + "[A return value from a native query]\n" + "type RaptureQueryResult(@package=rapture.common) {\n" + " List(JsonContent) rows;\n" + "}\n"); TLexer lexer = new TLexer(input); TokenStream tokenInputStream = new CommonTokenStream(lexer); TParser parser = new TParser(tokenInputStream); hmxdef_return returnVal = parser.hmxdef(); System.out.println("Done " + returnVal.getTree().toStringTree()); TreeNodeStream treeInput = new CommonTreeNodeStream(returnVal.getTree()); TTree walker = new TTree(treeInput); walker.setTemplateLib(TemplateRepo.getSdkTemplates("Java")); try { walker.sdkGen(); fail("This should have failed, sdk name is required but not present in code"); } catch (IllegalArgumentException e) { assertTrue(e.getMessage().contains("SDK Name")); } }
@Test public void test4() throws RecognitionException { CharStream input = new ANTLRStringStream("sdk(alan)\n" + "version(0.0.1)\n" + "minVer(0.0.1)\n" + "[ The Admin API is used to manipulate and access the low level entities in Rapture. Typically the methods in this API\n" + "are only used during significant setup events in a Rapture environment.]\n" + "api(Admin) {\n" + " [This method restores a user that has been deleted]\n" + " @entitle=/admin/main\n" + " @public Boolean restoreUser(String userName);\n" + "}\n" + "[A return value from a native query]\n" + "type RaptureQueryResult(@package=rapture.common) {\n" + " List(JsonContent) rows;\n" + "}\n"); TLexer lexer = new TLexer(input); TokenStream tokenInputStream = new CommonTokenStream(lexer); TParser parser = new TParser(tokenInputStream); hmxdef_return returnVal = parser.hmxdef(); System.out.println("Done " + returnVal.getTree().toStringTree()); TreeNodeStream treeInput = new CommonTreeNodeStream(returnVal.getTree()); TTree walker = new TTree(treeInput); walker.setTemplateLib(TemplateRepo.getSdkTemplates("Java")); walker.sdkGen(); }
@Test public void test5() throws RecognitionException { CharStream input = new ANTLRStringStream("sdk(alan)\n" + "version(0.0.1)\n" + "minVer(0.0.1)\n" + "[ The Admin API is used to manipulate and access the low level entities in Rapture. Typically the methods in this API\n" + "are only used during significant setup events in a Rapture environment.]\n" + "api(Admin) {\n" + " [This method restores a user that has been deleted]\n" + " @entitle=/admin/main\n" + " @public Boolean restoreUser(String userName);\n" + "}\n" + "[A return value from a native query]\n" + "type RaptureQueryResult(@package=rapture.common) {\n" + " List(JsonContent) rows;\n" + "}\n"); TLexer lexer = new TLexer(input); TokenStream tokenInputStream = new CommonTokenStream(lexer); TParser parser = new TParser(tokenInputStream); hmxdef_return returnVal = parser.hmxdef(); System.out.println("Done " + returnVal.getTree().toStringTree()); TreeNodeStream treeInput = new CommonTreeNodeStream(returnVal.getTree()); TTree walker = new TTree(treeInput); walker.setTemplateLib(TemplateRepo.getApiTemplates("Java")); try { walker.apiGen(); fail("This should have failed, sdk name is not expected but found"); } catch (IllegalArgumentException e) { assertTrue(e.getMessage().contains("SDK Name")); } }
@Test public void test7() throws RecognitionException { CharStream input = new ANTLRStringStream("version(1.1.0)\n" + "minVer(1.1.0)\n" + "[ The Admin API is used to manipulate and access the low level entities in Rapture. Typically the methods in this API\n" + "are only used during significant setup events in a Rapture environment.]\n" + "api(Admin) {\n" + " [This method restores a user that has been deleted]\n" + " @entitle=/admin/main\n" + " @public Boolean restoreUser(String userName);\n" + "}\n" + "[A return value from a native query]\n" + "@Storable(storagePath : {\"sys\", name}, separator=\".\")\n" + "type RepoConfig (@package=rapture.common.model) {\n" + " String name = \"test\";\n" + " String config;\n" + "}\n" + ""); TLexer lexer = new TLexer(input); TokenStream tokenInputStream = new CommonTokenStream(lexer); TParser parser = new TParser(tokenInputStream); hmxdef_return returnVal = parser.hmxdef(); System.out.println("Done " + returnVal.getTree().toStringTree()); TreeNodeStream treeInput = new CommonTreeNodeStream(returnVal.getTree()); TTree walker = new TTree(treeInput); walker.setTemplateLib(TemplateRepo.getApiTemplates("Java")); apiGen_return walkerResult = walker.apiGen(); System.out.println("Done, result=" + walkerResult.toString()); }
@Test public void testBean() throws RecognitionException { CharStream input = new ANTLRStringStream("version(1.1.0)\n" + "minVer(1.1.0)\n" + "[ The Admin API is used to manipulate and access the low level entities in Rapture. Typically the methods in this API\n" + "are only used during significant setup events in a Rapture environment.]\n" + "api(Admin) {\n" + " [This method restores a user that has been deleted]\n" + " @entitle=/admin/main\n" + " @public Boolean restoreUser(String userName);\n" + "}\n" + "[A Graph node]\n" + "@Bean\n" + "type Node(@package=rapture.common.dp) {\n" + " String nodeId; //this is not a URI, just a String id\n" + " List<XFer> xferValues;\n" + "}\n" + ""); TLexer lexer = new TLexer(input); TokenStream tokenInputStream = new CommonTokenStream(lexer); TParser parser = new TParser(tokenInputStream); hmxdef_return returnVal = parser.hmxdef(); System.out.println("Done " + returnVal.getTree().toStringTree()); TreeNodeStream treeInput = new CommonTreeNodeStream(returnVal.getTree()); TTree walker = new TTree(treeInput); walker.setTemplateLib(TemplateRepo.getApiTemplates("Java")); apiGen_return walkerResult = walker.apiGen(); System.out.println("Done, result=" + walkerResult.toString()); }
@Test public void test6() throws RecognitionException { System.out.println("6"); CharStream input = new ANTLRStringStream("[ This is a mailbox message, usually posted by an external user]\n" + "@Addressable(scheme = MAILBOX)\n" + "@Storable(storagePath : {authority , documentPath , id} )\n" + "type RaptureMailMessage(@package=rapture.common.model) {\n" + " String id;\n" + " String authority;\n" + " String category;\n" + " String content;\n" + " Date when;\n" + " String who;\n" + "}\n" + ""); TLexer lexer = new TLexer(input); TokenStream tokenInputStream = new CommonTokenStream(lexer); TParser parser = new TParser(tokenInputStream); typeExpr_return returnVal = parser.typeExpr(); System.out.println("Done " + returnVal.getTree().toStringTree()); TreeNodeStream treeInput = new CommonTreeNodeStream(returnVal.getTree()); TTree walker = new TTree(treeInput); walker.setTemplateLib(TemplateRepo.getApiTemplates("Java")); com.incapture.rapgen.TTree.typeExpr_return walkerResult = walker.typeExpr(); System.out.println("Done, result=" + walkerResult.toString()); }
@Test public void test1() throws RecognitionException, IOException { CharStream input = new ANTLRStringStream("version(1.1.0)\n" + "minVer(1.1.0)\n" + "[ The Admin API is used to manipulate and access the low level entities in Rapture. Typically the methods in this API\n" + "are only used during significant setup events in a Rapture environment.]\n" + "api(Admin) {\n" + " [This method restores a user that has been deleted]\n" + " @entitle=/admin/main\n" + " @public Boolean restoreUser(String userName);\n" + "}\n" + "[A return value from a native query]\n" + "type RaptureQueryResult(@package=rapture.common) {\n" + " List(JsonContent) rows;\n" + "}\n"); TLexer lexer = new TLexer(input); TokenStream tokenInputStream = new CommonTokenStream(lexer); TParser parser = new TParser(tokenInputStream); hmxdef_return returnVal = parser.hmxdef(); System.out.println("Done " + returnVal.getTree().toStringTree()); TreeNodeStream treeInput = new CommonTreeNodeStream(returnVal.getTree()); TTreeJS walker = new TTreeJS(treeInput); walker.setTemplateLib(getJsTemplate()); }
private ReflexTreeWalker getStandardWalker(CallingContext ctx, String script) throws RecognitionException { ReflexLexer lexer = new ReflexLexer(); lexer.dataHandler = new ReflexIncludeHelper(ctx); lexer.setCharStream(new ANTLRStringStream(script)); CommonTokenStream tokens = new CommonTokenStream(lexer); ReflexParser parser = new ReflexParser(tokens); CommonTree tree; tree = (CommonTree) parser.parse().getTree(); CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree); ReflexTreeWalker walker = new ReflexTreeWalker(nodes, parser.languageRegistry); walker.setReflexHandler(new AddingOutputReflexHandler()); walker.getReflexHandler().setOutputHandler(new SimpleCollectingOutputHandler()); walker.getReflexHandler().setOutputHandler(new DummyReflexOutputHandler()); walker.getReflexHandler().setDataHandler(new ReflexDataHelper(ctx)); walker.getReflexHandler().setIOHandler(new BlobOnlyIOHandler()); walker.setScriptInfo(parser.scriptInfo); return walker; }
protected void runTestForWithApi(String fileName, ScriptingApi api, IReflexDataHandler dataHandler) throws RecognitionException { ReflexLexer lexer = new ReflexLexer(); lexer.setCharStream(new ANTLRStringStream(getResourceAsString(this, fileName))); CommonTokenStream tokens = new CommonTokenStream(lexer); ReflexParser parser = new ReflexParser(tokens); CommonTree tree = (CommonTree) parser.parse().getTree(); CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree); ReflexTreeWalker walker = new ReflexTreeWalker(nodes, parser.languageRegistry); walker.getReflexHandler().setApi(api); walker.getReflexHandler().setDataHandler(dataHandler); ReflexNode returned = walker.walk(); System.out.println(returned == null ? "null" : returned.evaluateWithoutScope(new ReflexPrintingDebugger())); }
public String toTokenString() { CharStream input = this.token.getInputStream(); GrammarASTAdaptor adaptor = new GrammarASTAdaptor(input); CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor, this); StringBuilder buf = new StringBuilder(); GrammarAST o = (GrammarAST)nodes.LT(1); int type = adaptor.getType(o); while ( type!=Token.EOF ) { buf.append(" "); buf.append(o.getText()); nodes.consume(); o = (GrammarAST)nodes.LT(1); type = adaptor.getType(o); } return buf.toString(); }
protected void _createATN(Collection<Rule> rules) { createRuleStartAndStopATNStates(); GrammarASTAdaptor adaptor = new GrammarASTAdaptor(); for (Rule r : rules) { // find rule's block GrammarAST blk = (GrammarAST)r.ast.getFirstChildWithType(ANTLRParser.BLOCK); CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor,blk); ATNBuilder b = new ATNBuilder(nodes,this); try { setCurrentRuleName(r.name); Handle h = b.ruleBlock(null); rule(r.ast, r.name, h); } catch (RecognitionException re) { ErrorManager.fatalInternalError("bad grammar AST structure", re); } } }
/** Given list of X and r refs in alt, compute how many of each there are */ protected FrequencySet<String> getElementFrequenciesForAlt(AltAST ast) { try { ElementFrequenciesVisitor visitor = new ElementFrequenciesVisitor(new CommonTreeNodeStream(new GrammarASTAdaptor(), ast)); visitor.outerAlternative(); if (visitor.frequencies.size() != 1) { factory.getGrammar().tool.errMgr.toolError(ErrorType.INTERNAL_ERROR); return new FrequencySet<String>(); } return visitor.frequencies.peek(); } catch (RecognitionException ex) { factory.getGrammar().tool.errMgr.toolError(ErrorType.INTERNAL_ERROR, ex); return new FrequencySet<String>(); } }
public void buildNormalRuleFunction(Rule r, RuleFunction function) { CodeGenerator gen = delegate.getGenerator(); // TRIGGER factory functions for rule alts, elements GrammarASTAdaptor adaptor = new GrammarASTAdaptor(r.ast.token.getInputStream()); GrammarAST blk = (GrammarAST)r.ast.getFirstChildWithType(ANTLRParser.BLOCK); CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor,blk); walker = new SourceGenTriggers(nodes, this); try { // walk AST of rule alts/elements function.code = DefaultOutputModelFactory.list(walker.block(null, null)); function.hasLookaheadBlock = walker.hasLookaheadBlock; } catch (org.antlr.runtime.RecognitionException e){ e.printStackTrace(System.err); } function.ctxType = gen.getTarget().getRuleFunctionContextStructName(function); function.postamble = rulePostamble(function, r); }
/** * Generates a model from an Abstract Syntax Tree. * * @param ast the abstract syntax tree produced by parser * @return a list of root COBOL data items * @throws RecognizerException if tree cannot be walked */ public List < CobolDataItem > emitModel(final CommonTree ast) throws RecognizerException { List < CobolDataItem > cobolDataItems = new ArrayList < CobolDataItem >(); if (_log.isDebugEnabled()) { debug("4. Emitting Model from AST: ", ((ast == null) ? "null" : ast.toStringTree())); } if (ast == null) { return cobolDataItems; } try { TreeNodeStream nodes = new CommonTreeNodeStream(ast); CobolStructureEmitter emitter = new CobolStructureEmitterImpl( nodes, getErrorHandler()); emitter.cobdata(cobolDataItems); return cobolDataItems; } catch (RecognitionException e) { throw new RecognizerException(e); } }
/** * Starting from a COBOL source fragment translates to XML Schema. * @param source COBOL source fragment. * @return an XML Schema * @throws RecognizerException if emit fails */ public String emit(final String source) throws RecognizerException { try { CommonTree ast = parse(source); if (_log.isDebugEnabled()) { _log.debug(ast.toStringTree()); } TreeNodeStream nodes = new CommonTreeNodeStream(ast); CobolStructureEmitter emitter = new CobolStructureEmitterImpl( nodes, getErrorHandler()); List < CobolDataItem > dataEntries = new ArrayList < CobolDataItem >(); emitter.cobdata(dataEntries); return dataEntries.toString(); } catch (RecognitionException e) { throw new RecognizerException(e); } }
/** * Parses a snippet of maple code and returns a list of CFG nodes that implement the returned maple expressions. * * @param graph The control flow graph the new nodes should be created in. * @param mapleCode The code returned by Maple. * @return A list of control flow nodes modeling the returned code. */ List<AssignmentNode> parseMapleCode(ControlFlowGraph graph, String mapleCode) { oldMinVal = new HashMap<String, String>(); oldMaxVal = new HashMap<String, String>(); /* fill the Maps with the min and maxvalues from the nodes */ for (Variable v : graph.getInputVariables()) { if (v.getMinValue() != null) oldMinVal.put(v.getName(), v.getMinValue()); if (v.getMaxValue() != null) oldMaxVal.put(v.getName(), v.getMaxValue()); } MapleLexer lexer = new MapleLexer(new ANTLRStringStream(mapleCode)); MapleParser parser = new MapleParser(new CommonTokenStream(lexer)); try { MapleParser.program_return result = parser.program(); MapleTransformer transformer = new MapleTransformer(new CommonTreeNodeStream(result.getTree())); return transformer.script(graph, oldMinVal, oldMaxVal); } catch (RecognitionException e) { throw new RuntimeException(e); } }
private static CSSTreeParser feedAST(CommonTokenStream source, CommonTree ast, Preparator preparator, StyleSheet stylesheet) { if (log.isTraceEnabled()) { log.trace("Feeding tree parser with AST:\n{}", TreeUtil .toStringTree(ast)); } // Walk resulting tree; create tree-node stream first CommonTreeNodeStream nodes = new CommonTreeNodeStream(ast); // AST nodes have payloads that point into token stream nodes.setTokenStream(source); CSSTreeParser parser = new CSSTreeParser(nodes); return parser.init(stylesheet, preparator); }
/** * Generates a model from an Abstract Syntax Tree. * * @param ast the abstract syntax tree produced by parser * @return a list of root COBOL data items * @throws RecognizerException if tree cannot be walked */ public List < CobolDataItem > emitModel(final CommonTree ast) throws RecognizerException { List < CobolDataItem > cobolDataItems = new ArrayList < CobolDataItem >(); if (_log.isDebugEnabled()) { _log.debug("4. Emitting Model from AST: {}", ((ast == null) ? "null" : ast.toStringTree())); } if (ast == null) { return cobolDataItems; } try { TreeNodeStream nodes = new CommonTreeNodeStream(ast); CobolStructureEmitter emitter = new CobolStructureEmitterImpl( nodes, getErrorHandler()); emitter.cobdata(cobolDataItems); return cobolDataItems; } catch (RecognitionException e) { throw new RecognizerException(e); } }
/** * Verify if default data type is inserted. */ @Test public void testDefaultDataTypeInsertion() throws IOException, RecognitionException { CharStream input = new QueryParserFileStream( "test/org/apache/pig/parser/TestDefaultDataTypeInserter.pig" ); QueryLexer lex = new QueryLexer(input); CommonTokenStream tokens = new CommonTokenStream(lex); QueryParser parser = QueryParserUtils.createParser(tokens); QueryParser.query_return result = parser.query(); Tree ast = (Tree)result.getTree(); System.out.println( ast.toStringTree() ); TreePrinter.printTree( (CommonTree)ast, 0 ); CommonTreeNodeStream nodes = new CommonTreeNodeStream( ast ); AstValidator walker = new AstValidator( nodes ); AstValidator.query_return newResult = walker.query(); Assert.assertEquals( 0, walker.getNumberOfSyntaxErrors() ); ast = (Tree)newResult.getTree(); validateDataTypePresent( (CommonTree)ast ); TreePrinter.printTree( (CommonTree)ast, 0 ); }
public static LogicalPlan generateLogicalPlan(String query) throws RecognitionException, ParsingFailureException, IOException { Tree ast = validateAst( query ); CommonTreeNodeStream input = new CommonTreeNodeStream( ast ); LogicalPlanBuilder builder = new LogicalPlanBuilder( input ); LogicalPlanGenerator walker = new LogicalPlanGenerator( input, builder ); walker.query(); if( 0 < walker.getNumberOfSyntaxErrors() ) throw new ParsingFailureException( LogicalPlanGenerator.class ); LogicalPlan plan = walker.getLogicalPlan(); System.out.println( "Generated logical plan: " + plan.toString() ); return plan; }
public static void main(String[] args) throws Exception { if(args.length == 0) { throw new IllegalArgumentException("no Lua file(s) provided as a command line parameter"); } for (String fileName : args) { System.out.printf("\nParsing `%s`...\n\n", fileName); // Create the lexer and parser. Lua52Lexer lexer = new Lua52Lexer(new ANTLRFileStream(fileName)); Lua52Parser parser = new Lua52Parser(new CommonTokenStream(lexer)); // Print the AST of the source file by manually traversing the AST. CommonTree ast = parser.parse().getTree(); StringBuilder builder = new StringBuilder(); walk(ast, builder); System.out.println(builder); // Now let the tree walker traverse the AST and print some // information about the `assignment` rule. Lua52Walker walker = new Lua52Walker(new CommonTreeNodeStream(ast)); walker.walk(); } }
private void run(final CharStream input) throws RecognitionException, InterpreterRuntimeException { final TraciLexer lexer = new TraciLexer(input); final TraciParser parser = new TraciParser(new CommonTokenStream(lexer)); final CommonTree parseTree = (CommonTree) parser.scene().getTree(); assertTrue(lexer.getLexerErrors().isEmpty()); assertTrue(parser.getParseErrors().isEmpty()); final TraciTreeWalker walker = new TraciTreeWalker(new CommonTreeNodeStream(parseTree)); final BlockNode rootNode = walker.block(); scene = new Scene(); value = null; final Context rootContext = Context.newRootContext(scene); try { rootNode.eval(rootContext); } catch (final FunctionReturnException e) { assertNotNull(e.value); value = e.value; } }
public void generate(PrintStream ps, TNode baseCTypeNode, String s) { CommonTreeNodeStream nodes = new CommonTreeNodeStream((Tree)baseCTypeNode); KernelGeniusEmitter emitter=new KernelGeniusEmitter(nodes); emitter.setNoLineManagement(); emitter.setPrintStream(ps); try { emitter.declSpecifiers(); } catch (RecognitionException e) { // TODO Auto-generated catch block e.printStackTrace(); } //baseCType.generate(ps); ps.print(" "); ps.print(s); super.generate(ps); }