4 * Created on: Aug 26, 2012
14 bool Tokenizer::HasMore() {
19 void Tokenizer::ScanSpace() {
20 std::istream::char_type c;
22 while (in && std::isspace(c)) {
33 void Tokenizer::Putback(const Token &t) {
35 throw LexerError(line, "Tokenizer: double putback not supported");
42 const Tokenizer::Token &Tokenizer::Peek() {
50 Tokenizer::Token Tokenizer::GetNext() {
59 Tokenizer::Token Tokenizer::ReadToken() {
61 std::istream::char_type c;
64 case Token::ANGLE_BRACKET_OPEN:
65 case Token::ANGLE_BRACKET_CLOSE:
66 case Token::CHEVRON_OPEN:
67 case Token::CHEVRON_CLOSE:
70 case Token::BRACKET_OPEN:
71 case Token::BRACKET_CLOSE:
72 case Token::PARENTHESIS_OPEN:
73 case Token::PARENTHESIS_CLOSE:
74 return Token ((Token::Type) c);
76 case '0': case '1': case '2': case '3': case '4':
77 case '5': case '6': case '7': case '8': case '9':
86 Token t(ReadIdentifier());
87 if (std::isupper(c)) {
88 t.type = Token::TYPE_NAME;
89 } else if (std::islower(c)) {
92 throw LexerError(line, std::string("Tokenizer: cannot parse token: ") + c);
99 Tokenizer::Token Tokenizer::ReadNumber() {
100 Token t(Token::NUMBER);
101 bool isNegative(false);
103 std::istream::char_type c;
107 } else if (c != '+') {
112 if (!std::isdigit(c)) {
120 if (isNegative) t.number *= -1;
125 Tokenizer::Token Tokenizer::ReadString() {
126 Token t(Token::STRING);
129 std::istream::char_type c;
132 throw LexerError(line, "Tokenizer: strings must begin with '\"'");
140 t.str.push_back('\n');
143 t.str.push_back('\r');
146 t.str.push_back('\t');
152 } else if (c == '"') {
154 } else if (c == '\\') {
164 Tokenizer::Token Tokenizer::ReadIdentifier() {
165 Token t(Token::IDENTIFIER);
167 std::istream::char_type c;
169 if (std::isalnum(c) || c == '_') {
180 bool Tokenizer::CheckKeyword(Token &t) {
181 if (t.str == "export") {
182 t.type = Token::KEYWORD_EXPORT;
184 } else if (t.str == "false") {
185 t.type = Token::KEYWORD_FALSE;
187 } else if (t.str == "include") {
188 t.type = Token::KEYWORD_INCLUDE;
190 } else if (t.str == "true") {
191 t.type = Token::KEYWORD_TRUE;