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