blob: 347c47e307202b0c1b0e0a7831ec693bea768211 [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
alokp@chromium.org2c958ee2012-05-17 20:35:42 +000013#include "SourceLocation.h"
14
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 {
alokp@chromium.org2c958ee2012-05-17 20:35:42 +000022 LAST = 0, // EOF.
alokp@chromium.org78a35192012-04-19 17:16:26 +000023
alokp@chromium.org4b2a5222012-04-03 17:19:42 +000024 IDENTIFIER = 258,
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000025
alokp@chromium.org4b2a5222012-04-03 17:19:42 +000026 CONST_INT,
27 CONST_FLOAT,
alokp@chromium.orgb81c4012011-08-21 06:53:11 +000028
alokp@chromium.org3f990c42012-04-03 19:50:35 +000029 OP_INC,
30 OP_DEC,
31 OP_LEFT,
32 OP_RIGHT,
33 OP_LE,
34 OP_GE,
35 OP_EQ,
36 OP_NE,
37 OP_AND,
38 OP_XOR,
39 OP_OR,
40 OP_ADD_ASSIGN,
41 OP_SUB_ASSIGN,
42 OP_MUL_ASSIGN,
43 OP_DIV_ASSIGN,
44 OP_MOD_ASSIGN,
45 OP_LEFT_ASSIGN,
46 OP_RIGHT_ASSIGN,
47 OP_AND_ASSIGN,
48 OP_XOR_ASSIGN,
alokp@chromium.org432d6fc2012-06-27 22:13:21 +000049 OP_OR_ASSIGN,
50
51 // Preprocessing token types.
52 // These types are used by the preprocessor internally.
53 // Preprocessor clients must not depend or check for them.
54 PP_HASH,
55 PP_NUMBER,
56 PP_OTHER
alokp@chromium.org4b2a5222012-04-03 17:19:42 +000057 };
alokp@chromium.org40da4c52012-04-12 05:23:19 +000058 enum Flags
59 {
alokp@chromium.org7fc38dd2012-06-14 18:23:23 +000060 AT_START_OF_LINE = 1 << 0,
61 HAS_LEADING_SPACE = 1 << 1,
62 EXPANSION_DISABLED = 1 << 2
alokp@chromium.org40da4c52012-04-12 05:23:19 +000063 };
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000064
Zhenyao Mod526f982014-05-13 14:51:19 -070065 Token()
66 : type(0),
67 flags(0)
68 {
69 }
alokp@chromium.org40da4c52012-04-12 05:23:19 +000070
alokp@chromium.org19d7aa62012-05-31 17:34:05 +000071 void reset();
Zhenyao Mod526f982014-05-13 14:51:19 -070072 bool equals(const Token &other) const;
alokp@chromium.org04d7d222012-05-16 19:24:07 +000073
alokp@chromium.org19d7aa62012-05-31 17:34:05 +000074 // Returns true if this is the first token on line.
75 // It disregards any leading whitespace.
Zhenyao Mod526f982014-05-13 14:51:19 -070076 bool atStartOfLine() const
77 {
78 return (flags & AT_START_OF_LINE) != 0;
79 }
alokp@chromium.org19d7aa62012-05-31 17:34:05 +000080 void setAtStartOfLine(bool start);
alokp@chromium.org40da4c52012-04-12 05:23:19 +000081
Zhenyao Mod526f982014-05-13 14:51:19 -070082 bool hasLeadingSpace() const
83 {
84 return (flags & HAS_LEADING_SPACE) != 0;
85 }
alokp@chromium.org19d7aa62012-05-31 17:34:05 +000086 void setHasLeadingSpace(bool space);
alokp@chromium.org40da4c52012-04-12 05:23:19 +000087
Zhenyao Mod526f982014-05-13 14:51:19 -070088 bool expansionDisabled() const
89 {
90 return (flags & EXPANSION_DISABLED) != 0;
91 }
alokp@chromium.org7fc38dd2012-06-14 18:23:23 +000092 void setExpansionDisabled(bool disable);
93
alokp@chromium.org2e818912012-06-29 21:26:03 +000094 // Converts text into numeric value for CONST_INT and CONST_FLOAT token.
95 // Returns false if the parsed value cannot fit into an int or float.
Zhenyao Mod526f982014-05-13 14:51:19 -070096 bool iValue(int *value) const;
97 bool uValue(unsigned int *value) const;
98 bool fValue(float *value) const;
alokp@chromium.org2e818912012-06-29 21:26:03 +000099
alokp@chromium.org4b2a5222012-04-03 17:19:42 +0000100 int type;
alokp@chromium.org7fc38dd2012-06-14 18:23:23 +0000101 unsigned int flags;
alokp@chromium.org2c958ee2012-05-17 20:35:42 +0000102 SourceLocation location;
alokp@chromium.org5b6a68e2012-06-28 20:29:13 +0000103 std::string text;
alokp@chromium.org4e4b8072011-08-07 05:36:04 +0000104};
105
Zhenyao Mod526f982014-05-13 14:51:19 -0700106inline bool operator==(const Token &lhs, const Token &rhs)
alokp@chromium.org98d04ec2012-05-21 22:47:20 +0000107{
108 return lhs.equals(rhs);
109}
110
Zhenyao Mod526f982014-05-13 14:51:19 -0700111inline bool operator!=(const Token &lhs, const Token &rhs)
alokp@chromium.org98d04ec2012-05-21 22:47:20 +0000112{
113 return !lhs.equals(rhs);
114}
115
Zhenyao Mod526f982014-05-13 14:51:19 -0700116extern std::ostream &operator<<(std::ostream &out, const Token &token);
alokp@chromium.orgb81c4012011-08-21 06:53:11 +0000117
alokp@chromium.org4e4b8072011-08-07 05:36:04 +0000118} // namepsace pp
Geoff Lang0a73dd82014-11-19 16:18:08 -0500119
alokp@chromium.org4e4b8072011-08-07 05:36:04 +0000120#endif // COMPILER_PREPROCESSOR_TOKEN_H_