PageRenderTime 64ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/antlr-2.7.5/lib/cpp/antlr/ASTFactory.hpp

https://github.com/boo/boo-lang
C++ Header | 164 lines | 83 code | 14 blank | 67 comment | 0 complexity | 27b785eb7c7a72058e5b66b31756af8e MD5 | raw file
Possible License(s): GPL-2.0
  1. #ifndef INC_ASTFactory_hpp__
  2. #define INC_ASTFactory_hpp__
  3. /* ANTLR Translator Generator
  4. * Project led by Terence Parr at http://www.jGuru.com
  5. * Software rights: http://www.antlr.org/license.html
  6. *
  7. * $Id: //depot/code/org.antlr/release/antlr-2.7.5/lib/cpp/antlr/ASTFactory.hpp#1 $
  8. */
  9. #include <antlr/config.hpp>
  10. #include <antlr/AST.hpp>
  11. #include <antlr/ASTArray.hpp>
  12. #include <antlr/ASTPair.hpp>
  13. #include <utility>
  14. #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
  15. namespace antlr {
  16. #endif
  17. // Using these extra types to appease MSVC
  18. typedef RefAST (*factory_type_)();
  19. typedef ANTLR_USE_NAMESPACE(std)pair< const char*, factory_type_ > factory_descriptor_;
  20. typedef ANTLR_USE_NAMESPACE(std)vector< factory_descriptor_* > factory_descriptor_list_;
  21. /** AST Super Factory shared by TreeParser and Parser.
  22. * This super factory maintains a map of all AST node types to their respective
  23. * AST factories. One instance should be shared among a parser/treeparser
  24. * chain.
  25. *
  26. * @todo check all this code for possible use of references in
  27. * stead of RefAST's.
  28. */
  29. class ANTLR_API ASTFactory {
  30. public:
  31. typedef factory_type_ factory_type;
  32. typedef factory_descriptor_ factory_descriptor;
  33. typedef factory_descriptor_list_ factory_descriptor_list;
  34. protected:
  35. /* The mapping of AST node type to factory..
  36. */
  37. factory_descriptor default_factory_descriptor;
  38. factory_descriptor_list nodeFactories;
  39. public:
  40. /// Make new factory. Per default (Ref)CommonAST instances are generated.
  41. ASTFactory();
  42. /** Initialize factory with a non default node type.
  43. * factory_node_name should be the name of the AST node type the factory
  44. * generates. (should exist during the existance of this ASTFactory
  45. * instance)
  46. */
  47. ASTFactory( const char* factory_node_name, factory_type factory );
  48. /// Destroy factory
  49. virtual ~ASTFactory();
  50. /// Register a node factory for the node type type with name ast_name
  51. void registerFactory( int type, const char* ast_name, factory_type factory );
  52. /// Set the maximum node (AST) type this factory may encounter
  53. void setMaxNodeType( int type );
  54. /// Add a child to the current AST
  55. void addASTChild(ASTPair& currentAST, RefAST child);
  56. /// Create new empty AST node. The right default type shou
  57. virtual RefAST create();
  58. /// Create AST node of the right type for 'type'
  59. RefAST create(int type);
  60. /// Create AST node of the right type for 'type' and initialize with txt
  61. RefAST create(int type, const ANTLR_USE_NAMESPACE(std)string& txt);
  62. /// Create duplicate of tr
  63. RefAST create(RefAST tr);
  64. /// Create new AST node and initialize contents from a token.
  65. RefAST create(RefToken tok);
  66. /// Create new AST node and initialize contents from a stream.
  67. RefAST create(const ANTLR_USE_NAMESPACE(std)string& txt, ANTLR_USE_NAMESPACE(std)istream& infile );
  68. /** Deep copy a single node. This function the new clone() methods in the
  69. * AST interface. Returns a new RefAST(nullASTptr) if t is null.
  70. */
  71. RefAST dup(RefAST t);
  72. /// Duplicate tree including siblings of root.
  73. RefAST dupList(RefAST t);
  74. /** Duplicate a tree, assuming this is a root node of a tree--
  75. * duplicate that node and what's below; ignore siblings of root node.
  76. */
  77. RefAST dupTree(RefAST t);
  78. /** Make a tree from a list of nodes. The first element in the
  79. * array is the root. If the root is null, then the tree is
  80. * a simple list not a tree. Handles null children nodes correctly.
  81. * For example, make(a, b, null, c) yields tree (a b c). make(null,a,b)
  82. * yields tree (nil a b).
  83. */
  84. RefAST make(ANTLR_USE_NAMESPACE(std)vector<RefAST>& nodes);
  85. /** Make a tree from a list of nodes, where the nodes are contained
  86. * in an ASTArray object. The ASTArray is deleted after use.
  87. * @todo FIXME! I have a feeling we can get rid of this ugly ASTArray thing
  88. */
  89. RefAST make(ASTArray* nodes);
  90. /// Make an AST the root of current AST
  91. void makeASTRoot(ASTPair& currentAST, RefAST root);
  92. /** Set a new default AST type.
  93. * factory_node_name should be the name of the AST node type the factory
  94. * generates. (should exist during the existance of this ASTFactory
  95. * instance).
  96. * Only change factory between parser runs. You might get unexpected results
  97. * otherwise.
  98. */
  99. void setASTNodeFactory( const char* factory_node_name, factory_type factory );
  100. #ifdef ANTLR_SUPPORT_XML
  101. /** Load a XML AST from stream. Make sure you have all the factories
  102. * registered before use.
  103. * @note this 'XML' stuff is quite rough still. YMMV.
  104. */
  105. RefAST LoadAST( ANTLR_USE_NAMESPACE(std)istream& infile );
  106. #endif
  107. protected:
  108. void loadChildren( ANTLR_USE_NAMESPACE(std)istream& infile, RefAST current );
  109. void loadSiblings( ANTLR_USE_NAMESPACE(std)istream& infile, RefAST current );
  110. bool checkCloseTag( ANTLR_USE_NAMESPACE(std)istream& infile );
  111. #ifdef ANTLR_VECTOR_HAS_AT
  112. /// construct a node of 'type'
  113. inline RefAST getNodeOfType( unsigned int type )
  114. {
  115. return RefAST(nodeFactories.at(type)->second());
  116. }
  117. /// get the name of the node 'type'
  118. const char* getASTNodeType( unsigned int type )
  119. {
  120. return nodeFactories.at(type)->first;
  121. }
  122. /// get the factory used for node 'type'
  123. factory_type getASTNodeFactory( unsigned int type )
  124. {
  125. return nodeFactories.at(type)->second;
  126. }
  127. #else
  128. inline RefAST getNodeOfType( unsigned int type )
  129. {
  130. return RefAST(nodeFactories[type]->second());
  131. }
  132. /// get the name of the node 'type'
  133. const char* getASTNodeType( unsigned int type )
  134. {
  135. return nodeFactories[type]->first;
  136. }
  137. factory_type getASTNodeFactory( unsigned int type )
  138. {
  139. return nodeFactories[type]->second;
  140. }
  141. #endif
  142. private:
  143. // no copying and such..
  144. ASTFactory( const ASTFactory& );
  145. ASTFactory& operator=( const ASTFactory& );
  146. };
  147. #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
  148. }
  149. #endif
  150. #endif //INC_ASTFactory_hpp__