1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
---|---|
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | #include "Token.h" |
16 | |
17 | #include <cassert> |
18 | |
19 | #include "numeric_lex.h" |
20 | |
21 | namespace pp |
22 | { |
23 | |
24 | void Token::reset() |
25 | { |
26 | type = 0; |
27 | flags = 0; |
28 | location = SourceLocation(); |
29 | text.clear(); |
30 | } |
31 | |
32 | bool Token::equals(const Token &other) const |
33 | { |
34 | return (type == other.type) && |
35 | (flags == other.flags) && |
36 | (location == other.location) && |
37 | (text == other.text); |
38 | } |
39 | |
40 | void Token::setAtStartOfLine(bool start) |
41 | { |
42 | if (start) |
43 | flags |= AT_START_OF_LINE; |
44 | else |
45 | flags &= ~AT_START_OF_LINE; |
46 | } |
47 | |
48 | void Token::setHasLeadingSpace(bool space) |
49 | { |
50 | if (space) |
51 | flags |= HAS_LEADING_SPACE; |
52 | else |
53 | flags &= ~HAS_LEADING_SPACE; |
54 | } |
55 | |
56 | void Token::setExpansionDisabled(bool disable) |
57 | { |
58 | if (disable) |
59 | flags |= EXPANSION_DISABLED; |
60 | else |
61 | flags &= ~EXPANSION_DISABLED; |
62 | } |
63 | |
64 | bool Token::iValue(int *value) const |
65 | { |
66 | assert(type == CONST_INT); |
67 | return numeric_lex_int(text, value); |
68 | } |
69 | |
70 | bool Token::uValue(unsigned int *value) const |
71 | { |
72 | assert(type == CONST_INT); |
73 | return numeric_lex_int(text, value); |
74 | } |
75 | |
76 | bool Token::fValue(float *value) const |
77 | { |
78 | assert(type == CONST_FLOAT); |
79 | return numeric_lex_float(text, value); |
80 | } |
81 | |
82 | std::ostream &operator<<(std::ostream &out, const Token &token) |
83 | { |
84 | if (token.hasLeadingSpace()) |
85 | out << " "; |
86 | |
87 | out << token.text; |
88 | return out; |
89 | } |
90 | |
91 | } // namespace pp |
92 |