4 * Created on: Aug 26, 2012
15 bool Tokenizer::HasMore() {
19 void Tokenizer::Putback(const Token &t) {
21 throw std::runtime_error("Tokenizer: double putback not supported");
28 Tokenizer::Token Tokenizer::GetNext() {
37 Tokenizer::Token Tokenizer::ReadToken() {
38 std::istream::char_type c;
40 while (std::isspace(c)) in.get(c);
42 case Token::ANGLE_BRACKET_OPEN:
43 case Token::ANGLE_BRACKET_CLOSE:
44 case Token::CHEVRON_OPEN:
45 case Token::CHEVRON_CLOSE:
48 case Token::BRACKET_OPEN:
49 case Token::BRACKET_CLOSE:
50 return Token ((Token::Type) c);
52 case '0': case '1': case '2': case '3': case '4':
53 case '5': case '6': case '7': case '8': case '9':
62 Token t(ReadIdentifier());
63 if (std::isupper(c)) {
64 t.type = Token::TYPE_NAME;
65 } else if (std::islower(c)) {
68 throw std::runtime_error(std::string("Tokenizer: cannot parse token: ") + c);
75 Tokenizer::Token Tokenizer::ReadNumber() {
76 Token t(Token::NUMBER);
77 bool isNegative(false);
79 std::istream::char_type c;
83 } else if (c != '+') {
88 if (!std::isdigit(c)) {
96 if (isNegative) t.number *= -1;
101 Tokenizer::Token Tokenizer::ReadString() {
102 Token t(Token::STRING);
105 std::istream::char_type c;
108 throw std::runtime_error("Tokenizer: strings must begin with '\"'");
116 t.str.push_back('\n');
119 t.str.push_back('\r');
122 t.str.push_back('\t');
128 } else if (c == '"') {
130 } else if (c == '\\') {
140 Tokenizer::Token Tokenizer::ReadIdentifier() {
141 Token t(Token::IDENTIFIER);
143 std::istream::char_type c;
145 if (std::isalnum(c) || c == '_') {
156 bool Tokenizer::CheckKeyword(Token &t) {
157 if (t.str == "export") {
158 t.type = Token::KEYWORD_EXPORT;
160 } else if (t.str == "false") {
161 t.type = Token::KEYWORD_FALSE;
163 } else if (t.str == "include") {
164 t.type = Token::KEYWORD_INCLUDE;
166 } else if (t.str == "true") {
167 t.type = Token::KEYWORD_TRUE;