[80] | 1 | // |
---|
| 2 | // Token.h |
---|
| 3 | // |
---|
| 4 | // $Id: //poco/1.3/Foundation/include/Poco/Token.h#1 $ |
---|
| 5 | // |
---|
| 6 | // Library: Foundation |
---|
| 7 | // Package: Streams |
---|
| 8 | // Module: StreamTokenizer |
---|
| 9 | // |
---|
| 10 | // Definition of the Token class. |
---|
| 11 | // |
---|
| 12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
| 13 | // and Contributors. |
---|
| 14 | // |
---|
| 15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
| 16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
| 17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
| 18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
| 19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
| 20 | // do so, all subject to the following: |
---|
| 21 | // |
---|
| 22 | // The copyright notices in the Software and this entire statement, including |
---|
| 23 | // the above license grant, this restriction and the following disclaimer, |
---|
| 24 | // must be included in all copies of the Software, in whole or in part, and |
---|
| 25 | // all derivative works of the Software, unless such copies or derivative |
---|
| 26 | // works are solely in the form of machine-executable object code generated by |
---|
| 27 | // a source language processor. |
---|
| 28 | // |
---|
| 29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
| 32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
| 33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
| 34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
| 35 | // DEALINGS IN THE SOFTWARE. |
---|
| 36 | // |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | #ifndef Foundation_Token_INCLUDED |
---|
| 40 | #define Foundation_Token_INCLUDED |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | #include "Poco/Foundation.h" |
---|
| 44 | #include <istream> |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | namespace Poco { |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | class Foundation_API Token |
---|
| 51 | /// The base class for all token classes that can be |
---|
| 52 | /// registered with the StreamTokenizer. |
---|
| 53 | { |
---|
| 54 | public: |
---|
| 55 | enum Class |
---|
| 56 | { |
---|
| 57 | IDENTIFIER_TOKEN, |
---|
| 58 | KEYWORD_TOKEN, |
---|
| 59 | SEPARATOR_TOKEN, |
---|
| 60 | OPERATOR_TOKEN, |
---|
| 61 | STRING_LITERAL_TOKEN, |
---|
| 62 | CHAR_LITERAL_TOKEN, |
---|
| 63 | INTEGER_LITERAL_TOKEN, |
---|
| 64 | LONG_INTEGER_LITERAL_TOKEN, |
---|
| 65 | FLOAT_LITERAL_TOKEN, |
---|
| 66 | DOUBLE_LITERAL_TOKEN, |
---|
| 67 | COMMENT_TOKEN, |
---|
| 68 | SPECIAL_COMMENT_TOKEN, |
---|
| 69 | PREPROCESSOR_TOKEN, |
---|
| 70 | WHITESPACE_TOKEN, |
---|
| 71 | EOF_TOKEN, |
---|
| 72 | INVALID_TOKEN, |
---|
| 73 | USER_TOKEN |
---|
| 74 | }; |
---|
| 75 | |
---|
| 76 | Token(); |
---|
| 77 | /// Creates the Token. |
---|
| 78 | |
---|
| 79 | virtual ~Token(); |
---|
| 80 | /// Destroys the Token. |
---|
| 81 | |
---|
| 82 | virtual bool start(char c, std::istream& istr); |
---|
| 83 | /// Checks if the given character (and, optionally, |
---|
| 84 | /// the next character in the input stream) start |
---|
| 85 | /// a valid token. Returns true if so, false |
---|
| 86 | /// otherwise. |
---|
| 87 | /// |
---|
| 88 | /// The current read position in istr must not be |
---|
| 89 | /// changed. In other words, only the peek() method |
---|
| 90 | /// of istream may be used. |
---|
| 91 | /// |
---|
| 92 | /// If the character starts the token, it should |
---|
| 93 | /// be set as the token's value. |
---|
| 94 | |
---|
| 95 | virtual void finish(std::istream& istr); |
---|
| 96 | /// Builds the token by reading and appending |
---|
| 97 | /// the remaining characters from istr. |
---|
| 98 | |
---|
| 99 | virtual Class tokenClass() const; |
---|
| 100 | /// Returns the kind of the token. |
---|
| 101 | |
---|
| 102 | const std::string& tokenString() const; |
---|
| 103 | /// Returns the token's raw string. |
---|
| 104 | |
---|
| 105 | virtual std::string asString() const; |
---|
| 106 | /// Returns a string representation of the token. |
---|
| 107 | |
---|
| 108 | virtual int asInteger() const; |
---|
| 109 | /// Returns an integer representation of the token. |
---|
| 110 | |
---|
| 111 | virtual double asFloat() const; |
---|
| 112 | /// Returns a floating-point representation of the token. |
---|
| 113 | |
---|
| 114 | virtual char asChar() const; |
---|
| 115 | /// Returns a char representation of the token. |
---|
| 116 | |
---|
| 117 | bool is(Class tokenClass) const; |
---|
| 118 | /// Returns true iff the token has the given class. |
---|
| 119 | |
---|
| 120 | protected: |
---|
| 121 | std::string _value; |
---|
| 122 | |
---|
| 123 | private: |
---|
| 124 | Token(const Token&); |
---|
| 125 | Token& operator = (const Token&); |
---|
| 126 | }; |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | class Foundation_API InvalidToken: public Token |
---|
| 130 | /// This token class is used for signalling that |
---|
| 131 | /// an invalid character sequence has been encountered |
---|
| 132 | /// in the input stream. |
---|
| 133 | { |
---|
| 134 | public: |
---|
| 135 | InvalidToken(); |
---|
| 136 | ~InvalidToken(); |
---|
| 137 | Class tokenClass() const; |
---|
| 138 | }; |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | class Foundation_API EOFToken: public Token |
---|
| 142 | /// This token class is used to signal the |
---|
| 143 | /// end of the input stream. |
---|
| 144 | { |
---|
| 145 | public: |
---|
| 146 | EOFToken(); |
---|
| 147 | ~EOFToken(); |
---|
| 148 | Class tokenClass() const; |
---|
| 149 | }; |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | class Foundation_API WhitespaceToken: public Token |
---|
| 153 | /// This pseudo token class is used to eat |
---|
| 154 | /// up whitespace in between real tokens. |
---|
| 155 | { |
---|
| 156 | public: |
---|
| 157 | WhitespaceToken(); |
---|
| 158 | ~WhitespaceToken(); |
---|
| 159 | Class tokenClass() const; |
---|
| 160 | bool start(char c, std::istream& istr); |
---|
| 161 | void finish(std::istream& istr); |
---|
| 162 | }; |
---|
| 163 | |
---|
| 164 | |
---|
| 165 | // |
---|
| 166 | // inlines |
---|
| 167 | // |
---|
| 168 | inline const std::string& Token::tokenString() const |
---|
| 169 | { |
---|
| 170 | return _value; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | inline bool Token::is(Token::Class cls) const |
---|
| 175 | { |
---|
| 176 | return tokenClass() == cls; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | |
---|
| 180 | } // namespace Poco |
---|
| 181 | |
---|
| 182 | |
---|
| 183 | #endif // Foundation_Token_INCLUDED |
---|