@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()); }
/** * 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); } }
/** * 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); } }
@Override public String getText() { String badText; if (start instanceof Token) { int i = start.getTokenIndex(); int j = stop.getTokenIndex(); if (stop.getType() == Token.EOF) { j = ((TokenStream)input).size(); } badText = ((TokenStream)input).toString(i, j); } else if (start instanceof Tree) { badText = ((TreeNodeStream)input).toString(start, stop); } else { // people should subclass if they alter the tree type so this // next one is for sure correct. badText = "<unknown>"; } return badText; }
public String getText() { String badText = null; if (start instanceof Token) { int i = (start).getTokenIndex(); int j = (stop).getTokenIndex(); if ((stop).getType() == Token.EOF) { j = ((TokenStream) input).size(); } badText = ((TokenStream) input).toString(i, j); } else if (start instanceof Tree) { badText = ((TreeNodeStream) input).toString(start, stop); } else { // people should subclass if they alter the tree type so this // next one is for sure correct. badText = "<unknown>"; } return badText; }
/** Creates a tree parser for a given tree of this kind. */ public <P extends TreeParser> P createTreeParser(Class<P> parserType, I info) { try { // instantiate the parser ParseTreeAdaptor<T,I> adaptor = new ParseTreeAdaptor<>(this); Constructor<P> parserConstructor = parserType.getConstructor(TreeNodeStream.class); P result = parserConstructor.newInstance(adaptor.createTreeNodeStream(this)); Method adaptorSetter = parserType.getMethod("setTreeAdaptor", TreeAdaptor.class); adaptorSetter.invoke(result, adaptor); callInitialise(result, info); return result; } catch (Exception e) { throw toRuntime(e); } }
@VisibleForTesting static Statement createStatement(CommonTree tree) { TreeNodeStream stream = new BufferedTreeNodeStream(tree); StatementBuilder builder = new StatementBuilder(stream); try { return builder.statement().value; } catch (RecognitionException e) { throw new AssertionError(e); // RecognitionException is not thrown } }
private static Expression createExpression(CommonTree tree) { TreeNodeStream stream = new BufferedTreeNodeStream(tree); StatementBuilder builder = new StatementBuilder(stream); try { return builder.singleExpression().value; } catch (RecognitionException e) { throw new AssertionError(e); // RecognitionException is not thrown } }
private static String createIdentifier(CommonTree identTree) { TreeNodeStream stream = new BufferedTreeNodeStream(identTree); StatementBuilder builder = new StatementBuilder(stream); try { return builder.ident().value; } catch (RecognitionException e) { throw new AssertionError(e); // RecognitionException is not thrown } }
@Test public void testTypeGenericURI() 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<String> stringValues;\n" + " List<JobURI> jobUriValues;\n" + " JobURI myJobURI;\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")); walker.apiGen(); walker.dumpFiles(outputKernelFolder, outputApiFolder, outputWebFolder); System.out.println("Done, folder=" + parentFolder.getAbsolutePath()); }
/** * Construct from a tree nodes stream and a shared state. * @param input the tree nodes stream * @param state the shared state * @param errorHandler handles error messages */ public CobolStructureEmitterImpl( final TreeNodeStream input, final RecognizerSharedState state, final RecognizerErrorHandler errorHandler) { super(input, state); _errorHandler = errorHandler; }
public DebugTreeNodeStream(TreeNodeStream input, DebugEventListener dbg) { this.input = input; this.adaptor = input.getTreeAdaptor(); this.input.setUniqueNavigationNodes(true); setDebugListener(dbg); }
public static Document parseThriftIdl(CharSource input) throws IOException { Tree tree = parseTree(input); TreeNodeStream stream = new BufferedTreeNodeStream(tree); DocumentGenerator generator = new DocumentGenerator(stream); try { return generator.document().value; } catch (RecognitionException e) { throw new IllegalArgumentException(e); } }
/** Creates a tree node stream based on this adaptor. */ public TreeNodeStream createTreeNodeStream(Tree tree) { return new CommonTreeNodeStream(this, tree); }
public smaliTreeWalker(TreeNodeStream input) { this(input, new RecognizerSharedState()); }
public smaliTreeWalker(TreeNodeStream input, RecognizerSharedState state) { super(input, state); }
protected AbstractPythonTTree(TreeNodeStream input) { super(input); }
protected AbstractPythonTTree(TreeNodeStream input, RecognizerSharedState state) { super(input, state); }
protected AbstractCppTTree(TreeNodeStream input) { super(input); }
protected AbstractCppTTree(TreeNodeStream input, RecognizerSharedState state) { super(input, state); }
@Test public void test6() throws RecognitionException { CharStream input = new ANTLRStringStream("sdk(alan)\n" + "version(0.0.1)\n" + "minVer(0.0.1)\n" + "\n" + "[Information about a folder or a file in a repo.]\n" + "type RaptureFolderInfo(@package=rapture.common) {\n" + " String name;\n" + " Boolean isFolder;\n" + "}\n" + "\n" + "[ This is a test api of the SDK.]\n" + "api(TestSDK) {\n" + " [Log a message to standard out]\n" + " @entitle=/admin/main\n" + " @public Boolean logMessage(String msg);\n" + "}\n" + "\n" + "//include \"testApi/crudSamp.api\"\n" + "\n" + "[Alan Info type]\n" + "\n" + "crud AlanInfo( region name ) { \n" + " String region;\n" + " String name;\n" + " String value;\n" + "}\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(); }
public ElementFrequenciesVisitor(TreeNodeStream input) { super(input); frequencies = new ArrayDeque<FrequencySet<String>>(); frequencies.push(new FrequencySet<String>()); }
/** * Construct from a tree nodes stream. * @param input the tree nodes stream * @param errorHandler handles error messages */ public CobolStructureEmitterImpl( final TreeNodeStream input, final RecognizerErrorHandler errorHandler) { this(input, new RecognizerSharedState(), errorHandler); }