]> andersk Git - libyaml.git/blob - tests/run-test-suite/src/libyaml-parser.c
Skip 5 tests in libyaml-emitter.list
[libyaml.git] / tests / run-test-suite / src / libyaml-parser.c
1 #include <yaml.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <assert.h>
5
6 void print_escaped(yaml_char_t * str, size_t length);
7
8 int main(int argc, char *argv[])
9 {
10     FILE *input;
11     yaml_parser_t parser;
12     yaml_event_t event;
13
14     if (argc == 1)
15         input = stdin;
16     else if (argc == 2)
17         input = fopen(argv[1], "rb");
18     else {
19         fprintf(stderr, "Usage: libyaml-parser [<input-file>]\n");
20         return 1;
21     }
22     assert(input);
23
24     if (!yaml_parser_initialize(&parser)) {
25         fprintf(stderr, "Could not initialize the parser object\n");
26         return 1;
27     }
28     yaml_parser_set_input_file(&parser, input);
29
30     while (1) {
31         yaml_event_type_t type;
32         if (!yaml_parser_parse(&parser, &event)) {
33             fprintf(stderr, "Parse error: %s\n", parser.problem);
34             return 1;
35         }
36         type = event.type;
37
38         if (type == YAML_NO_EVENT)
39             printf("???\n");
40         else if (type == YAML_STREAM_START_EVENT)
41             printf("+STR\n");
42         else if (type == YAML_STREAM_END_EVENT)
43             printf("-STR\n");
44         else if (type == YAML_DOCUMENT_START_EVENT) {
45             printf("+DOC");
46             if (!event.data.document_start.implicit)
47                 printf(" ---");
48             printf("\n");
49         }
50         else if (type == YAML_DOCUMENT_END_EVENT) {
51             printf("-DOC");
52             if (!event.data.document_end.implicit)
53                 printf(" ...");
54             printf("\n");
55         }
56         else if (type == YAML_MAPPING_START_EVENT) {
57             printf("+MAP");
58             if (event.data.mapping_start.anchor)
59                 printf(" &%s", event.data.mapping_start.anchor);
60             if (event.data.mapping_start.tag)
61                 printf(" <%s>", event.data.mapping_start.tag);
62             printf("\n");
63         }
64         else if (type == YAML_MAPPING_END_EVENT)
65             printf("-MAP\n");
66         else if (type == YAML_SEQUENCE_START_EVENT) {
67             printf("+SEQ");
68             if (event.data.sequence_start.anchor)
69                 printf(" &%s", event.data.sequence_start.anchor);
70             if (event.data.sequence_start.tag)
71                 printf(" <%s>", event.data.sequence_start.tag);
72             printf("\n");
73         }
74         else if (type == YAML_SEQUENCE_END_EVENT)
75             printf("-SEQ\n");
76         else if (type == YAML_SCALAR_EVENT) {
77             printf("=VAL");
78             if (event.data.scalar.anchor)
79                 printf(" &%s", event.data.scalar.anchor);
80             if (event.data.scalar.tag)
81                 printf(" <%s>", event.data.scalar.tag);
82             switch (event.data.scalar.style) {
83             case YAML_PLAIN_SCALAR_STYLE:
84                 printf(" :");
85                 break;
86             case YAML_SINGLE_QUOTED_SCALAR_STYLE:
87                 printf(" '");
88                 break;
89             case YAML_DOUBLE_QUOTED_SCALAR_STYLE:
90                 printf(" \"");
91                 break;
92             case YAML_LITERAL_SCALAR_STYLE:
93                 printf(" |");
94                 break;
95             case YAML_FOLDED_SCALAR_STYLE:
96                 printf(" >");
97                 break;
98             case YAML_ANY_SCALAR_STYLE:
99                 abort();
100             }
101             print_escaped(event.data.scalar.value, event.data.scalar.length);
102             printf("\n");
103         }
104         else if (type == YAML_ALIAS_EVENT)
105             printf("=ALI *%s\n", event.data.alias.anchor);
106         else
107             abort();
108
109         yaml_event_delete(&event);
110
111         if (type == YAML_STREAM_END_EVENT)
112             break;
113     }
114
115     assert(!fclose(input));
116     yaml_parser_delete(&parser);
117     fflush(stdout);
118
119     return 0;
120 }
121
122 void print_escaped(yaml_char_t * str, size_t length)
123 {
124     int i;
125     char c;
126
127     for (i = 0; i < length; i++) {
128         c = *(str + i);
129         if (c == '\\')
130             printf("\\\\");
131         else if (c == '\0')
132             printf("\\0");
133         else if (c == '\b')
134             printf("\\b");
135         else if (c == '\n')
136             printf("\\n");
137         else if (c == '\r')
138             printf("\\r");
139         else if (c == '\t')
140             printf("\\t");
141         else
142             printf("%c", c);
143     }
144 }
This page took 0.041449 seconds and 5 git commands to generate.