TreeParser.hpp
上传用户:afrynkmhm
上传日期:2007-01-06
资源大小:1262k
文件大小:5k
源码类别:

编译器/解释器

开发平台:

Others

  1. #ifndef INC_TreeParser_hpp__
  2. #define INC_TreeParser_hpp__
  3. /**
  4.  * <b>SOFTWARE RIGHTS</b>
  5.  * <p>
  6.  * ANTLR 2.6.0 MageLang Insitute, 1999
  7.  * <p>
  8.  * We reserve no legal rights to the ANTLR--it is fully in the
  9.  * public domain. An individual or company may do whatever
  10.  * they wish with source code distributed with ANTLR or the
  11.  * code generated by ANTLR, including the incorporation of
  12.  * ANTLR, or its output, into commerical software.
  13.  * <p>
  14.  * We encourage users to develop software with ANTLR. However,
  15.  * we do ask that credit is given to us for developing
  16.  * ANTLR. By "credit", we mean that if you use ANTLR or
  17.  * incorporate any source code into one of your programs
  18.  * (commercial product, research project, or otherwise) that
  19.  * you acknowledge this fact somewhere in the documentation,
  20.  * research report, etc... If you like ANTLR and have
  21.  * developed a nice tool with the output, please mention that
  22.  * you developed it using ANTLR. In addition, we ask that the
  23.  * headers remain intact in our source code. As long as these
  24.  * guidelines are kept, we expect to continue enhancing this
  25.  * system and expect to make other tools available as they are
  26.  * completed.
  27.  * <p>
  28.  * The ANTLR gang:
  29.  * @version ANTLR 2.6.0 MageLang Insitute, 1999
  30.  * @author Terence Parr, <a href=http://www.MageLang.com>MageLang Institute</a>
  31.  * @author <br>John Lilley, <a href=http://www.Empathy.com>Empathy Software</a>
  32.  * @author <br><a href="mailto:pete@yamuna.demon.co.uk">Pete Wells</a>
  33.  */
  34. #include "antlr/config.hpp"
  35. #include "antlr/AST.hpp"
  36. #include "antlr/ASTFactory.hpp"
  37. #include "antlr/BitSet.hpp"
  38. #include "antlr/RecognitionException.hpp"
  39. #include "antlr/TreeParserSharedInputState.hpp"
  40. ANTLR_BEGIN_NAMESPACE(antlr)
  41. class TreeParser {
  42. public:
  43. TreeParser();
  44. TreeParser(const TreeParserSharedInputState& state);
  45. virtual ~TreeParser();
  46. protected:
  47. void setTokenNames(const char** tokenNames_);
  48. public:
  49. /** The AST Null object; the parsing cursor is set to this when
  50.  *  it is found to be null.  This way, we can test the
  51.  *  token type of a node without having to have tests for null
  52.  *  everywhere.
  53.  */
  54. static RefAST ASTNULL;
  55. protected:
  56. /** Where did this rule leave off parsing; avoids a return parameter */
  57. RefAST _retTree;
  58. /** guessing nesting level; guessing==0 implies not guessing */
  59. // int guessing; // = 0;
  60. /** Nesting level of registered handlers */
  61. // int exceptionLevel; // = 0;
  62. TreeParserSharedInputState inputState;
  63. /** Table of token type to token names */
  64. ANTLR_USE_NAMESPACE(std)vector<ANTLR_USE_NAMESPACE(std)string> tokenNames;
  65. /** AST return value for a rule is squirreled away here */
  66. RefAST returnAST;
  67. /** AST support code; parser and treeparser delegate to this object */
  68. ASTFactory astFactory; // = new ASTFactory();
  69. public:
  70. /** Get the AST return value squirreled away in the parser */
  71. RefAST getAST() const {
  72. return returnAST;
  73. }
  74. ANTLR_USE_NAMESPACE(std)string getTokenName(int num) const;
  75. ANTLR_USE_NAMESPACE(std)vector<ANTLR_USE_NAMESPACE(std)string> getTokenNames() const;
  76. protected:
  77. void match(RefAST t, int ttype);
  78. public:
  79. /**Make sure current lookahead symbol matches the given set
  80.  * Throw an exception upon mismatch, which is catch by either the
  81.  * error handler or by the syntactic predicate.
  82.  */
  83. void match(RefAST t, const BitSet& b);
  84. protected:
  85. void matchNot(RefAST t, int ttype);
  86. public:
  87. static void panic();
  88. /** Parser error-reporting function can be overridden in subclass */
  89. virtual void reportError(const RecognitionException& ex);
  90. /** Parser error-reporting function can be overridden in subclass */
  91. virtual void reportError(const ANTLR_USE_NAMESPACE(std)string& s);
  92. /** Parser warning-reporting function can be overridden in subclass */
  93. virtual void reportWarning(const ANTLR_USE_NAMESPACE(std)string& s);
  94. /** Specify an object with support code (shared by
  95.  *  Parser and TreeParser.  Normally, the programmer
  96.  *  does not play with this, using setASTNodeType instead.
  97.  */
  98. // void setASTFactory(ASTFactory f);
  99. /** Specify the type of node to create during tree building */
  100. void setASTNodeFactory(ASTFactory::factory_type factory);
  101. protected:
  102. class Tracer {
  103. private:
  104. TreeParser* parser;
  105. ANTLR_USE_NAMESPACE(std)string text;
  106. RefAST tree;
  107. public:
  108. Tracer(TreeParser* p,const ANTLR_USE_NAMESPACE(std)string& t, RefAST a)
  109. : parser(p), text(t), tree(a) { parser->traceIn(text,tree); }
  110. ~Tracer()
  111. { parser->traceOut(text,tree); }
  112. };
  113. public:
  114. void traceIn(const ANTLR_USE_NAMESPACE(std)string& rname, RefAST t);
  115. void traceOut(const ANTLR_USE_NAMESPACE(std)string& rname, RefAST t);
  116. private:
  117. TreeParser(const TreeParser& other);
  118. TreeParser& operator=(const TreeParser& other);
  119. };
  120. ANTLR_END_NAMESPACE
  121. #endif //INC_TreeParser_hpp__