blob: 26732ab64d22dee5f0d121afbeecb8a39b6ce904 [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
7#ifndef COMPILER_PREPROCESSOR_TOKEN_H_
8#define COMPILER_PREPROCESSOR_TOKEN_H_
9
zmo@google.com5e75f342012-04-12 23:47:10 +000010#include <ostream>
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000011#include <string>
alokp@chromium.org3a01d1b2011-08-30 05:10:53 +000012
Corentin Wallez054f7ed2016-09-20 17:15:59 -040013#include "compiler/preprocessor/SourceLocation.h"
alokp@chromium.org2c958ee2012-05-17 20:35:42 +000014
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000015namespace pp
16{
17
alokp@chromium.org4b2a5222012-04-03 17:19:42 +000018struct Token
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000019{
alokp@chromium.org4b2a5222012-04-03 17:19:42 +000020 enum Type
21 {
Corentin Wallezdc0fa462017-02-01 14:44:43 -050022 // Calling this ERROR causes a conflict with wingdi.h
23 GOT_ERROR = -1,
24 LAST = 0, // EOF.
alokp@chromium.org78a35192012-04-19 17:16:26 +000025
alokp@chromium.org4b2a5222012-04-03 17:19:42 +000026 IDENTIFIER = 258,
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000027
alokp@chromium.org4b2a5222012-04-03 17:19:42 +000028 CONST_INT,
29 CONST_FLOAT,
alokp@chromium.orgb81c4012011-08-21 06:53:11 +000030
alokp@chromium.org3f990c42012-04-03 19:50:35 +000031 OP_INC,
32 OP_DEC,
33 OP_LEFT,
34 OP_RIGHT,
35 OP_LE,
36 OP_GE,
37 OP_EQ,
38 OP_NE,
39 OP_AND,
40 OP_XOR,
41 OP_OR,
42 OP_ADD_ASSIGN,
43 OP_SUB_ASSIGN,
44 OP_MUL_ASSIGN,
45 OP_DIV_ASSIGN,
46 OP_MOD_ASSIGN,
47 OP_LEFT_ASSIGN,
48 OP_RIGHT_ASSIGN,
49 OP_AND_ASSIGN,
50 OP_XOR_ASSIGN,
alokp@chromium.org432d6fc2012-06-27 22:13:21 +000051 OP_OR_ASSIGN,
52
53 // Preprocessing token types.
54 // These types are used by the preprocessor internally.
55 // Preprocessor clients must not depend or check for them.
56 PP_HASH,
57 PP_NUMBER,
58 PP_OTHER
alokp@chromium.org4b2a5222012-04-03 17:19:42 +000059 };
alokp@chromium.org40da4c52012-04-12 05:23:19 +000060 enum Flags
61 {
alokp@chromium.org7fc38dd2012-06-14 18:23:23 +000062 AT_START_OF_LINE = 1 << 0,
63 HAS_LEADING_SPACE = 1 << 1,
64 EXPANSION_DISABLED = 1 << 2
alokp@chromium.org40da4c52012-04-12 05:23:19 +000065 };
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000066
Jamie Madillf832c9d2016-12-12 17:38:48 -050067 Token() : type(0), flags(0) {}
alokp@chromium.org40da4c52012-04-12 05:23:19 +000068
alokp@chromium.org19d7aa62012-05-31 17:34:05 +000069 void reset();
Zhenyao Mod526f982014-05-13 14:51:19 -070070 bool equals(const Token &other) const;
alokp@chromium.org04d7d222012-05-16 19:24:07 +000071
alokp@chromium.org19d7aa62012-05-31 17:34:05 +000072 // Returns true if this is the first token on line.
73 // It disregards any leading whitespace.
Jamie Madillf832c9d2016-12-12 17:38:48 -050074 bool atStartOfLine() const { return (flags & AT_START_OF_LINE) != 0; }
alokp@chromium.org19d7aa62012-05-31 17:34:05 +000075 void setAtStartOfLine(bool start);
alokp@chromium.org40da4c52012-04-12 05:23:19 +000076
Jamie Madillf832c9d2016-12-12 17:38:48 -050077 bool hasLeadingSpace() const { return (flags & HAS_LEADING_SPACE) != 0; }
alokp@chromium.org19d7aa62012-05-31 17:34:05 +000078 void setHasLeadingSpace(bool space);
alokp@chromium.org40da4c52012-04-12 05:23:19 +000079
Jamie Madillf832c9d2016-12-12 17:38:48 -050080 bool expansionDisabled() const { return (flags & EXPANSION_DISABLED) != 0; }
alokp@chromium.org7fc38dd2012-06-14 18:23:23 +000081 void setExpansionDisabled(bool disable);
82
alokp@chromium.org2e818912012-06-29 21:26:03 +000083 // Converts text into numeric value for CONST_INT and CONST_FLOAT token.
84 // Returns false if the parsed value cannot fit into an int or float.
Zhenyao Mod526f982014-05-13 14:51:19 -070085 bool iValue(int *value) const;
86 bool uValue(unsigned int *value) const;
87 bool fValue(float *value) const;
alokp@chromium.org2e818912012-06-29 21:26:03 +000088
alokp@chromium.org4b2a5222012-04-03 17:19:42 +000089 int type;
alokp@chromium.org7fc38dd2012-06-14 18:23:23 +000090 unsigned int flags;
alokp@chromium.org2c958ee2012-05-17 20:35:42 +000091 SourceLocation location;
alokp@chromium.org5b6a68e2012-06-28 20:29:13 +000092 std::string text;
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000093};
94
Zhenyao Mod526f982014-05-13 14:51:19 -070095inline bool operator==(const Token &lhs, const Token &rhs)
alokp@chromium.org98d04ec2012-05-21 22:47:20 +000096{
97 return lhs.equals(rhs);
98}
99
Zhenyao Mod526f982014-05-13 14:51:19 -0700100inline bool operator!=(const Token &lhs, const Token &rhs)
alokp@chromium.org98d04ec2012-05-21 22:47:20 +0000101{
102 return !lhs.equals(rhs);
103}
104
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400105std::ostream &operator<<(std::ostream &out, const Token &token);
alokp@chromium.orgb81c4012011-08-21 06:53:11 +0000106
alokp@chromium.org4e4b8072011-08-07 05:36:04 +0000107} // namepsace pp
Geoff Lang0a73dd82014-11-19 16:18:08 -0500108
alokp@chromium.org4e4b8072011-08-07 05:36:04 +0000109#endif // COMPILER_PREPROCESSOR_TOKEN_H_