diff options
author | sanine <sanine.not@pm.me> | 2023-02-03 22:34:04 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-02-03 22:34:04 -0600 |
commit | ea512c3b1d2d85ff76aa7d4979c73dd7f1217757 (patch) | |
tree | c5bd7d5151a86ae4a54a3241fe745c0e2dd64d58 /yy/demo.c | |
parent | 0710a40e50d12cd731b0372d79d0ccdfbaee10d4 (diff) |
begin refactor with bison xml parser
Diffstat (limited to 'yy/demo.c')
-rw-r--r-- | yy/demo.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/yy/demo.c b/yy/demo.c new file mode 100644 index 0000000..52058d3 --- /dev/null +++ b/yy/demo.c @@ -0,0 +1,65 @@ +#include <stdio.h> + +#define YYLTYPE KALMIALTYPE +#define YYSTYPE KALMIASTYPE +#include "kalmia.tab.h" +#include "kalmia.lex.h" + + +void print_attrs(struct kai_attr_t *attr) +{ + while(attr != NULL) { + printf("%s=\"%s\" ", attr->key, attr->value); + attr = attr->next; + } +} + + +void print_tag(char *indent, struct kai_tag_t *tag) +{ + printf("%s%s[ ", indent, tag->type); + print_attrs(tag->attrs); + printf("]\n"); +} + + +void print_level(int indent_level, struct kai_tag_t *tag) +{ + char indent[64]; + for (int i=0; i<indent_level; i++) { + indent[i] = '\t'; + } + indent[indent_level] = 0; + + while (tag != NULL) { + print_tag(indent, tag); + if (tag->children != NULL) { + print_level(indent_level+1, tag->children); + } + if (tag->content != NULL) { + printf("%s\t%s\n", indent, tag->content); + } + tag = tag->next; + } +} + + +int main(int argc, char **argv) +{ + if (argc < 2) { + fprintf(stderr, "You must specify a file to parse!\n"); + return -1; + } + + struct kai_tag_t *document = kai_parse_file(argv[1]); + if (document == NULL) { + return -1; + } + + print_tag("", document); + print_level(1, document->children); + + kai_tag_destroy(document); + + return 0; +} |