src/lexer.cc

changeset 74
007fbadfa7f9
parent 73
1ee9b312dc18
child 75
bf8c57437231
equal deleted inserted replaced
73:1ee9b312dc18 74:007fbadfa7f9
58 lexer_scanner sc (fp); 58 lexer_scanner sc (fp);
59 59
60 while (sc.get_next_token()) 60 while (sc.get_next_token())
61 { 61 {
62 // Preprocessor commands: 62 // Preprocessor commands:
63 if (sc.get_e_token() == tk_hash) 63 if (sc.get_token_type() == tk_hash)
64 { 64 {
65 must_get_next_from_scanner (sc, tk_symbol); 65 must_get_next_from_scanner (sc, tk_symbol);
66 66
67 if (sc.get_token_text() == "include") 67 if (sc.get_token_text() == "include")
68 { 68 {
83 { 83 {
84 token tok; 84 token tok;
85 tok.file = file_name; 85 tok.file = file_name;
86 tok.line = sc.get_line(); 86 tok.line = sc.get_line();
87 tok.column = sc.get_column(); 87 tok.column = sc.get_column();
88 tok.type = sc.get_e_token(); 88 tok.type = sc.get_token_type();
89 tok.text = sc.get_token_text(); 89 tok.text = sc.get_token_text();
90 m_tokens << tok; 90 m_tokens << tok;
91 devf ("Lexer: added %1 (%2)\n", describe_e_token (tok.type), 91 devf ("Lexer: added %1 (%2)\n", describe_token_type (tok.type),
92 describe_token (&tok)); 92 describe_token (&tok));
93 } 93 }
94 } 94 }
95 95
96 devf ("Lexer: File %1 processed.\n", file_name); 96 devf ("Lexer: File %1 processed.\n", file_name);
100 // ============================================================================= 100 // =============================================================================
101 // 101 //
102 bool lexer::get_next (e_token req) 102 bool lexer::get_next (e_token req)
103 { 103 {
104 iterator pos = m_token_position; 104 iterator pos = m_token_position;
105 devf ("Lexer: Requested next token, requirement: %1\n", describe_e_token (req)); 105 devf ("Lexer: Requested next token, requirement: %1\n", describe_token_type (req));
106 106
107 if (is_at_end()) 107 if (is_at_end())
108 { 108 {
109 devf ("Lexer: at end of tokens. Failed.\n"); 109 devf ("Lexer: at end of tokens. Failed.\n");
110 return false; 110 return false;
139 void lexer::must_get_next_from_scanner (lexer_scanner& sc, e_token tok) 139 void lexer::must_get_next_from_scanner (lexer_scanner& sc, e_token tok)
140 { 140 {
141 if (!sc.get_next_token()) 141 if (!sc.get_next_token())
142 error ("unexpected EOF"); 142 error ("unexpected EOF");
143 143
144 if (tok != tk_any && sc.get_e_token() != tok) 144 if (tok != tk_any && sc.get_token_type() != tok)
145 error ("expected %1, got %2", describe_e_token (tok), 145 error ("expected %1, got %2", describe_token_type (tok),
146 describe_token (get_token())); 146 describe_token (get_token()));
147 } 147 }
148 148
149 // ============================================================================= 149 // =============================================================================
150 // 150 //
166 if (&tok_type == &toks.last()) 166 if (&tok_type == &toks.last())
167 toknames += " or "; 167 toknames += " or ";
168 elif (toknames.is_empty() == false) 168 elif (toknames.is_empty() == false)
169 toknames += ", "; 169 toknames += ", ";
170 170
171 toknames += describe_e_token (tok_type); 171 toknames += describe_token_type (tok_type);
172 } 172 }
173 173
174 error ("expected %1, got %2", toknames, describe_token (get_token())); 174 error ("expected %1, got %2", toknames, describe_token (get_token()));
175 } 175 }
176 176
197 // ============================================================================= 197 // =============================================================================
198 // 198 //
199 void lexer::must_be (e_token tok) 199 void lexer::must_be (e_token tok)
200 { 200 {
201 if (get_token() != tok) 201 if (get_token() != tok)
202 error ("expected %1, got %2", describe_e_token (tok), 202 error ("expected %1, got %2", describe_token_type (tok),
203 describe_token (get_token())); 203 describe_token (get_token()));
204 } 204 }
205 205
206 // ============================================================================= 206 // =============================================================================
207 // 207 //
249 // 249 //
250 lexer* lexer::get_main_lexer() 250 lexer* lexer::get_main_lexer()
251 { 251 {
252 return g_main_lexer; 252 return g_main_lexer;
253 } 253 }
254
255 // =============================================================================
256 //
257 string lexer::peek_next_string (int a)
258 {
259 if (m_token_position + a >= m_tokens.end())
260 return "";
261
262 iterator oldpos = m_token_position;
263 m_token_position += a;
264 string result = get_token()->text;
265 m_token_position = oldpos;
266 }

mercurial