CommonTokenStream.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
  2. * Use of this file is governed by the BSD 3-clause license that
  3. * can be found in the LICENSE.txt file in the project root.
  4. */
  5. #pragma once
  6. #include "BufferedTokenStream.h"
  7. namespace antlr4 {
  8. /**
  9. * This class extends {@link BufferedTokenStream} with functionality to filter
  10. * token streams to tokens on a particular channel (tokens where
  11. * {@link Token#getChannel} returns a particular value).
  12. *
  13. * <p>
  14. * This token stream provides access to all tokens by index or when calling
  15. * methods like {@link #getText}. The channel filtering is only used for code
  16. * accessing tokens via the lookahead methods {@link #LA}, {@link #LT}, and
  17. * {@link #LB}.</p>
  18. *
  19. * <p>
  20. * By default, tokens are placed on the default channel
  21. * ({@link Token#DEFAULT_CHANNEL}), but may be reassigned by using the
  22. * {@code ->channel(HIDDEN)} lexer command, or by using an embedded action to
  23. * call {@link Lexer#setChannel}.
  24. * </p>
  25. *
  26. * <p>
  27. * Note: lexer rules which use the {@code ->skip} lexer command or call
  28. * {@link Lexer#skip} do not produce tokens at all, so input text matched by
  29. * such a rule will not be available as part of the token stream, regardless of
  30. * channel.</p>
  31. */
  32. class ANTLR4CPP_PUBLIC CommonTokenStream : public BufferedTokenStream {
  33. public:
  34. /**
  35. * Constructs a new {@link CommonTokenStream} using the specified token
  36. * source and the default token channel ({@link Token#DEFAULT_CHANNEL}).
  37. *
  38. * @param tokenSource The token source.
  39. */
  40. CommonTokenStream(TokenSource *tokenSource);
  41. /**
  42. * Constructs a new {@link CommonTokenStream} using the specified token
  43. * source and filtering tokens to the specified channel. Only tokens whose
  44. * {@link Token#getChannel} matches {@code channel} or have the
  45. * {@link Token#getType} equal to {@link Token#EOF} will be returned by the
  46. * token stream lookahead methods.
  47. *
  48. * @param tokenSource The token source.
  49. * @param channel The channel to use for filtering tokens.
  50. */
  51. CommonTokenStream(TokenSource *tokenSource, size_t channel);
  52. virtual Token* LT(ssize_t k) override;
  53. /// Count EOF just once.
  54. virtual int getNumberOfOnChannelTokens();
  55. protected:
  56. /**
  57. * Specifies the channel to use for filtering tokens.
  58. *
  59. * <p>
  60. * The default value is {@link Token#DEFAULT_CHANNEL}, which matches the
  61. * default channel assigned to tokens created by the lexer.</p>
  62. */
  63. size_t channel;
  64. virtual ssize_t adjustSeekIndex(size_t i) override;
  65. virtual Token* LB(size_t k) override;
  66. };
  67. } // namespace antlr4