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
|
/*
* Copyright (c) 2026, Apollo Telephone Laboratories.
* Provided under the BSD-3 clause.
*/
#include "defconf/parser.h"
#include "defconf/lexer.h"
#include "defconf/token.h"
#include "defconf/data.h"
#include "defconf/emit.h"
#include "defconf/trace.h"
/* Symbolic token */
#define SYMTOK(TOKSTR) \
"[" TOKSTR "]"
/* Literal token */
#define TOKLIT(TOKSTR) \
"'" TOKSTR "'"
/* Safe macro to convert token type to string */
#define TOKSTR(TT) \
((TT) >= _TT_MAX) \
? "bad" \
: toktab[(TT)]
/* Token to string table */
static const char *toktab[_TT_MAX] = {
[TT_NONE] = SYMTOK("none"),
[TT_DEFINE] = SYMTOK("define"),
[TT_TRUE] = TOKLIT("true"),
[TT_FALSE] = TOKLIT("false"),
[TT_INTLIT] = SYMTOK("int"),
[TT_IDENT] = SYMTOK("ident")
};
static int
token_to_data(struct token *tok, struct data_value *result)
{
if (tok == NULL || result == NULL) {
return -1;
}
switch (tok->type) {
case TT_TRUE:
result->v = 1;
result->type = DATA_TYPE_INT;
return 0;
case TT_FALSE:
result->v = 0;
result->type = DATA_TYPE_INT;
return 0;
case TT_INTLIT:
result->v = tok->v;
result->type = DATA_TYPE_INT;
return 0;
default:
return -1;
}
return -1;
}
/*
* Assert that the next token is of a specific type
*
* @state: Defconf state
* @tok: Last token
* @exp: Expected token
*/
static int
parse_expect(struct defconf_state *state, struct token *tok, tt_t exp)
{
int error;
if (state == NULL || tok == NULL) {
return -1;
}
/* Try to grab the next token */
if ((error = lexer_scan(state, tok)) < 0) {
trace_fatal(state, "expected '%s', got eof\n", TOKSTR(exp));
return error;
}
if (tok->type != exp) {
trace_fatal(state, "expected '%s', got %s instead\n",
TOKSTR(exp), TOKSTR(tok->type));
return -1;
}
return 0;
}
/*
* Parse a define directive
*
* @state: Defconf state
* @tok: Last token
*
* Returns zero on success
*/
static int
parse_define(struct defconf_state *state, struct token *tok)
{
struct data_value value;
char *ident;
int error;
if (state == NULL || tok == NULL) {
return -1;
}
/* Expect an identifier */
if ((error = parse_expect(state, tok, TT_IDENT)) < 0) {
return error;
}
/* Save the identifier */
ident = tok->s;
/* Scan the value */
if ((error = lexer_scan(state, tok)) < 0) {
trace_fatal(state, "expected value, got eof\n");
return error;
}
/* Convert token to data */
if ((error = token_to_data(tok, &value)) < 0) {
trace_fatal(state, "got bad data [%s]\n", TOKSTR(tok->type));
return error;
}
/* Emit the define */
if ((error = emit_data(ident, &value)) < 0) {
return error;
}
return 0;
}
int
parse_begin(struct defconf_state *state)
{
struct token tok;
int error;
if (state == NULL) {
return -1;
}
while (lexer_scan(state, &tok) == 0) {
switch (tok.type) {
case TT_DEFINE:
if ((error = parse_define(state, &tok)) < 0) {
return error;
}
break;
default:
trace_fatal(state, "got unexpected token: %s\n", TOKSTR(tok.type));
return -1;
}
}
return 0;
}
|