blob: c915e0b593b549d983486c63c6e0da7e0cee8f19 [file] [log] [blame]
John Kessenich9c86c6a2016-05-03 22:49:24 -06001//
2//Copyright (C) 2016 Google, Inc.
3//
4//All rights reserved.
5//
6//Redistribution and use in source and binary forms, with or without
7//modification, are permitted provided that the following conditions
8//are met:
9//
10// Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12//
13// Redistributions in binary form must reproduce the above
14// copyright notice, this list of conditions and the following
15// disclaimer in the documentation and/or other materials provided
16// with the distribution.
17//
18// Neither the name of Google, Inc., nor the names of its
19// contributors may be used to endorse or promote products derived
20// from this software without specific prior written permission.
21//
22//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25//FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26//COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27//INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30//CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31//LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32//ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33//POSSIBILITY OF SUCH DAMAGE.
34//
35
36#include "hlslTokenStream.h"
37
38namespace glslang {
39
John Kessenich1cc1a282016-06-03 16:55:49 -060040void HlslTokenStream::pushPreToken(const HlslToken& tok)
41{
steve-lunarg5964c642016-07-30 07:38:55 -060042 assert(preTokenStackSize < tokenBufferSize);
43 preTokenStack[preTokenStackSize++] = tok;
John Kessenich1cc1a282016-06-03 16:55:49 -060044}
45
46HlslToken HlslTokenStream::popPreToken()
47{
steve-lunarg5964c642016-07-30 07:38:55 -060048 assert(preTokenStackSize > 0);
John Kessenich1cc1a282016-06-03 16:55:49 -060049
steve-lunarg5964c642016-07-30 07:38:55 -060050 return preTokenStack[--preTokenStackSize];
John Kessenich1cc1a282016-06-03 16:55:49 -060051}
52
53void HlslTokenStream::pushTokenBuffer(const HlslToken& tok)
54{
steve-lunarg5964c642016-07-30 07:38:55 -060055 tokenBuffer[tokenBufferPos] = tok;
56 tokenBufferPos = (tokenBufferPos+1) % tokenBufferSize;
John Kessenich1cc1a282016-06-03 16:55:49 -060057}
58
59HlslToken HlslTokenStream::popTokenBuffer()
60{
steve-lunarg5964c642016-07-30 07:38:55 -060061 // Back up
62 tokenBufferPos = (tokenBufferPos+tokenBufferSize-1) % tokenBufferSize;
63
64 return tokenBuffer[tokenBufferPos];
John Kessenich1cc1a282016-06-03 16:55:49 -060065}
66
John Kessenich9c86c6a2016-05-03 22:49:24 -060067// Load 'token' with the next token in the stream of tokens.
68void HlslTokenStream::advanceToken()
69{
John Kessenich1cc1a282016-06-03 16:55:49 -060070 pushTokenBuffer(token);
71 if (preTokenStackSize > 0)
72 token = popPreToken();
73 else
74 scanner.tokenize(token);
75}
76
77void HlslTokenStream::recedeToken()
78{
79 pushPreToken(token);
80 token = popTokenBuffer();
John Kessenich9c86c6a2016-05-03 22:49:24 -060081}
82
83// Return the current token class.
84EHlslTokenClass HlslTokenStream::peek() const
85{
86 return token.tokenClass;
87}
88
89// Return true, without advancing to the next token, if the current token is
90// the expected (passed in) token class.
91bool HlslTokenStream::peekTokenClass(EHlslTokenClass tokenClass) const
92{
93 return peek() == tokenClass;
94}
95
96// Return true and advance to the next token if the current token is the
97// expected (passed in) token class.
98bool HlslTokenStream::acceptTokenClass(EHlslTokenClass tokenClass)
99{
100 if (peekTokenClass(tokenClass)) {
101 advanceToken();
102 return true;
103 }
104
105 return false;
106}
107
108} // end namespace glslang