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
|
/*
* Copyright (c) 2026, Apollo Telephone Laboratories.
* Provided under the BSD-3 clause.
*/
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "defconf/lexer.h"
#include "defconf/ptrbox.h"
#include "defconf/trace.h"
/* Constants */
#define MAX_KW_LEN 32
#define MAX_NUM_LEN 20
/*
* Returns true if the given character is a whitespace character
*
* @c: Character to check
*/
static inline bool
lexer_is_ws(char c)
{
switch (c) {
case '\t':
case ' ':
case '\f':
case '\r':
case '\n':
return true;
}
return false;
}
/*
* Place a character back in the putback buffer
*
* @state: Defconf state
* @c: Character to put back
*/
static inline void
lexer_putback(struct defconf_state *state, int c)
{
if (state == NULL) {
return;
}
state->lex_cache = c;
}
/*
* Nom a single character from the input source file
*
* @state: Defconf state machine
* @skip_ws: If true, skip whitespace
*
* Returns the character on success, otherwise EOF on failure
*/
static int
lexer_nom(struct defconf_state *state, bool skip_ws)
{
int c;
if (state == NULL) {
return EOF;
}
if ((c = state->lex_cache) != '\0') {
state->lex_cache = '\0';
if (lexer_is_ws(c) && !skip_ws)
return c;
if (!lexer_is_ws(c))
return c;
}
while ((c = fgetc(state->in_fp)) != EOF) {
if (lexer_is_ws(c) && skip_ws)
continue;
break;
}
return c;
}
/*
* Scan a keyword
*
* @state: Defconf state
* @lc: Last character provided
*/
static char *
lexer_scan_ident(struct defconf_state *state, int lc)
{
char c, buf[MAX_KW_LEN];
size_t bufind = 0;
if (state == NULL) {
return NULL;
}
if (!isalpha(lc)) {
return NULL;
}
buf[bufind++] = lc;
for (;;) {
if (bufind >= sizeof(buf) - 1) {
trace_fatal(state, "token exceeds maximum length\n");
return NULL;
}
c = lexer_nom(state, false);
if (!isalnum(c) && c != '_') {
lexer_putback(state, c);
buf[bufind] = '\0';
return ptrbox_strdup(&state->ptrbox, buf);
}
buf[bufind++] = c;
}
return NULL;
}
/*
* Scan a number
*
* @state: Defconf state
* @lc: Last character provided
* @result: Result is written here
*/
static int
lexer_scan_num(struct defconf_state *state, int lc, struct token *result)
{
char c, buf[MAX_NUM_LEN + 1];
size_t bufind = 0;
if (state == NULL || result == NULL) {
return -1;
}
if (!isdigit(lc)) {
return -1;
}
buf[bufind++] = lc;
for (;;) {
if (bufind >= sizeof(buf) - 1) {
trace_fatal(state, "digits exceed maximum length\n");
return -1;
}
c = lexer_nom(state, false);
if (!isdigit(c)) {
lexer_putback(state, c);
buf[bufind] = '\0';
/* Set the token */
result->type = TT_INTLIT;
result->v = atoi(buf);
return 0;
}
buf[bufind++] = c;
}
return -1;
}
/*
* Check if a keyword matches against known keywords
*
* @kw: Keyword to check
* @result: Result is written here
*
* Returns zero on success
*/
static int
lexer_check_kw(const char *kw, struct token *result)
{
if (kw == NULL || result == NULL) {
return -1;
}
switch (*kw) {
case 'd':
if (strcmp(kw, "define") == 0) {
result->type = TT_DEFINE;
return 0;
}
break;
case 't':
if (strcmp(kw, "true") == 0) {
result->type = TT_TRUE;
return 0;
}
break;
case 'f':
if (strcmp(kw, "false") == 0) {
result->type = TT_FALSE;
return 0;
}
break;
}
return -1;
}
int
lexer_scan(struct defconf_state *state, struct token *result)
{
int c;
char *ident;
int error;
if (state == NULL || result == NULL) {
return -1;
}
if ((c = lexer_nom(state, true)) == EOF) {
return -1;
}
/* Scan for an identifier */
if ((ident = lexer_scan_ident(state, c)) != NULL) {
if (lexer_check_kw(ident, result) == 0) {
return 0;
}
result->type = TT_IDENT;
result->s = ident;
return 0;
}
/* Is this a digit? */
if (isdigit(c)) {
error = lexer_scan_num(state, c, result);
if (error < 0) {
return -1;
}
return 0;
}
trace_fatal(state, "unexpected token '%c'\n", c);
return -1;
}
|