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

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

https://github.com/w4x/boolangstudio
C# | 179 lines | 111 code | 10 blank | 58 comment | 14 complexity | c7a340a6d5162985da02da40ef04dab7 MD5 | raw file
Possible License(s): GPL-2.0
  1. using System;
  2. using BitSet = antlr.collections.impl.BitSet;
  3. namespace antlr
  4. {
  5. /*ANTLR Translator Generator
  6. * Project led by Terence Parr at http://www.jGuru.com
  7. * Software rights: http://www.antlr.org/license.html
  8. *
  9. * $Id:$
  10. */
  11. //
  12. // ANTLR C# Code Generator by Micheal Jordan
  13. // Kunle Odutola : kunle UNDERSCORE odutola AT hotmail DOT com
  14. // Anthony Oguntimehin
  15. //
  16. // With many thanks to Eric V. Smith from the ANTLR list.
  17. //
  18. /*This object filters a token stream coming from a lexer
  19. * or another TokenStream so that only certain token channels
  20. * get transmitted to the parser.
  21. *
  22. * Any of the channels can be filtered off as "hidden" channels whose
  23. * tokens can be accessed from the parser.
  24. */
  25. public class TokenStreamHiddenTokenFilter : TokenStreamBasicFilter, TokenStream
  26. {
  27. // protected BitSet discardMask;
  28. protected internal BitSet hideMask;
  29. private IHiddenStreamToken nextMonitoredToken;
  30. /*track tail of hidden list emanating from previous
  31. * monitored token
  32. */
  33. protected internal IHiddenStreamToken lastHiddenToken;
  34. protected internal IHiddenStreamToken firstHidden = null;
  35. public TokenStreamHiddenTokenFilter(TokenStream input) : base(input)
  36. {
  37. hideMask = new BitSet();
  38. }
  39. protected internal virtual void consume()
  40. {
  41. nextMonitoredToken = (IHiddenStreamToken) input.nextToken();
  42. }
  43. private void consumeFirst()
  44. {
  45. consume(); // get first token of input stream
  46. // Handle situation where hidden or discarded tokens
  47. // appear first in input stream
  48. IHiddenStreamToken p = null;
  49. // while hidden or discarded scarf tokens
  50. while (hideMask.member(LA(1).Type) || discardMask.member(LA(1).Type))
  51. {
  52. if (hideMask.member(LA(1).Type))
  53. {
  54. if (p == null)
  55. {
  56. p = LA(1);
  57. }
  58. else
  59. {
  60. p.setHiddenAfter(LA(1));
  61. LA(1).setHiddenBefore(p); // double-link
  62. p = LA(1);
  63. }
  64. lastHiddenToken = p;
  65. if (firstHidden == null)
  66. {
  67. firstHidden = p; // record hidden token if first
  68. }
  69. }
  70. consume();
  71. }
  72. }
  73. public virtual BitSet getDiscardMask()
  74. {
  75. return discardMask;
  76. }
  77. /*Return a ptr to the hidden token appearing immediately after
  78. * token t in the input stream.
  79. */
  80. public virtual IHiddenStreamToken getHiddenAfter(IHiddenStreamToken t)
  81. {
  82. return t.getHiddenAfter();
  83. }
  84. /*Return a ptr to the hidden token appearing immediately before
  85. * token t in the input stream.
  86. */
  87. public virtual IHiddenStreamToken getHiddenBefore(IHiddenStreamToken t)
  88. {
  89. return t.getHiddenBefore();
  90. }
  91. public virtual BitSet getHideMask()
  92. {
  93. return hideMask;
  94. }
  95. /*Return the first hidden token if one appears
  96. * before any monitored token.
  97. */
  98. public virtual IHiddenStreamToken getInitialHiddenToken()
  99. {
  100. return firstHidden;
  101. }
  102. public virtual void hide(int m)
  103. {
  104. hideMask.add(m);
  105. }
  106. public virtual void hide(BitSet mask)
  107. {
  108. hideMask = mask;
  109. }
  110. protected internal virtual IHiddenStreamToken LA(int i)
  111. {
  112. return nextMonitoredToken;
  113. }
  114. /*Return the next monitored token.
  115. * Test the token following the monitored token.
  116. * If following is another monitored token, save it
  117. * for the next invocation of nextToken (like a single
  118. * lookahead token) and return it then.
  119. * If following is unmonitored, nondiscarded (hidden)
  120. * channel token, add it to the monitored token.
  121. *
  122. * Note: EOF must be a monitored Token.
  123. */
  124. override public IToken nextToken()
  125. {
  126. // handle an initial condition; don't want to get lookahead
  127. // token of this splitter until first call to nextToken
  128. if (LA(1) == null)
  129. {
  130. consumeFirst();
  131. }
  132. // we always consume hidden tokens after monitored, thus,
  133. // upon entry LA(1) is a monitored token.
  134. IHiddenStreamToken monitored = LA(1);
  135. // point to hidden tokens found during last invocation
  136. monitored.setHiddenBefore(lastHiddenToken);
  137. lastHiddenToken = null;
  138. // Look for hidden tokens, hook them into list emanating
  139. // from the monitored tokens.
  140. consume();
  141. IHiddenStreamToken p = monitored;
  142. // while hidden or discarded scarf tokens
  143. while (hideMask.member(LA(1).Type) || discardMask.member(LA(1).Type))
  144. {
  145. if (hideMask.member(LA(1).Type))
  146. {
  147. // attach the hidden token to the monitored in a chain
  148. // link forwards
  149. p.setHiddenAfter(LA(1));
  150. // link backwards
  151. if (p != monitored)
  152. {
  153. //hidden cannot point to monitored tokens
  154. LA(1).setHiddenBefore(p);
  155. }
  156. p = (lastHiddenToken = LA(1));
  157. }
  158. consume();
  159. }
  160. return monitored;
  161. }
  162. public virtual void resetState()
  163. {
  164. firstHidden = null;
  165. lastHiddenToken = null;
  166. nextMonitoredToken = null;
  167. }
  168. }
  169. }