Changeset 263 for libyaml/trunk/src/parser.c
- Timestamp:
- 12/27/07 07:05:17 (5 years ago)
- File:
-
- 1 edited
-
libyaml/trunk/src/parser.c (modified) (30 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libyaml/trunk/src/parser.c
r250 r263 47 47 48 48 #define PEEK_TOKEN(parser) \ 49 ((parser-> token_available || yaml_parser_fetch_more_tokens(parser)) ?\50 parser->tokens. head : NULL)49 ((parser->is_token_available || yaml_parser_fetch_more_tokens(parser)) ? \ 50 parser->tokens.list + parser->tokens.head : NULL) 51 51 52 52 /* … … 55 55 56 56 #define SKIP_TOKEN(parser) \ 57 (parser-> token_available = 0,\57 (parser->is_token_available = 0, \ 58 58 parser->tokens_parsed ++, \ 59 parser-> stream_end_produced =\60 (parser->tokens. head->type == YAML_STREAM_END_TOKEN),\59 parser->is_stream_end_produced = \ 60 (parser->tokens.list[parser->tokens.head].type == YAML_STREAM_END_TOKEN), \ 61 61 parser->tokens.head ++) 62 62 … … 69 69 70 70 /* 71 * Error handling.72 */73 74 static int75 yaml_parser_set_parser_error(yaml_parser_t *parser,76 const char *problem, yaml_mark_t problem_mark);77 78 static int79 yaml_parser_set_parser_error_context(yaml_parser_t *parser,80 const char *context, yaml_mark_t context_mark,81 const char *problem, yaml_mark_t problem_mark);82 83 /*84 71 * State functions. 85 72 */ … … 156 143 yaml_parser_process_directives(yaml_parser_t *parser, 157 144 yaml_version_directive_t **version_directive_ref, 158 yaml_tag_directive_t **tag_directives_start_ref, 159 yaml_tag_directive_t **tag_directives_end_ref); 145 yaml_tag_directive_t **tag_directives_list_ref, 146 size_t *tag_directives_length_ref, 147 size_t *tag_directives_capacity_ref); 160 148 161 149 static int … … 179 167 /* No events after the end of the stream or error. */ 180 168 181 if (parser-> stream_end_produced || parser->error ||182 parser->state == YAML_PARSE_END_STATE) {169 if (parser->is_stream_end_produced || parser->error.type 170 || parser->state == YAML_PARSE_END_STATE) { 183 171 return 1; 184 172 } … … 188 176 return yaml_parser_state_machine(parser, event); 189 177 } 190 191 /*192 * Set parser error.193 */194 195 static int196 yaml_parser_set_parser_error(yaml_parser_t *parser,197 const char *problem, yaml_mark_t problem_mark)198 {199 parser->error = YAML_PARSER_ERROR;200 parser->problem = problem;201 parser->problem_mark = problem_mark;202 203 return 0;204 }205 206 static int207 yaml_parser_set_parser_error_context(yaml_parser_t *parser,208 const char *context, yaml_mark_t context_mark,209 const char *problem, yaml_mark_t problem_mark)210 {211 parser->error = YAML_PARSER_ERROR;212 parser->context = context;213 parser->context_mark = context_mark;214 parser->problem = problem;215 parser->problem_mark = problem_mark;216 217 return 0;218 }219 220 178 221 179 /* … … 319 277 320 278 if (token->type != YAML_STREAM_START_TOKEN) { 321 return yaml_parser_set_parser_error(parser,279 return PARSER_ERROR_INIT(parser, 322 280 "did not found expected <stream-start>", token->start_mark); 323 281 } … … 346 304 yaml_version_directive_t *version_directive = NULL; 347 305 struct { 348 yaml_tag_directive_t *start; 349 yaml_tag_directive_t *end; 350 } tag_directives = { NULL, NULL }; 306 yaml_tag_directive_t *list; 307 size_t length; 308 size_t capacity; 309 } tag_directives = { NULL, 0, 0 }; 351 310 352 311 token = PEEK_TOKEN(parser); … … 371 330 token->type != YAML_STREAM_END_TOKEN) 372 331 { 373 if (!yaml_parser_process_directives(parser, NULL, NULL, NULL ))332 if (!yaml_parser_process_directives(parser, NULL, NULL, NULL, NULL)) 374 333 return 0; 375 334 if (!PUSH(parser, parser->states, YAML_PARSE_DOCUMENT_END_STATE)) 376 335 return 0; 377 336 parser->state = YAML_PARSE_BLOCK_NODE_STATE; 378 DOCUMENT_START_EVENT_INIT(*event, NULL, NULL, NULL, 1,337 DOCUMENT_START_EVENT_INIT(*event, NULL, NULL, 0, 0, 1, 379 338 token->start_mark, token->start_mark); 380 339 return 1; … … 388 347 start_mark = token->start_mark; 389 348 if (!yaml_parser_process_directives(parser, &version_directive, 390 &tag_directives.start, &tag_directives.end)) 349 &tag_directives.list, &tag_directives.length, 350 &tag_directives.capacity)) 391 351 return 0; 392 352 token = PEEK_TOKEN(parser); 393 353 if (!token) goto error; 394 354 if (token->type != YAML_DOCUMENT_START_TOKEN) { 395 yaml_parser_set_parser_error(parser,355 PARSER_ERROR_INIT(parser, 396 356 "did not found expected <document start>", token->start_mark); 397 357 goto error; … … 402 362 end_mark = token->end_mark; 403 363 DOCUMENT_START_EVENT_INIT(*event, version_directive, 404 tag_directives.start, tag_directives.end, 0, 364 tag_directives.list, tag_directives.length, 365 tag_directives.capacity, 0, 405 366 start_mark, end_mark); 406 367 SKIP_TOKEN(parser); 407 368 version_directive = NULL; 408 tag_directives.start = tag_directives.end = NULL; 369 tag_directives.list = NULL; 370 tag_directives.length = tag_directives.capacity = 0; 409 371 return 1; 410 372 } … … 422 384 error: 423 385 yaml_free(version_directive); 424 while ( tag_directives.start != tag_directives.end) {425 yaml_ free(tag_directives.end[-1].handle);426 yaml_free(tag_directive s.end[-1].prefix);427 tag_directives.end --;428 } 429 yaml_free(tag_directives.start);386 while (!STACK_EMPTY(parser, tag_directives)) { 387 yaml_tag_directive_t tag_directive = POP(parser, tag_directives); 388 yaml_free(tag_directive.handle); 389 yaml_free(tag_directive.prefix); 390 } 391 STACK_DEL(parser, tag_directives); 430 392 return 0; 431 393 } … … 599 561 } 600 562 else { 601 yaml_tag_directive_t *tag_directive; 602 for (tag_directive = parser->tag_directives.start; 603 tag_directive != parser->tag_directives.top; 604 tag_directive ++) { 563 int idx; 564 for (idx = 0; idx < parser->tag_directives.length; idx++) { 565 yaml_tag_directive_t *tag_directive = parser->tag_directives.list + idx; 605 566 if (strcmp((char *)tag_directive->handle, (char *)tag_handle) == 0) { 606 567 size_t prefix_len = strlen((char *)tag_directive->prefix); … … 608 569 tag = yaml_malloc(prefix_len+suffix_len+1); 609 570 if (!tag) { 610 parser->error = YAML_MEMORY_ERROR;571 MEMORY_ERROR_INIT(parser); 611 572 goto error; 612 573 } … … 621 582 } 622 583 if (!tag) { 623 yaml_parser_set_parser_error_context(parser,584 PARSER_ERROR_WITH_CONTEXT_INIT(parser, 624 585 "while parsing a node", start_mark, 625 586 "found undefined tag handle", tag_mark); … … 688 649 yaml_char_t *value = yaml_malloc(1); 689 650 if (!value) { 690 parser->error = YAML_MEMORY_ERROR;651 MEMORY_ERROR_INIT(parser); 691 652 goto error; 692 653 } … … 699 660 } 700 661 else { 701 yaml_parser_set_parser_error_context(parser,662 PARSER_ERROR_WITH_CONTEXT_INIT(parser, 702 663 (block ? "while parsing a block node" 703 664 : "while parsing a flow node"), start_mark, … … 770 731 else 771 732 { 772 return yaml_parser_set_parser_error_context(parser,733 return PARSER_ERROR_WITH_CONTEXT_INIT(parser, 773 734 "while parsing a block collection", POP(parser, parser->marks), 774 735 "did not found expected '-' indicator", token->start_mark); … … 880 841 else 881 842 { 882 return yaml_parser_set_parser_error_context(parser,843 return PARSER_ERROR_WITH_CONTEXT_INIT(parser, 883 844 "while parsing a block mapping", POP(parser, parser->marks), 884 845 "did not found expected key", token->start_mark); … … 974 935 } 975 936 else { 976 return yaml_parser_set_parser_error_context(parser,937 return PARSER_ERROR_WITH_CONTEXT_INIT(parser, 977 938 "while parsing a flow sequence", POP(parser, parser->marks), 978 939 "did not found expected ',' or ']'", token->start_mark); … … 1126 1087 } 1127 1088 else { 1128 return yaml_parser_set_parser_error_context(parser,1089 return PARSER_ERROR_WITH_CONTEXT_INIT(parser, 1129 1090 "while parsing a flow mapping", POP(parser, parser->marks), 1130 1091 "did not found expected ',' or '}'", token->start_mark); … … 1215 1176 value = yaml_malloc(1); 1216 1177 if (!value) { 1217 parser->error = YAML_MEMORY_ERROR; 1218 return 0; 1178 return MEMORY_ERROR_INIT(parser); 1219 1179 } 1220 1180 value[0] = '\0'; … … 1233 1193 yaml_parser_process_directives(yaml_parser_t *parser, 1234 1194 yaml_version_directive_t **version_directive_ref, 1235 yaml_tag_directive_t **tag_directives_start_ref, 1236 yaml_tag_directive_t **tag_directives_end_ref) 1195 yaml_tag_directive_t **tag_directives_list_ref, 1196 size_t *tag_directives_length_ref, 1197 size_t *tag_directives_capacity_ref) 1237 1198 { 1238 1199 yaml_tag_directive_t default_tag_directives[] = { … … 1244 1205 yaml_version_directive_t *version_directive = NULL; 1245 1206 struct { 1246 yaml_tag_directive_t * start;1247 yaml_tag_directive_t *end;1248 yaml_tag_directive_t *top;1249 } tag_directives = { NULL, NULL, NULL};1250 yaml_token_t *token; 1251 1252 if (!STACK_INIT(parser, tag_directives, INITIAL_STACK_ SIZE))1207 yaml_tag_directive_t *list; 1208 size_t length; 1209 size_t capacity; 1210 } tag_directives = { NULL, 0, 0 }; 1211 yaml_token_t *token; 1212 1213 if (!STACK_INIT(parser, tag_directives, INITIAL_STACK_CAPACITY)) 1253 1214 goto error; 1254 1215 … … 1261 1222 if (token->type == YAML_VERSION_DIRECTIVE_TOKEN) { 1262 1223 if (version_directive) { 1263 yaml_parser_set_parser_error(parser,1224 PARSER_ERROR_INIT(parser, 1264 1225 "found duplicate %YAML directive", token->start_mark); 1265 1226 goto error; … … 1267 1228 if (token->data.version_directive.major != 1 1268 1229 || token->data.version_directive.minor != 1) { 1269 yaml_parser_set_parser_error(parser,1230 PARSER_ERROR_INIT(parser, 1270 1231 "found incompatible YAML document", token->start_mark); 1271 1232 goto error; … … 1273 1234 version_directive = yaml_malloc(sizeof(yaml_version_directive_t)); 1274 1235 if (!version_directive) { 1275 parser->error = YAML_MEMORY_ERROR;1236 MEMORY_ERROR_INIT(parser); 1276 1237 goto error; 1277 1238 } … … 1307 1268 *version_directive_ref = version_directive; 1308 1269 } 1309 if (tag_directives_ start_ref) {1270 if (tag_directives_list_ref) { 1310 1271 if (STACK_EMPTY(parser, tag_directives)) { 1311 *tag_directives_start_ref = *tag_directives_end_ref = NULL; 1272 *tag_directives_list_ref = NULL; 1273 *tag_directives_length_ref = 0; 1274 *tag_directives_capacity_ref = 0; 1312 1275 STACK_DEL(parser, tag_directives); 1313 1276 } 1314 1277 else { 1315 *tag_directives_start_ref = tag_directives.start; 1316 *tag_directives_end_ref = tag_directives.top; 1278 *tag_directives_list_ref = tag_directives.list; 1279 *tag_directives_length_ref = tag_directives.length; 1280 *tag_directives_capacity_ref = tag_directives.capacity; 1317 1281 } 1318 1282 } … … 1344 1308 yaml_tag_directive_t *tag_directive; 1345 1309 yaml_tag_directive_t copy = { NULL, NULL }; 1346 1347 for (tag_directive = parser->tag_directives.start; 1348 tag_directive != parser->tag_directives.top; tag_directive ++) { 1310 int idx; 1311 1312 for (idx = 0; idx < parser->tag_directives.length; idx++) { 1313 yaml_tag_directive_t *tag_directive = parser->tag_directives.list + idx; 1349 1314 if (strcmp((char *)value.handle, (char *)tag_directive->handle) == 0) { 1350 1315 if (allow_duplicates) 1351 1316 return 1; 1352 return yaml_parser_set_parser_error(parser,1317 return PARSER_ERROR_INIT(parser, 1353 1318 "found duplicate %TAG directive", mark); 1354 1319 } … … 1358 1323 copy.prefix = yaml_strdup(value.prefix); 1359 1324 if (!copy.handle || !copy.prefix) { 1360 parser->error = YAML_MEMORY_ERROR;1325 MEMORY_ERROR_INIT(parser); 1361 1326 goto error; 1362 1327 }
Note: See TracChangeset
for help on using the changeset viewer.
