-
Notifications
You must be signed in to change notification settings - Fork 0
/
condparser.h
326 lines (267 loc) · 9.37 KB
/
condparser.h
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
#ifndef CONDPARSER_H_INCLUDED
#define CONDPARSER_H_INCLUDED
/*
condparser.h - A simple logical expression parser
The parser supports the following operators:
- && (AND)
- || (OR)
- ! (NOT)
The parser supports parentheses and identifiers, which are looked up using a callback function.
It's reentrant, does not use any global variables and does not allocate memory.
Constants are not supported, but you can use identifiers to represent them.
I use this parser in my game engine to toggle runtime configuration options based on platform, build configuration, etc.
USAGE
==================================================
Do this:
#define CONDPARSER_IMPLEMENTATION
before you include this file in *one* C or C++ file to create the implementation.
You need to define two callback functions to use the parser:
- bool getValue(const char* id): This function should return the value of an identifier.
- void error(const char* message): This function is called to output an error message. Do not add newlines.
The only API function is:
bool condParserEvaluate(const char* expr, PFN_condParserGetValue getValue, PFN_condParserError errorFn);
It takes a logical expression, a callback function to get the value of an identifier, and a callback function to output errors,
and returns the result of the expression.
You can define the following macros to customise the parser:
- CONDPARSER_STRNCMP: The string comparison function to use. Default: strncmp
- CONDPARSER_ID_LENGTH: The maximum length of an identifier. Default: 32
string.h is only included if CONDPARSER_STRNCMP is not defined.
LICENSE
==================================================
zlib/libpng license
Copyright (c) 2024 Andrei Tsurkan
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in a
product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <stdbool.h>
typedef bool(*PFN_condParserGetValue)(const char*);
typedef void (*PFN_condParserError)(const char*);
#ifdef __cplusplus
extern "C" {
#endif
bool condParserEvaluate(const char* expr, PFN_condParserGetValue getValue, PFN_condParserError errorFn);
#ifdef __cplusplus
}
#endif
#ifdef CONDPARSER_IMPLEMENTATION
#ifndef CONDPARSER_STRNCMP
#include <string.h>
#define CONDPARSER_STRNCMP strncmp
#endif
#ifndef CONDPARSER_ID_LENGTH
#define CONDPARSER_ID_LENGTH 32
#endif
typedef enum
{
CondParserToken_ID,
CondParserToken_LParen,
CondParserToken_RParen,
CondParserToken_Not,
CondParserToken_And,
CondParserToken_Or,
CondParserToken_End
} CondParserTokenType;
typedef struct
{
CondParserTokenType type;
char id[CONDPARSER_ID_LENGTH];
} CondParserToken;
typedef struct
{
const char* cur;
CondParserToken curToken;
bool error;
PFN_condParserGetValue getValue;
PFN_condParserError errorFn;
} CondParserContext;
#ifdef __cplusplus
extern "C" {
#endif
static bool condParserIsSpace(char c)
{
return (c == ' ') || (c == '\t') || (c == '\n') || (c == '\v') || (c == '\f') || (c == '\r');
}
static bool condParserIsAlpha(char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
static bool condParserIsAlnum(char c)
{
return condParserIsAlpha(c) || (c >= '0' && c <= '9');
}
static void condParserPrintError(const CondParserContext* ctx, const char* msg)
{
if (ctx->errorFn)
{
ctx->errorFn(msg);
}
}
static void condParserPrintToken(const CondParserContext* ctx)
{
switch (ctx->curToken.type)
{
case CondParserToken_ID:
condParserPrintError(ctx, "ID [");
condParserPrintError(ctx, ctx->curToken.id);
condParserPrintError(ctx, "]");
break;
case CondParserToken_LParen:
condParserPrintError(ctx, "LPAREN");
break;
case CondParserToken_RParen:
condParserPrintError(ctx, "RPAREN");
break;
case CondParserToken_Not:
condParserPrintError(ctx, "NOT");
break;
case CondParserToken_And:
condParserPrintError(ctx, "AND");
break;
case CondParserToken_Or:
condParserPrintError(ctx, "OR");
break;
case CondParserToken_End:
condParserPrintError(ctx, "END");
break;
default:
condParserPrintError(ctx, "<<UNKNOWN TOKEN>>");
break;
}
}
static void condParserNextToken(CondParserContext* ctx)
{
// skip ws
while (condParserIsSpace(*ctx->cur)) ctx->cur++;
if (*ctx->cur == '\0') {
ctx->curToken.type = CondParserToken_End;
}
else if (CONDPARSER_STRNCMP(ctx->cur, "&&", 2) == 0) {
ctx->curToken.type = CondParserToken_And;
ctx->cur += 2;
}
else if (CONDPARSER_STRNCMP(ctx->cur, "||", 2) == 0) {
ctx->curToken.type = CondParserToken_Or;
ctx->cur += 2;
}
else if (*ctx->cur == '!') {
ctx->curToken.type = CondParserToken_Not;
ctx->cur++;
}
else if (*ctx->cur == '(') {
ctx->curToken.type = CondParserToken_LParen;
ctx->cur++;
}
else if (*ctx->cur == ')') {
ctx->curToken.type = CondParserToken_RParen;
ctx->cur++;
}
else if (condParserIsAlpha(*ctx->cur)) {
ctx->curToken.type = CondParserToken_ID;
int i = 0;
while (condParserIsAlnum(*ctx->cur) && i < (CONDPARSER_ID_LENGTH - 1)) {
ctx->curToken.id[i++] = *ctx->cur++;
}
ctx->curToken.id[i] = '\0';
}
else {
const char ctxCur[2] = { *ctx->cur, '\0' };
condParserPrintError(ctx, "Unknown character: ");
condParserPrintError(ctx, ctxCur);
condParserPrintError(ctx, "\n");
ctx->cur++;
ctx->error = true;
}
}
static bool condParserParseExpr(CondParserContext* ctx);
static bool condParserParsePrimary(CondParserContext* ctx)
{
if (ctx->curToken.type == CondParserToken_ID) {
bool value = ctx->getValue(ctx->curToken.id);
condParserNextToken(ctx);
return value;
}
else if (ctx->curToken.type == CondParserToken_LParen) {
condParserNextToken(ctx); // consume '('
bool value = condParserParseExpr(ctx);
if (ctx->curToken.type != CondParserToken_RParen) {
condParserPrintError(ctx, "Error: expected ')', found: ");
condParserPrintToken(ctx);
condParserPrintError(ctx, "\n");
return 0;
}
condParserNextToken(ctx); // consume ')'
return value;
}
else {
condParserPrintError(ctx, "Error: expected identifier or '('\n");
return 0;
}
}
static bool condParserParseNot(CondParserContext* ctx)
{
int notCount = 0;
// count nots
while (ctx->curToken.type == CondParserToken_Not)
{
notCount++;
condParserNextToken(ctx);
}
bool res = condParserParsePrimary(ctx);
// negate if odd
if (notCount % 2 != 0)
{
res = !res;
}
return res;
}
static bool condParserParseAnd(CondParserContext* ctx)
{
bool value = condParserParseNot(ctx);
while (ctx->curToken.type == CondParserToken_And) {
condParserNextToken(ctx);
bool res = condParserParseNot(ctx);
value = value && res;
}
return value;
}
static bool condParserParseOr(CondParserContext* ctx)
{
bool value = condParserParseAnd(ctx);
while (ctx->curToken.type == CondParserToken_Or) {
condParserNextToken(ctx);
bool res = condParserParseAnd(ctx);
value = value || res;
}
return value;
}
static bool condParserParseExpr(CondParserContext* ctx)
{
bool res = condParserParseOr(ctx);
return res;
}
bool condParserEvaluate(const char* expr, PFN_condParserGetValue getValue, PFN_condParserError errorFn)
{
CondParserContext ctx;
ctx.cur = expr;
ctx.error = false;
ctx.getValue = getValue;
ctx.errorFn = errorFn;
condParserNextToken(&ctx);
return condParserParseExpr(&ctx);
}
#ifdef __cplusplus
}
#endif
#endif // CONDPARSER_IMPLEMENTATION
#endif // CONDPARSER_H_INCLUDED