PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Dependencies/boo/src/Boo.Lang.Parser/antlr/antlr/TreeParser.cs

https://github.com/w4x/boolangstudio
C# | 178 lines | 111 code | 16 blank | 51 comment | 20 complexity | 5a1c969a832aff31307638dbacfd1b33 MD5 | raw file
Possible License(s): GPL-2.0
  1. using System;
  2. using AST = antlr.collections.AST;
  3. using BitSet = antlr.collections.impl.BitSet;
  4. namespace antlr
  5. {
  6. /*ANTLR Translator Generator
  7. * Project led by Terence Parr at http://www.jGuru.com
  8. * Software rights: http://www.antlr.org/license.html
  9. *
  10. * $Id:$
  11. */
  12. //
  13. // ANTLR C# Code Generator by Micheal Jordan
  14. // Kunle Odutola : kunle UNDERSCORE odutola AT hotmail DOT com
  15. // Anthony Oguntimehin
  16. //
  17. // With many thanks to Eric V. Smith from the ANTLR list.
  18. //
  19. public class TreeParser
  20. {
  21. /*The AST Null object; the parsing cursor is set to this when
  22. * it is found to be null. This way, we can test the
  23. * token type of a node without having to have tests for null
  24. * everywhere.
  25. */
  26. public static ASTNULLType ASTNULL = new ASTNULLType();
  27. /*Where did this rule leave off parsing; avoids a return parameter */
  28. protected internal AST retTree_;
  29. /*guessing nesting level; guessing==0 implies not guessing */
  30. // protected int guessing = 0;
  31. /*Nesting level of registered handlers */
  32. // protected int exceptionLevel = 0;
  33. protected internal TreeParserSharedInputState inputState;
  34. /*Table of token type to token names */
  35. protected internal string[] tokenNames;
  36. /*AST return value for a rule is squirreled away here */
  37. protected internal AST returnAST;
  38. /*AST support code; parser and treeparser delegate to this object */
  39. protected internal ASTFactory astFactory = new ASTFactory();
  40. /*Used to keep track of indentdepth for traceIn/Out */
  41. protected internal int traceDepth = 0;
  42. public TreeParser()
  43. {
  44. inputState = new TreeParserSharedInputState();
  45. }
  46. /*Get the AST return value squirreled away in the parser */
  47. public virtual AST getAST()
  48. {
  49. return returnAST;
  50. }
  51. public virtual ASTFactory getASTFactory()
  52. {
  53. return astFactory;
  54. }
  55. public virtual void resetState()
  56. {
  57. traceDepth = 0;
  58. returnAST = null;
  59. retTree_ = null;
  60. inputState.reset();
  61. }
  62. public virtual string getTokenName(int num)
  63. {
  64. return tokenNames[num];
  65. }
  66. public virtual string[] getTokenNames()
  67. {
  68. return tokenNames;
  69. }
  70. protected internal virtual void match(AST t, int ttype)
  71. {
  72. //System.out.println("match("+ttype+"); cursor is "+t);
  73. if (t == null || t == ASTNULL || t.Type != ttype)
  74. {
  75. throw new MismatchedTokenException(getTokenNames(), t, ttype, false);
  76. }
  77. }
  78. /*Make sure current lookahead symbol matches the given set
  79. * Throw an exception upon mismatch, which is catch by either the
  80. * error handler or by the syntactic predicate.
  81. */
  82. public virtual void match(AST t, BitSet b)
  83. {
  84. if (t == null || t == ASTNULL || !b.member(t.Type))
  85. {
  86. throw new MismatchedTokenException(getTokenNames(), t, b, false);
  87. }
  88. }
  89. protected internal virtual void matchNot(AST t, int ttype)
  90. {
  91. //System.out.println("match("+ttype+"); cursor is "+t);
  92. if (t == null || t == ASTNULL || t.Type == ttype)
  93. {
  94. throw new MismatchedTokenException(getTokenNames(), t, ttype, true);
  95. }
  96. }
  97. /// <summary>
  98. /// @deprecated as of 2.7.2. This method calls System.exit() and writes
  99. /// directly to stderr, which is usually not appropriate when
  100. /// a parser is embedded into a larger application. Since the method is
  101. /// <code>static</code>, it cannot be overridden to avoid these problems.
  102. /// ANTLR no longer uses this method internally or in generated code.
  103. /// </summary>
  104. ///
  105. [Obsolete("De-activated since version 2.7.2.6 as it cannot be overidden.", true)]
  106. public static void panic()
  107. {
  108. Console.Error.WriteLine("TreeWalker: panic");
  109. System.Environment.Exit(1);
  110. }
  111. /*Parser error-reporting function can be overridden in subclass */
  112. public virtual void reportError(RecognitionException ex)
  113. {
  114. Console.Error.WriteLine(ex.ToString());
  115. }
  116. /*Parser error-reporting function can be overridden in subclass */
  117. public virtual void reportError(string s)
  118. {
  119. Console.Error.WriteLine("error: " + s);
  120. }
  121. /*Parser warning-reporting function can be overridden in subclass */
  122. public virtual void reportWarning(string s)
  123. {
  124. Console.Error.WriteLine("warning: " + s);
  125. }
  126. /*Specify an object with support code (shared by
  127. * Parser and TreeParser. Normally, the programmer
  128. * does not play with this, using setASTNodeType instead.
  129. */
  130. public virtual void setASTFactory(ASTFactory f)
  131. {
  132. astFactory = f;
  133. }
  134. /*Specify the type of node to create during tree building */
  135. public virtual void setASTNodeType(string nodeType)
  136. {
  137. setASTNodeClass(nodeType);
  138. }
  139. /*Specify the type of node to create during tree building */
  140. public virtual void setASTNodeClass(string nodeType)
  141. {
  142. astFactory.setASTNodeType(nodeType);
  143. }
  144. public virtual void traceIndent()
  145. {
  146. for (int i = 0; i < traceDepth; i++)
  147. Console.Out.Write(" ");
  148. }
  149. public virtual void traceIn(string rname, AST t)
  150. {
  151. traceDepth += 1;
  152. traceIndent();
  153. Console.Out.WriteLine("> " + rname + "(" + ((t != null) ? t.ToString() : "null") + ")" + ((inputState.guessing > 0) ? " [guessing]" : ""));
  154. }
  155. public virtual void traceOut(string rname, AST t)
  156. {
  157. traceIndent();
  158. Console.Out.WriteLine("< " + rname + "(" + ((t != null) ? t.ToString() : "null") + ")" + ((inputState.guessing > 0) ? " [guessing]" : ""));
  159. traceDepth--;
  160. }
  161. }
  162. }