1 /* MIT (BSD) license - see LICENSE file for details */
3 #include <ccan/tal/str/str.h>
11 static void add_token(struct token **toks, const char *p, size_t len)
13 size_t n = tal_count(*toks);
14 tal_resize(toks, n+1);
19 /* Simplified tokenizer: comments and preproc directives removed,
20 identifiers are a token, others are single char tokens. */
21 static struct token *tokenize(const void *ctx, const char *code)
23 unsigned int i, len, tok_start = -1;
24 bool start_of_line = true;
25 struct token *toks = tal_arr(ctx, struct token, 0);
27 for (i = 0; code[i]; i += len) {
28 if (code[i] == '#' && start_of_line) {
29 /* Preprocessor line. */
30 len = strcspn(code+i, "\n");
31 } else if (code[i] == '/' && code[i+1] == '/') {
32 /* One line comment. */
33 len = strcspn(code+i, "\n");
34 if (tok_start != -1U) {
35 add_token(&toks, code+tok_start, i - tok_start);
38 } else if (code[i] == '/' && code[i+1] == '*') {
39 /* Multi-line comment. */
40 const char *end = strstr(code+i+2, "*/");
41 len = (end + 2) - (code + i);
43 len = strlen(code + i);
44 if (tok_start != -1U) {
45 add_token(&toks, code+tok_start, i - tok_start);
48 } else if (cisalnum(code[i]) || code[i] == '_') {
49 /* Identifier or part thereof */
53 } else if (!cisspace(code[i])) {
54 /* Punctuation: treat as single char token. */
55 if (tok_start != -1U) {
56 add_token(&toks, code+tok_start, i - tok_start);
59 add_token(&toks, code+i, 1);
63 if (tok_start != -1U) {
64 add_token(&toks, code+tok_start, i - tok_start);
71 else if (!cisspace(code[i]))
72 start_of_line = false;
75 /* Add terminating NULL. */
76 tal_resizez(&toks, tal_count(toks) + 1);
82 const struct token *toks;
83 struct cdump_definitions *defs;
87 static bool tok_is(const struct token **toks, const char *target)
89 return (*toks)->p && (*toks)->len == strlen(target)
90 && memcmp((*toks)->p, target, (*toks)->len) == 0;
93 static const struct token *tok_peek(const struct token **toks)
100 static const struct token *tok_take(const struct token **toks)
108 static const struct token *tok_take_if(const struct token **toks,
111 if (tok_is(toks, target))
112 return tok_take(toks);
116 static const char *tok_take_ident(const tal_t *ctx, const struct token **toks)
118 const struct token *t = tok_peek(toks);
123 if (strspn(t->p, "_0123456789"
124 "abcdefghijklmnopqrstuvwxyz"
125 "ABCDEFGHIJKLMNOPQRSTUVWXYZ") < t->len)
129 return tal_strndup(ctx, t->p, t->len);
132 static char *string_of_toks(const tal_t *ctx,
133 const struct token *first,
134 const struct token *until)
136 const struct token *end = until - 1;
137 return tal_strndup(ctx, first->p, end->p - first->p + end->len);
140 static char *tok_take_until(const tal_t *ctx,
141 const struct token **toks,
144 const struct token *t, *start;
146 start = tok_peek(toks);
147 while ((t = tok_peek(toks)) != NULL) {
148 /* If this contains a delimiter, copy up to prev token. */
149 if (strcspn(t->p, delims) < t->len)
150 return string_of_toks(ctx, start, t);
154 /* EOF without finding delimiter */
158 static bool type_defined(const struct cdump_type *t)
163 return (t->u.members != NULL);
165 return (t->u.enum_vals != NULL);
167 /* These shouldn't happen; we don't try to define them. */
176 /* May allocate a new type if not already found (steals @name) */
177 static struct cdump_type *get_type(struct cdump_definitions *defs,
178 enum cdump_type_kind kind,
182 struct cdump_type *t;
200 /* Do we already have it? */
202 t = strmap_get(m, name);
207 t = tal(defs, struct cdump_type);
209 t->name = name ? tal_steal(t, name) : NULL;
210 /* These are actually the same, but be thorough */
212 t->u.enum_vals = NULL;
214 strmap_add(m, t->name, t);
219 static void complain(struct parse_state *ps, const char *complaint)
221 unsigned int linenum;
222 const char *p = ps->code;
224 for (linenum = 1; p < ps->toks[0].p; linenum++) {
225 p = strchr(p+1, '\n');
230 tal_append_fmt(&ps->complaints,
231 "Line %u: '%.*s': %s\n",
232 linenum, (int)ps->toks[0].len,
233 ps->toks[0].p, complaint);
236 static void tok_take_unknown_statement(struct parse_state *ps)
238 complain(ps, "Ignoring unknown statement until next semicolon");
239 tal_free(tok_take_until(NULL, &ps->toks, ";"));
240 tok_take_if(&ps->toks, ";");
244 static bool tok_take_array(struct parse_state *ps, struct cdump_type **type)
246 /* This will be some arbitrary expression! */
247 struct cdump_type *arr = get_type(ps->defs, CDUMP_ARRAY, NULL);
249 arr->u.arr.size = tok_take_until(arr, &ps->toks, "]");
250 if (!arr->u.arr.size) {
251 complain(ps, "Could not find closing array size ]");
255 arr->u.arr.type = *type;
263 static struct cdump_type *ptr_of(struct parse_state *ps,
264 const struct cdump_type *ptr_to)
266 struct cdump_type *ptr = get_type(ps->defs, CDUMP_POINTER, NULL);
271 static bool tok_take_type(struct parse_state *ps, struct cdump_type **type)
274 const struct token *types;
275 enum cdump_type_kind kind;
277 /* Ignoring weird typedefs, only these can be combined. */
279 while (tok_take_if(&ps->toks, "int")
280 || tok_take_if(&ps->toks, "long")
281 || tok_take_if(&ps->toks, "short")
282 || tok_take_if(&ps->toks, "double")
283 || tok_take_if(&ps->toks, "float")
284 || tok_take_if(&ps->toks, "char")
285 || tok_take_if(&ps->toks, "signed")
286 || tok_take_if(&ps->toks, "unsigned"));
288 /* Did we get some? */
289 if (ps->toks != types) {
290 name = string_of_toks(NULL, types, tok_peek(&ps->toks));
291 kind = CDUMP_UNKNOWN;
293 /* Try normal types (or simple typedefs, etc). */
294 if (tok_take_if(&ps->toks, "struct")) {
296 } else if (tok_take_if(&ps->toks, "union")) {
298 } else if (tok_take_if(&ps->toks, "enum")) {
301 kind = CDUMP_UNKNOWN;
303 name = tok_take_ident(ps->defs, &ps->toks);
305 complain(ps, "Invalid typename");
310 *type = get_type(ps->defs, kind, name);
314 /* struct|union ... */
315 static bool tok_take_conglom(struct parse_state *ps,
316 enum cdump_type_kind conglom_kind)
318 struct cdump_type *e;
322 assert(conglom_kind == CDUMP_STRUCT || conglom_kind == CDUMP_UNION);
324 name = tok_take_ident(ps->defs, &ps->toks);
326 complain(ps, "Invalid struct/union name");
330 e = get_type(ps->defs, conglom_kind, name);
331 if (type_defined(e)) {
332 complain(ps, "Type already defined");
336 if (!tok_take_if(&ps->toks, "{")) {
337 complain(ps, "Expected { for struct/union");
341 e->u.members = tal_arr(e, struct cdump_member, n = 0);
342 while (!tok_is(&ps->toks, "}")) {
343 struct cdump_type *basetype;
344 const struct token *quals;
345 unsigned int num_quals = 0;
347 /* Anything can have these prepended. */
349 while (tok_take_if(&ps->toks, "const")
350 || tok_take_if(&ps->toks, "volatile"))
353 /* eg. "struct foo" or "varint_t" */
354 if (!tok_take_type(ps, &basetype)) {
355 complain(ps, "Expected typename inside struct/union");
360 struct cdump_member *m;
362 tal_resize(&e->u.members, n+1);
363 m = &e->u.members[n++];
367 = string_of_toks(e, quals,
370 m->qualifiers = NULL;
372 /* May have multiple asterisks. */
373 while (tok_take_if(&ps->toks, "*"))
374 m->type = ptr_of(ps, m->type);
376 m->name = tok_take_ident(e, &ps->toks);
378 complain(ps, "Expected name for member");
382 /* May be an array. */
383 while (tok_take_if(&ps->toks, "[")) {
384 if (!tok_take_array(ps, &m->type))
387 } while (tok_take_if(&ps->toks, ","));
389 if (!tok_take_if(&ps->toks, ";")) {
390 complain(ps, "Expected ; at end of member");
395 if (tok_take_if(&ps->toks, "}") && tok_take_if(&ps->toks, ";"))
397 complain(ps, "Expected }; at end of struct/union");
402 static bool tok_take_enum(struct parse_state *ps)
405 struct cdump_type *e;
408 name = tok_take_ident(ps->defs, &ps->toks);
410 complain(ps, "Expected enum name");
414 e = get_type(ps->defs, CDUMP_ENUM, name);
416 /* Duplicate name? */
417 if (type_defined(e)) {
418 complain(ps, "enum already defined");
422 if (!tok_take_if(&ps->toks, "{")) {
423 complain(ps, "Expected { after enum name");
427 e->u.enum_vals = tal_arr(e, struct cdump_enum_val, n);
429 struct cdump_enum_val *v;
431 /* GCC extension: comma and end of enum */
432 if (tok_is(&ps->toks, "}"))
435 tal_resize(&e->u.enum_vals, n+1);
436 v = &e->u.enum_vals[n++];
438 v->name = tok_take_ident(e, &ps->toks);
440 complain(ps, "Expected enum value name");
443 if (tok_take_if(&ps->toks, "=")) {
444 v->value = tok_take_until(e, &ps->toks, ",}");
446 complain(ps, "Expected , or } to end value");
451 } while (tok_take_if(&ps->toks, ","));
453 if (tok_take_if(&ps->toks, "}") && tok_take_if(&ps->toks, ";"))
456 complain(ps, "Expected }; at end of enum");
460 static bool gather_undefines(const char *name,
461 struct cdump_type *t,
462 struct cdump_map *undefs)
464 if (!type_defined(t))
465 strmap_add(undefs, name, t);
469 static bool remove_from_map(const char *name,
470 struct cdump_type *t,
471 struct cdump_map *map)
473 strmap_del(map, name, NULL);
477 static void remove_undefined(struct cdump_map *map)
479 struct cdump_map undefs;
481 /* We can't delete inside iterator, so gather all the undefs
482 * then remove them. */
483 strmap_init(&undefs);
485 strmap_iterate(map, gather_undefines, &undefs);
486 strmap_iterate(&undefs, remove_from_map, map);
487 strmap_clear(&undefs);
490 static void destroy_definitions(struct cdump_definitions *defs)
492 strmap_clear(&defs->enums);
493 strmap_clear(&defs->structs);
494 strmap_clear(&defs->unions);
497 /* Simple LL(1) parser, inspired by Tridge's genstruct.pl. */
498 struct cdump_definitions *cdump_extract(const tal_t *ctx, const char *code,
501 struct parse_state ps;
502 const struct token *toks;
504 ps.defs = tal(ctx, struct cdump_definitions);
505 ps.complaints = tal_strdup(ctx, "");
508 strmap_init(&ps.defs->enums);
509 strmap_init(&ps.defs->structs);
510 strmap_init(&ps.defs->unions);
511 tal_add_destructor(ps.defs, destroy_definitions);
513 toks = ps.toks = tokenize(ps.defs, code);
514 while (tok_peek(&ps.toks)) {
515 if (tok_take_if(&ps.toks, "struct")) {
516 if (!tok_take_conglom(&ps, CDUMP_STRUCT))
518 } else if (tok_take_if(&ps.toks, "union")) {
519 if (!tok_take_conglom(&ps, CDUMP_UNION))
521 } else if (tok_take_if(&ps.toks, "enum")) {
522 if (!tok_take_enum(&ps))
525 tok_take_unknown_statement(&ps);
528 /* Now, remove any undefined types! */
529 remove_undefined(&ps.defs->enums);
530 remove_undefined(&ps.defs->structs);
531 remove_undefined(&ps.defs->unions);
535 if (streq(ps.complaints, ""))
536 ps.complaints = tal_free(ps.complaints);
539 *complaints = ps.complaints;
541 tal_free(ps.complaints);
545 ps.defs = tal_free(ps.defs);