blob: ce0ce94f49a8a133184297f7d2b4de78dccb7624 [file] [log] [blame]
alokp@chromium.org4e4b8072011-08-07 05:36:04 +00001//
2// Copyright (c) 2011 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Corentin Wallez054f7ed2016-09-20 17:15:59 -04007#include "compiler/preprocessor/Token.h"
alokp@chromium.org4e4b8072011-08-07 05:36:04 +00008
Corentin Wallez054f7ed2016-09-20 17:15:59 -04009#include "common/debug.h"
10#include "compiler/preprocessor/numeric_lex.h"
alokp@chromium.org2e818912012-06-29 21:26:03 +000011
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000012namespace pp
13{
14
alokp@chromium.org19d7aa62012-05-31 17:34:05 +000015void Token::reset()
16{
Jamie Madillf832c9d2016-12-12 17:38:48 -050017 type = 0;
18 flags = 0;
alokp@chromium.org19d7aa62012-05-31 17:34:05 +000019 location = SourceLocation();
alokp@chromium.org5b6a68e2012-06-28 20:29:13 +000020 text.clear();
alokp@chromium.org19d7aa62012-05-31 17:34:05 +000021}
22
Zhenyao Mod526f982014-05-13 14:51:19 -070023bool Token::equals(const Token &other) const
alokp@chromium.org19d7aa62012-05-31 17:34:05 +000024{
Jamie Madillf832c9d2016-12-12 17:38:48 -050025 return (type == other.type) && (flags == other.flags) && (location == other.location) &&
alokp@chromium.org5b6a68e2012-06-28 20:29:13 +000026 (text == other.text);
alokp@chromium.org19d7aa62012-05-31 17:34:05 +000027}
28
29void Token::setAtStartOfLine(bool start)
30{
31 if (start)
32 flags |= AT_START_OF_LINE;
33 else
34 flags &= ~AT_START_OF_LINE;
35}
36
37void Token::setHasLeadingSpace(bool space)
38{
39 if (space)
40 flags |= HAS_LEADING_SPACE;
41 else
42 flags &= ~HAS_LEADING_SPACE;
43}
44
alokp@chromium.org7fc38dd2012-06-14 18:23:23 +000045void Token::setExpansionDisabled(bool disable)
46{
47 if (disable)
48 flags |= EXPANSION_DISABLED;
49 else
50 flags &= ~EXPANSION_DISABLED;
51}
52
Zhenyao Mod526f982014-05-13 14:51:19 -070053bool Token::iValue(int *value) const
alokp@chromium.org2e818912012-06-29 21:26:03 +000054{
Corentin Wallez054f7ed2016-09-20 17:15:59 -040055 ASSERT(type == CONST_INT);
alokp@chromium.orgfc0543f2012-07-11 20:31:02 +000056 return numeric_lex_int(text, value);
alokp@chromium.org2e818912012-06-29 21:26:03 +000057}
58
Zhenyao Mod526f982014-05-13 14:51:19 -070059bool Token::uValue(unsigned int *value) const
alokp@chromium.org2e818912012-06-29 21:26:03 +000060{
Corentin Wallez054f7ed2016-09-20 17:15:59 -040061 ASSERT(type == CONST_INT);
alokp@chromium.orgfc0543f2012-07-11 20:31:02 +000062 return numeric_lex_int(text, value);
alokp@chromium.org2e818912012-06-29 21:26:03 +000063}
64
Zhenyao Mod526f982014-05-13 14:51:19 -070065bool Token::fValue(float *value) const
alokp@chromium.org2e818912012-06-29 21:26:03 +000066{
Corentin Wallez054f7ed2016-09-20 17:15:59 -040067 ASSERT(type == CONST_FLOAT);
alokp@chromium.orgfc0543f2012-07-11 20:31:02 +000068 return numeric_lex_float(text, value);
alokp@chromium.org2e818912012-06-29 21:26:03 +000069}
70
Zhenyao Mod526f982014-05-13 14:51:19 -070071std::ostream &operator<<(std::ostream &out, const Token &token)
alokp@chromium.orgb81c4012011-08-21 06:53:11 +000072{
alokp@chromium.org40da4c52012-04-12 05:23:19 +000073 if (token.hasLeadingSpace())
74 out << " ";
75
alokp@chromium.org5b6a68e2012-06-28 20:29:13 +000076 out << token.text;
alokp@chromium.orgb81c4012011-08-21 06:53:11 +000077 return out;
78}
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000079
alokp@chromium.org4b2a5222012-04-03 17:19:42 +000080} // namespace pp