RetroArch
Scan.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3 // Copyright (C) 2013 LunarG, Inc.
4 //
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 // Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //
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.
18 //
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.
22 //
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.
35 //
36 #ifndef _GLSLANG_SCAN_INCLUDED_
37 #define _GLSLANG_SCAN_INCLUDED_
38 
39 #include "Versions.h"
40 
41 namespace glslang {
42 
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;
47 
48 //
49 // A character scanner that seamlessly, on read-only strings, reads across an
50 // array of strings without assuming null termination.
51 //
53 public:
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) :
56  numSources(n),
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)),
61  {
62  loc = new TSourceLoc[numSources];
63  for (int i = 0; i < numSources; ++i) {
64  loc[i].init(i - stringBias);
65  }
66  if (names != nullptr) {
67  for (int i = 0; i < numSources; ++i)
68  loc[i].name = names[i];
69  }
70  loc[currentSource].line = 1;
73  }
74 
75  virtual ~TInputScanner()
76  {
77  delete [] loc;
78  }
79 
80  // retrieve the next character and advance one character
81  int get()
82  {
83  int ret = peek();
84  if (ret == EndOfInput)
85  return ret;
88  if (ret == '\n') {
93  }
94  advance();
95 
96  return ret;
97  }
98 
99  // retrieve the next character, no advance
100  int peek()
101  {
102  if (currentSource >= numSources) {
103  endOfFileReached = true;
104  return EndOfInput;
105  }
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]) {
111  charToRead = 0;
112  sourceToRead += 1;
113  if (sourceToRead >= numSources) {
114  return EndOfInput;
115  }
116  }
117 
118  // Here, we care about making negative valued characters positive
119  return sources[sourceToRead][charToRead];
120  }
121 
122  // go back one character
123  void unget()
124  {
125  // Do not roll back once we've reached the end of the file.
126  if (endOfFileReached)
127  return;
128 
129  if (currentChar > 0) {
130  --currentChar;
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') {
140  break;
141  }
142  --chIndex;
143  }
144  logicalSourceLoc.column = (int)(currentChar - chIndex);
145  loc[currentSource].column = (int)(currentChar - chIndex);
146  }
147  } else {
148  do {
149  --currentSource;
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
153  currentChar = 0;
154  } else
156  }
157  if (peek() == '\n') {
160  }
161  }
162 
163  // for #line override
164  void setLine(int newLine)
165  {
166  logicalSourceLoc.line = newLine;
167  loc[getLastValidSourceIndex()].line = newLine;
168  }
169 
170  // for #line override in filename based parsing
171  void setFile(const char* filename)
172  {
173  logicalSourceLoc.name = filename;
174  loc[getLastValidSourceIndex()].name = filename;
175  }
176 
177  void setFile(const char* filename, int i)
178  {
179  if (i == getLastValidSourceIndex()) {
180  logicalSourceLoc.name = filename;
181  }
182  loc[i].name = filename;
183  }
184 
185  void setString(int newString)
186  {
187  logicalSourceLoc.string = newString;
188  loc[getLastValidSourceIndex()].string = newString;
189  logicalSourceLoc.name = nullptr;
190  loc[getLastValidSourceIndex()].name = nullptr;
191  }
192 
193  // for #include content indentation
194  void setColumn(int col)
195  {
198  }
199 
201  {
202  endOfFileReached = true;
204  }
205 
206  bool atEndOfInput() const { return endOfFileReached; }
207 
208  const TSourceLoc& getSourceLoc() const
209  {
210  if (singleLogical) {
211  return logicalSourceLoc;
212  } else {
213  return loc[std::max(0, std::min(currentSource, numSources - finale - 1))];
214  }
215  }
216  // Returns the index (starting from 0) of the most recent valid source string we are reading from.
217  int getLastValidSourceIndex() const { return std::min(currentSource, numSources - 1); }
218 
219  void consumeWhiteSpace(bool& foundNonSpaceTab);
220  bool consumeComment();
221  void consumeWhitespaceComment(bool& foundNonSpaceTab);
222  bool scanVersion(int& version, EProfile& profile, bool& notFirstToken);
223 
224 protected:
225 
226  // advance one character
227  void advance()
228  {
229  ++currentChar;
231  ++currentSource;
232  if (currentSource < numSources) {
234  loc[currentSource].line = 1;
235  loc[currentSource].column = 0;
236  }
237  while (currentSource < numSources && lengths[currentSource] == 0) {
238  ++currentSource;
239  if (currentSource < numSources) {
241  loc[currentSource].line = 1;
242  loc[currentSource].column = 0;
243  }
244  }
245  currentChar = 0;
246  }
247  }
248 
249  int numSources; // number of strings in source
250  const unsigned char* const *sources; // array of strings; must be converted to positive values on use, to avoid aliasing with -1 as EndOfInput
251  const size_t *lengths; // length of each string
253  size_t currentChar;
254 
255  // This is for reporting what string/line an error occurred on, and can be overridden by #line.
256  // It remembers the last state of each source string as it is left for the next one, so unget()
257  // can restore that state.
258  TSourceLoc* loc; // an array
259 
260  int stringBias; // the first string that is the user's string number 0
261  int finale; // number of internal strings after user's last string
262 
264  bool singleLogical; // treats the strings as a single logical string.
265  // locations will be reported from the first string.
266 
267  // Set to true once peek() returns EndOfFile, so that we won't roll back
268  // once we've reached EndOfFile.
270 };
271 
272 } // end namespace glslang
273 
274 #endif // _GLSLANG_SCAN_INCLUDED_
int stringBias
Definition: Scan.h:260
GLuint const GLchar * name
Definition: glext.h:6671
#define const
Definition: zconf.h:217
int getLastValidSourceIndex() const
Definition: Scan.h:217
void consumeWhitespaceComment(bool &foundNonSpaceTab)
Definition: Scan.cpp:144
Definition: Common.h:231
int get()
Definition: Scan.h:81
int line
Definition: Common.h:243
void advance()
Definition: Scan.h:227
void setFile(const char *filename, int i)
Definition: Scan.h:177
bool atEndOfInput() const
Definition: Scan.h:206
GLfloat f
Definition: glext.h:8207
const unsigned char *const * sources
Definition: Scan.h:250
GLdouble s
Definition: glext.h:6390
int numSources
Definition: Scan.h:249
GLsizei GLenum GLenum GLuint GLenum GLsizei * lengths
Definition: glext.h:8420
bool singleLogical
Definition: Scan.h:264
GLboolean GLboolean GLboolean b
Definition: glext.h:6844
TSourceLoc logicalSourceLoc
Definition: Scan.h:263
const size_t * lengths
Definition: Scan.h:251
virtual ~TInputScanner()
Definition: Scan.h:75
void setString(int newString)
Definition: Scan.h:185
bool endOfFileReached
Definition: Scan.h:269
void setColumn(int col)
Definition: Scan.h:194
EProfile
Definition: Versions.h:51
size_t currentChar
Definition: Scan.h:253
void consumeWhiteSpace(bool &foundNonSpaceTab)
Definition: Scan.cpp:65
version
Definition: setup.py:6
int currentSource
Definition: Scan.h:252
bool scanVersion(int &version, EProfile &profile, bool &notFirstToken)
Definition: Scan.cpp:173
const char * name
Definition: Common.h:241
void unget()
Definition: Scan.h:123
int peek()
Definition: Scan.h:100
void setEndOfInput()
Definition: Scan.h:200
GLenum GLenum GLvoid GLvoid * column
Definition: glext.h:6316
const int EndOfInput
Definition: Scan.h:46
Definition: arrays.h:46
void setLine(int newLine)
Definition: Scan.h:164
int finale
Definition: Scan.h:261
const TSourceLoc & getSourceLoc() const
Definition: Scan.h:208
u32 col
Definition: gx_regdef.h:5093
GLuint GLuint * names
Definition: glext.h:12452
GLsizei GLenum * sources
Definition: glext.h:8420
Ιστορικό Εικόνα Πληροφορίες Όλοι Οι Χρήστες Χειρίζονται Το Μενού Αριστερό Αναλογικό Αριστερό Αναλογικό Αριστερό Αναλογικό Y Αριστερό Αναλογικό Δεξί Αναλογικό X Δεξί Αναλογικό Δεξί Αναλογικό Y Δεξί Αναλογικό Σκανδάλη Όπλου Όπλο Aux A Όπλο Aux C Όπλο Select Όπλο D pad Κάτω Όπλο D pad Δεξιά Νεκρή Ζώνη Αναλογικού Σύνδεση Όλων Λήξη Χρόνου Σύνδεσης Hide Unbound Core Input Descriptors Κατάλογος Συσκευών Κατάλογος Ποντικιού Duty Cycle Keyboard Gamepad Mapping Enable Κουμπί D pad κάτω Κουμπί Κουμπί L(πίσω)" ) MSG_HASH( MENU_ENUM_LABEL_VALUE_INPUT_JOYPAD_LEFT
bool consumeComment()
Definition: Scan.cpp:77
TSourceLoc * loc
Definition: Scan.h:258
int column
Definition: Common.h:244
#define false
Definition: ordinals.h:83
Definition: Scan.h:52
GLdouble n
Definition: glext.h:8396
TInputScanner(int n, const char *const s[], size_t L[], const char *const *names=nullptr, int b=0, int f=0, bool single=false)
Definition: Scan.h:54
void init()
Definition: Common.h:232
void setFile(const char *filename)
Definition: Scan.h:171
int string
Definition: Common.h:242