src/lexer.h

changeset 73
1ee9b312dc18
child 74
007fbadfa7f9
equal deleted inserted replaced
72:03e4d9db3fd9 73:1ee9b312dc18
1 /*
2 Copyright (c) 2013-2014, Santeri Piippo
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 * Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 * Neither the name of the <organization> nor the
16 names of its contributors may be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
23 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef LEXER_H
32 #define LEXER_H
33
34 #include "main.h"
35 #include "lexer_scanner.h"
36
37 class lexer
38 {
39 types:
40 struct token
41 {
42 e_token type;
43 string text;
44 string file;
45 int line;
46 int column;
47 };
48
49 using token_list = list<token>;
50 using iterator = token_list::iterator;
51
52 public:
53 lexer();
54 ~lexer();
55
56 void process_file (string file_name);
57 bool get_next (e_token req = tk_any);
58 void must_get_next (e_token tok);
59 void must_get_any_of (const list<e_token>& toks);
60 int get_one_symbol (const string_list& syms);
61 void must_be (e_token tok);
62 bool peek_next (token* tk = null);
63
64 inline token* get_token() const
65 {
66 assert (is_at_end() == false);
67 return & (*m_token_position);
68 }
69
70 inline bool is_at_end() const
71 {
72 return m_token_position == m_tokens.end();
73 }
74
75 inline token get_token_type() const
76 {
77 return get_token()->type;
78 }
79
80 // If @tok is given, describes the token. If not, describes @tok_type.
81 static inline string describe_token_type (e_token tok_type)
82 {
83 return describe_token_private (tok_type, null);
84 }
85
86 static inline string describe_token (token* tok)
87 {
88 return describe_token_private (tok->type, tok);
89 }
90
91 static lexer* get_main_lexer();
92 void skip();
93
94 private:
95 token_list m_tokens;
96 iterator m_token_position;
97
98 // read a mandatory token from scanner
99 void must_get_next_from_scanner (lexer_scanner& sc, e_token tok = tk_any);
100
101 static string describe_token_private (e_token tok_type, token* tok);
102 };
103
104 #endif // LEXER_H

mercurial