CommonTokenStream.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #
  2. # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
  3. # Use of this file is governed by the BSD 3-clause license that
  4. # can be found in the LICENSE.txt file in the project root.
  5. #/
  6. #
  7. # This class extends {@link BufferedTokenStream} with functionality to filter
  8. # token streams to tokens on a particular channel (tokens where
  9. # {@link Token#getChannel} returns a particular value).
  10. #
  11. # <p>
  12. # This token stream provides access to all tokens by index or when calling
  13. # methods like {@link #getText}. The channel filtering is only used for code
  14. # accessing tokens via the lookahead methods {@link #LA}, {@link #LT}, and
  15. # {@link #LB}.</p>
  16. #
  17. # <p>
  18. # By default, tokens are placed on the default channel
  19. # ({@link Token#DEFAULT_CHANNEL}), but may be reassigned by using the
  20. # {@code ->channel(HIDDEN)} lexer command, or by using an embedded action to
  21. # call {@link Lexer#setChannel}.
  22. # </p>
  23. #
  24. # <p>
  25. # Note: lexer rules which use the {@code ->skip} lexer command or call
  26. # {@link Lexer#skip} do not produce tokens at all, so input text matched by
  27. # such a rule will not be available as part of the token stream, regardless of
  28. # channel.</p>
  29. #/
  30. from antlr4.BufferedTokenStream import BufferedTokenStream
  31. from antlr4.Lexer import Lexer
  32. from antlr4.Token import Token
  33. class CommonTokenStream(BufferedTokenStream):
  34. __slots__ = 'channel'
  35. def __init__(self, lexer:Lexer, channel:int=Token.DEFAULT_CHANNEL):
  36. super().__init__(lexer)
  37. self.channel = channel
  38. def adjustSeekIndex(self, i:int):
  39. return self.nextTokenOnChannel(i, self.channel)
  40. def LB(self, k:int):
  41. if k==0 or (self.index-k)<0:
  42. return None
  43. i = self.index
  44. n = 1
  45. # find k good tokens looking backwards
  46. while n <= k:
  47. # skip off-channel tokens
  48. i = self.previousTokenOnChannel(i - 1, self.channel)
  49. n += 1
  50. if i < 0:
  51. return None
  52. return self.tokens[i]
  53. def LT(self, k:int):
  54. self.lazyInit()
  55. if k == 0:
  56. return None
  57. if k < 0:
  58. return self.LB(-k)
  59. i = self.index
  60. n = 1 # we know tokens[pos] is a good one
  61. # find k good tokens
  62. while n < k:
  63. # skip off-channel tokens, but make sure to not look past EOF
  64. if self.sync(i + 1):
  65. i = self.nextTokenOnChannel(i + 1, self.channel)
  66. n += 1
  67. return self.tokens[i]
  68. # Count EOF just once.#/
  69. def getNumberOfOnChannelTokens(self):
  70. n = 0
  71. self.fill()
  72. for i in range(0, len(self.tokens)):
  73. t = self.tokens[i]
  74. if t.channel==self.channel:
  75. n += 1
  76. if t.type==Token.EOF:
  77. break
  78. return n