OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Token.h
Go to the documentation of this file.
1 #ifndef CLASSIC_Token_HH
2 #define CLASSIC_Token_HH 1
3 
4 // ------------------------------------------------------------------------
5 // $RCSfile: Token.h,v $
6 // ------------------------------------------------------------------------
7 // $Revision: 1.1.1.1 $
8 // ------------------------------------------------------------------------
9 // Copyright: see Copyright.readme
10 // ------------------------------------------------------------------------
11 //
12 // Class: Token
13 //
14 // ------------------------------------------------------------------------
15 // Class category: Parser
16 // ------------------------------------------------------------------------
17 //
18 // $Date: 2000/03/27 09:32:37 $
19 // $Author: fci $
20 //
21 // ------------------------------------------------------------------------
22 
23 #include <iosfwd>
24 #include <string>
25 
26 
27 // Class Token
28 // ------------------------------------------------------------------------
30 // All tokens contain the name of the input stream and the line number
31 // where they came from.
32 
33 class Token {
34 
35 public:
36 
38  enum Type {
46  };
47 
49  // Construct empty token.
50  Token();
51 
53  // Construct character token with type [b]type[/b] and value [b]c[/b].
54  Token(const std::string &file, int line, Type type, char c);
55 
57  // Construct std::string token with type [b]type[/b] and value [b]s[/b].
58  Token(const std::string &file, int line, Type type, const char *s);
59 
61  // Construct string token with type [b]type[/b] and value [b]lex[/b].
62  Token(const std::string &file, int line, Type type, const std::string &lex);
63 
65  // Construct real numeric token with lexeme [b]lex[/b] and value
66  // [b]value[/b].
67  Token(const std::string &file, int line, const std::string &lex,
68  double value);
69 
71  // Construct integer token with lexeme [b]lex[/b] and value [b]value[/b].
72  Token(const std::string &file, int line, const std::string &lex, int value);
73 
74  Token(const Token &);
75  ~Token();
76  const Token &operator=(const Token &);
77 
79  // Return true, if token is single character [b]del[/b].
80  bool isDel(char del) const;
81 
83  // Return true, if token is character string [b]del[/b].
84  bool isDel(const char *del) const;
85 
87  bool isDel() const;
88 
90  bool isEOF() const;
91 
93  bool isError() const;
94 
96  bool isInteger() const;
97 
99  bool isReal() const;
100 
102  bool isWord() const;
103 
105  bool isString() const;
106 
108  bool isKey(const char *key) const;
109 
111  // Throw ParseError, if token is not boolean.
112  bool getBool() const;
113 
115  // Throw ParseError, if token is not numeric.
116  int getInteger() const;
117 
119  // Throw ParseError, if token is not numeric.
120  double getReal() const;
121 
123  // Throw ParseError, if token is not string.
124  std::string getString() const;
125 
127  // Throw ParseError, if token is not word.
128  std::string getWord() const;
129 
131  const std::string &getLex() const;
132 
134  Type getType() const;
135 
137  const std::string &getFile() const;
138 
140  int getLine() const;
141 
142 private:
143 
144  // Invalid token type.
145  void invalid(const char *) const;
146 
147  // Input line and file.
148  std::string file;
149  int line;
150 
151  // Token type.
153 
154  // Lexeme for token.
155  std::string lexeme;
156 
157  // Value for token.
158  double d_value;
159  int i_value;
160  char c_value;
161 };
162 
163 
164 // Output operator.
165 std::ostream &operator<<(std::ostream &, const Token &);
166 
167 #endif // CLASSIC_Token_HH
bool isDel() const
Test for any delimiter.
Definition: Token.cpp:102
std::ostream & operator<<(std::ostream &os, const Attribute &attr)
Definition: Attribute.cpp:167
bool isEOF() const
Test for end of file.
Definition: Token.cpp:107
bool isError() const
Test for error.
Definition: Token.cpp:112
bool isWord() const
Test for word.
Definition: Token.cpp:127
std::string lexeme
Definition: Token.h:155
Type type
Definition: Token.h:152
std::string file
Definition: Token.h:148
double d_value
Definition: Token.h:158
int line
Definition: Token.h:149
std::string getString() const
Return string value.
Definition: Token.cpp:180
bool isReal() const
Test for real number.
Definition: Token.cpp:122
const std::string & getLex() const
Return the lexeme.
Definition: Token.cpp:200
int getInteger() const
Return integer value.
Definition: Token.cpp:156
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:52
bool getBool() const
Return boolean value.
Definition: Token.cpp:142
Representation of a single input token.
Definition: Token.h:33
double getReal() const
Return real value.
Definition: Token.cpp:168
bool isInteger() const
Test for integer.
Definition: Token.cpp:117
Type getType() const
Return the token type.
Definition: Token.cpp:205
int i_value
Definition: Token.h:159
Token()
Constructor.
Definition: Token.cpp:31
~Token()
Definition: Token.cpp:76
char c_value
Definition: Token.h:160
bool isString() const
Test for string.
Definition: Token.cpp:132
bool isKey(const char *key) const
Test for keyword.
Definition: Token.cpp:137
void invalid(const char *) const
Definition: Token.cpp:256
Type
Possible token types.
Definition: Token.h:38
const Token & operator=(const Token &)
Definition: Token.cpp:80
std::string getWord() const
Return word value.
Definition: Token.cpp:190
int getLine() const
Return the token&#39;s line number.
Definition: Token.cpp:215
const std::string & getFile() const
Return the token&#39;s file name.
Definition: Token.cpp:210