summaryrefslogtreecommitdiff
path: root/yy/main.c
blob: 823db5a79c56a9e44395c7eec9bcce857a96428c (plain)
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
#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;
	}
	FILE *in = fopen(argv[1], "r");
	if (in == NULL) {
		fprintf(stderr, "Could not open file \"%s\"\n", argv[1]);
		return -1;
	}

	struct kalmia_t result;

	yyscan_t scanner;
	kalmialex_init(&scanner);
	kalmiaset_in(in, scanner);
	kalmiaparse(scanner, &result);
	kalmialex_destroy(scanner);

	print_tag("", result.tag);
	print_level(1, result.tag->children);

	return 0;
}