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