4 * Created on: Aug 26, 2012
8 #ifndef LOADER_TOKENIZER_H_
9 #define LOADER_TOKENIZER_H_
21 explicit Tokenizer(std::istream &in)
22 : in(in), line(1), isPutback(false), skipComments(true) { }
25 Tokenizer(const Tokenizer &);
26 Tokenizer &operator =(const Tokenizer &);
33 ANGLE_BRACKET_OPEN = '{',
34 ANGLE_BRACKET_CLOSE = '}',
41 PARENTHESIS_OPEN = '(',
42 PARENTHESIS_CLOSE = ')',
47 KEYWORD_INCLUDE = 'i',
58 Token() : type(UNKNOWN), number(0) { }
59 explicit Token(Type t) : type(t), number(0) { }
67 class LexerError: public std::runtime_error {
69 LexerError(int line, const std::string &msg)
70 : std::runtime_error(msg), line(line) { }
71 int Line() const { return line; }
79 void Putback(const Token &);
80 int Line() const { return line; }
88 Token ReadIdentifier();
91 Token ReadMultilineComment();
93 bool CheckKeyword(Token &);
104 inline const char *TokenTypeToString(Tokenizer::Token::Type t) {
106 case Tokenizer::Token::ANGLE_BRACKET_OPEN:
107 return "ANGLE_BRACKET_OPEN";
108 case Tokenizer::Token::ANGLE_BRACKET_CLOSE:
109 return "ANGLE_BRACKET_CLOSE";
110 case Tokenizer::Token::CHEVRON_OPEN:
111 return "CHEVRON_OPEN";
112 case Tokenizer::Token::CHEVRON_CLOSE:
113 return "CHEVRON_CLOSE";
114 case Tokenizer::Token::COLON:
116 case Tokenizer::Token::COMMA:
118 case Tokenizer::Token::COMMAND:
120 case Tokenizer::Token::BRACKET_OPEN:
121 return "BRACKET_OPEN";
122 case Tokenizer::Token::BRACKET_CLOSE:
123 return "BRACKET_CLOSE";
124 case Tokenizer::Token::PARENTHESIS_OPEN:
125 return "PARENTHESIS_OPEN";
126 case Tokenizer::Token::PARENTHESIS_CLOSE:
127 return "PARENTHESIS_CLOSE";
128 case Tokenizer::Token::NUMBER:
130 case Tokenizer::Token::REGISTER:
132 case Tokenizer::Token::SCRIPT_BEGIN:
133 return "SCRIPT_BEGIN";
134 case Tokenizer::Token::SCRIPT_END:
136 case Tokenizer::Token::STRING:
138 case Tokenizer::Token::KEYWORD_EXPORT:
139 return "KEYWORD_EXPORT";
140 case Tokenizer::Token::KEYWORD_FALSE:
141 return "KEYWORD_FALSE";
142 case Tokenizer::Token::KEYWORD_INCLUDE:
143 return "KEYWORD_INCLUDE";
144 case Tokenizer::Token::KEYWORD_TRUE:
145 return "KEYWORD_TRUE";
146 case Tokenizer::Token::IDENTIFIER:
148 case Tokenizer::Token::TYPE_NAME:
155 inline std::ostream &operator <<(std::ostream &out, Tokenizer::Token::Type t) {
156 out << TokenTypeToString(t);
162 #endif /* LOADER_TOKENIZER_H_ */