Path: blob/main/contrib/libyaml/tests/run-parser-test-suite.c
39534 views
#include <yaml.h>1#include <stdlib.h>2#include <stdio.h>3#include <assert.h>45void print_escaped(yaml_char_t * str, size_t length);6int usage(int ret);78int main(int argc, char *argv[])9{10FILE *input;11yaml_parser_t parser;12yaml_event_t event;13int flow = -1; /** default no flow style collections */14int i = 0;15int foundfile = 0;1617for (i = 1; i < argc; i++) {18if (strncmp(argv[i], "--flow", 6) == 0) {19if (i+1 == argc)20return usage(1);21i++;22if (strncmp(argv[i], "keep", 4) == 0)23flow = 0;24else if (strncmp(argv[i], "on", 2) == 0)25flow = 1;26else if (strncmp(argv[i], "off", 3) == 0)27flow = -1;28else29return usage(1);30}31else if (strncmp(argv[i], "--help", 6) == 0)32return usage(0);33else if (strncmp(argv[i], "-h", 2) == 0)34return usage(0);35else if (!foundfile) {36input = fopen(argv[i], "rb");37foundfile = 1;38}39else40return usage(1);41}42if (!foundfile) {43input = stdin;44}45assert(input);4647if (!yaml_parser_initialize(&parser)) {48fprintf(stderr, "Could not initialize the parser object\n");49return 1;50}51yaml_parser_set_input_file(&parser, input);5253while (1) {54yaml_event_type_t type;55if (!yaml_parser_parse(&parser, &event)) {56if ( parser.problem_mark.line || parser.problem_mark.column ) {57fprintf(stderr, "Parse error: %s\nLine: %lu Column: %lu\n",58parser.problem,59(unsigned long)parser.problem_mark.line + 1,60(unsigned long)parser.problem_mark.column + 1);61}62else {63fprintf(stderr, "Parse error: %s\n", parser.problem);64}65return 1;66}67type = event.type;6869if (type == YAML_NO_EVENT)70printf("???\n");71else if (type == YAML_STREAM_START_EVENT)72printf("+STR\n");73else if (type == YAML_STREAM_END_EVENT)74printf("-STR\n");75else if (type == YAML_DOCUMENT_START_EVENT) {76printf("+DOC");77if (!event.data.document_start.implicit)78printf(" ---");79printf("\n");80}81else if (type == YAML_DOCUMENT_END_EVENT) {82printf("-DOC");83if (!event.data.document_end.implicit)84printf(" ...");85printf("\n");86}87else if (type == YAML_MAPPING_START_EVENT) {88printf("+MAP");89if (flow == 0 && event.data.mapping_start.style == YAML_FLOW_MAPPING_STYLE)90printf(" {}");91else if (flow == 1)92printf(" {}");93if (event.data.mapping_start.anchor)94printf(" &%s", event.data.mapping_start.anchor);95if (event.data.mapping_start.tag)96printf(" <%s>", event.data.mapping_start.tag);97printf("\n");98}99else if (type == YAML_MAPPING_END_EVENT)100printf("-MAP\n");101else if (type == YAML_SEQUENCE_START_EVENT) {102printf("+SEQ");103if (flow == 0 && event.data.sequence_start.style == YAML_FLOW_SEQUENCE_STYLE)104printf(" []");105else if (flow == 1)106printf(" []");107if (event.data.sequence_start.anchor)108printf(" &%s", event.data.sequence_start.anchor);109if (event.data.sequence_start.tag)110printf(" <%s>", event.data.sequence_start.tag);111printf("\n");112}113else if (type == YAML_SEQUENCE_END_EVENT)114printf("-SEQ\n");115else if (type == YAML_SCALAR_EVENT) {116printf("=VAL");117if (event.data.scalar.anchor)118printf(" &%s", event.data.scalar.anchor);119if (event.data.scalar.tag)120printf(" <%s>", event.data.scalar.tag);121switch (event.data.scalar.style) {122case YAML_PLAIN_SCALAR_STYLE:123printf(" :");124break;125case YAML_SINGLE_QUOTED_SCALAR_STYLE:126printf(" '");127break;128case YAML_DOUBLE_QUOTED_SCALAR_STYLE:129printf(" \"");130break;131case YAML_LITERAL_SCALAR_STYLE:132printf(" |");133break;134case YAML_FOLDED_SCALAR_STYLE:135printf(" >");136break;137case YAML_ANY_SCALAR_STYLE:138abort();139}140print_escaped(event.data.scalar.value, event.data.scalar.length);141printf("\n");142}143else if (type == YAML_ALIAS_EVENT)144printf("=ALI *%s\n", event.data.alias.anchor);145else146abort();147148yaml_event_delete(&event);149150if (type == YAML_STREAM_END_EVENT)151break;152}153154assert(!fclose(input));155yaml_parser_delete(&parser);156fflush(stdout);157158return 0;159}160161void print_escaped(yaml_char_t * str, size_t length)162{163int i;164char c;165166for (i = 0; i < length; i++) {167c = *(str + i);168if (c == '\\')169printf("\\\\");170else if (c == '\0')171printf("\\0");172else if (c == '\b')173printf("\\b");174else if (c == '\n')175printf("\\n");176else if (c == '\r')177printf("\\r");178else if (c == '\t')179printf("\\t");180else181printf("%c", c);182}183}184185int usage(int ret) {186fprintf(stderr, "Usage: libyaml-parser [--flow (on|off|keep)] [<input-file>]\n");187return ret;188}189190191