Sat, 11 Jan 2014 22:36:31 +0200
- *hot stuff coming through*
73
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
1 | /* |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
2 | Copyright (c) 2013-2014, Santeri Piippo |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
3 | All rights reserved. |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
4 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
5 | Redistribution and use in source and binary forms, with or without |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
6 | modification, are permitted provided that the following conditions are met: |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
7 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
8 | * Redistributions of source code must retain the above copyright |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
9 | notice, this list of conditions and the following disclaimer. |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
10 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
11 | * Redistributions in binary form must reproduce the above copyright |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
12 | notice, this list of conditions and the following disclaimer in the |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
13 | documentation and/or other materials provided with the distribution. |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
14 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
15 | * Neither the name of the <organization> nor the |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
16 | names of its contributors may be used to endorse or promote products |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
17 | derived from this software without specific prior written permission. |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
18 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
20 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
22 | DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
23 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
26 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
29 | */ |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
30 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
31 | #include <cstring> |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
32 | #include "lexer.h" |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
33 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
34 | static string_list g_file_name_stack; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
35 | static lexer* g_main_lexer = null; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
36 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
37 | lexer::lexer() |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
38 | { |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
39 | assert (g_main_lexer == null); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
40 | g_main_lexer = this; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
41 | devf ("Lexer initialized\n"); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
42 | } |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
43 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
44 | lexer::~lexer() |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
45 | { |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
46 | g_main_lexer = null; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
47 | devf ("Lexer de-initialized\n"); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
48 | } |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
49 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
50 | void lexer::process_file (string file_name) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
51 | { |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
52 | devf ("Lexer: processing %1\n", file_name); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
53 | FILE* fp = fopen (file_name, "r"); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
54 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
55 | if (fp == null) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
56 | error ("couldn't open %1 for reading: %2", file_name, strerror (errno)); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
57 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
58 | lexer_scanner sc (fp); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
59 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
60 | while (sc.get_next_token()) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
61 | { |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
62 | // Preprocessor commands: |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
63 | if (sc.get_e_token() == tk_hash) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
64 | { |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
65 | must_get_next_from_scanner (sc, tk_symbol); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
66 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
67 | if (sc.get_token_text() == "include") |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
68 | { |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
69 | devf ("Lexer: encountered #include\n"); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
70 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
71 | must_get_next_from_scanner (sc, tk_string); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
72 | string file_name = sc.get_token_text(); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
73 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
74 | if (g_file_name_stack.contains (file_name)) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
75 | error ("attempted to #include %1 recursively", sc.get_token_text()); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
76 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
77 | process_file (file_name); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
78 | } |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
79 | else |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
80 | error ("unknown preprocessor directive \"#%1\"", sc.get_token_text()); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
81 | } |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
82 | else |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
83 | { |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
84 | token tok; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
85 | tok.file = file_name; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
86 | tok.line = sc.get_line(); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
87 | tok.column = sc.get_column(); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
88 | tok.type = sc.get_e_token(); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
89 | tok.text = sc.get_token_text(); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
90 | m_tokens << tok; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
91 | devf ("Lexer: added %1 (%2)\n", describe_e_token (tok.type), |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
92 | describe_token (&tok)); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
93 | } |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
94 | } |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
95 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
96 | devf ("Lexer: File %1 processed.\n", file_name); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
97 | m_token_position = m_tokens.begin() - 1; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
98 | } |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
99 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
100 | // ============================================================================= |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
101 | // |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
102 | bool lexer::get_next (e_token req) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
103 | { |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
104 | iterator pos = m_token_position; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
105 | devf ("Lexer: Requested next token, requirement: %1\n", describe_e_token (req)); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
106 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
107 | if (is_at_end()) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
108 | { |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
109 | devf ("Lexer: at end of tokens. Failed.\n"); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
110 | return false; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
111 | } |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
112 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
113 | m_token_position++; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
114 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
115 | if (req != tk_any && get_token() != req) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
116 | { |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
117 | devf ("Lexer: Token %1 does not meet the requirement\n", describe_token (get_token())); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
118 | m_token_position = pos; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
119 | return false; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
120 | } |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
121 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
122 | devf ("Lexer: Get successful: %1\n", describe_token (get_token())); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
123 | return true; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
124 | } |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
125 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
126 | // ============================================================================= |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
127 | // |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
128 | void lexer::must_get_next (e_token tok) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
129 | { |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
130 | if (!get_next()) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
131 | error ("unexpected EOF"); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
132 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
133 | must_be (tok); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
134 | } |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
135 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
136 | // ============================================================================= |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
137 | // eugh.. |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
138 | // |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
139 | void lexer::must_get_next_from_scanner (lexer_scanner& sc, e_token tok) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
140 | { |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
141 | if (!sc.get_next_token()) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
142 | error ("unexpected EOF"); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
143 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
144 | if (tok != tk_any && sc.get_e_token() != tok) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
145 | error ("expected %1, got %2", describe_e_token (tok), |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
146 | describe_token (get_token())); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
147 | } |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
148 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
149 | // ============================================================================= |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
150 | // |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
151 | void lexer::must_get_any_of (const list<e_token>& toks) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
152 | { |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
153 | devf ("Lexer: need to get a token that is any of: %1\n", toks); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
154 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
155 | if (!get_next()) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
156 | error ("unexpected EOF"); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
157 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
158 | for (e_token tok : toks) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
159 | if (get_token() == tok) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
160 | return; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
161 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
162 | string toknames; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
163 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
164 | for (const e_token& tok_type : toks) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
165 | { |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
166 | if (&tok_type == &toks.last()) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
167 | toknames += " or "; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
168 | elif (toknames.is_empty() == false) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
169 | toknames += ", "; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
170 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
171 | toknames += describe_e_token (tok_type); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
172 | } |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
173 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
174 | error ("expected %1, got %2", toknames, describe_token (get_token())); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
175 | } |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
176 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
177 | // ============================================================================= |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
178 | // |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
179 | int lexer::get_one_symbol (const string_list& syms) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
180 | { |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
181 | if (!get_next()) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
182 | error ("unexpected EOF"); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
183 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
184 | if (get_token() == tk_symbol) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
185 | { |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
186 | for (int i = 0; i < syms.size(); ++i) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
187 | { |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
188 | if (syms[i] == get_token()->text) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
189 | return i; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
190 | } |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
191 | } |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
192 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
193 | error ("expected one of %1, got %2", syms, describe_token (get_token())); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
194 | return -1; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
195 | } |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
196 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
197 | // ============================================================================= |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
198 | // |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
199 | void lexer::must_be (e_token tok) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
200 | { |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
201 | if (get_token() != tok) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
202 | error ("expected %1, got %2", describe_e_token (tok), |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
203 | describe_token (get_token())); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
204 | } |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
205 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
206 | // ============================================================================= |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
207 | // |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
208 | string lexer::describe_token_private (e_token tok_type, lexer::token* tok) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
209 | { |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
210 | if ( (int) tok_type < (int) last_named_token) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
211 | return "\"" + lexer_scanner::get_token_string (tok_type) + "\""; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
212 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
213 | switch (tok_type) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
214 | { |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
215 | case tk_symbol: |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
216 | return tok ? tok->text : "a symbol"; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
217 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
218 | case tk_number: |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
219 | return tok ? tok->text : "a number"; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
220 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
221 | case tk_string: |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
222 | return tok ? ("\"" + tok->text + "\"") : "a string"; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
223 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
224 | case tk_any: |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
225 | return tok ? tok->text : "any token"; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
226 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
227 | default: |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
228 | break; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
229 | } |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
230 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
231 | return ""; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
232 | } |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
233 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
234 | // ============================================================================= |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
235 | // |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
236 | bool lexer::peek_next (lexer::token* tk) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
237 | { |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
238 | iterator pos = m_token_position; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
239 | bool r = get_next(); |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
240 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
241 | if (r && tk != null) |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
242 | *tk = *m_token_position; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
243 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
244 | m_token_position = pos; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
245 | return r; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
246 | } |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
247 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
248 | // ============================================================================= |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
249 | // |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
250 | lexer* lexer::get_main_lexer() |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
251 | { |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
252 | return g_main_lexer; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
253 | } |