PageRenderTime 22ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Dependencies/boo/lib/antlr-2.7.5/antlr/TokenStreamHiddenTokenFilter.java

https://github.com/w4x/boolangstudio
Java | 152 lines | 81 code | 21 blank | 50 comment | 15 complexity | 5fac7182bd89f5646d419cb572bb55cb MD5 | raw file
Possible License(s): GPL-2.0
  1. package antlr;
  2. /* ANTLR Translator Generator
  3. * Project led by Terence Parr at http://www.jGuru.com
  4. * Software rights: http://www.antlr.org/license.html
  5. *
  6. * $Id: //depot/code/org.antlr/release/antlr-2.7.5/antlr/TokenStreamHiddenTokenFilter.java#1 $
  7. */
  8. import antlr.collections.impl.BitSet;
  9. /**This object filters a token stream coming from a lexer
  10. * or another TokenStream so that only certain token channels
  11. * get transmitted to the parser.
  12. *
  13. * Any of the channels can be filtered off as "hidden" channels whose
  14. * tokens can be accessed from the parser.
  15. */
  16. public class TokenStreamHiddenTokenFilter extends TokenStreamBasicFilter implements TokenStream {
  17. // protected BitSet discardMask;
  18. protected BitSet hideMask;
  19. protected CommonHiddenStreamToken nextMonitoredToken;
  20. /** track tail of hidden list emanating from previous
  21. * monitored token
  22. */
  23. protected CommonHiddenStreamToken lastHiddenToken;
  24. protected CommonHiddenStreamToken firstHidden = null;
  25. public TokenStreamHiddenTokenFilter(TokenStream input) {
  26. super(input);
  27. hideMask = new BitSet();
  28. }
  29. protected void consume() throws TokenStreamException {
  30. nextMonitoredToken = (CommonHiddenStreamToken)input.nextToken();
  31. }
  32. private void consumeFirst() throws TokenStreamException {
  33. consume(); // get first token of input stream
  34. // Handle situation where hidden or discarded tokens
  35. // appear first in input stream
  36. CommonHiddenStreamToken p = null;
  37. // while hidden or discarded scarf tokens
  38. while (hideMask.member(LA(1).getType()) || discardMask.member(LA(1).getType())) {
  39. if (hideMask.member(LA(1).getType())) {
  40. if (p == null) {
  41. p = LA(1);
  42. }
  43. else {
  44. p.setHiddenAfter(LA(1));
  45. LA(1).setHiddenBefore(p); // double-link
  46. p = LA(1);
  47. }
  48. lastHiddenToken = p;
  49. if (firstHidden == null) {
  50. firstHidden = p; // record hidden token if first
  51. }
  52. }
  53. consume();
  54. }
  55. }
  56. public BitSet getDiscardMask() {
  57. return discardMask;
  58. }
  59. /** Return a ptr to the hidden token appearing immediately after
  60. * token t in the input stream.
  61. */
  62. public CommonHiddenStreamToken getHiddenAfter(CommonHiddenStreamToken t) {
  63. return t.getHiddenAfter();
  64. }
  65. /** Return a ptr to the hidden token appearing immediately before
  66. * token t in the input stream.
  67. */
  68. public CommonHiddenStreamToken getHiddenBefore(CommonHiddenStreamToken t) {
  69. return t.getHiddenBefore();
  70. }
  71. public BitSet getHideMask() {
  72. return hideMask;
  73. }
  74. /** Return the first hidden token if one appears
  75. * before any monitored token.
  76. */
  77. public CommonHiddenStreamToken getInitialHiddenToken() {
  78. return firstHidden;
  79. }
  80. public void hide(int m) {
  81. hideMask.add(m);
  82. }
  83. public void hide(BitSet mask) {
  84. hideMask = mask;
  85. }
  86. protected CommonHiddenStreamToken LA(int i) {
  87. return nextMonitoredToken;
  88. }
  89. /** Return the next monitored token.
  90. * Test the token following the monitored token.
  91. * If following is another monitored token, save it
  92. * for the next invocation of nextToken (like a single
  93. * lookahead token) and return it then.
  94. * If following is unmonitored, nondiscarded (hidden)
  95. * channel token, add it to the monitored token.
  96. *
  97. * Note: EOF must be a monitored Token.
  98. */
  99. public Token nextToken() throws TokenStreamException {
  100. // handle an initial condition; don't want to get lookahead
  101. // token of this splitter until first call to nextToken
  102. if (LA(1) == null) {
  103. consumeFirst();
  104. }
  105. // we always consume hidden tokens after monitored, thus,
  106. // upon entry LA(1) is a monitored token.
  107. CommonHiddenStreamToken monitored = LA(1);
  108. // point to hidden tokens found during last invocation
  109. monitored.setHiddenBefore(lastHiddenToken);
  110. lastHiddenToken = null;
  111. // Look for hidden tokens, hook them into list emanating
  112. // from the monitored tokens.
  113. consume();
  114. CommonHiddenStreamToken p = monitored;
  115. // while hidden or discarded scarf tokens
  116. while (hideMask.member(LA(1).getType()) || discardMask.member(LA(1).getType())) {
  117. if (hideMask.member(LA(1).getType())) {
  118. // attach the hidden token to the monitored in a chain
  119. // link forwards
  120. p.setHiddenAfter(LA(1));
  121. // link backwards
  122. if (p != monitored) { //hidden cannot point to monitored tokens
  123. LA(1).setHiddenBefore(p);
  124. }
  125. p = lastHiddenToken = LA(1);
  126. }
  127. consume();
  128. }
  129. return monitored;
  130. }
  131. }