OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
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 "OpalParser/Token.h"
22#include <cctype>
23#include <iostream>
24#include <sstream>
26
27// Class Token
28// ------------------------------------------------------------------------
29
30Token::Token() : file(), line(0), type(IS_ERROR), lexeme(), d_value(0.0), i_value(0), c_value(0) {}
31
33 : file(rhs.file),
34 line(rhs.line),
35 type(rhs.type),
36 lexeme(rhs.lexeme),
37 d_value(rhs.d_value),
38 i_value(rhs.i_value),
39 c_value(rhs.c_value) {}
40
41Token::Token(const std::string& fil, int lin, Type typ, char value)
42 : file(fil),
43 line(lin),
44 type(typ),
45 lexeme(1u, value),
46 d_value(0.0),
47 i_value(0),
48 c_value(value) {}
49
50Token::Token(const std::string& fil, int lin, Type typ, const char* value)
51 : file(fil), line(lin), type(typ), lexeme(value), d_value(0.0), i_value(0), c_value(0) {}
52
53Token::Token(const std::string& fil, int lin, Type typ, const std::string& lex)
54 : file(fil), line(lin), type(typ), lexeme(lex), d_value(0.0), i_value(0), c_value(0) {}
55
56Token::Token(const std::string& fil, int lin, const std::string& lex, double value)
57 : file(fil), line(lin), type(IS_REAL), lexeme(lex), d_value(value), i_value(0), c_value(0) {}
58
59Token::Token(const std::string& fil, int lin, const std::string& lex, int value)
60 : file(fil),
61 line(lin),
62 type(IS_INTEGER),
63 lexeme(lex),
64 d_value(0.0),
65 i_value(value),
66 c_value(0) {}
67
69
70const Token& Token::operator=(const Token& rhs) {
71 file = rhs.file;
72 line = rhs.line;
73 type = rhs.type;
74 lexeme = rhs.lexeme;
75 d_value = rhs.d_value;
76 i_value = rhs.i_value;
77 c_value = rhs.c_value;
78 return *this;
79}
80
81bool Token::isDel(char del) const {
82 return (type == IS_DELIMITER && lexeme == std::string(1u, del));
83}
84
85bool Token::isDel(const char* del) const { return (type == IS_DELIMITER && lexeme == del); }
86
87bool Token::isDel() const { return (type == IS_DELIMITER); }
88
89bool Token::isEOF() const { return (type == IS_EOF); }
90
91bool Token::isError() const { return (type == IS_ERROR); }
92
93bool Token::isInteger() const { return (type == IS_INTEGER); }
94
95bool Token::isReal() const { return (type == IS_REAL); }
96
97bool Token::isWord() const { return (type == IS_WORD); }
98
99bool Token::isString() const { return (type == IS_STRING); }
100
101bool Token::isKey(const char* key) const { return (type == IS_WORD && lexeme == key); }
102
103bool Token::getBool() const {
104 if (type == IS_WORD) {
105 if (lexeme == "TRUE") {
106 return true;
107 } else if (lexeme == "FALSE") {
108 return false;
109 }
110 }
111
112 invalid("boolean");
113 return false;
114}
115
116int Token::getInteger() const {
117 if (type == IS_INTEGER) {
118 return i_value;
119 } else if (type == IS_REAL) {
120 return int(d_value + 0.5);
121 }
122
123 invalid("integer");
124 return 0;
125}
126
127double Token::getReal() const {
128 if (type == IS_INTEGER) {
129 return double(i_value);
130 } else if (type == IS_REAL) {
131 return d_value;
132 }
133
134 invalid("real");
135 return 0.0;
136}
137
138std::string Token::getString() const {
139 if (type == IS_STRING) {
140 return lexeme;
141 }
142
143 invalid("string");
144 return "";
145}
146
147std::string Token::getWord() const {
148 if (type == IS_WORD) {
149 return lexeme;
150 }
151
152 invalid("identifier");
153 return "";
154}
155
156const std::string& Token::getLex() const { return lexeme; }
157
158Token::Type Token::getType() const { return type; }
159
160const std::string& Token::getFile() const { return file; }
161
162int Token::getLine() const { return line; }
163
164std::ostream& operator<<(std::ostream& os, const Token& token) {
165 switch (token.getType()) {
167 os << token.getLex();
168 break;
169
170 case Token::IS_EOF:
171 os << "End of file";
172 break;
173
174 case Token::IS_ERROR:
175 os << "Error token: " << token.getLex();
176 break;
177
179 os << token.getInteger();
180 break;
181
182 case Token::IS_REAL:
183 os << token.getReal();
184 break;
185
186 case Token::IS_WORD:
187 os << token.getLex();
188 break;
189
190 case Token::IS_STRING:
191 os << '"' << token.getLex() << '"';
192 break;
193 }
194
195 return os;
196}
197
198void Token::invalid(const char* type) const {
199 std::ostringstream os;
200 os << "Expected " << type << " token in line " << line << " of file \"" << file << "\".";
201 throw ParseError("Token::invalid()", os.str().c_str());
202}
std::ostream & operator<<(std::ostream &os, const Token &token)
Definition Token.cpp:164
Parse exception.
Definition ParseError.h:31
Representation of a single input token.
Definition Token.h:32
bool isError() const
Test for error.
Definition Token.cpp:91
Type getType() const
Return the token type.
Definition Token.cpp:158
bool isString() const
Test for string.
Definition Token.cpp:99
std::string file
Definition Token.h:135
bool isWord() const
Test for word.
Definition Token.cpp:97
~Token()
Definition Token.cpp:68
bool isEOF() const
Test for end of file.
Definition Token.cpp:89
char c_value
Definition Token.h:147
int line
Definition Token.h:136
const std::string & getLex() const
Return the lexeme.
Definition Token.cpp:156
std::string getWord() const
Return word value.
Definition Token.cpp:147
bool isKey(const char *key) const
Test for keyword.
Definition Token.cpp:101
Type type
Definition Token.h:139
bool isDel() const
Test for any delimiter.
Definition Token.cpp:87
bool getBool() const
Return boolean value.
Definition Token.cpp:103
Token()
Constructor.
Definition Token.cpp:30
int getLine() const
Return the token's line number.
Definition Token.cpp:162
std::string getString() const
Return string value.
Definition Token.cpp:138
void invalid(const char *) const
Definition Token.cpp:198
bool isInteger() const
Test for integer.
Definition Token.cpp:93
double getReal() const
Return real value.
Definition Token.cpp:127
const Token & operator=(const Token &)
Definition Token.cpp:70
std::string lexeme
Definition Token.h:142
int i_value
Definition Token.h:146
Type
Possible token types.
Definition Token.h:35
@ IS_STRING
Definition Token.h:35
@ IS_ERROR
Definition Token.h:35
@ IS_DELIMITER
Definition Token.h:35
@ IS_INTEGER
Definition Token.h:35
@ IS_WORD
Definition Token.h:35
@ IS_EOF
Definition Token.h:35
@ IS_REAL
Definition Token.h:35
bool isReal() const
Test for real number.
Definition Token.cpp:95
int getInteger() const
Return integer value.
Definition Token.cpp:116
const std::string & getFile() const
Return the token's file name.
Definition Token.cpp:160
double d_value
Definition Token.h:145