Coverage Report

Created: 2026-01-09 07:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/glslang/glslang/HLSL/hlslTokenStream.cpp
Line
Count
Source
1
//
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
38
namespace glslang {
39
40
void HlslTokenStream::pushPreToken(const HlslToken& tok)
41
79
{
42
79
    assert(preTokenStackSize < tokenBufferSize);
43
79
    preTokenStack[preTokenStackSize++] = tok;
44
79
}
45
46
HlslToken HlslTokenStream::popPreToken()
47
0
{
48
0
    assert(preTokenStackSize > 0);
49
50
0
    return preTokenStack[--preTokenStackSize];
51
0
}
52
53
void HlslTokenStream::pushTokenBuffer(const HlslToken& tok)
54
785k
{
55
785k
    tokenBuffer[tokenBufferPos] = tok;
56
785k
    tokenBufferPos = (tokenBufferPos+1) % tokenBufferSize;
57
785k
}
58
59
HlslToken HlslTokenStream::popTokenBuffer()
60
79
{
61
    // Back up
62
79
    tokenBufferPos = (tokenBufferPos+tokenBufferSize-1) % tokenBufferSize;
63
64
79
    return tokenBuffer[tokenBufferPos];
65
79
}
66
67
//
68
// Make a new source of tokens, not from the source, but from an
69
// already pre-processed token stream.
70
//
71
// This interrupts current token processing which must be restored
72
// later.  Some simplifying assumptions are made (and asserted).
73
//
74
void HlslTokenStream::pushTokenStream(const TVector<HlslToken>* tokens)
75
0
{
76
    // not yet setup to interrupt a stream that has been receded
77
    // and not yet reconsumed
78
0
    assert(preTokenStackSize == 0);
79
80
    // save current state
81
0
    currentTokenStack.push_back(token);
82
83
    // set up new token stream
84
0
    tokenStreamStack.push_back(tokens);
85
86
    // start position at first token:
87
0
    token = (*tokens)[0];
88
0
    tokenPosition.push_back(0);
89
0
}
90
91
// Undo pushTokenStream(), see above
92
void HlslTokenStream::popTokenStream()
93
0
{
94
0
    tokenStreamStack.pop_back();
95
0
    tokenPosition.pop_back();
96
0
    token = currentTokenStack.back();
97
0
    currentTokenStack.pop_back();
98
0
}
99
100
// Load 'token' with the next token in the stream of tokens.
101
void HlslTokenStream::advanceToken()
102
785k
{
103
785k
    pushTokenBuffer(token);
104
785k
    if (preTokenStackSize > 0)
105
0
        token = popPreToken();
106
785k
    else {
107
785k
        if (tokenStreamStack.size() == 0)
108
785k
            scanner.tokenize(token);
109
0
        else {
110
0
            ++tokenPosition.back();
111
0
            if (tokenPosition.back() >= (int)tokenStreamStack.back()->size())
112
0
                token.tokenClass = EHTokNone;
113
0
            else
114
0
                token = (*tokenStreamStack.back())[tokenPosition.back()];
115
0
        }
116
785k
    }
117
785k
}
118
119
void HlslTokenStream::recedeToken()
120
79
{
121
79
    pushPreToken(token);
122
79
    token = popTokenBuffer();
123
79
}
124
125
// Return the current token class.
126
EHlslTokenClass HlslTokenStream::peek() const
127
4.67M
{
128
4.67M
    return token.tokenClass;
129
4.67M
}
130
131
// Return true, without advancing to the next token, if the current token is
132
// the expected (passed in) token class.
133
bool HlslTokenStream::peekTokenClass(EHlslTokenClass tokenClass) const
134
3.22M
{
135
3.22M
    return peek() == tokenClass;
136
3.22M
}
137
138
// Return true and advance to the next token if the current token is the
139
// expected (passed in) token class.
140
bool HlslTokenStream::acceptTokenClass(EHlslTokenClass tokenClass)
141
1.63M
{
142
1.63M
    if (peekTokenClass(tokenClass)) {
143
392k
        advanceToken();
144
392k
        return true;
145
392k
    }
146
147
1.23M
    return false;
148
1.63M
}
149
150
} // end namespace glslang