src/lexer.h

changeset 88
5def6ff8b466
parent 87
8f65914e7046
child 89
029a330a9bef
equal deleted inserted replaced
87:8f65914e7046 88:5def6ff8b466
1 /*
2 Copyright 2012-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
7 are met:
8
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. 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 3. The name of the author may not be used to endorse or promote products
15 derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifndef BOTC_LEXER_H
30 #define BOTC_LEXER_H
31
32 #include "main.h"
33 #include "lexer_scanner.h"
34
35 class lexer
36 {
37 types:
38 struct token
39 {
40 e_token type;
41 string text;
42 string file;
43 int line;
44 int column;
45 };
46
47 using token_list = list<token>;
48 using iterator = token_list::iterator;
49
50 public:
51 lexer();
52 ~lexer();
53
54 void process_file (string file_name);
55 bool get_next (e_token req = tk_any);
56 void must_get_next (e_token tok = tk_any);
57 void must_get_any_of (const list<e_token>& toks);
58 int get_one_symbol (const string_list& syms);
59 void must_be (e_token tok);
60 bool peek_next (token* tk = null);
61
62 inline bool has_valid_token() const
63 {
64 return (m_token_position < m_tokens.end() && m_token_position >= m_tokens.begin());
65 }
66
67 inline token* get_token() const
68 {
69 assert (has_valid_token() == true);
70 return &(*m_token_position);
71 }
72
73 inline bool is_at_end() const
74 {
75 return m_token_position == m_tokens.end();
76 }
77
78 inline e_token get_token_type() const
79 {
80 return get_token()->type;
81 }
82
83 // If @tok is given, describes the token. If not, describes @tok_type.
84 static inline string describe_token_type (e_token tok_type)
85 {
86 return describe_token_private (tok_type, null);
87 }
88
89 static inline string describe_token (token* tok)
90 {
91 return describe_token_private (tok->type, tok);
92 }
93
94 static lexer* get_current_lexer();
95
96 inline void skip (int a = 1)
97 {
98 m_token_position += a;
99 }
100
101 string peek_next_string (int a = 1);
102
103 private:
104 token_list m_tokens;
105 iterator m_token_position;
106
107 // read a mandatory token from scanner
108 void must_get_next_from_scanner (lexer_scanner& sc, e_token tt = tk_any);
109 void check_file_header (lexer_scanner& sc);
110
111 static string describe_token_private (e_token tok_type, token* tok);
112 };
113
114 #endif // BOTC_LEXER_H

mercurial