/src/poco/Foundation/include/Poco/Token.h
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // Token.h |
3 | | // |
4 | | // Library: Foundation |
5 | | // Package: Streams |
6 | | // Module: StreamTokenizer |
7 | | // |
8 | | // Definition of the Token class. |
9 | | // |
10 | | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
11 | | // and Contributors. |
12 | | // |
13 | | // SPDX-License-Identifier: BSL-1.0 |
14 | | // |
15 | | |
16 | | |
17 | | #ifndef Foundation_Token_INCLUDED |
18 | | #define Foundation_Token_INCLUDED |
19 | | |
20 | | |
21 | | #include "Poco/Foundation.h" |
22 | | #include <istream> |
23 | | |
24 | | |
25 | | namespace Poco { |
26 | | |
27 | | |
28 | | class Foundation_API Token |
29 | | /// The base class for all token classes that can be |
30 | | /// registered with the StreamTokenizer. |
31 | | { |
32 | | public: |
33 | | enum Class |
34 | | { |
35 | | IDENTIFIER_TOKEN, |
36 | | KEYWORD_TOKEN, |
37 | | SEPARATOR_TOKEN, |
38 | | OPERATOR_TOKEN, |
39 | | STRING_LITERAL_TOKEN, |
40 | | CHAR_LITERAL_TOKEN, |
41 | | INTEGER_LITERAL_TOKEN, |
42 | | LONG_INTEGER_LITERAL_TOKEN, |
43 | | FLOAT_LITERAL_TOKEN, |
44 | | DOUBLE_LITERAL_TOKEN, |
45 | | COMMENT_TOKEN, |
46 | | SPECIAL_COMMENT_TOKEN, |
47 | | PREPROCESSOR_TOKEN, |
48 | | WHITESPACE_TOKEN, |
49 | | EOF_TOKEN, |
50 | | INVALID_TOKEN, |
51 | | USER_TOKEN |
52 | | }; |
53 | | |
54 | | Token(); |
55 | | /// Creates the Token. |
56 | | |
57 | | virtual ~Token(); |
58 | | /// Destroys the Token. |
59 | | |
60 | | virtual bool start(char c, std::istream& istr); |
61 | | /// Checks if the given character (and, optionally, |
62 | | /// the next character in the input stream) start |
63 | | /// a valid token. Returns true if so, false |
64 | | /// otherwise. |
65 | | /// |
66 | | /// The current read position in istr must not be |
67 | | /// changed. In other words, only the peek() method |
68 | | /// of istream may be used. |
69 | | /// |
70 | | /// If the character starts the token, it should |
71 | | /// be set as the token's value. |
72 | | |
73 | | virtual void finish(std::istream& istr); |
74 | | /// Builds the token by reading and appending |
75 | | /// the remaining characters from istr. |
76 | | |
77 | | virtual Class tokenClass() const; |
78 | | /// Returns the kind of the token. |
79 | | |
80 | | const std::string& tokenString() const; |
81 | | /// Returns the token's raw string. |
82 | | |
83 | | virtual std::string asString() const; |
84 | | /// Returns a string representation of the token. |
85 | | |
86 | | #if defined(POCO_HAVE_INT64) |
87 | | virtual Int64 asInteger64() const; |
88 | | /// Returns a 64-bit integer representation of the token. |
89 | | |
90 | | virtual UInt64 asUnsignedInteger64() const; |
91 | | /// Returns an unsigned 64-bit integer representation of the token. |
92 | | #endif |
93 | | |
94 | | virtual int asInteger() const; |
95 | | /// Returns an integer representation of the token. |
96 | | |
97 | | virtual unsigned asUnsignedInteger() const; |
98 | | /// Returns an unsigned integer representation of the token. |
99 | | |
100 | | virtual double asFloat() const; |
101 | | /// Returns a floating-point representation of the token. |
102 | | |
103 | | virtual char asChar() const; |
104 | | /// Returns a char representation of the token. |
105 | | |
106 | | bool is(Class tokenClass) const; |
107 | | /// Returns true iff the token has the given class. |
108 | | |
109 | | protected: |
110 | | std::string _value; |
111 | | |
112 | | private: |
113 | | Token(const Token&); |
114 | | Token& operator = (const Token&); |
115 | | }; |
116 | | |
117 | | |
118 | | class Foundation_API InvalidToken: public Token |
119 | | /// This token class is used for signalling that |
120 | | /// an invalid character sequence has been encountered |
121 | | /// in the input stream. |
122 | | { |
123 | | public: |
124 | | InvalidToken(); |
125 | | ~InvalidToken(); |
126 | | Class tokenClass() const; |
127 | | }; |
128 | | |
129 | | |
130 | | class Foundation_API EOFToken: public Token |
131 | | /// This token class is used to signal the |
132 | | /// end of the input stream. |
133 | | { |
134 | | public: |
135 | | EOFToken(); |
136 | | ~EOFToken(); |
137 | | Class tokenClass() const; |
138 | | }; |
139 | | |
140 | | |
141 | | class Foundation_API WhitespaceToken: public Token |
142 | | /// This pseudo token class is used to eat |
143 | | /// up whitespace in between real tokens. |
144 | | { |
145 | | public: |
146 | | WhitespaceToken(); |
147 | | ~WhitespaceToken(); |
148 | | Class tokenClass() const; |
149 | | bool start(char c, std::istream& istr); |
150 | | void finish(std::istream& istr); |
151 | | }; |
152 | | |
153 | | |
154 | | // |
155 | | // inlines |
156 | | // |
157 | | inline const std::string& Token::tokenString() const |
158 | 0 | { |
159 | 0 | return _value; |
160 | 0 | } |
161 | | |
162 | | |
163 | | inline bool Token::is(Token::Class cls) const |
164 | 0 | { |
165 | 0 | return tokenClass() == cls; |
166 | 0 | } |
167 | | |
168 | | |
169 | | } // namespace Poco |
170 | | |
171 | | |
172 | | #endif // Foundation_Token_INCLUDED |