]> git.localhorst.tv Git - l2e.git/blob - src/loader/Tokenizer.h
added error message for missing definitions in PLAY mode
[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)
22         : in(in), line(1), isPutback(false), skipComments(true) { }
23         ~Tokenizer() { }
24 private:
25         Tokenizer(const Tokenizer &);
26         Tokenizer &operator =(const Tokenizer &);
27
28 public:
29         struct Token {
30
31                 enum Type {
32                         UNKNOWN = 0,
33                         ANGLE_BRACKET_OPEN = '{',
34                         ANGLE_BRACKET_CLOSE = '}',
35                         CHEVRON_OPEN = '<',
36                         CHEVRON_CLOSE = '>',
37                         COLON = ':',
38                         COMMA = ',',
39                         BRACKET_OPEN = '[',
40                         BRACKET_CLOSE = ']',
41                         PARENTHESIS_OPEN = '(',
42                         PARENTHESIS_CLOSE = ')',
43                         NUMBER = '0',
44                         STRING = '"',
45                         KEYWORD_EXPORT = 'e',
46                         KEYWORD_FALSE = 'f',
47                         KEYWORD_INCLUDE = 'i',
48                         KEYWORD_TRUE = 't',
49                         IDENTIFIER = 'x',
50                         TYPE_NAME = 'n',
51                         COMMENT = 'c'
52                 };
53
54                 Token() : type(UNKNOWN), number(0) { }
55                 explicit Token(Type t) : type(t), number(0) { }
56
57                 Type type;
58                 std::string str;
59                 int number;
60
61         };
62
63         class LexerError: public std::runtime_error {
64         public:
65                 LexerError(int line, const std::string &msg)
66                 : std::runtime_error(msg), line(line) { }
67                 int Line() const { return line; }
68         private:
69                 int line;
70         };
71
72         bool HasMore();
73         Token GetNext();
74         const Token &Peek();
75         void Putback(const Token &);
76         int Line() const { return line; }
77
78 private:
79         void ScanSpace();
80         Token ReadToken();
81
82         Token ReadNumber();
83         Token ReadString();
84         Token ReadIdentifier();
85
86         Token ReadComment();
87         Token ReadMultilineComment();
88
89         bool CheckKeyword(Token &);
90
91 private:
92         std::istream &in;
93         Token putback;
94         int line;
95         bool isPutback;
96         bool skipComments;
97
98 };
99
100 inline const char *TokenTypeToString(Tokenizer::Token::Type t) {
101         switch (t) {
102                 case Tokenizer::Token::ANGLE_BRACKET_OPEN:
103                         return "ANGLE_BRACKET_OPEN";
104                 case Tokenizer::Token::ANGLE_BRACKET_CLOSE:
105                         return "ANGLE_BRACKET_CLOSE";
106                 case Tokenizer::Token::CHEVRON_OPEN:
107                         return "CHEVRON_OPEN";
108                 case Tokenizer::Token::CHEVRON_CLOSE:
109                         return "CHEVRON_CLOSE";
110                 case Tokenizer::Token::COLON:
111                         return "COLON";
112                 case Tokenizer::Token::COMMA:
113                         return "COMMA";
114                 case Tokenizer::Token::BRACKET_OPEN:
115                         return "BRACKET_OPEN";
116                 case Tokenizer::Token::BRACKET_CLOSE:
117                         return "BRACKET_CLOSE";
118                 case Tokenizer::Token::PARENTHESIS_OPEN:
119                         return "PARENTHESIS_OPEN";
120                 case Tokenizer::Token::PARENTHESIS_CLOSE:
121                         return "PARENTHESIS_CLOSE";
122                 case Tokenizer::Token::NUMBER:
123                         return "NUMBER";
124                 case Tokenizer::Token::STRING:
125                         return "STRING";
126                 case Tokenizer::Token::KEYWORD_EXPORT:
127                         return "KEYWORD_EXPORT";
128                 case Tokenizer::Token::KEYWORD_FALSE:
129                         return "KEYWORD_FALSE";
130                 case Tokenizer::Token::KEYWORD_INCLUDE:
131                         return "KEYWORD_INCLUDE";
132                 case Tokenizer::Token::KEYWORD_TRUE:
133                         return "KEYWORD_TRUE";
134                 case Tokenizer::Token::IDENTIFIER:
135                         return "IDENTIFIER";
136                 case Tokenizer::Token::TYPE_NAME:
137                         return "TYPE_NAME";
138                 default:
139                         return "UNKNOWN";
140         }
141 }
142
143 inline std::ostream &operator <<(std::ostream &out, Tokenizer::Token::Type t) {
144         out << TokenTypeToString(t);
145         return out;
146 }
147
148 }
149
150 #endif /* LOADER_TOKENIZER_H_ */