OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
Token.cpp
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2// $RCSfile: Token.cpp,v $
3// ------------------------------------------------------------------------
4// $Revision: 1.1.1.1.2.2 $
5// ------------------------------------------------------------------------
6// Copyright: see Copyright.readme
7// ------------------------------------------------------------------------
8//
9// Class: Token:
10// Holds a single input token.
11//
12// ------------------------------------------------------------------------
13// Class category: Parser
14// ------------------------------------------------------------------------
15//
16// $Date: 2004/11/12 18:57:54 $
17// $Author: adelmann $
18//
19// ------------------------------------------------------------------------
20
21#include "Parser/Token.h"
23#include <cctype>
24#include <iostream>
25#include <sstream>
26
27
28// Class Token
29// ------------------------------------------------------------------------
30
32 file(), line(0), type(IS_ERROR), lexeme(),
33 d_value(0.0), i_value(0), c_value(0)
34{}
35
36
37Token::Token(const Token &rhs):
38 file(rhs.file), line(rhs.line), type(rhs.type), lexeme(rhs.lexeme),
39 d_value(rhs.d_value), i_value(rhs.i_value), c_value(rhs.c_value)
40{}
41
42
43Token::Token(const std::string &fil, int lin, Type typ, char value):
44 file(fil), line(lin), type(typ), lexeme(1u, value),
45 d_value(0.0), i_value(0), c_value(value)
46{}
47
48
49Token::Token(const std::string &fil, int lin, Type typ, const char *value):
50 file(fil), line(lin), type(typ), lexeme(value),
51 d_value(0.0), i_value(0), c_value(0)
52{}
53
54
56(const std::string &fil, int lin, Type typ, const std::string &lex):
57 file(fil), line(lin), type(typ), lexeme(lex),
58 d_value(0.0), i_value(0), c_value(0)
59{}
60
61
63(const std::string &fil, int lin, const std::string &lex, double value):
64 file(fil), line(lin), type(IS_REAL), lexeme(lex),
65 d_value(value), i_value(0), c_value(0)
66{}
67
68
70(const std::string &fil, int lin, const std::string &lex, int value):
71 file(fil), line(lin), type(IS_INTEGER), lexeme(lex),
72 d_value(0.0), i_value(value), c_value(0)
73{}
74
75
77{}
78
79
80const Token &Token::operator=(const Token &rhs) {
81 file = rhs.file;
82 line = rhs.line;
83 type = rhs.type;
84 lexeme = rhs.lexeme;
85 d_value = rhs.d_value;
86 i_value = rhs.i_value;
87 c_value = rhs.c_value;
88 return *this;
89}
90
91
92bool Token::isDel(char del) const {
93 return (type == IS_DELIMITER && lexeme == std::string(1u, del));
94}
95
96
97bool Token::isDel(const char *del) const {
98 return (type == IS_DELIMITER && lexeme == del);
99}
100
101
102bool Token::isDel() const {
103 return (type == IS_DELIMITER);
104}
105
106
107bool Token::isEOF() const {
108 return (type == IS_EOF);
109}
110
111
112bool Token::isError() const {
113 return (type == IS_ERROR);
114}
115
116
117bool Token::isInteger() const {
118 return (type == IS_INTEGER);
119}
120
121
122bool Token::isReal() const {
123 return (type == IS_REAL);
124}
125
126
127bool Token::isWord() const {
128 return (type == IS_WORD);
129}
130
131
132bool Token::isString() const {
133 return (type == IS_STRING);
134}
135
136
137bool Token::isKey(const char *key) const {
138 return (type == IS_WORD && lexeme == key);
139}
140
141
142bool Token::getBool() const {
143 if(type == IS_WORD) {
144 if(lexeme == "TRUE") {
145 return true;
146 } else if(lexeme == "FALSE") {
147 return false;
148 }
149 }
150
151 invalid("boolean");
152 return false;
153}
154
155
156int Token::getInteger() const {
157 if(type == IS_INTEGER) {
158 return i_value;
159 } else if(type == IS_REAL) {
160 return int(d_value + 0.5);
161 }
162
163 invalid("integer");
164 return 0;
165}
166
167
168double Token::getReal() const {
169 if(type == IS_INTEGER) {
170 return double(i_value);
171 } else if(type == IS_REAL) {
172 return d_value;
173 }
174
175 invalid("real");
176 return 0.0;
177}
178
179
180std::string Token::getString() const {
181 if(type == IS_STRING) {
182 return lexeme;
183 }
184
185 invalid("string");
186 return "";
187}
188
189
190std::string Token::getWord() const {
191 if(type == IS_WORD) {
192 return lexeme;
193 }
194
195 invalid("identifier");
196 return "";
197}
198
199
200const std::string &Token::getLex() const {
201 return lexeme;
202}
203
204
206 return type;
207}
208
209
210const std::string &Token::getFile() const {
211 return file;
212}
213
214
215int Token::getLine() const {
216 return line;
217}
218
219
220std::ostream &operator<<(std::ostream &os, const Token &token) {
221 switch(token.getType()) {
222
224 os << token.getLex();
225 break;
226
227 case Token::IS_EOF:
228 os << "End of file";
229 break;
230
231 case Token::IS_ERROR:
232 os << "Error token: " << token.getLex();
233 break;
234
236 os << token.getInteger();
237 break;
238
239 case Token::IS_REAL:
240 os << token.getReal();
241 break;
242
243 case Token::IS_WORD:
244 os << token.getLex();
245 break;
246
247 case Token::IS_STRING:
248 os << '"' << token.getLex() << '"';
249 break;
250 }
251
252 return os;
253}
254
255
256void Token::invalid(const char *type) const {
257 std::ostringstream os;
258 os << "Expected " << type << " token in line " << line << " of file \""
259 << file << "\".";
260 throw ParseError("Token::invalid()", os.str().c_str());
261}
std::ostream & operator<<(std::ostream &os, const Token &token)
Definition: Token.cpp:220
boost::function< boost::tuple< double, bool >(arguments_t)> type
Definition: function.hpp:21
Representation of a single input token.
Definition: Token.h:33
bool isError() const
Test for error.
Definition: Token.cpp:112
Type getType() const
Return the token type.
Definition: Token.cpp:205
bool isString() const
Test for string.
Definition: Token.cpp:132
std::string file
Definition: Token.h:148
bool isWord() const
Test for word.
Definition: Token.cpp:127
~Token()
Definition: Token.cpp:76
bool isEOF() const
Test for end of file.
Definition: Token.cpp:107
char c_value
Definition: Token.h:160
int line
Definition: Token.h:149
const std::string & getLex() const
Return the lexeme.
Definition: Token.cpp:200
std::string getWord() const
Return word value.
Definition: Token.cpp:190
bool isKey(const char *key) const
Test for keyword.
Definition: Token.cpp:137
Type type
Definition: Token.h:152
bool isDel() const
Test for any delimiter.
Definition: Token.cpp:102
bool getBool() const
Return boolean value.
Definition: Token.cpp:142
Token()
Constructor.
Definition: Token.cpp:31
int getLine() const
Return the token's line number.
Definition: Token.cpp:215
std::string getString() const
Return string value.
Definition: Token.cpp:180
void invalid(const char *) const
Definition: Token.cpp:256
bool isInteger() const
Test for integer.
Definition: Token.cpp:117
double getReal() const
Return real value.
Definition: Token.cpp:168
const Token & operator=(const Token &)
Definition: Token.cpp:80
std::string lexeme
Definition: Token.h:155
int i_value
Definition: Token.h:159
Type
Possible token types.
Definition: Token.h:38
@ IS_STRING
Definition: Token.h:45
@ IS_ERROR
Definition: Token.h:41
@ IS_DELIMITER
Definition: Token.h:39
@ IS_INTEGER
Definition: Token.h:42
@ IS_WORD
Definition: Token.h:44
@ IS_EOF
Definition: Token.h:40
@ IS_REAL
Definition: Token.h:43
bool isReal() const
Test for real number.
Definition: Token.cpp:122
int getInteger() const
Return integer value.
Definition: Token.cpp:156
const std::string & getFile() const
Return the token's file name.
Definition: Token.cpp:210
double d_value
Definition: Token.h:158
Parse exception.
Definition: ParseError.h:32