]> git.localhorst.tv Git - l2e.git/blob - src/loader/Tokenizer.h
f2ab4aaaadd2fb5883741f8641f24917e0cff5ab
[l2e.git] / src / loader / Tokenizer.h
1 /*
2  * Tokenizer.h
3  *
4  *  Created on: Aug 26, 2012
5  *      Author: holy
6  */
7
8 #ifndef LOADER_TOKENIZER_H_
9 #define LOADER_TOKENIZER_H_
10
11 #include <iosfwd>
12 #include <ostream>
13 #include <stdexcept>
14 #include <string>
15
16 namespace loader {
17
18 class Tokenizer {
19
20 public:
21         explicit Tokenizer(std::istream &in) : in(in), isPutback(false) { }
22         ~Tokenizer() { }
23 private:
24         Tokenizer(const Tokenizer &);
25         Tokenizer &operator =(const Tokenizer &);
26
27 public:
28         struct Token {
29
30                 enum Type {
31                         UNKNOWN,
32                         ANGLE_BRACKET_OPEN = '{',
33                         ANGLE_BRACKET_CLOSE = '}',
34                         CHEVRON_OPEN = '<',
35                         CHEVRON_CLOSE = '>',
36                         COLON = ':',
37                         COMMA = ',',
38                         BRACKET_OPEN = '[',
39                         BRACKET_CLOSE = ']',
40                         NUMBER,
41                         STRING,
42                         KEYWORD_EXPORT,
43                         KEYWORD_FALSE,
44                         KEYWORD_INCLUDE,
45                         KEYWORD_TRUE,
46                         IDENTIFIER,
47                         TYPE_NAME,
48                 };
49
50                 Token() : type(UNKNOWN), number(0) { }
51                 explicit Token(Type t) : type(t), number(0) { }
52
53                 Type type;
54                 std::string str;
55                 int number;
56
57         };
58
59         class LexerError: public std::runtime_error {
60         public:
61                 explicit LexerError(const std::string &msg) : std::runtime_error(msg) { }
62         };
63
64         bool HasMore();
65         Token GetNext();
66         void Putback(const Token &);
67
68 private:
69         Token ReadToken();
70
71         Token ReadNumber();
72         Token ReadString();
73         Token ReadIdentifier();
74
75         bool CheckKeyword(Token &);
76
77 private:
78         std::istream &in;
79         Token putback;
80         bool isPutback;
81
82 };
83
84 inline const char *TokenTypeToString(Tokenizer::Token::Type t) {
85         switch (t) {
86                 case Tokenizer::Token::ANGLE_BRACKET_OPEN:
87                         return "ANGLE_BRACKET_OPEN";
88                 case Tokenizer::Token::ANGLE_BRACKET_CLOSE:
89                         return "ANGLE_BRACKET_CLOSE";
90                 case Tokenizer::Token::CHEVRON_OPEN:
91                         return "CHEVRON_OPEN";
92                 case Tokenizer::Token::CHEVRON_CLOSE:
93                         return "CHEVRON_CLOSE";
94                 case Tokenizer::Token::COLON:
95                         return "COLON";
96                 case Tokenizer::Token::COMMA:
97                         return "COMMA";
98                 case Tokenizer::Token::BRACKET_OPEN:
99                         return "BRACKET_OPEN";
100                 case Tokenizer::Token::BRACKET_CLOSE:
101                         return "BRACKET_CLOSE";
102                 case Tokenizer::Token::NUMBER:
103                         return "NUMBER";
104                 case Tokenizer::Token::STRING:
105                         return "STRING";
106                 case Tokenizer::Token::KEYWORD_EXPORT:
107                         return "KEYWORD_EXPORT";
108                 case Tokenizer::Token::KEYWORD_FALSE:
109                         return "KEYWORD_FALSE";
110                 case Tokenizer::Token::KEYWORD_INCLUDE:
111                         return "KEYWORD_INCLUDE";
112                 case Tokenizer::Token::KEYWORD_TRUE:
113                         return "KEYWORD_TRUE";
114                 case Tokenizer::Token::IDENTIFIER:
115                         return "IDENTIFIER";
116                 case Tokenizer::Token::TYPE_NAME:
117                         return "TYPE_NAME";
118                 default:
119                         return "UNKNOWN";
120         }
121 }
122
123 inline std::ostream &operator <<(std::ostream &out, Tokenizer::Token::Type t) {
124         out << TokenTypeToString(t);
125         return out;
126 }
127
128 }
129
130 #endif /* LOADER_TOKENIZER_H_ */