-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtoken.h
198 lines (176 loc) · 2.96 KB
/
token.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#ifndef TOKEN_H
#define TOKEN_H
#include "location.h"
#include "utils.h"
#include <cstdint>
#include <string>
class Token
{
public:
enum TokenType
{
// Values of different types
Integer,
Real,
StringLiteral,
Char,
Boolean,
// Type/var/const declarations:
Type,
Var,
Packed,
Array,
Of,
Record,
Class,
Identifier,
Const,
File,
Set,
String,
Nil,
Bindable,
Value,
// Symbols and such
LeftParen,
RightParen,
LeftSquare,
RightSquare,
Plus,
Minus,
Multiply,
Divide,
Power,
Comma,
Semicolon,
Colon,
Assign,
Period,
DotDot,
Uparrow,
Div,
Mod,
Pow,
Xor,
Shr,
Shl,
SymDiff,
// Comparison symbols - these return bool in binary ops.
LessOrEqual,
FirstComparison = LessOrEqual,
LessThan,
GreaterOrEqual,
GreaterThan,
Equal,
NotEqual,
In,
LastComparison = In,
// Keywords
For,
To,
Downto,
Do,
If,
Then,
Else,
While,
Repeat,
Until,
Function,
Procedure,
Begin,
End,
Program,
Unit,
Module,
Write,
Writeln,
WriteStr,
Read,
Readln,
ReadStr,
Builtin,
Forward,
Inline,
Interface,
Implementation,
And,
And_Then,
Or,
Or_Else,
Not,
Case,
Otherwise,
With,
Virtual,
Override,
Static,
Private,
Public,
Protected,
Restricted,
Constructor,
Destructor,
Label,
Goto,
Uses,
At,
Default,
Import,
// Specials
LineNumber,
FileName,
SizeOf,
// Errors
Overflow,
UntermString,
EndOfFile = -1,
Unknown = -1000,
};
Token(TokenType t = Unknown, const Location& w = { "", 0, 0 });
Token(TokenType t, const Location& w, const std::string& str);
Token(TokenType t, const Location& w, uint64_t v);
Token(const Location& w, double v);
static TokenType KeyWordToToken(const std::string& str);
TokenType GetToken() const { return type; }
std::string GetIdentName() const
{
ICE_IF(type != Token::Identifier, "Incorrect type for identname");
ICE_IF(strVal.empty(), "String should not be empty!");
return strVal;
}
uint64_t GetIntVal() const
{
ICE_IF(type != Token::Integer && type != Token::Char, "Request for integer value from wrong type???");
return intVal;
}
double GetRealVal() const
{
ICE_IF(type != Token::Real, "Request for real from wrong type???");
return realVal;
}
std::string GetStrVal() const
{
ICE_IF(type != Token::StringLiteral, "Request for string from wrong type???");
return strVal;
}
// For debug purposes.
void dump(std::ostream& out) const;
void dump() const;
std::string ToString() const;
std::string TypeStr() const;
void SetWhere(const std::string& file, int line, int col);
std::string Where();
const Location& Loc() const { return where; }
int Precedence() const;
bool IsCompare() const { return type >= Token::FirstComparison && type <= Token::LastComparison; }
private:
TokenType type;
// Store location where token started.
Location where;
// Values.
std::string strVal;
uint64_t intVal;
double realVal;
};
#endif