summaryrefslogtreecommitdiff
path: root/tests/run-parser.c
blob: 7189bb91a7c2b90c3167d0a09617055777ee77e9 (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
72
73
#include <yaml.h>

#include <stdlib.h>
#include <stdio.h>

#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>

int
main(int argc, char *argv[])
{
    int idx;

    if (argc < 2) {
        printf("Usage: %s file1.yaml ...\n", argv[0]);
        return 0;
    }

    for (idx = 1; idx < argc; idx ++)
    {
        FILE *file;
        yaml_parser_t *parser;
        yaml_event_t event;
        int failed = 0;
        int count = 0;

        printf("[%d] Parsing '%s': ", idx, argv[idx]);
        fflush(stdout);

        file = fopen(argv[idx], "rb");
        assert(file);

        parser = yaml_parser_new();
        assert(parser);

        yaml_parser_set_file_reader(parser, file);

        while (1)
        {
            if (!yaml_parser_parse_event(parser, &event)) {
                failed = 1;
                break;
            }

            if (event.type == YAML_NO_EVENT)
                break;

            yaml_event_destroy(&event);

            count ++;
        }

        if (!failed) {
            printf("SUCCESS (%d tokens)\n", count);
        }
        else {
            yaml_error_t error;
            char message[256];
            yaml_parser_get_error(parser, &error);
            yaml_error_message(&error, message, 256);
            printf("FAILURE (%d events)\n -> %s\n", count, message);
        }

        yaml_parser_delete(parser);

        fclose(file);
    }

    return 0;
}