1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
/*
* Copyright (C) 2026 Peter Mikkelsen <petermikkelsen10@gmail.com>
*
* This file is part of aplwc.
*
* Aplwc is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Aplwc is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with aplwc. If not, see <https://www.gnu.org/licenses/>.
*/
#include "aplwc_internal.h"
struct scan_state {
struct string *text;
size_t offset;
int skip_number_and_identifier;
};
static void
init_scan_state(struct scan_state *state, struct string *text)
{
state->text = text;
state->offset = 0;
state->skip_number_and_identifier = 0;
}
static int
is_space(uint32_t c)
{
return c == ' ';
}
static int
is_letter(uint32_t c)
{
return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
}
static int
is_digit(uint32_t c)
{
return (c >= '0') && (c <= '9');
}
static uint32_t
peek(struct scan_state *state)
{
return string_char(state->text, state->offset);
}
static uint32_t
advance(struct scan_state *state)
{
state->offset += char_length(state->text, state->offset);
state->skip_number_and_identifier = 0;
return peek(state);
}
static void
skip_space(struct scan_state *state)
{
while(is_space(peek(state)))
advance(state);
}
static int
single_sym(struct scan_state *state, struct token *token)
{
uint32_t c;
c = peek(state);
switch(c){
case 0x2190: token->tag = TOKEN_TAG_LEFT_ARROW; break;
case 0x22C4: token->tag = TOKEN_TAG_DIAMOND; break;
case 0x2207: token->tag = TOKEN_TAG_DEL; break;
case 0x2336: token->tag = TOKEN_TAG_IBEAM; break;
case 0x2395: token->tag = TOKEN_TAG_QUAD; break;
case '\n': token->tag = TOKEN_TAG_NEWLINE; break;
case '(': token->tag = TOKEN_TAG_LEFT_PAREN; break;
case ')': token->tag = TOKEN_TAG_RIGHT_PAREN; break;
default: token->tag = TOKEN_TAG_UNKNOWN_SYM; break;
}
advance(state);
return 1;
}
static int
identifier(struct scan_state *state, struct token *token)
{
uint32_t c;
if(state->skip_number_and_identifier || (!is_letter(peek(state))))
return 0;
do
c = advance(state);
while(is_letter(c) || is_digit(c) || (c == '_'));
token->tag = TOKEN_TAG_IDENTIFIER;
state->skip_number_and_identifier = 1;
return 1;
}
static int
keyword(struct scan_state *state, struct token *token)
{
uint32_t c;
if(peek(state) != ':')
return 0;
do
c = advance(state);
while(is_letter(c));
token->tag = TOKEN_TAG_KEYWORD;
return 1;
}
static int
number(struct scan_state *state, struct token *token)
{
uint32_t c;
int has_digits;
c = peek(state);
if(state->skip_number_and_identifier)
return 0;
has_digits = 0;
if(c == 0xAF) /* High minus */
c = advance(state);
if(is_digit(c)){ /* integer part */
has_digits = 1;
do
c = advance(state);
while(is_digit(c));
}
if(c == '.'){
c = advance(state);
if(is_digit(c)){ /* fractional part */
has_digits = 1;
do
c = advance(state);
while(is_digit(c));
}
}
if(has_digits == 0)
return 0;
token->tag = TOKEN_TAG_NUMBER;
state->skip_number_and_identifier = 1;
return 1;
}
static int
text(struct scan_state *state, struct token *token)
{
uint32_t c;
c = peek(state);
if(c != '\'')
return 0;
do
c = advance(state);
while((c != '\n') && (c != 0) && (c != '\''));
if(c == '\'')
advance(state);
else
return 0;
token->tag = TOKEN_TAG_TEXT;
return 1;
}
static int
comment(struct scan_state *state, struct token *token)
{
uint32_t c;
c = peek(state);
if(c != 0x235D)
return 0;
do
c = advance(state);
while(c != '\n');
token->tag = TOKEN_TAG_COMMENT;
return 1;
}
static enum aplwc_rc
next_token(struct scan_state *state, struct token *token)
{
size_t i;
int (*scans[])(struct scan_state *, struct token *token) = {
identifier,
keyword,
number,
text,
comment,
single_sym /* Must be last */
};
skip_space(state);
token->start = state->offset;
for(i = 0; i < NELEM(scans); i++){
if(scans[i](state, token))
break;
state->offset = token->start;
}
token->end = state->offset;
skip_space(state);
if(token->start == token->end)
return APLWC_INVALID_SYNTAX;
else
return APLWC_OK;
}
static enum aplwc_rc
check(struct string *text, size_t *n)
{
struct scan_state state;
struct token token;
enum aplwc_rc ret;
size_t count;
init_scan_state(&state, text);
for(count = 0; peek(&state); count++){
ret = next_token(&state, &token);
if(ret != APLWC_OK)
return ret;
}
*n = count;
return APLWC_OK;
}
static void
scan(struct string *text, struct token_list *tokens)
{
struct scan_state state;
enum aplwc_rc ret;
size_t count;
init_scan_state(&state, text);
for(count = 0; peek(&state); count++){
ret = next_token(&state, &tokens->tokens[count]);
assert(ret == APLWC_OK);
}
}
struct token_list *
scan_string(struct aplwc *aplwc, struct string *text, int oneline)
{
enum aplwc_rc rc;
struct token_list *tokens;
size_t n, i;
MEMPTRS(aplwc, 1);
MEMPTR(0, text);
tokens = NULL;
rc = check(text, &n);
if(rc != APLWC_OK)
goto end;
tokens = memory_alloc_token_list(aplwc, n);
if(tokens == NULL)
goto end;
tokens->text = text;
tokens->length = n;
scan(text, tokens);
for(i = 0; oneline && (i < n); i++){
if(tokens->tokens[i].tag != TOKEN_TAG_NEWLINE)
continue;
for(i++; i < n; i++){
if(tokens->tokens[i].tag != TOKEN_TAG_NEWLINE){
rc = APLWC_INVALID_SYNTAX;
tokens = NULL;
goto end;
}
}
}
end:
aplwc->last_error = rc;
MEMPTRS_EXIT(aplwc);
return tokens;
}
enum aplwc_rc
check_tokens(struct token_list *tokens)
{
size_t i;
enum aplwc_rc rc;
for(i = 0, rc = APLWC_OK; (rc == APLWC_OK) && (i < tokens->length); i++){
switch(tokens->tokens[i].tag){
case TOKEN_TAG_UNKNOWN_SYM:
rc = APLWC_INVALID_SYNTAX;
break;
default:
break;
}
}
return rc;
}
|