Debian Codegraph @

src/lexical/scanner.c

raw
1 #include "lexical/scanner.h"
2 #include "config.h"
3 #include "lexical/token.h"
4 #include "main.h"
5 #include "utils/arch_trans.h"
6 #include "utils/read_source.h"
7 #include <assert.h>
8 #include <ctype.h>
9 #include <limits.h>
10 #include <stdbool.h>
11 #include <stddef.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16
17 static scanner_t scanner;
18 static uint8_t unknown_count = 0;
19
20 #define INIT_TOKEN(type) \
21 do \
22 { \
23 init_token (token, &scanner, type); \
24 return; \
25 } \
26 while (0)
27 #define INIT_TOKEN_DATA(type, data) \
28 do \
29 { \
30 init_token_data (token, &scanner, type, data); \
31 return; \
32 } \
33 while (0)
34 #define INIT_TOKEN_ADV1(type) \
35 do \
36 { \
37 advance (1); \
38 INIT_TOKEN (type); \
39 } \
40 while (0)
41
42 static inline bool
43 isidentifier (char c)
44 {
45 return isalnum_l (c, lc_c) || c == '_';
46 }
47
48 static char
49 peek (uint32_t offset)
50 {
51 return scanner.current_char[offset];
52 }
53
54 static void
55 advance (uint16_t advance_len)
56 {
57 scanner.current_char += advance_len;
58 }
59
60 static bool
61 match_offset (char expected, uint32_t offset)
62 {
63 if (peek (offset) != expected)
64 return false;
65
66 advance (offset + 1);
67 return true;
68 }
69
70 static bool
71 match (char expected)
72 {
73 return match_offset (expected, 0);
74 }
75
76 static bool
77 match_token (token_type tk)
78 {
79 register string_t token = token_pairs[tk];
80 if (strncmp (token.start, scanner.current_char, token.len))
81 return false;
82 if (UNLIKELY (isidentifier (peek (token.len))))
83 return false;
84
85 advance (token.len);
86 return true;
87 }
88
89 // from and to all included
90 static token_type
91 match_token_range (token_type from, token_type to)
92 {
93 for (uint32_t i = from; i <= to; i++)
94 if (match_token (i))
95 return i;
96 return UNKNOWN;
97 }
98
99 static void
100 reset_to_nextline (token_t *token)
101 {
102 init_token (token, &scanner, EOL);
103
104 // if nextline == NULL, it means we meet_eof
105 char *nextline = next_line ();
106 scanner.token_start = nextline;
107 scanner.current_char = scanner.token_start;
108 scanner.line_nr++;
109 }
110
111 void
112 init_scanner (char *start)
113 {
114 scanner.token_start = start;
115 scanner.current_char = start;
116 scanner.line_nr = 1;
117 }
118
119 void
120 scan_token (token_t *token)
121 {
122 // if scanner.token_start == NULL, we meet_eof
123 if (scanner.token_start == NULL)
124 INIT_TOKEN (TOKEN_EOF);
125
126 // skip spaces
127 while (isspace_l (peek (0), lc_c) && peek (0) != '\n')
128 advance (1);
129
130 // sync
131 scanner.token_start = scanner.current_char;
132
133 // COMMENT
134 if (match ('#'))
135 {
136 while (peek (0) != '\n')
137 advance (1);
138 INIT_TOKEN (COMMENT);
139 }
140
141 // EOL
142 if (match ('\n'))
143 {
144 reset_to_nextline (token);
145 return;
146 }
147
148 char cur_char = peek (0);
149 if (islower_l (cur_char, lc_c))
150 {
151 token_type tk = match_token_range (RETURN, ELSE);
152 if (tk != UNKNOWN)
153 INIT_TOKEN (tk);
154 tk = str_to_internal_arch (scanner.current_char, false);
155 if (tk != UNKNOWN && !isidentifier (peek (token_pairs[tk].len)))
156 {
157 advance (token_pairs[tk].len);
158 INIT_TOKEN (tk);
159 }
160 }
161 else if (isupper_l (cur_char, lc_c))
162 {
163 token_type tk = match_token_range (KILL_PROC, ERRNO);
164 if (tk != UNKNOWN)
165 INIT_TOKEN (tk);
166 }
167 else if (ispunct_l (cur_char, lc_c))
168 {
169 switch (cur_char)
170 {
171 case '$':
172 if (match_offset ('A', 1))
173 INIT_TOKEN (A);
174 if (match_offset ('X', 1))
175 INIT_TOKEN (X);
176 token_type tk = match_token_range (MEM, ATTR_HIGHARG);
177 if (tk != UNKNOWN)
178 INIT_TOKEN (tk);
179 break;
180 case ',':
181 INIT_TOKEN_ADV1 (COMMA);
182 case '.':
183 INIT_TOKEN_ADV1 (DOT);
184 case '[':
185 INIT_TOKEN_ADV1 (LEFT_BRACKET);
186 case ']':
187 INIT_TOKEN_ADV1 (RIGHT_BRACKET);
188 case '(':
189 INIT_TOKEN_ADV1 (LEFT_PAREN);
190 case ')':
191 INIT_TOKEN_ADV1 (RIGHT_PAREN);
192 case '+':
193 if (match_offset ('=', 1))
194 INIT_TOKEN (ADD_TO);
195 break;
196 case '-':
197 if (match_offset ('=', 1))
198 INIT_TOKEN (SUB_TO);
199 INIT_TOKEN_ADV1 (NEGATIVE);
200 case '*':
201 if (match_offset ('=', 1))
202 INIT_TOKEN (MULTI_TO);
203 break;
204 case '/':
205 if (match_offset ('=', 1))
206 INIT_TOKEN (DIVIDE_TO);
207 break;
208 case '<':
209 if (match_offset ('=', 1))
210 INIT_TOKEN (LESS_EQUAL);
211 if (peek (1) == '<' && match_offset ('=', 2))
212 INIT_TOKEN (LSH_TO);
213 INIT_TOKEN_ADV1 (LESS_THAN);
214 case '>':
215 if (match_offset ('=', 1))
216 INIT_TOKEN (GREATER_EQUAL);
217 if (peek (1) == '>' && match_offset ('=', 2))
218 INIT_TOKEN (RSH_TO);
219 INIT_TOKEN_ADV1 (GREATER_THAN);
220 case '&':
221 if (match_offset ('=', 1))
222 INIT_TOKEN (AND_TO);
223 INIT_TOKEN_ADV1 (AND);
224 case '|':
225 if (match_offset ('=', 1))
226 INIT_TOKEN (OR_TO);
227 break;
228 case '^':
229 if (match_offset ('=', 1))
230 INIT_TOKEN (XOR_TO);
231 break;
232 case '!':
233 if (match_offset ('=', 1))
234 INIT_TOKEN (BANG_EQUAL);
235 INIT_TOKEN_ADV1 (BANG);
236 case '=':
237 if (match_offset ('=', 1))
238 INIT_TOKEN (EQUAL_EQUAL);
239 INIT_TOKEN_ADV1 (EQUAL);
240 }
241 goto unknown;
242 }
243
244 // LABEL_DECL : IDENTIFIER
245 // IDENTIFIER include SYSCALL and label
246 // We don't want hash the SYSCALL, so leave it later
247 if (isalpha_l (peek (0), lc_c))
248 {
249 do
250 advance (1);
251 while (isidentifier (peek (0)));
252
253 INIT_TOKEN (match (':') ? LABEL_DECL : IDENTIFIER);
254 }
255
256 // NUMBER
257 if (isdigit_l (peek (0), lc_c))
258 {
259 char *end;
260 unsigned long num = strtoul (scanner.token_start, &end, 0);
261 if (scanner.token_start == end)
262 goto unknown;
263 #if __SIZEOF_POINTER__ == 8
264 static_assert (sizeof (unsigned long) == 8,
265 "64-bit machine has 32-bit long?");
266 if ((uint32_t)num != num)
267 INIT_TOKEN_ADV1 (OVERFLOW_NUMBER);
268 #else
269 #include <errno.h>
270 static_assert (sizeof (unsigned long) == 4,
271 "long is not 32-bit on 32-bit machine?");
272 if (num == ULONG_MAX && errno == ERANGE)
273 INIT_TOKEN_ADV1 (OVERFLOW_NUMBER);
274 #endif
275 scanner.current_char = end;
276 INIT_TOKEN_DATA (NUMBER, (uint32_t)num);
277 }
278
279 unknown:
280 // perhaps that's a wrong file? tell parser to stop using EOF
281 if (unknown_count > 5)
282 INIT_TOKEN (TOKEN_EOF);
283
284 unknown_count += 1;
285 advance (1);
286 INIT_TOKEN (UNKNOWN);
287 }