2 // Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3 // Copyright (C) 2013 LunarG, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
14 // Redistributions in binary form must reproduce the above
15 // copyright notice, this list of conditions and the following
16 // disclaimer in the documentation and/or other materials provided
17 // with the distribution.
19 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
20 // contributors may be used to endorse or promote products derived
21 // from this software without specific prior written permission.
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 // POSSIBILITY OF SUCH DAMAGE.
36 #ifndef _GLSLANG_SCAN_INCLUDED_
37 #define _GLSLANG_SCAN_INCLUDED_
43 // Use a global end-of-input character, so no translation is needed across
44 // layers of encapsulation. Characters are all 8 bit, and positive, so there is
45 // no aliasing of character 255 onto -1, for example.
46 const int EndOfInput = -1;
49 // A character scanner that seamlessly, on read-only strings, reads across an
50 // array of strings without assuming null termination.
54 TInputScanner(int n, const char* const s[], size_t L[], const char* const* names = nullptr,
55 int b = 0, int f = 0, bool single = false) :
57 // up to this point, common usage is "char*", but now we need positive 8-bit characters
58 sources(reinterpret_cast<const unsigned char* const *>(s)),
59 lengths(L), currentSource(0), currentChar(0), stringBias(b), finale(f), singleLogical(single),
60 endOfFileReached(false)
62 loc = new TSourceLoc[numSources];
63 for (int i = 0; i < numSources; ++i) {
64 loc[i].init(i - stringBias);
66 if (names != nullptr) {
67 for (int i = 0; i < numSources; ++i)
68 loc[i].name = names[i] != nullptr ? NewPoolTString(names[i]) : nullptr;
70 loc[currentSource].line = 1;
71 logicalSourceLoc.init(1);
72 logicalSourceLoc.name = loc[0].name;
75 virtual ~TInputScanner()
80 // retrieve the next character and advance one character
84 if (ret == EndOfInput)
86 ++loc[currentSource].column;
87 ++logicalSourceLoc.column;
89 ++loc[currentSource].line;
90 ++logicalSourceLoc.line;
91 logicalSourceLoc.column = 0;
92 loc[currentSource].column = 0;
99 // retrieve the next character, no advance
102 if (currentSource >= numSources) {
103 endOfFileReached = true;
106 // Make sure we do not read off the end of a string.
107 // N.B. Sources can have a length of 0.
108 int sourceToRead = currentSource;
109 size_t charToRead = currentChar;
110 while(charToRead >= lengths[sourceToRead]) {
113 if (sourceToRead >= numSources) {
118 // Here, we care about making negative valued characters positive
119 return sources[sourceToRead][charToRead];
122 // go back one character
125 // Do not roll back once we've reached the end of the file.
126 if (endOfFileReached)
129 if (currentChar > 0) {
131 --loc[currentSource].column;
132 --logicalSourceLoc.column;
133 if (loc[currentSource].column < 0) {
134 // We've moved back past a new line. Find the
135 // previous newline (or start of the file) to compute
136 // the column count on the now current line.
137 size_t chIndex = currentChar;
138 while (chIndex > 0) {
139 if (sources[currentSource][chIndex] == '\n') {
144 logicalSourceLoc.column = (int)(currentChar - chIndex);
145 loc[currentSource].column = (int)(currentChar - chIndex);
150 } while (currentSource > 0 && lengths[currentSource] == 0);
151 if (lengths[currentSource] == 0) {
152 // set to 0 if we've backed up to the start of an empty string
155 currentChar = lengths[currentSource] - 1;
157 if (peek() == '\n') {
158 --loc[currentSource].line;
159 --logicalSourceLoc.line;
163 // for #line override
164 void setLine(int newLine)
166 logicalSourceLoc.line = newLine;
167 loc[getLastValidSourceIndex()].line = newLine;
170 // for #line override in filename based parsing
171 void setFile(const char* filename)
173 TString* fn_tstr = NewPoolTString(filename);
174 logicalSourceLoc.name = fn_tstr;
175 loc[getLastValidSourceIndex()].name = fn_tstr;
178 void setFile(const char* filename, int i)
180 TString* fn_tstr = NewPoolTString(filename);
181 if (i == getLastValidSourceIndex()) {
182 logicalSourceLoc.name = fn_tstr;
184 loc[i].name = fn_tstr;
187 void setString(int newString)
189 logicalSourceLoc.string = newString;
190 loc[getLastValidSourceIndex()].string = newString;
191 logicalSourceLoc.name = nullptr;
192 loc[getLastValidSourceIndex()].name = nullptr;
195 // for #include content indentation
196 void setColumn(int col)
198 logicalSourceLoc.column = col;
199 loc[getLastValidSourceIndex()].column = col;
204 endOfFileReached = true;
205 currentSource = numSources;
208 bool atEndOfInput() const { return endOfFileReached; }
210 const TSourceLoc& getSourceLoc() const
213 return logicalSourceLoc;
215 return loc[std::max(0, std::min(currentSource, numSources - finale - 1))];
218 // Returns the index (starting from 0) of the most recent valid source string we are reading from.
219 int getLastValidSourceIndex() const { return std::min(currentSource, numSources - 1); }
221 void consumeWhiteSpace(bool& foundNonSpaceTab);
222 bool consumeComment();
223 void consumeWhitespaceComment(bool& foundNonSpaceTab);
224 bool scanVersion(int& version, EProfile& profile, bool& notFirstToken);
228 // advance one character
232 if (currentChar >= lengths[currentSource]) {
234 if (currentSource < numSources) {
235 loc[currentSource].string = loc[currentSource - 1].string + 1;
236 loc[currentSource].line = 1;
237 loc[currentSource].column = 0;
239 while (currentSource < numSources && lengths[currentSource] == 0) {
241 if (currentSource < numSources) {
242 loc[currentSource].string = loc[currentSource - 1].string + 1;
243 loc[currentSource].line = 1;
244 loc[currentSource].column = 0;
251 int numSources; // number of strings in source
252 const unsigned char* const *sources; // array of strings; must be converted to positive values on use, to avoid aliasing with -1 as EndOfInput
253 const size_t *lengths; // length of each string
257 // This is for reporting what string/line an error occurred on, and can be overridden by #line.
258 // It remembers the last state of each source string as it is left for the next one, so unget()
259 // can restore that state.
260 TSourceLoc* loc; // an array
262 int stringBias; // the first string that is the user's string number 0
263 int finale; // number of internal strings after user's last string
265 TSourceLoc logicalSourceLoc;
266 bool singleLogical; // treats the strings as a single logical string.
267 // locations will be reported from the first string.
269 // Set to true once peek() returns EndOfFile, so that we won't roll back
270 // once we've reached EndOfFile.
271 bool endOfFileReached;
274 } // end namespace glslang
276 #endif // _GLSLANG_SCAN_INCLUDED_