]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/UnbufferedCharStream.h
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / UnbufferedCharStream.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 "CharStream.h"
9
10 namespace antlr4 {
11
12   /// Do not buffer up the entire char stream. It does keep a small buffer
13   /// for efficiency and also buffers while a mark exists (set by the
14   /// lookahead prediction in parser). "Unbuffered" here refers to fact
15   /// that it doesn't buffer all data, not that's it's on demand loading of char.
16   class ANTLR4CPP_PUBLIC UnbufferedCharStream : public CharStream {
17   public:
18     /// The name or source of this char stream.
19     std::string name;
20
21     UnbufferedCharStream(std::wistream &input);
22
23     virtual void consume() override;
24     virtual size_t LA(ssize_t i) override;
25
26     /// <summary>
27     /// Return a marker that we can release later.
28     /// <p/>
29     /// The specific marker value used for this class allows for some level of
30     /// protection against misuse where {@code seek()} is called on a mark or
31     /// {@code release()} is called in the wrong order.
32     /// </summary>
33     virtual ssize_t mark() override;
34
35     /// <summary>
36     /// Decrement number of markers, resetting buffer if we hit 0. </summary>
37     /// <param name="marker"> </param>
38     virtual void release(ssize_t marker) override;
39     virtual size_t index() override;
40
41     /// <summary>
42     /// Seek to absolute character index, which might not be in the current
43     ///  sliding window.  Move {@code p} to {@code index-bufferStartIndex}.
44     /// </summary>
45     virtual void seek(size_t index) override;
46     virtual size_t size() override;
47     virtual std::string getSourceName() const override;
48     virtual std::string getText(const misc::Interval &interval) override;
49
50   protected:
51     /// A moving window buffer of the data being scanned. While there's a marker,
52     /// we keep adding to buffer. Otherwise, <seealso cref="#consume consume()"/> resets so
53     /// we start filling at index 0 again.
54     // UTF-32 encoded.
55 #if defined(_MSC_VER) && _MSC_VER == 1900
56     i32string _data; // Custom type for VS 2015.
57     typedef __int32 storage_type;
58 #else
59     std::u32string _data;
60     typedef char32_t storage_type;
61 #endif
62
63     /// <summary>
64     /// 0..n-1 index into <seealso cref="#data data"/> of next character.
65     /// <p/>
66     /// The {@code LA(1)} character is {@code data[p]}. If {@code p == n}, we are
67     /// out of buffered characters.
68     /// </summary>
69     size_t _p;
70
71     /// <summary>
72     /// Count up with <seealso cref="#mark mark()"/> and down with
73     /// <seealso cref="#release release()"/>. When we {@code release()} the last mark,
74     /// {@code numMarkers} reaches 0 and we reset the buffer. Copy
75     /// {@code data[p]..data[n-1]} to {@code data[0]..data[(n-1)-p]}.
76     /// </summary>
77     size_t _numMarkers;
78
79     /// This is the {@code LA(-1)} character for the current position.
80     size_t _lastChar; // UTF-32
81
82     /// <summary>
83     /// When {@code numMarkers > 0}, this is the {@code LA(-1)} character for the
84     /// first character in <seealso cref="#data data"/>. Otherwise, this is unspecified.
85     /// </summary>
86     size_t _lastCharBufferStart; // UTF-32
87
88     /// <summary>
89     /// Absolute character index. It's the index of the character about to be
90     /// read via {@code LA(1)}. Goes from 0 to the number of characters in the
91     /// entire stream, although the stream size is unknown before the end is
92     /// reached.
93     /// </summary>
94     size_t _currentCharIndex;
95
96     std::wistream &_input;
97
98     /// <summary>
99     /// Make sure we have 'want' elements from current position <seealso cref="#p p"/>.
100     /// Last valid {@code p} index is {@code data.length-1}. {@code p+need-1} is
101     /// the char index 'need' elements ahead. If we need 1 element,
102     /// {@code (p+1-1)==p} must be less than {@code data.length}.
103     /// </summary>
104     virtual void sync(size_t want);
105
106     /// <summary>
107     /// Add {@code n} characters to the buffer. Returns the number of characters
108     /// actually added to the buffer. If the return value is less than {@code n},
109     /// then EOF was reached before {@code n} characters could be added.
110     /// </summary>
111     virtual size_t fill(size_t n);
112
113     /// Override to provide different source of characters than
114     /// <seealso cref="#input input"/>.
115     virtual char32_t nextChar();
116     virtual void add(char32_t c);
117     size_t getBufferStartIndex() const;
118
119   private:
120     void InitializeInstanceFields();
121   };
122
123 } // namespace antlr4