1 //
2 // Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3 // Copyright (C) 2013 LunarG, Inc.
4 // Copyright (C) 2015-2018 Google, Inc.
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 // Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above
16 // copyright notice, this list of conditions and the following
17 // disclaimer in the documentation and/or other materials provided
18 // with the distribution.
19 //
20 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
21 // contributors may be used to endorse or promote products derived
22 // from this software without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 // POSSIBILITY OF SUCH DAMAGE.
36 //
37 /****************************************************************************\
38 Copyright (c) 2002, NVIDIA Corporation.
39
40 NVIDIA Corporation("NVIDIA") supplies this software to you in
41 consideration of your agreement to the following terms, and your use,
42 installation, modification or redistribution of this NVIDIA software
43 constitutes acceptance of these terms. If you do not agree with these
44 terms, please do not use, install, modify or redistribute this NVIDIA
45 software.
46
47 In consideration of your agreement to abide by the following terms, and
48 subject to these terms, NVIDIA grants you a personal, non-exclusive
49 license, under NVIDIA's copyrights in this original NVIDIA software (the
50 "NVIDIA Software"), to use, reproduce, modify and redistribute the
51 NVIDIA Software, with or without modifications, in source and/or binary
52 forms; provided that if you redistribute the NVIDIA Software, you must
53 retain the copyright notice of NVIDIA, this notice and the following
54 text and disclaimers in all such redistributions of the NVIDIA Software.
55 Neither the name, trademarks, service marks nor logos of NVIDIA
56 Corporation may be used to endorse or promote products derived from the
57 NVIDIA Software without specific prior written permission from NVIDIA.
58 Except as expressly stated in this notice, no other rights or licenses
59 express or implied, are granted by NVIDIA herein, including but not
60 limited to any patent rights that may be infringed by your derivative
61 works or by other works in which the NVIDIA Software may be
62 incorporated. No hardware is licensed hereunder.
63
64 THE NVIDIA SOFTWARE IS BEING PROVIDED ON AN "AS IS" BASIS, WITHOUT
65 WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED,
66 INCLUDING WITHOUT LIMITATION, WARRANTIES OR CONDITIONS OF TITLE,
67 NON-INFRINGEMENT, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
68 ITS USE AND OPERATION EITHER ALONE OR IN COMBINATION WITH OTHER
69 PRODUCTS.
70
71 IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT,
72 INCIDENTAL, EXEMPLARY, CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
73 TO, LOST PROFITS; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
74 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) OR ARISING IN ANY WAY
75 OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE
76 NVIDIA SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT,
77 TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
78 NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
79 \****************************************************************************/
80
81 //
82 // For recording and playing back the stream of tokens in a macro definition.
83 //
84
85 #ifndef _CRT_SECURE_NO_WARNINGS
86 #define _CRT_SECURE_NO_WARNINGS
87 #endif
88
89 #include <cassert>
90 #include <cstdlib>
91 #include <cstring>
92 #include <cctype>
93
94 #include "PpContext.h"
95 #include "PpTokens.h"
96
97 namespace glslang {
98
99 // Add a token (including backing string) to the end of a macro
100 // token stream, for later playback.
putToken(int atom,TPpToken * ppToken)101 void TPpContext::TokenStream::putToken(int atom, TPpToken* ppToken)
102 {
103 TokenStream::Token streamToken(atom, *ppToken);
104 stream.push_back(streamToken);
105 }
106
107 // Read the next token from a macro token stream.
getToken(TParseContextBase & parseContext,TPpToken * ppToken)108 int TPpContext::TokenStream::getToken(TParseContextBase& parseContext, TPpToken *ppToken)
109 {
110 if (atEnd())
111 return EndOfInput;
112
113 int atom = stream[currentPos++].get(*ppToken);
114 ppToken->loc = parseContext.getCurrentLoc();
115
116 // Check for ##, unless the current # is the last character
117 if (atom == '#') {
118 if (peekToken('#')) {
119 parseContext.requireProfile(ppToken->loc, ~EEsProfile, "token pasting (##)");
120 parseContext.profileRequires(ppToken->loc, ~EEsProfile, 130, nullptr, "token pasting (##)");
121 currentPos++;
122 atom = PpAtomPaste;
123 }
124 }
125
126 return atom;
127 }
128
129 // We are pasting if
130 // 1. we are preceding a pasting operator within this stream
131 // or
132 // 2. the entire macro is preceding a pasting operator (lastTokenPastes)
133 // and we are also on the last token
peekTokenizedPasting(bool lastTokenPastes)134 bool TPpContext::TokenStream::peekTokenizedPasting(bool lastTokenPastes)
135 {
136 // 1. preceding ##?
137
138 size_t savePos = currentPos;
139 // skip white space
140 while (peekToken(' '))
141 ++currentPos;
142 if (peekToken(PpAtomPaste)) {
143 currentPos = savePos;
144 return true;
145 }
146
147 // 2. last token and we've been told after this there will be a ##
148
149 if (! lastTokenPastes)
150 return false;
151 // Getting here means the last token will be pasted, after this
152
153 // Are we at the last non-whitespace token?
154 savePos = currentPos;
155 bool moreTokens = false;
156 do {
157 if (atEnd())
158 break;
159 if (!peekToken(' ')) {
160 moreTokens = true;
161 break;
162 }
163 ++currentPos;
164 } while (true);
165 currentPos = savePos;
166
167 return !moreTokens;
168 }
169
170 // See if the next non-white-space tokens are two consecutive #
peekUntokenizedPasting()171 bool TPpContext::TokenStream::peekUntokenizedPasting()
172 {
173 // don't return early, have to restore this
174 size_t savePos = currentPos;
175
176 // skip white-space
177 while (peekToken(' '))
178 ++currentPos;
179
180 // check for ##
181 bool pasting = false;
182 if (peekToken('#')) {
183 ++currentPos;
184 if (peekToken('#'))
185 pasting = true;
186 }
187
188 currentPos = savePos;
189
190 return pasting;
191 }
192
pushTokenStreamInput(TokenStream & ts,bool prepasting,bool expanded)193 void TPpContext::pushTokenStreamInput(TokenStream& ts, bool prepasting, bool expanded)
194 {
195 pushInput(new tTokenInput(this, &ts, prepasting, expanded));
196 ts.reset();
197 }
198
scan(TPpToken * ppToken)199 int TPpContext::tUngotTokenInput::scan(TPpToken* ppToken)
200 {
201 if (done)
202 return EndOfInput;
203
204 int ret = token;
205 *ppToken = lval;
206 done = true;
207
208 return ret;
209 }
210
UngetToken(int token,TPpToken * ppToken)211 void TPpContext::UngetToken(int token, TPpToken* ppToken)
212 {
213 pushInput(new tUngotTokenInput(this, token, ppToken));
214 }
215
216 } // end namespace glslang
217