UnbufferedTokenStream.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "TokenStream.h"
  7. namespace antlr4 {
  8. class ANTLR4CPP_PUBLIC UnbufferedTokenStream : public TokenStream {
  9. public:
  10. UnbufferedTokenStream(TokenSource *tokenSource);
  11. UnbufferedTokenStream(TokenSource *tokenSource, int bufferSize);
  12. UnbufferedTokenStream(const UnbufferedTokenStream& other) = delete;
  13. virtual ~UnbufferedTokenStream();
  14. UnbufferedTokenStream& operator = (const UnbufferedTokenStream& other) = delete;
  15. virtual Token* get(size_t i) const override;
  16. virtual Token* LT(ssize_t i) override;
  17. virtual size_t LA(ssize_t i) override;
  18. virtual TokenSource* getTokenSource() const override;
  19. virtual std::string getText(const misc::Interval &interval) override;
  20. virtual std::string getText() override;
  21. virtual std::string getText(RuleContext *ctx) override;
  22. virtual std::string getText(Token *start, Token *stop) override;
  23. virtual void consume() override;
  24. /// <summary>
  25. /// Return a marker that we can release later.
  26. /// <p/>
  27. /// The specific marker value used for this class allows for some level of
  28. /// protection against misuse where {@code seek()} is called on a mark or
  29. /// {@code release()} is called in the wrong order.
  30. /// </summary>
  31. virtual ssize_t mark() override;
  32. virtual void release(ssize_t marker) override;
  33. virtual size_t index() override;
  34. virtual void seek(size_t index) override;
  35. virtual size_t size() override;
  36. virtual std::string getSourceName() const override;
  37. protected:
  38. /// Make sure we have 'need' elements from current position p. Last valid
  39. /// p index is tokens.length - 1. p + need - 1 is the tokens index 'need' elements
  40. /// ahead. If we need 1 element, (p+1-1)==p must be less than tokens.length.
  41. TokenSource *_tokenSource;
  42. /// <summary>
  43. /// A moving window buffer of the data being scanned. While there's a marker,
  44. /// we keep adding to buffer. Otherwise, <seealso cref="#consume consume()"/> resets so
  45. /// we start filling at index 0 again.
  46. /// </summary>
  47. std::vector<std::unique_ptr<Token>> _tokens;
  48. /// <summary>
  49. /// 0..n-1 index into <seealso cref="#tokens tokens"/> of next token.
  50. /// <p/>
  51. /// The {@code LT(1)} token is {@code tokens[p]}. If {@code p == n}, we are
  52. /// out of buffered tokens.
  53. /// </summary>
  54. size_t _p;
  55. /// <summary>
  56. /// Count up with <seealso cref="#mark mark()"/> and down with
  57. /// <seealso cref="#release release()"/>. When we {@code release()} the last mark,
  58. /// {@code numMarkers} reaches 0 and we reset the buffer. Copy
  59. /// {@code tokens[p]..tokens[n-1]} to {@code tokens[0]..tokens[(n-1)-p]}.
  60. /// </summary>
  61. int _numMarkers;
  62. /// <summary>
  63. /// This is the {@code LT(-1)} token for the current position.
  64. /// </summary>
  65. Token *_lastToken;
  66. /// <summary>
  67. /// When {@code numMarkers > 0}, this is the {@code LT(-1)} token for the
  68. /// first token in <seealso cref="#tokens"/>. Otherwise, this is {@code null}.
  69. /// </summary>
  70. Token *_lastTokenBufferStart;
  71. /// <summary>
  72. /// Absolute token index. It's the index of the token about to be read via
  73. /// {@code LT(1)}. Goes from 0 to the number of tokens in the entire stream,
  74. /// although the stream size is unknown before the end is reached.
  75. /// <p/>
  76. /// This value is used to set the token indexes if the stream provides tokens
  77. /// that implement <seealso cref="WritableToken"/>.
  78. /// </summary>
  79. size_t _currentTokenIndex;
  80. virtual void sync(ssize_t want);
  81. /// <summary>
  82. /// Add {@code n} elements to the buffer. Returns the number of tokens
  83. /// actually added to the buffer. If the return value is less than {@code n},
  84. /// then EOF was reached before {@code n} tokens could be added.
  85. /// </summary>
  86. virtual size_t fill(size_t n);
  87. virtual void add(std::unique_ptr<Token> t);
  88. size_t getBufferStartIndex() const;
  89. private:
  90. void InitializeInstanceFields();
  91. };
  92. } // namespace antlr4