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 const struct token *tok_peek(const struct token **toks)
89 /* Ignore removed tokens (eg. comments) */
90 while (toks[0]->len == 0) {
98 static bool tok_is(const struct token **toks, const char *target)
100 const struct token *t = tok_peek(toks);
101 return (t && t->len == strlen(target)
102 && memcmp(t->p, target, t->len) == 0);
105 static const struct token *tok_take(const struct token **toks)
107 const struct token *t = tok_peek(toks);
114 static const struct token *tok_take_if(const struct token **toks,
117 if (tok_is(toks, target))
118 return tok_take(toks);
122 static const char *tok_take_ident(const tal_t *ctx, const struct token **toks)
124 const struct token *t = tok_peek(toks);
129 if (strspn(t->p, "_0123456789"
130 "abcdefghijklmnopqrstuvwxyz"
131 "ABCDEFGHIJKLMNOPQRSTUVWXYZ") < t->len)
135 return tal_strndup(ctx, t->p, t->len);
138 static char *string_of_toks(const tal_t *ctx,
139 const struct token *first,
140 const struct token *until)
144 /* Careful to skip erased tokens (eg. comments) */
145 str = p = tal_arr(ctx, char, until->p - first->p + 1);
146 while (first != until) {
147 const struct token *next = first + 1;
150 memcpy(p, first->p, first->len);
152 /* Insert space if they weren't adjacent, unless last */
154 if (first->p + first->len != next->p)
165 static char *tok_take_until(const tal_t *ctx,
166 const struct token **toks,
169 const struct token *t, *start;
171 start = tok_peek(toks);
172 while ((t = tok_peek(toks)) != NULL) {
173 /* If this contains a delimiter, copy up to prev token. */
174 if (strcspn(t->p, delims) < t->len)
175 return string_of_toks(ctx, start, t);
179 /* EOF without finding delimiter */
183 static bool type_defined(const struct cdump_type *t)
188 return (t->u.members != NULL);
190 return (t->u.enum_vals != NULL);
192 /* These shouldn't happen; we don't try to define them. */
201 /* May allocate a new type if not already found (steals @name) */
202 static struct cdump_type *get_type(struct cdump_definitions *defs,
203 enum cdump_type_kind kind,
207 struct cdump_type *t;
225 /* Do we already have it? */
227 t = strmap_get(m, name);
232 t = tal(defs, struct cdump_type);
234 t->name = name ? tal_steal(t, name) : NULL;
235 /* These are actually the same, but be thorough */
237 t->u.enum_vals = NULL;
239 strmap_add(m, t->name, t);
244 static void complain(struct parse_state *ps, const char *complaint)
246 unsigned int linenum;
247 const char *p = ps->code;
249 for (linenum = 1; p < ps->toks[0].p; linenum++) {
250 p = strchr(p+1, '\n');
255 tal_append_fmt(&ps->complaints,
256 "Line %u: '%.*s': %s\n",
257 linenum, (int)ps->toks[0].len,
258 ps->toks[0].p, complaint);
261 static void tok_take_unknown_statement(struct parse_state *ps)
263 complain(ps, "Ignoring unknown statement until next semicolon");
264 tal_free(tok_take_until(NULL, &ps->toks, ";"));
265 tok_take_if(&ps->toks, ";");
268 static bool tok_take_expr(struct parse_state *ps, const char *term)
270 while (!tok_is(&ps->toks, term)) {
271 if (tok_take_if(&ps->toks, "(")) {
272 if (!tok_take_expr(ps, ")"))
274 } else if (tok_take_if(&ps->toks, "[")) {
275 if (!tok_take_expr(ps, "]"))
277 } else if (!tok_take(&ps->toks))
280 return tok_take(&ps->toks);
283 static char *tok_take_expr_str(const tal_t *ctx,
284 struct parse_state *ps,
287 const struct token *start = tok_peek(&ps->toks);
289 if (!tok_take_expr(ps, term))
292 return string_of_toks(ctx, start, ps->toks - 1);
296 static bool tok_take_array(struct parse_state *ps, struct cdump_type **type)
298 /* This will be some arbitrary expression! */
299 struct cdump_type *arr = get_type(ps->defs, CDUMP_ARRAY, NULL);
301 arr->u.arr.size = tok_take_expr_str(arr, ps, "]");
302 if (!arr->u.arr.size) {
303 complain(ps, "Could not find closing array size ]");
307 arr->u.arr.type = *type;
313 static struct cdump_type *ptr_of(struct parse_state *ps,
314 const struct cdump_type *ptr_to)
316 struct cdump_type *ptr = get_type(ps->defs, CDUMP_POINTER, NULL);
321 static bool tok_take_type(struct parse_state *ps, struct cdump_type **type)
324 const struct token *types;
325 enum cdump_type_kind kind;
327 /* Ignoring weird typedefs, only these can be combined. */
329 while (tok_take_if(&ps->toks, "int")
330 || tok_take_if(&ps->toks, "long")
331 || tok_take_if(&ps->toks, "short")
332 || tok_take_if(&ps->toks, "double")
333 || tok_take_if(&ps->toks, "float")
334 || tok_take_if(&ps->toks, "char")
335 || tok_take_if(&ps->toks, "signed")
336 || tok_take_if(&ps->toks, "unsigned"));
338 /* Did we get some? */
339 if (ps->toks != types) {
340 name = string_of_toks(NULL, types, tok_peek(&ps->toks));
341 kind = CDUMP_UNKNOWN;
343 /* Try normal types (or simple typedefs, etc). */
344 if (tok_take_if(&ps->toks, "struct")) {
346 } else if (tok_take_if(&ps->toks, "union")) {
348 } else if (tok_take_if(&ps->toks, "enum")) {
351 kind = CDUMP_UNKNOWN;
353 name = tok_take_ident(ps->defs, &ps->toks);
355 complain(ps, "Invalid typename");
360 *type = get_type(ps->defs, kind, name);
365 static bool tok_maybe_take_cdump_note(const tal_t *ctx,
366 struct parse_state *ps, const char **note)
369 if (tok_take_if(&ps->toks, "CDUMP")) {
370 if (!tok_take_if(&ps->toks, "(")) {
371 complain(ps, "Expected ( after CDUMP");
374 *note = tok_take_expr_str(ctx, ps, ")");
376 complain(ps, "Expected ) after CDUMP(");
383 /* struct|union ... */
384 static bool tok_take_conglom(struct parse_state *ps,
385 enum cdump_type_kind conglom_kind)
387 struct cdump_type *e;
391 assert(conglom_kind == CDUMP_STRUCT || conglom_kind == CDUMP_UNION);
393 name = tok_take_ident(ps->defs, &ps->toks);
395 complain(ps, "Invalid struct/union name");
399 e = get_type(ps->defs, conglom_kind, name);
400 if (type_defined(e)) {
401 complain(ps, "Type already defined");
405 if (!tok_maybe_take_cdump_note(e, ps, &e->note))
408 if (!tok_take_if(&ps->toks, "{")) {
409 complain(ps, "Expected { for struct/union");
413 e->u.members = tal_arr(e, struct cdump_member, n = 0);
414 while (!tok_is(&ps->toks, "}")) {
415 struct cdump_type *basetype;
416 const struct token *quals;
417 unsigned int num_quals = 0;
419 /* Anything can have these prepended. */
421 while (tok_take_if(&ps->toks, "const")
422 || tok_take_if(&ps->toks, "volatile"))
425 /* eg. "struct foo" or "varint_t" */
426 if (!tok_take_type(ps, &basetype)) {
427 complain(ps, "Expected typename inside struct/union");
432 struct cdump_member *m;
434 tal_resize(&e->u.members, n+1);
435 m = &e->u.members[n++];
439 = string_of_toks(e, quals,
442 m->qualifiers = NULL;
444 /* May have multiple asterisks. */
445 while (tok_take_if(&ps->toks, "*"))
446 m->type = ptr_of(ps, m->type);
448 m->name = tok_take_ident(e, &ps->toks);
450 complain(ps, "Expected name for member");
454 /* May be an array. */
455 while (tok_take_if(&ps->toks, "[")) {
456 if (!tok_take_array(ps, &m->type))
461 if (!tok_maybe_take_cdump_note(e->u.members,
464 } while (tok_take_if(&ps->toks, ","));
466 if (!tok_take_if(&ps->toks, ";")) {
467 complain(ps, "Expected ; at end of member");
472 if (tok_take_if(&ps->toks, "}") && tok_take_if(&ps->toks, ";"))
474 complain(ps, "Expected }; at end of struct/union");
479 static bool tok_take_enum(struct parse_state *ps)
482 struct cdump_type *e;
485 name = tok_take_ident(ps->defs, &ps->toks);
487 complain(ps, "Expected enum name");
491 e = get_type(ps->defs, CDUMP_ENUM, name);
493 /* Duplicate name? */
494 if (type_defined(e)) {
495 complain(ps, "enum already defined");
500 if (!tok_maybe_take_cdump_note(e, ps, &e->note))
503 if (!tok_take_if(&ps->toks, "{")) {
504 complain(ps, "Expected { after enum name");
508 e->u.enum_vals = tal_arr(e, struct cdump_enum_val, n);
510 struct cdump_enum_val *v;
512 /* GCC extension: comma and end of enum */
513 if (tok_is(&ps->toks, "}"))
516 tal_resize(&e->u.enum_vals, n+1);
517 v = &e->u.enum_vals[n++];
519 v->name = tok_take_ident(e, &ps->toks);
521 complain(ps, "Expected enum value name");
526 if (!tok_maybe_take_cdump_note(e->u.enum_vals, ps, &v->note))
529 if (tok_take_if(&ps->toks, "=")) {
530 v->value = tok_take_until(e, &ps->toks, ",}");
532 complain(ps, "Expected , or } to end value");
537 } while (tok_take_if(&ps->toks, ","));
539 if (tok_take_if(&ps->toks, "}") && tok_take_if(&ps->toks, ";"))
542 complain(ps, "Expected }; at end of enum");
546 static bool gather_undefines(const char *name,
547 struct cdump_type *t,
548 struct cdump_map *undefs)
550 if (!type_defined(t))
551 strmap_add(undefs, name, t);
555 static bool remove_from_map(const char *name,
556 struct cdump_type *t,
557 struct cdump_map *map)
559 strmap_del(map, name, NULL);
563 static void remove_undefined(struct cdump_map *map)
565 struct cdump_map undefs;
567 /* We can't delete inside iterator, so gather all the undefs
568 * then remove them. */
569 strmap_init(&undefs);
571 strmap_iterate(map, gather_undefines, &undefs);
572 strmap_iterate(&undefs, remove_from_map, map);
573 strmap_clear(&undefs);
576 static void destroy_definitions(struct cdump_definitions *defs)
578 strmap_clear(&defs->enums);
579 strmap_clear(&defs->structs);
580 strmap_clear(&defs->unions);
583 /* Simple LL(1) parser, inspired by Tridge's genstruct.pl. */
584 struct cdump_definitions *cdump_extract(const tal_t *ctx, const char *code,
587 struct parse_state ps;
588 const struct token *toks;
590 ps.defs = tal(ctx, struct cdump_definitions);
591 ps.complaints = tal_strdup(ctx, "");
594 strmap_init(&ps.defs->enums);
595 strmap_init(&ps.defs->structs);
596 strmap_init(&ps.defs->unions);
597 tal_add_destructor(ps.defs, destroy_definitions);
599 toks = ps.toks = tokenize(ps.defs, code);
600 while (tok_peek(&ps.toks)) {
601 if (tok_take_if(&ps.toks, "struct")) {
602 if (!tok_take_conglom(&ps, CDUMP_STRUCT))
604 } else if (tok_take_if(&ps.toks, "union")) {
605 if (!tok_take_conglom(&ps, CDUMP_UNION))
607 } else if (tok_take_if(&ps.toks, "enum")) {
608 if (!tok_take_enum(&ps))
611 tok_take_unknown_statement(&ps);
614 /* Now, remove any undefined types! */
615 remove_undefined(&ps.defs->enums);
616 remove_undefined(&ps.defs->structs);
617 remove_undefined(&ps.defs->unions);
621 if (streq(ps.complaints, ""))
622 ps.complaints = tal_free(ps.complaints);
625 *complaints = ps.complaints;
627 tal_free(ps.complaints);
631 ps.defs = tal_free(ps.defs);