Simple LL(1) parser, inspired by Tridge's genstruct.pl. */
| 646 | |
| 647 | /* Simple LL(1) parser, inspired by Tridge's genstruct.pl. */ |
| 648 | struct cdump_definitions *cdump_extract(const tal_t *ctx, const char *code, |
| 649 | char **complaints) |
| 650 | { |
| 651 | struct parse_state ps; |
| 652 | const struct token *toks; |
| 653 | |
| 654 | ps.defs = tal(ctx, struct cdump_definitions); |
| 655 | ps.complaints = tal_strdup(ctx, ""); |
| 656 | ps.code = code; |
| 657 | |
| 658 | strmap_init(&ps.defs->enums); |
| 659 | strmap_init(&ps.defs->structs); |
| 660 | strmap_init(&ps.defs->unions); |
| 661 | tal_add_destructor(ps.defs, destroy_definitions); |
| 662 | |
| 663 | toks = ps.toks = tokenize(ps.defs, code); |
| 664 | while (tok_peek(&ps.toks)) { |
| 665 | if (!tok_ignore_attribute(&ps)) |
| 666 | goto fail; |
| 667 | if (tok_take_if(&ps.toks, "struct")) { |
| 668 | if (!tok_take_conglom(&ps, CDUMP_STRUCT)) |
| 669 | goto fail; |
| 670 | } else if (tok_take_if(&ps.toks, "union")) { |
| 671 | if (!tok_take_conglom(&ps, CDUMP_UNION)) |
| 672 | goto fail; |
| 673 | } else if (tok_take_if(&ps.toks, "enum")) { |
| 674 | if (!tok_take_enum(&ps)) |
| 675 | goto fail; |
| 676 | } else |
| 677 | tok_take_unknown_statement(&ps); |
| 678 | } |
| 679 | |
| 680 | /* Now, remove any undefined types! */ |
| 681 | remove_undefined(&ps.defs->enums); |
| 682 | remove_undefined(&ps.defs->structs); |
| 683 | remove_undefined(&ps.defs->unions); |
| 684 | tal_free(toks); |
| 685 | |
| 686 | out: |
| 687 | if (streq(ps.complaints, "")) |
| 688 | ps.complaints = tal_free(ps.complaints); |
| 689 | |
| 690 | if (complaints) |
| 691 | *complaints = ps.complaints; |
| 692 | else |
| 693 | tal_free(ps.complaints); |
| 694 | return ps.defs; |
| 695 | |
| 696 | fail: |
| 697 | ps.defs = tal_free(ps.defs); |
| 698 | goto out; |
| 699 | } |