]>
Commit | Line | Data |
---|---|---|
03be97ab KS |
1 | |
2 | /* | |
3 | * Introduction | |
4 | * ************ | |
5 | * | |
6 | * The following notes assume that you are familiar with the YAML specification | |
7 | * (http://yaml.org/spec/cvs/current.html). We mostly follow it, although in | |
8 | * some cases we are less restrictive that it requires. | |
9 | * | |
10 | * The process of transforming a YAML stream into a sequence of events is | |
11 | * divided on two steps: Scanning and Parsing. | |
12 | * | |
13 | * The Scanner transforms the input stream into a sequence of tokens, while the | |
14 | * parser transform the sequence of tokens produced by the Scanner into a | |
15 | * sequence of parsing events. | |
16 | * | |
17 | * The Scanner is rather clever and complicated. The Parser, on the contrary, | |
18 | * is a straightforward implementation of a recursive-descendant parser (or, | |
19 | * LL(1) parser, as it is usually called). | |
20 | * | |
21 | * Actually there are two issues of Scanning that might be called "clever", the | |
22 | * rest is quite straightforward. The issues are "block collection start" and | |
23 | * "simple keys". Both issues are explained below in details. | |
24 | * | |
25 | * Here the Scanning step is explained and implemented. We start with the list | |
26 | * of all the tokens produced by the Scanner together with short descriptions. | |
27 | * | |
28 | * Now, tokens: | |
29 | * | |
30 | * STREAM-START(encoding) # The stream start. | |
31 | * STREAM-END # The stream end. | |
32 | * VERSION-DIRECTIVE(major,minor) # The '%YAML' directive. | |
33 | * TAG-DIRECTIVE(handle,prefix) # The '%TAG' directive. | |
34 | * DOCUMENT-START # '---' | |
35 | * DOCUMENT-END # '...' | |
36 | * BLOCK-SEQUENCE-START # Indentation increase denoting a block | |
37 | * BLOCK-MAPPING-START # sequence or a block mapping. | |
38 | * BLOCK-END # Indentation decrease. | |
39 | * FLOW-SEQUENCE-START # '[' | |
40 | * FLOW-SEQUENCE-END # ']' | |
41 | * BLOCK-SEQUENCE-START # '{' | |
42 | * BLOCK-SEQUENCE-END # '}' | |
43 | * BLOCK-ENTRY # '-' | |
44 | * FLOW-ENTRY # ',' | |
45 | * KEY # '?' or nothing (simple keys). | |
46 | * VALUE # ':' | |
47 | * ALIAS(anchor) # '*anchor' | |
48 | * ANCHOR(anchor) # '&anchor' | |
49 | * TAG(handle,suffix) # '!handle!suffix' | |
50 | * SCALAR(value,style) # A scalar. | |
51 | * | |
52 | * The following two tokens are "virtual" tokens denoting the beginning and the | |
53 | * end of the stream: | |
54 | * | |
55 | * STREAM-START(encoding) | |
56 | * STREAM-END | |
57 | * | |
58 | * We pass the information about the input stream encoding with the | |
59 | * STREAM-START token. | |
60 | * | |
61 | * The next two tokens are responsible for tags: | |
62 | * | |
63 | * VERSION-DIRECTIVE(major,minor) | |
64 | * TAG-DIRECTIVE(handle,prefix) | |
65 | * | |
66 | * Example: | |
67 | * | |
68 | * %YAML 1.1 | |
69 | * %TAG ! !foo | |
70 | * %TAG !yaml! tag:yaml.org,2002: | |
71 | * --- | |
72 | * | |
bdf4d192 | 73 | * The corresponding sequence of tokens: |
03be97ab KS |
74 | * |
75 | * STREAM-START(utf-8) | |
76 | * VERSION-DIRECTIVE(1,1) | |
77 | * TAG-DIRECTIVE("!","!foo") | |
78 | * TAG-DIRECTIVE("!yaml","tag:yaml.org,2002:") | |
79 | * DOCUMENT-START | |
80 | * STREAM-END | |
81 | * | |
82 | * Note that the VERSION-DIRECTIVE and TAG-DIRECTIVE tokens occupy a whole | |
83 | * line. | |
84 | * | |
85 | * The document start and end indicators are represented by: | |
86 | * | |
87 | * DOCUMENT-START | |
88 | * DOCUMENT-END | |
89 | * | |
90 | * Note that if a YAML stream contains an implicit document (without '---' | |
91 | * and '...' indicators), no DOCUMENT-START and DOCUMENT-END tokens will be | |
92 | * produced. | |
93 | * | |
94 | * In the following examples, we present whole documents together with the | |
95 | * produced tokens. | |
96 | * | |
97 | * 1. An implicit document: | |
98 | * | |
99 | * 'a scalar' | |
100 | * | |
101 | * Tokens: | |
102 | * | |
103 | * STREAM-START(utf-8) | |
104 | * SCALAR("a scalar",single-quoted) | |
105 | * STREAM-END | |
106 | * | |
107 | * 2. An explicit document: | |
108 | * | |
109 | * --- | |
110 | * 'a scalar' | |
111 | * ... | |
112 | * | |
113 | * Tokens: | |
114 | * | |
115 | * STREAM-START(utf-8) | |
116 | * DOCUMENT-START | |
117 | * SCALAR("a scalar",single-quoted) | |
118 | * DOCUMENT-END | |
119 | * STREAM-END | |
120 | * | |
121 | * 3. Several documents in a stream: | |
122 | * | |
123 | * 'a scalar' | |
124 | * --- | |
125 | * 'another scalar' | |
126 | * --- | |
127 | * 'yet another scalar' | |
128 | * | |
129 | * Tokens: | |
130 | * | |
131 | * STREAM-START(utf-8) | |
132 | * SCALAR("a scalar",single-quoted) | |
133 | * DOCUMENT-START | |
134 | * SCALAR("another scalar",single-quoted) | |
135 | * DOCUMENT-START | |
136 | * SCALAR("yet another scalar",single-quoted) | |
137 | * STREAM-END | |
138 | * | |
139 | * We have already introduced the SCALAR token above. The following tokens are | |
140 | * used to describe aliases, anchors, tag, and scalars: | |
141 | * | |
142 | * ALIAS(anchor) | |
143 | * ANCHOR(anchor) | |
144 | * TAG(handle,suffix) | |
145 | * SCALAR(value,style) | |
146 | * | |
147 | * The following series of examples illustrate the usage of these tokens: | |
148 | * | |
149 | * 1. A recursive sequence: | |
150 | * | |
151 | * &A [ *A ] | |
152 | * | |
153 | * Tokens: | |
154 | * | |
155 | * STREAM-START(utf-8) | |
156 | * ANCHOR("A") | |
157 | * FLOW-SEQUENCE-START | |
158 | * ALIAS("A") | |
159 | * FLOW-SEQUENCE-END | |
160 | * STREAM-END | |
161 | * | |
162 | * 2. A tagged scalar: | |
163 | * | |
164 | * !!float "3.14" # A good approximation. | |
165 | * | |
166 | * Tokens: | |
167 | * | |
168 | * STREAM-START(utf-8) | |
169 | * TAG("!!","float") | |
170 | * SCALAR("3.14",double-quoted) | |
171 | * STREAM-END | |
172 | * | |
173 | * 3. Various scalar styles: | |
174 | * | |
175 | * --- # Implicit empty plain scalars do not produce tokens. | |
176 | * --- a plain scalar | |
177 | * --- 'a single-quoted scalar' | |
178 | * --- "a double-quoted scalar" | |
179 | * --- |- | |
180 | * a literal scalar | |
181 | * --- >- | |
182 | * a folded | |
183 | * scalar | |
184 | * | |
185 | * Tokens: | |
186 | * | |
187 | * STREAM-START(utf-8) | |
188 | * DOCUMENT-START | |
189 | * DOCUMENT-START | |
190 | * SCALAR("a plain scalar",plain) | |
191 | * DOCUMENT-START | |
192 | * SCALAR("a single-quoted scalar",single-quoted) | |
193 | * DOCUMENT-START | |
194 | * SCALAR("a double-quoted scalar",double-quoted) | |
195 | * DOCUMENT-START | |
196 | * SCALAR("a literal scalar",literal) | |
197 | * DOCUMENT-START | |
198 | * SCALAR("a folded scalar",folded) | |
199 | * STREAM-END | |
200 | * | |
201 | * Now it's time to review collection-related tokens. We will start with | |
202 | * flow collections: | |
203 | * | |
204 | * FLOW-SEQUENCE-START | |
205 | * FLOW-SEQUENCE-END | |
206 | * FLOW-MAPPING-START | |
207 | * FLOW-MAPPING-END | |
208 | * FLOW-ENTRY | |
209 | * KEY | |
210 | * VALUE | |
211 | * | |
212 | * The tokens FLOW-SEQUENCE-START, FLOW-SEQUENCE-END, FLOW-MAPPING-START, and | |
213 | * FLOW-MAPPING-END represent the indicators '[', ']', '{', and '}' | |
214 | * correspondingly. FLOW-ENTRY represent the ',' indicator. Finally the | |
215 | * indicators '?' and ':', which are used for denoting mapping keys and values, | |
216 | * are represented by the KEY and VALUE tokens. | |
217 | * | |
218 | * The following examples show flow collections: | |
219 | * | |
220 | * 1. A flow sequence: | |
221 | * | |
222 | * [item 1, item 2, item 3] | |
223 | * | |
224 | * Tokens: | |
225 | * | |
226 | * STREAM-START(utf-8) | |
227 | * FLOW-SEQUENCE-START | |
228 | * SCALAR("item 1",plain) | |
229 | * FLOW-ENTRY | |
230 | * SCALAR("item 2",plain) | |
231 | * FLOW-ENTRY | |
232 | * SCALAR("item 3",plain) | |
233 | * FLOW-SEQUENCE-END | |
234 | * STREAM-END | |
235 | * | |
236 | * 2. A flow mapping: | |
237 | * | |
238 | * { | |
239 | * a simple key: a value, # Note that the KEY token is produced. | |
240 | * ? a complex key: another value, | |
241 | * } | |
242 | * | |
243 | * Tokens: | |
244 | * | |
245 | * STREAM-START(utf-8) | |
246 | * FLOW-MAPPING-START | |
247 | * KEY | |
248 | * SCALAR("a simple key",plain) | |
249 | * VALUE | |
250 | * SCALAR("a value",plain) | |
251 | * FLOW-ENTRY | |
252 | * KEY | |
253 | * SCALAR("a complex key",plain) | |
254 | * VALUE | |
255 | * SCALAR("another value",plain) | |
256 | * FLOW-ENTRY | |
257 | * FLOW-MAPPING-END | |
258 | * STREAM-END | |
259 | * | |
260 | * A simple key is a key which is not denoted by the '?' indicator. Note that | |
261 | * the Scanner still produce the KEY token whenever it encounters a simple key. | |
262 | * | |
263 | * For scanning block collections, the following tokens are used (note that we | |
264 | * repeat KEY and VALUE here): | |
265 | * | |
266 | * BLOCK-SEQUENCE-START | |
267 | * BLOCK-MAPPING-START | |
268 | * BLOCK-END | |
269 | * BLOCK-ENTRY | |
270 | * KEY | |
271 | * VALUE | |
272 | * | |
273 | * The tokens BLOCK-SEQUENCE-START and BLOCK-MAPPING-START denote indentation | |
274 | * increase that precedes a block collection (cf. the INDENT token in Python). | |
275 | * The token BLOCK-END denote indentation decrease that ends a block collection | |
276 | * (cf. the DEDENT token in Python). However YAML has some syntax pecularities | |
277 | * that makes detections of these tokens more complex. | |
278 | * | |
279 | * The tokens BLOCK-ENTRY, KEY, and VALUE are used to represent the indicators | |
280 | * '-', '?', and ':' correspondingly. | |
281 | * | |
282 | * The following examples show how the tokens BLOCK-SEQUENCE-START, | |
283 | * BLOCK-MAPPING-START, and BLOCK-END are emitted by the Scanner: | |
284 | * | |
285 | * 1. Block sequences: | |
286 | * | |
287 | * - item 1 | |
288 | * - item 2 | |
289 | * - | |
290 | * - item 3.1 | |
291 | * - item 3.2 | |
292 | * - | |
293 | * key 1: value 1 | |
294 | * key 2: value 2 | |
295 | * | |
296 | * Tokens: | |
297 | * | |
298 | * STREAM-START(utf-8) | |
299 | * BLOCK-SEQUENCE-START | |
300 | * BLOCK-ENTRY | |
301 | * SCALAR("item 1",plain) | |
302 | * BLOCK-ENTRY | |
303 | * SCALAR("item 2",plain) | |
304 | * BLOCK-ENTRY | |
305 | * BLOCK-SEQUENCE-START | |
306 | * BLOCK-ENTRY | |
307 | * SCALAR("item 3.1",plain) | |
308 | * BLOCK-ENTRY | |
309 | * SCALAR("item 3.2",plain) | |
310 | * BLOCK-END | |
311 | * BLOCK-ENTRY | |
312 | * BLOCK-MAPPING-START | |
313 | * KEY | |
314 | * SCALAR("key 1",plain) | |
315 | * VALUE | |
316 | * SCALAR("value 1",plain) | |
317 | * KEY | |
318 | * SCALAR("key 2",plain) | |
319 | * VALUE | |
320 | * SCALAR("value 2",plain) | |
321 | * BLOCK-END | |
322 | * BLOCK-END | |
323 | * STREAM-END | |
324 | * | |
325 | * 2. Block mappings: | |
326 | * | |
327 | * a simple key: a value # The KEY token is produced here. | |
328 | * ? a complex key | |
329 | * : another value | |
330 | * a mapping: | |
331 | * key 1: value 1 | |
332 | * key 2: value 2 | |
333 | * a sequence: | |
334 | * - item 1 | |
335 | * - item 2 | |
336 | * | |
337 | * Tokens: | |
338 | * | |
339 | * STREAM-START(utf-8) | |
340 | * BLOCK-MAPPING-START | |
341 | * KEY | |
342 | * SCALAR("a simple key",plain) | |
343 | * VALUE | |
344 | * SCALAR("a value",plain) | |
345 | * KEY | |
346 | * SCALAR("a complex key",plain) | |
347 | * VALUE | |
348 | * SCALAR("another value",plain) | |
349 | * KEY | |
350 | * SCALAR("a mapping",plain) | |
351 | * BLOCK-MAPPING-START | |
352 | * KEY | |
353 | * SCALAR("key 1",plain) | |
354 | * VALUE | |
355 | * SCALAR("value 1",plain) | |
356 | * KEY | |
357 | * SCALAR("key 2",plain) | |
358 | * VALUE | |
359 | * SCALAR("value 2",plain) | |
360 | * BLOCK-END | |
361 | * KEY | |
362 | * SCALAR("a sequence",plain) | |
363 | * VALUE | |
364 | * BLOCK-SEQUENCE-START | |
365 | * BLOCK-ENTRY | |
366 | * SCALAR("item 1",plain) | |
367 | * BLOCK-ENTRY | |
368 | * SCALAR("item 2",plain) | |
369 | * BLOCK-END | |
370 | * BLOCK-END | |
371 | * STREAM-END | |
372 | * | |
373 | * YAML does not always require to start a new block collection from a new | |
374 | * line. If the current line contains only '-', '?', and ':' indicators, a new | |
375 | * block collection may start at the current line. The following examples | |
376 | * illustrate this case: | |
377 | * | |
378 | * 1. Collections in a sequence: | |
379 | * | |
380 | * - - item 1 | |
381 | * - item 2 | |
382 | * - key 1: value 1 | |
383 | * key 2: value 2 | |
384 | * - ? complex key | |
385 | * : complex value | |
386 | * | |
387 | * Tokens: | |
388 | * | |
389 | * STREAM-START(utf-8) | |
390 | * BLOCK-SEQUENCE-START | |
391 | * BLOCK-ENTRY | |
392 | * BLOCK-SEQUENCE-START | |
393 | * BLOCK-ENTRY | |
394 | * SCALAR("item 1",plain) | |
395 | * BLOCK-ENTRY | |
396 | * SCALAR("item 2",plain) | |
397 | * BLOCK-END | |
398 | * BLOCK-ENTRY | |
399 | * BLOCK-MAPPING-START | |
400 | * KEY | |
401 | * SCALAR("key 1",plain) | |
402 | * VALUE | |
403 | * SCALAR("value 1",plain) | |
404 | * KEY | |
405 | * SCALAR("key 2",plain) | |
406 | * VALUE | |
407 | * SCALAR("value 2",plain) | |
408 | * BLOCK-END | |
409 | * BLOCK-ENTRY | |
410 | * BLOCK-MAPPING-START | |
411 | * KEY | |
412 | * SCALAR("complex key") | |
413 | * VALUE | |
414 | * SCALAR("complex value") | |
415 | * BLOCK-END | |
416 | * BLOCK-END | |
417 | * STREAM-END | |
418 | * | |
419 | * 2. Collections in a mapping: | |
420 | * | |
421 | * ? a sequence | |
422 | * : - item 1 | |
423 | * - item 2 | |
424 | * ? a mapping | |
425 | * : key 1: value 1 | |
426 | * key 2: value 2 | |
427 | * | |
428 | * Tokens: | |
429 | * | |
430 | * STREAM-START(utf-8) | |
431 | * BLOCK-MAPPING-START | |
432 | * KEY | |
433 | * SCALAR("a sequence",plain) | |
434 | * VALUE | |
435 | * BLOCK-SEQUENCE-START | |
436 | * BLOCK-ENTRY | |
437 | * SCALAR("item 1",plain) | |
438 | * BLOCK-ENTRY | |
439 | * SCALAR("item 2",plain) | |
440 | * BLOCK-END | |
441 | * KEY | |
442 | * SCALAR("a mapping",plain) | |
443 | * VALUE | |
444 | * BLOCK-MAPPING-START | |
445 | * KEY | |
446 | * SCALAR("key 1",plain) | |
447 | * VALUE | |
448 | * SCALAR("value 1",plain) | |
449 | * KEY | |
450 | * SCALAR("key 2",plain) | |
451 | * VALUE | |
452 | * SCALAR("value 2",plain) | |
453 | * BLOCK-END | |
454 | * BLOCK-END | |
455 | * STREAM-END | |
456 | * | |
457 | * YAML also permits non-indented sequences if they are included into a block | |
458 | * mapping. In this case, the token BLOCK-SEQUENCE-START is not produced: | |
459 | * | |
460 | * key: | |
461 | * - item 1 # BLOCK-SEQUENCE-START is NOT produced here. | |
462 | * - item 2 | |
463 | * | |
464 | * Tokens: | |
465 | * | |
466 | * STREAM-START(utf-8) | |
467 | * BLOCK-MAPPING-START | |
468 | * KEY | |
469 | * SCALAR("key",plain) | |
470 | * VALUE | |
471 | * BLOCK-ENTRY | |
472 | * SCALAR("item 1",plain) | |
473 | * BLOCK-ENTRY | |
474 | * SCALAR("item 2",plain) | |
475 | * BLOCK-END | |
476 | */ | |
477 | ||
625fcfe9 | 478 | #include "yaml_private.h" |
03be97ab | 479 | |
f2b59d4d KS |
480 | /* |
481 | * Ensure that the buffer contains the required number of characters. | |
482 | * Return 1 on success, 0 on failure (reader error or memory error). | |
483 | */ | |
484 | ||
625fcfe9 KS |
485 | #define CACHE(parser,length) \ |
486 | (parser->unread >= (length) \ | |
487 | ? 1 \ | |
f2b59d4d KS |
488 | : yaml_parser_update_buffer(parser, (length))) |
489 | ||
eb9cceb5 KS |
490 | /* |
491 | * Advance the buffer pointer. | |
492 | */ | |
493 | ||
625fcfe9 KS |
494 | #define SKIP(parser) \ |
495 | (parser->mark.index ++, \ | |
496 | parser->mark.column ++, \ | |
497 | parser->unread --, \ | |
e35af832 | 498 | parser->buffer.pointer += WIDTH(parser->buffer)) |
e71095e3 | 499 | |
625fcfe9 | 500 | #define SKIP_LINE(parser) \ |
e35af832 | 501 | (IS_CRLF(parser->buffer) ? \ |
625fcfe9 KS |
502 | (parser->mark.index += 2, \ |
503 | parser->mark.column = 0, \ | |
504 | parser->mark.line ++, \ | |
505 | parser->unread -= 2, \ | |
506 | parser->buffer.pointer += 2) : \ | |
e35af832 | 507 | IS_BREAK(parser->buffer) ? \ |
625fcfe9 KS |
508 | (parser->mark.index ++, \ |
509 | parser->mark.column = 0, \ | |
510 | parser->mark.line ++, \ | |
511 | parser->unread --, \ | |
e35af832 | 512 | parser->buffer.pointer += WIDTH(parser->buffer)) : 0) |
e71095e3 KS |
513 | |
514 | /* | |
515 | * Copy a character to a string buffer and advance pointers. | |
516 | */ | |
517 | ||
625fcfe9 KS |
518 | #define READ(parser,string) \ |
519 | (STRING_EXTEND(parser,string) ? \ | |
e35af832 | 520 | (COPY(string,parser->buffer), \ |
625fcfe9 KS |
521 | parser->mark.index ++, \ |
522 | parser->mark.column ++, \ | |
523 | parser->unread --, \ | |
524 | 1) : 0) | |
92d41fe1 KS |
525 | |
526 | /* | |
527 | * Copy a line break character to a string buffer and advance pointers. | |
528 | */ | |
529 | ||
625fcfe9 KS |
530 | #define READ_LINE(parser,string) \ |
531 | (STRING_EXTEND(parser,string) ? \ | |
e35af832 KS |
532 | (((CHECK_AT(parser->buffer,'\r',0) \ |
533 | && CHECK_AT(parser->buffer,'\n',1)) ? /* CR LF -> LF */ \ | |
92d41fe1 | 534 | (*((string).pointer++) = (yaml_char_t) '\n', \ |
625fcfe9 KS |
535 | parser->buffer.pointer += 2, \ |
536 | parser->mark.index += 2, \ | |
537 | parser->mark.column = 0, \ | |
538 | parser->mark.line ++, \ | |
92d41fe1 | 539 | parser->unread -= 2) : \ |
e35af832 KS |
540 | (CHECK_AT(parser->buffer,'\r',0) \ |
541 | || CHECK_AT(parser->buffer,'\n',0)) ? /* CR|LF -> LF */ \ | |
92d41fe1 | 542 | (*((string).pointer++) = (yaml_char_t) '\n', \ |
625fcfe9 KS |
543 | parser->buffer.pointer ++, \ |
544 | parser->mark.index ++, \ | |
545 | parser->mark.column = 0, \ | |
546 | parser->mark.line ++, \ | |
92d41fe1 | 547 | parser->unread --) : \ |
e35af832 KS |
548 | (CHECK_AT(parser->buffer,'\xC2',0) \ |
549 | && CHECK_AT(parser->buffer,'\x85',1)) ? /* NEL -> LF */ \ | |
92d41fe1 | 550 | (*((string).pointer++) = (yaml_char_t) '\n', \ |
625fcfe9 KS |
551 | parser->buffer.pointer += 2, \ |
552 | parser->mark.index ++, \ | |
553 | parser->mark.column = 0, \ | |
554 | parser->mark.line ++, \ | |
92d41fe1 | 555 | parser->unread --) : \ |
e35af832 KS |
556 | (CHECK_AT(parser->buffer,'\xE2',0) && \ |
557 | CHECK_AT(parser->buffer,'\x80',1) && \ | |
558 | (CHECK_AT(parser->buffer,'\xA8',2) || \ | |
559 | CHECK_AT(parser->buffer,'\xA9',2))) ? /* LS|PS -> LS|PS */ \ | |
625fcfe9 KS |
560 | (*((string).pointer++) = *(parser->buffer.pointer++), \ |
561 | *((string).pointer++) = *(parser->buffer.pointer++), \ | |
562 | *((string).pointer++) = *(parser->buffer.pointer++), \ | |
563 | parser->mark.index ++, \ | |
564 | parser->mark.column = 0, \ | |
565 | parser->mark.line ++, \ | |
566 | parser->unread --) : 0), \ | |
567 | 1) : 0) | |
92d41fe1 | 568 | |
625fcfe9 KS |
569 | /* |
570 | * Public API declarations. | |
571 | */ | |
e71095e3 | 572 | |
625fcfe9 KS |
573 | YAML_DECLARE(int) |
574 | yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token); | |
92d41fe1 | 575 | |
625fcfe9 KS |
576 | /* |
577 | * Error handling. | |
578 | */ | |
92d41fe1 | 579 | |
e71095e3 | 580 | static int |
625fcfe9 KS |
581 | yaml_parser_set_scanner_error(yaml_parser_t *parser, const char *context, |
582 | yaml_mark_t context_mark, const char *problem); | |
e71095e3 | 583 | |
03be97ab KS |
584 | /* |
585 | * High-level token API. | |
586 | */ | |
587 | ||
625fcfe9 | 588 | YAML_DECLARE(int) |
03be97ab KS |
589 | yaml_parser_fetch_more_tokens(yaml_parser_t *parser); |
590 | ||
591 | static int | |
592 | yaml_parser_fetch_next_token(yaml_parser_t *parser); | |
593 | ||
594 | /* | |
595 | * Potential simple keys. | |
596 | */ | |
597 | ||
598 | static int | |
599 | yaml_parser_stale_simple_keys(yaml_parser_t *parser); | |
600 | ||
601 | static int | |
602 | yaml_parser_save_simple_key(yaml_parser_t *parser); | |
603 | ||
604 | static int | |
605 | yaml_parser_remove_simple_key(yaml_parser_t *parser); | |
606 | ||
eb9cceb5 KS |
607 | static int |
608 | yaml_parser_increase_flow_level(yaml_parser_t *parser); | |
609 | ||
610 | static int | |
611 | yaml_parser_decrease_flow_level(yaml_parser_t *parser); | |
612 | ||
03be97ab KS |
613 | /* |
614 | * Indentation treatment. | |
615 | */ | |
616 | ||
617 | static int | |
c201bf64 KS |
618 | yaml_parser_roll_indent(yaml_parser_t *parser, ptrdiff_t column, |
619 | ptrdiff_t number, yaml_token_type_t type, yaml_mark_t mark); | |
03be97ab KS |
620 | |
621 | static int | |
c201bf64 | 622 | yaml_parser_unroll_indent(yaml_parser_t *parser, ptrdiff_t column); |
03be97ab KS |
623 | |
624 | /* | |
625 | * Token fetchers. | |
626 | */ | |
627 | ||
628 | static int | |
629 | yaml_parser_fetch_stream_start(yaml_parser_t *parser); | |
630 | ||
631 | static int | |
632 | yaml_parser_fetch_stream_end(yaml_parser_t *parser); | |
633 | ||
634 | static int | |
635 | yaml_parser_fetch_directive(yaml_parser_t *parser); | |
636 | ||
03be97ab KS |
637 | static int |
638 | yaml_parser_fetch_document_indicator(yaml_parser_t *parser, | |
639 | yaml_token_type_t type); | |
640 | ||
03be97ab KS |
641 | static int |
642 | yaml_parser_fetch_flow_collection_start(yaml_parser_t *parser, | |
643 | yaml_token_type_t type); | |
644 | ||
03be97ab KS |
645 | static int |
646 | yaml_parser_fetch_flow_collection_end(yaml_parser_t *parser, | |
647 | yaml_token_type_t type); | |
648 | ||
649 | static int | |
650 | yaml_parser_fetch_flow_entry(yaml_parser_t *parser); | |
651 | ||
652 | static int | |
653 | yaml_parser_fetch_block_entry(yaml_parser_t *parser); | |
654 | ||
655 | static int | |
656 | yaml_parser_fetch_key(yaml_parser_t *parser); | |
657 | ||
658 | static int | |
659 | yaml_parser_fetch_value(yaml_parser_t *parser); | |
660 | ||
661 | static int | |
eb9cceb5 | 662 | yaml_parser_fetch_anchor(yaml_parser_t *parser, yaml_token_type_t type); |
03be97ab KS |
663 | |
664 | static int | |
665 | yaml_parser_fetch_tag(yaml_parser_t *parser); | |
666 | ||
03be97ab KS |
667 | static int |
668 | yaml_parser_fetch_block_scalar(yaml_parser_t *parser, int literal); | |
669 | ||
03be97ab KS |
670 | static int |
671 | yaml_parser_fetch_flow_scalar(yaml_parser_t *parser, int single); | |
672 | ||
673 | static int | |
674 | yaml_parser_fetch_plain_scalar(yaml_parser_t *parser); | |
675 | ||
676 | /* | |
677 | * Token scanners. | |
678 | */ | |
679 | ||
680 | static int | |
681 | yaml_parser_scan_to_next_token(yaml_parser_t *parser); | |
682 | ||
625fcfe9 KS |
683 | static int |
684 | yaml_parser_scan_directive(yaml_parser_t *parser, yaml_token_t *token); | |
03be97ab KS |
685 | |
686 | static int | |
687 | yaml_parser_scan_directive_name(yaml_parser_t *parser, | |
688 | yaml_mark_t start_mark, yaml_char_t **name); | |
689 | ||
690 | static int | |
e71095e3 | 691 | yaml_parser_scan_version_directive_value(yaml_parser_t *parser, |
03be97ab KS |
692 | yaml_mark_t start_mark, int *major, int *minor); |
693 | ||
694 | static int | |
e71095e3 | 695 | yaml_parser_scan_version_directive_number(yaml_parser_t *parser, |
03be97ab KS |
696 | yaml_mark_t start_mark, int *number); |
697 | ||
698 | static int | |
699 | yaml_parser_scan_tag_directive_value(yaml_parser_t *parser, | |
e71095e3 | 700 | yaml_mark_t mark, yaml_char_t **handle, yaml_char_t **prefix); |
03be97ab | 701 | |
625fcfe9 KS |
702 | static int |
703 | yaml_parser_scan_anchor(yaml_parser_t *parser, yaml_token_t *token, | |
03be97ab KS |
704 | yaml_token_type_t type); |
705 | ||
625fcfe9 KS |
706 | static int |
707 | yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token); | |
03be97ab KS |
708 | |
709 | static int | |
710 | yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive, | |
711 | yaml_mark_t start_mark, yaml_char_t **handle); | |
712 | ||
713 | static int | |
714 | yaml_parser_scan_tag_uri(yaml_parser_t *parser, int directive, | |
e71095e3 KS |
715 | yaml_char_t *head, yaml_mark_t start_mark, yaml_char_t **uri); |
716 | ||
717 | static int | |
718 | yaml_parser_scan_uri_escapes(yaml_parser_t *parser, int directive, | |
719 | yaml_mark_t start_mark, yaml_string_t *string); | |
03be97ab | 720 | |
625fcfe9 KS |
721 | static int |
722 | yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token, | |
723 | int literal); | |
03be97ab | 724 | |
92d41fe1 KS |
725 | static int |
726 | yaml_parser_scan_block_scalar_breaks(yaml_parser_t *parser, | |
727 | int *indent, yaml_string_t *breaks, | |
728 | yaml_mark_t start_mark, yaml_mark_t *end_mark); | |
729 | ||
625fcfe9 KS |
730 | static int |
731 | yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token, | |
732 | int single); | |
03be97ab | 733 | |
625fcfe9 KS |
734 | static int |
735 | yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token); | |
03be97ab | 736 | |
f2b59d4d | 737 | /* |
625fcfe9 | 738 | * Get the next token. |
f2b59d4d KS |
739 | */ |
740 | ||
625fcfe9 KS |
741 | YAML_DECLARE(int) |
742 | yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token) | |
f2b59d4d | 743 | { |
f2b59d4d | 744 | assert(parser); /* Non-NULL parser object is expected. */ |
625fcfe9 | 745 | assert(token); /* Non-NULL token object is expected. */ |
f2b59d4d | 746 | |
5a00d8fe KS |
747 | /* Erase the token object. */ |
748 | ||
749 | memset(token, 0, sizeof(yaml_token_t)); | |
750 | ||
625fcfe9 | 751 | /* No tokens after STREAM-END or error. */ |
f2b59d4d | 752 | |
625fcfe9 | 753 | if (parser->stream_end_produced || parser->error) { |
625fcfe9 | 754 | return 1; |
7e32c194 KS |
755 | } |
756 | ||
f2b59d4d KS |
757 | /* Ensure that the tokens queue contains enough tokens. */ |
758 | ||
625fcfe9 KS |
759 | if (!parser->token_available) { |
760 | if (!yaml_parser_fetch_more_tokens(parser)) | |
761 | return 0; | |
92d41fe1 KS |
762 | } |
763 | ||
625fcfe9 | 764 | /* Fetch the next token from the queue. */ |
986dbde7 | 765 | |
625fcfe9 KS |
766 | *token = DEQUEUE(parser, parser->tokens); |
767 | parser->token_available = 0; | |
768 | parser->tokens_parsed ++; | |
e71095e3 | 769 | |
625fcfe9 KS |
770 | if (token->type == YAML_STREAM_END_TOKEN) { |
771 | parser->stream_end_produced = 1; | |
e71095e3 KS |
772 | } |
773 | ||
e71095e3 KS |
774 | return 1; |
775 | } | |
776 | ||
f2b59d4d KS |
777 | /* |
778 | * Set the scanner error and return 0. | |
779 | */ | |
780 | ||
781 | static int | |
782 | yaml_parser_set_scanner_error(yaml_parser_t *parser, const char *context, | |
783 | yaml_mark_t context_mark, const char *problem) | |
784 | { | |
785 | parser->error = YAML_SCANNER_ERROR; | |
786 | parser->context = context; | |
787 | parser->context_mark = context_mark; | |
788 | parser->problem = problem; | |
625fcfe9 | 789 | parser->problem_mark = parser->mark; |
7e32c194 KS |
790 | |
791 | return 0; | |
f2b59d4d KS |
792 | } |
793 | ||
f2b59d4d KS |
794 | /* |
795 | * Ensure that the tokens queue contains at least one token which can be | |
796 | * returned to the Parser. | |
797 | */ | |
798 | ||
625fcfe9 | 799 | YAML_DECLARE(int) |
f2b59d4d KS |
800 | yaml_parser_fetch_more_tokens(yaml_parser_t *parser) |
801 | { | |
802 | int need_more_tokens; | |
f2b59d4d KS |
803 | |
804 | /* While we need more tokens to fetch, do it. */ | |
805 | ||
806 | while (1) | |
807 | { | |
808 | /* | |
809 | * Check if we really need to fetch more tokens. | |
810 | */ | |
811 | ||
812 | need_more_tokens = 0; | |
813 | ||
625fcfe9 | 814 | if (parser->tokens.head == parser->tokens.tail) |
f2b59d4d KS |
815 | { |
816 | /* Queue is empty. */ | |
817 | ||
818 | need_more_tokens = 1; | |
819 | } | |
820 | else | |
821 | { | |
625fcfe9 KS |
822 | yaml_simple_key_t *simple_key; |
823 | ||
f2b59d4d KS |
824 | /* Check if any potential simple key may occupy the head position. */ |
825 | ||
7e32c194 KS |
826 | if (!yaml_parser_stale_simple_keys(parser)) |
827 | return 0; | |
828 | ||
625fcfe9 KS |
829 | for (simple_key = parser->simple_keys.start; |
830 | simple_key != parser->simple_keys.top; simple_key++) { | |
831 | if (simple_key->possible | |
832 | && simple_key->token_number == parser->tokens_parsed) { | |
f2b59d4d KS |
833 | need_more_tokens = 1; |
834 | break; | |
835 | } | |
836 | } | |
837 | } | |
838 | ||
839 | /* We are finished. */ | |
840 | ||
841 | if (!need_more_tokens) | |
842 | break; | |
843 | ||
844 | /* Fetch the next token. */ | |
845 | ||
846 | if (!yaml_parser_fetch_next_token(parser)) | |
847 | return 0; | |
848 | } | |
849 | ||
625fcfe9 KS |
850 | parser->token_available = 1; |
851 | ||
f2b59d4d KS |
852 | return 1; |
853 | } | |
854 | ||
855 | /* | |
856 | * The dispatcher for token fetchers. | |
857 | */ | |
858 | ||
859 | static int | |
860 | yaml_parser_fetch_next_token(yaml_parser_t *parser) | |
861 | { | |
862 | /* Ensure that the buffer is initialized. */ | |
863 | ||
625fcfe9 | 864 | if (!CACHE(parser, 1)) |
f2b59d4d KS |
865 | return 0; |
866 | ||
867 | /* Check if we just started scanning. Fetch STREAM-START then. */ | |
868 | ||
869 | if (!parser->stream_start_produced) | |
870 | return yaml_parser_fetch_stream_start(parser); | |
871 | ||
872 | /* Eat whitespaces and comments until we reach the next token. */ | |
873 | ||
874 | if (!yaml_parser_scan_to_next_token(parser)) | |
875 | return 0; | |
876 | ||
7e32c194 KS |
877 | /* Remove obsolete potential simple keys. */ |
878 | ||
879 | if (!yaml_parser_stale_simple_keys(parser)) | |
880 | return 0; | |
881 | ||
f2b59d4d KS |
882 | /* Check the indentation level against the current column. */ |
883 | ||
625fcfe9 | 884 | if (!yaml_parser_unroll_indent(parser, parser->mark.column)) |
f2b59d4d KS |
885 | return 0; |
886 | ||
887 | /* | |
888 | * Ensure that the buffer contains at least 4 characters. 4 is the length | |
889 | * of the longest indicators ('--- ' and '... '). | |
890 | */ | |
891 | ||
625fcfe9 | 892 | if (!CACHE(parser, 4)) |
f2b59d4d KS |
893 | return 0; |
894 | ||
895 | /* Is it the end of the stream? */ | |
896 | ||
e35af832 | 897 | if (IS_Z(parser->buffer)) |
f2b59d4d KS |
898 | return yaml_parser_fetch_stream_end(parser); |
899 | ||
900 | /* Is it a directive? */ | |
901 | ||
e35af832 | 902 | if (parser->mark.column == 0 && CHECK(parser->buffer, '%')) |
f2b59d4d KS |
903 | return yaml_parser_fetch_directive(parser); |
904 | ||
905 | /* Is it the document start indicator? */ | |
906 | ||
625fcfe9 | 907 | if (parser->mark.column == 0 |
e35af832 KS |
908 | && CHECK_AT(parser->buffer, '-', 0) |
909 | && CHECK_AT(parser->buffer, '-', 1) | |
910 | && CHECK_AT(parser->buffer, '-', 2) | |
911 | && IS_BLANKZ_AT(parser->buffer, 3)) | |
eb9cceb5 KS |
912 | return yaml_parser_fetch_document_indicator(parser, |
913 | YAML_DOCUMENT_START_TOKEN); | |
f2b59d4d KS |
914 | |
915 | /* Is it the document end indicator? */ | |
916 | ||
625fcfe9 | 917 | if (parser->mark.column == 0 |
e35af832 KS |
918 | && CHECK_AT(parser->buffer, '.', 0) |
919 | && CHECK_AT(parser->buffer, '.', 1) | |
920 | && CHECK_AT(parser->buffer, '.', 2) | |
921 | && IS_BLANKZ_AT(parser->buffer, 3)) | |
eb9cceb5 KS |
922 | return yaml_parser_fetch_document_indicator(parser, |
923 | YAML_DOCUMENT_END_TOKEN); | |
f2b59d4d KS |
924 | |
925 | /* Is it the flow sequence start indicator? */ | |
926 | ||
e35af832 | 927 | if (CHECK(parser->buffer, '[')) |
eb9cceb5 KS |
928 | return yaml_parser_fetch_flow_collection_start(parser, |
929 | YAML_FLOW_SEQUENCE_START_TOKEN); | |
f2b59d4d KS |
930 | |
931 | /* Is it the flow mapping start indicator? */ | |
932 | ||
e35af832 | 933 | if (CHECK(parser->buffer, '{')) |
eb9cceb5 KS |
934 | return yaml_parser_fetch_flow_collection_start(parser, |
935 | YAML_FLOW_MAPPING_START_TOKEN); | |
f2b59d4d KS |
936 | |
937 | /* Is it the flow sequence end indicator? */ | |
938 | ||
e35af832 | 939 | if (CHECK(parser->buffer, ']')) |
eb9cceb5 KS |
940 | return yaml_parser_fetch_flow_collection_end(parser, |
941 | YAML_FLOW_SEQUENCE_END_TOKEN); | |
f2b59d4d KS |
942 | |
943 | /* Is it the flow mapping end indicator? */ | |
944 | ||
e35af832 | 945 | if (CHECK(parser->buffer, '}')) |
eb9cceb5 KS |
946 | return yaml_parser_fetch_flow_collection_end(parser, |
947 | YAML_FLOW_MAPPING_END_TOKEN); | |
f2b59d4d KS |
948 | |
949 | /* Is it the flow entry indicator? */ | |
950 | ||
e35af832 | 951 | if (CHECK(parser->buffer, ',')) |
f2b59d4d KS |
952 | return yaml_parser_fetch_flow_entry(parser); |
953 | ||
954 | /* Is it the block entry indicator? */ | |
955 | ||
e35af832 | 956 | if (CHECK(parser->buffer, '-') && IS_BLANKZ_AT(parser->buffer, 1)) |
f2b59d4d KS |
957 | return yaml_parser_fetch_block_entry(parser); |
958 | ||
959 | /* Is it the key indicator? */ | |
960 | ||
e35af832 KS |
961 | if (CHECK(parser->buffer, '?') |
962 | && (parser->flow_level || IS_BLANKZ_AT(parser->buffer, 1))) | |
f2b59d4d KS |
963 | return yaml_parser_fetch_key(parser); |
964 | ||
965 | /* Is it the value indicator? */ | |
966 | ||
e35af832 KS |
967 | if (CHECK(parser->buffer, ':') |
968 | && (parser->flow_level || IS_BLANKZ_AT(parser->buffer, 1))) | |
f2b59d4d KS |
969 | return yaml_parser_fetch_value(parser); |
970 | ||
971 | /* Is it an alias? */ | |
972 | ||
e35af832 | 973 | if (CHECK(parser->buffer, '*')) |
eb9cceb5 | 974 | return yaml_parser_fetch_anchor(parser, YAML_ALIAS_TOKEN); |
f2b59d4d KS |
975 | |
976 | /* Is it an anchor? */ | |
977 | ||
e35af832 | 978 | if (CHECK(parser->buffer, '&')) |
eb9cceb5 | 979 | return yaml_parser_fetch_anchor(parser, YAML_ANCHOR_TOKEN); |
f2b59d4d KS |
980 | |
981 | /* Is it a tag? */ | |
982 | ||
e35af832 | 983 | if (CHECK(parser->buffer, '!')) |
f2b59d4d KS |
984 | return yaml_parser_fetch_tag(parser); |
985 | ||
986 | /* Is it a literal scalar? */ | |
987 | ||
e35af832 | 988 | if (CHECK(parser->buffer, '|') && !parser->flow_level) |
f2b59d4d KS |
989 | return yaml_parser_fetch_block_scalar(parser, 1); |
990 | ||
991 | /* Is it a folded scalar? */ | |
992 | ||
e35af832 | 993 | if (CHECK(parser->buffer, '>') && !parser->flow_level) |
f2b59d4d KS |
994 | return yaml_parser_fetch_block_scalar(parser, 0); |
995 | ||
996 | /* Is it a single-quoted scalar? */ | |
997 | ||
e35af832 | 998 | if (CHECK(parser->buffer, '\'')) |
f2b59d4d KS |
999 | return yaml_parser_fetch_flow_scalar(parser, 1); |
1000 | ||
1001 | /* Is it a double-quoted scalar? */ | |
1002 | ||
e35af832 | 1003 | if (CHECK(parser->buffer, '"')) |
f2b59d4d KS |
1004 | return yaml_parser_fetch_flow_scalar(parser, 0); |
1005 | ||
1006 | /* | |
1007 | * Is it a plain scalar? | |
1008 | * | |
1009 | * A plain scalar may start with any non-blank characters except | |
1010 | * | |
1011 | * '-', '?', ':', ',', '[', ']', '{', '}', | |
1012 | * '#', '&', '*', '!', '|', '>', '\'', '\"', | |
1013 | * '%', '@', '`'. | |
1014 | * | |
7e32c194 KS |
1015 | * In the block context (and, for the '-' indicator, in the flow context |
1016 | * too), it may also start with the characters | |
f2b59d4d KS |
1017 | * |
1018 | * '-', '?', ':' | |
1019 | * | |
1020 | * if it is followed by a non-space character. | |
1021 | * | |
1022 | * The last rule is more restrictive than the specification requires. | |
1023 | */ | |
1024 | ||
e35af832 KS |
1025 | if (!(IS_BLANKZ(parser->buffer) || CHECK(parser->buffer, '-') |
1026 | || CHECK(parser->buffer, '?') || CHECK(parser->buffer, ':') | |
1027 | || CHECK(parser->buffer, ',') || CHECK(parser->buffer, '[') | |
1028 | || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '{') | |
1029 | || CHECK(parser->buffer, '}') || CHECK(parser->buffer, '#') | |
1030 | || CHECK(parser->buffer, '&') || CHECK(parser->buffer, '*') | |
1031 | || CHECK(parser->buffer, '!') || CHECK(parser->buffer, '|') | |
1032 | || CHECK(parser->buffer, '>') || CHECK(parser->buffer, '\'') | |
1033 | || CHECK(parser->buffer, '"') || CHECK(parser->buffer, '%') | |
1034 | || CHECK(parser->buffer, '@') || CHECK(parser->buffer, '`')) || | |
1035 | (CHECK(parser->buffer, '-') && !IS_BLANK_AT(parser->buffer, 1)) || | |
f2b59d4d | 1036 | (!parser->flow_level && |
e35af832 KS |
1037 | (CHECK(parser->buffer, '?') || CHECK(parser->buffer, ':')) |
1038 | && !IS_BLANKZ_AT(parser->buffer, 1))) | |
f2b59d4d KS |
1039 | return yaml_parser_fetch_plain_scalar(parser); |
1040 | ||
1041 | /* | |
1042 | * If we don't determine the token type so far, it is an error. | |
1043 | */ | |
1044 | ||
625fcfe9 KS |
1045 | return yaml_parser_set_scanner_error(parser, |
1046 | "while scanning for the next token", parser->mark, | |
1047 | "found character that cannot start any token"); | |
f2b59d4d KS |
1048 | } |
1049 | ||
eb9cceb5 KS |
1050 | /* |
1051 | * Check the list of potential simple keys and remove the positions that | |
1052 | * cannot contain simple keys anymore. | |
1053 | */ | |
1054 | ||
1055 | static int | |
1056 | yaml_parser_stale_simple_keys(yaml_parser_t *parser) | |
1057 | { | |
625fcfe9 | 1058 | yaml_simple_key_t *simple_key; |
eb9cceb5 KS |
1059 | |
1060 | /* Check for a potential simple key for each flow level. */ | |
1061 | ||
625fcfe9 KS |
1062 | for (simple_key = parser->simple_keys.start; |
1063 | simple_key != parser->simple_keys.top; simple_key ++) | |
eb9cceb5 | 1064 | { |
eb9cceb5 KS |
1065 | /* |
1066 | * The specification requires that a simple key | |
1067 | * | |
1068 | * - is limited to a single line, | |
1069 | * - is shorter than 1024 characters. | |
1070 | */ | |
1071 | ||
625fcfe9 KS |
1072 | if (simple_key->possible |
1073 | && (simple_key->mark.line < parser->mark.line | |
1074 | || simple_key->mark.index+1024 < parser->mark.index)) { | |
eb9cceb5 KS |
1075 | |
1076 | /* Check if the potential simple key to be removed is required. */ | |
1077 | ||
1078 | if (simple_key->required) { | |
1079 | return yaml_parser_set_scanner_error(parser, | |
1080 | "while scanning a simple key", simple_key->mark, | |
6be8109b | 1081 | "could not find expected ':'"); |
eb9cceb5 KS |
1082 | } |
1083 | ||
625fcfe9 | 1084 | simple_key->possible = 0; |
eb9cceb5 KS |
1085 | } |
1086 | } | |
1087 | ||
1088 | return 1; | |
1089 | } | |
1090 | ||
1091 | /* | |
1092 | * Check if a simple key may start at the current position and add it if | |
1093 | * needed. | |
1094 | */ | |
1095 | ||
1096 | static int | |
1097 | yaml_parser_save_simple_key(yaml_parser_t *parser) | |
1098 | { | |
1099 | /* | |
1100 | * A simple key is required at the current position if the scanner is in | |
1101 | * the block context and the current column coincides with the indentation | |
1102 | * level. | |
1103 | */ | |
1104 | ||
625fcfe9 | 1105 | int required = (!parser->flow_level |
c201bf64 | 1106 | && parser->indent == (ptrdiff_t)parser->mark.column); |
eb9cceb5 | 1107 | |
eb9cceb5 KS |
1108 | /* |
1109 | * If the current position may start a simple key, save it. | |
1110 | */ | |
1111 | ||
1112 | if (parser->simple_key_allowed) | |
1113 | { | |
252c575a KS |
1114 | yaml_simple_key_t simple_key; |
1115 | simple_key.possible = 1; | |
1116 | simple_key.required = required; | |
986dbde7 | 1117 | simple_key.token_number = |
3b160b60 | 1118 | parser->tokens_parsed + (parser->tokens.tail - parser->tokens.head); |
0174ed6e | 1119 | simple_key.mark = parser->mark; |
eb9cceb5 KS |
1120 | |
1121 | if (!yaml_parser_remove_simple_key(parser)) return 0; | |
1122 | ||
625fcfe9 | 1123 | *(parser->simple_keys.top-1) = simple_key; |
eb9cceb5 KS |
1124 | } |
1125 | ||
1126 | return 1; | |
1127 | } | |
1128 | ||
1129 | /* | |
1130 | * Remove a potential simple key at the current flow level. | |
1131 | */ | |
1132 | ||
1133 | static int | |
1134 | yaml_parser_remove_simple_key(yaml_parser_t *parser) | |
1135 | { | |
625fcfe9 | 1136 | yaml_simple_key_t *simple_key = parser->simple_keys.top-1; |
eb9cceb5 | 1137 | |
625fcfe9 | 1138 | if (simple_key->possible) |
eb9cceb5 KS |
1139 | { |
1140 | /* If the key is required, it is an error. */ | |
1141 | ||
1142 | if (simple_key->required) { | |
1143 | return yaml_parser_set_scanner_error(parser, | |
1144 | "while scanning a simple key", simple_key->mark, | |
6be8109b | 1145 | "could not find expected ':'"); |
eb9cceb5 | 1146 | } |
625fcfe9 | 1147 | } |
eb9cceb5 | 1148 | |
625fcfe9 | 1149 | /* Remove the key from the stack. */ |
eb9cceb5 | 1150 | |
625fcfe9 | 1151 | simple_key->possible = 0; |
eb9cceb5 KS |
1152 | |
1153 | return 1; | |
1154 | } | |
1155 | ||
1156 | /* | |
1157 | * Increase the flow level and resize the simple key list if needed. | |
1158 | */ | |
1159 | ||
1160 | static int | |
1161 | yaml_parser_increase_flow_level(yaml_parser_t *parser) | |
1162 | { | |
625fcfe9 | 1163 | yaml_simple_key_t empty_simple_key = { 0, 0, 0, { 0, 0, 0 } }; |
eb9cceb5 | 1164 | |
625fcfe9 | 1165 | /* Reset the simple key on the next level. */ |
eb9cceb5 | 1166 | |
625fcfe9 KS |
1167 | if (!PUSH(parser, parser->simple_keys, empty_simple_key)) |
1168 | return 0; | |
1169 | ||
1170 | /* Increase the flow level. */ | |
eb9cceb5 | 1171 | |
1ef11717 KS |
1172 | if (parser->flow_level == INT_MAX) { |
1173 | parser->error = YAML_MEMORY_ERROR; | |
c201bf64 | 1174 | return 0; |
1ef11717 | 1175 | } |
c201bf64 | 1176 | |
625fcfe9 | 1177 | parser->flow_level++; |
eb9cceb5 KS |
1178 | |
1179 | return 1; | |
1180 | } | |
1181 | ||
1182 | /* | |
1183 | * Decrease the flow level. | |
1184 | */ | |
1185 | ||
1186 | static int | |
1187 | yaml_parser_decrease_flow_level(yaml_parser_t *parser) | |
1188 | { | |
625fcfe9 KS |
1189 | if (parser->flow_level) { |
1190 | parser->flow_level --; | |
fc2dd942 | 1191 | (void)POP(parser, parser->simple_keys); |
eb9cceb5 KS |
1192 | } |
1193 | ||
eb9cceb5 KS |
1194 | return 1; |
1195 | } | |
1196 | ||
1197 | /* | |
1198 | * Push the current indentation level to the stack and set the new level | |
1199 | * the current column is greater than the indentation level. In this case, | |
1200 | * append or insert the specified token into the token queue. | |
986dbde7 | 1201 | * |
eb9cceb5 KS |
1202 | */ |
1203 | ||
1204 | static int | |
c201bf64 KS |
1205 | yaml_parser_roll_indent(yaml_parser_t *parser, ptrdiff_t column, |
1206 | ptrdiff_t number, yaml_token_type_t type, yaml_mark_t mark) | |
eb9cceb5 | 1207 | { |
625fcfe9 | 1208 | yaml_token_t token; |
eb9cceb5 KS |
1209 | |
1210 | /* In the flow context, do nothing. */ | |
1211 | ||
1212 | if (parser->flow_level) | |
1213 | return 1; | |
1214 | ||
1215 | if (parser->indent < column) | |
1216 | { | |
eb9cceb5 KS |
1217 | /* |
1218 | * Push the current indentation level to the stack and set the new | |
1219 | * indentation level. | |
1220 | */ | |
1221 | ||
625fcfe9 KS |
1222 | if (!PUSH(parser, parser->indents, parser->indent)) |
1223 | return 0; | |
1224 | ||
1ef11717 KS |
1225 | if (column > INT_MAX) { |
1226 | parser->error = YAML_MEMORY_ERROR; | |
c201bf64 | 1227 | return 0; |
1ef11717 | 1228 | } |
c201bf64 | 1229 | |
eb9cceb5 KS |
1230 | parser->indent = column; |
1231 | ||
625fcfe9 | 1232 | /* Create a token and insert it into the queue. */ |
eb9cceb5 | 1233 | |
625fcfe9 | 1234 | TOKEN_INIT(token, type, mark, mark); |
eb9cceb5 | 1235 | |
625fcfe9 KS |
1236 | if (number == -1) { |
1237 | if (!ENQUEUE(parser, parser->tokens, token)) | |
1238 | return 0; | |
1239 | } | |
1240 | else { | |
1241 | if (!QUEUE_INSERT(parser, | |
1242 | parser->tokens, number - parser->tokens_parsed, token)) | |
1243 | return 0; | |
eb9cceb5 KS |
1244 | } |
1245 | } | |
1246 | ||
1247 | return 1; | |
1248 | } | |
1249 | ||
1250 | /* | |
1251 | * Pop indentation levels from the indents stack until the current level | |
bdf4d192 | 1252 | * becomes less or equal to the column. For each indentation level, append |
eb9cceb5 KS |
1253 | * the BLOCK-END token. |
1254 | */ | |
1255 | ||
1256 | ||
1257 | static int | |
c201bf64 | 1258 | yaml_parser_unroll_indent(yaml_parser_t *parser, ptrdiff_t column) |
eb9cceb5 | 1259 | { |
625fcfe9 | 1260 | yaml_token_t token; |
eb9cceb5 KS |
1261 | |
1262 | /* In the flow context, do nothing. */ | |
1263 | ||
1264 | if (parser->flow_level) | |
1265 | return 1; | |
1266 | ||
bdf4d192 | 1267 | /* Loop through the indentation levels in the stack. */ |
eb9cceb5 KS |
1268 | |
1269 | while (parser->indent > column) | |
1270 | { | |
625fcfe9 | 1271 | /* Create a token and append it to the queue. */ |
eb9cceb5 | 1272 | |
625fcfe9 | 1273 | TOKEN_INIT(token, YAML_BLOCK_END_TOKEN, parser->mark, parser->mark); |
eb9cceb5 | 1274 | |
625fcfe9 | 1275 | if (!ENQUEUE(parser, parser->tokens, token)) |
eb9cceb5 | 1276 | return 0; |
eb9cceb5 KS |
1277 | |
1278 | /* Pop the indentation level. */ | |
1279 | ||
625fcfe9 | 1280 | parser->indent = POP(parser, parser->indents); |
eb9cceb5 KS |
1281 | } |
1282 | ||
1283 | return 1; | |
1284 | } | |
1285 | ||
1286 | /* | |
1287 | * Initialize the scanner and produce the STREAM-START token. | |
1288 | */ | |
1289 | ||
1290 | static int | |
1291 | yaml_parser_fetch_stream_start(yaml_parser_t *parser) | |
1292 | { | |
625fcfe9 KS |
1293 | yaml_simple_key_t simple_key = { 0, 0, 0, { 0, 0, 0 } }; |
1294 | yaml_token_t token; | |
eb9cceb5 KS |
1295 | |
1296 | /* Set the initial indentation. */ | |
1297 | ||
1298 | parser->indent = -1; | |
1299 | ||
625fcfe9 KS |
1300 | /* Initialize the simple key stack. */ |
1301 | ||
1302 | if (!PUSH(parser, parser->simple_keys, simple_key)) | |
1303 | return 0; | |
1304 | ||
eb9cceb5 KS |
1305 | /* A simple key is allowed at the beginning of the stream. */ |
1306 | ||
1307 | parser->simple_key_allowed = 1; | |
1308 | ||
1309 | /* We have started. */ | |
1310 | ||
1311 | parser->stream_start_produced = 1; | |
1312 | ||
625fcfe9 | 1313 | /* Create the STREAM-START token and append it to the queue. */ |
eb9cceb5 | 1314 | |
625fcfe9 KS |
1315 | STREAM_START_TOKEN_INIT(token, parser->encoding, |
1316 | parser->mark, parser->mark); | |
eb9cceb5 | 1317 | |
625fcfe9 | 1318 | if (!ENQUEUE(parser, parser->tokens, token)) |
eb9cceb5 | 1319 | return 0; |
eb9cceb5 KS |
1320 | |
1321 | return 1; | |
1322 | } | |
1323 | ||
1324 | /* | |
1325 | * Produce the STREAM-END token and shut down the scanner. | |
1326 | */ | |
1327 | ||
1328 | static int | |
1329 | yaml_parser_fetch_stream_end(yaml_parser_t *parser) | |
1330 | { | |
625fcfe9 | 1331 | yaml_token_t token; |
eb9cceb5 | 1332 | |
c83b67a6 KS |
1333 | /* Force new line. */ |
1334 | ||
1335 | if (parser->mark.column != 0) { | |
1336 | parser->mark.column = 0; | |
1337 | parser->mark.line ++; | |
1338 | } | |
1339 | ||
eb9cceb5 KS |
1340 | /* Reset the indentation level. */ |
1341 | ||
1342 | if (!yaml_parser_unroll_indent(parser, -1)) | |
1343 | return 0; | |
1344 | ||
7e32c194 | 1345 | /* Reset simple keys. */ |
eb9cceb5 | 1346 | |
7e32c194 KS |
1347 | if (!yaml_parser_remove_simple_key(parser)) |
1348 | return 0; | |
1349 | ||
1350 | parser->simple_key_allowed = 0; | |
eb9cceb5 | 1351 | |
625fcfe9 | 1352 | /* Create the STREAM-END token and append it to the queue. */ |
eb9cceb5 | 1353 | |
625fcfe9 | 1354 | STREAM_END_TOKEN_INIT(token, parser->mark, parser->mark); |
eb9cceb5 | 1355 | |
625fcfe9 | 1356 | if (!ENQUEUE(parser, parser->tokens, token)) |
eb9cceb5 | 1357 | return 0; |
eb9cceb5 KS |
1358 | |
1359 | return 1; | |
1360 | } | |
1361 | ||
1362 | /* | |
625fcfe9 | 1363 | * Produce a VERSION-DIRECTIVE or TAG-DIRECTIVE token. |
eb9cceb5 KS |
1364 | */ |
1365 | ||
1366 | static int | |
1367 | yaml_parser_fetch_directive(yaml_parser_t *parser) | |
1368 | { | |
625fcfe9 | 1369 | yaml_token_t token; |
eb9cceb5 KS |
1370 | |
1371 | /* Reset the indentation level. */ | |
1372 | ||
1373 | if (!yaml_parser_unroll_indent(parser, -1)) | |
1374 | return 0; | |
1375 | ||
1376 | /* Reset simple keys. */ | |
1377 | ||
1378 | if (!yaml_parser_remove_simple_key(parser)) | |
1379 | return 0; | |
1380 | ||
1381 | parser->simple_key_allowed = 0; | |
1382 | ||
1383 | /* Create the YAML-DIRECTIVE or TAG-DIRECTIVE token. */ | |
1384 | ||
625fcfe9 KS |
1385 | if (!yaml_parser_scan_directive(parser, &token)) |
1386 | return 0; | |
eb9cceb5 KS |
1387 | |
1388 | /* Append the token to the queue. */ | |
1389 | ||
625fcfe9 KS |
1390 | if (!ENQUEUE(parser, parser->tokens, token)) { |
1391 | yaml_token_delete(&token); | |
eb9cceb5 KS |
1392 | return 0; |
1393 | } | |
1394 | ||
1395 | return 1; | |
1396 | } | |
1397 | ||
1398 | /* | |
1399 | * Produce the DOCUMENT-START or DOCUMENT-END token. | |
1400 | */ | |
1401 | ||
1402 | static int | |
1403 | yaml_parser_fetch_document_indicator(yaml_parser_t *parser, | |
1404 | yaml_token_type_t type) | |
1405 | { | |
1406 | yaml_mark_t start_mark, end_mark; | |
625fcfe9 | 1407 | yaml_token_t token; |
eb9cceb5 KS |
1408 | |
1409 | /* Reset the indentation level. */ | |
1410 | ||
1411 | if (!yaml_parser_unroll_indent(parser, -1)) | |
1412 | return 0; | |
1413 | ||
1414 | /* Reset simple keys. */ | |
1415 | ||
1416 | if (!yaml_parser_remove_simple_key(parser)) | |
1417 | return 0; | |
1418 | ||
1419 | parser->simple_key_allowed = 0; | |
1420 | ||
1421 | /* Consume the token. */ | |
1422 | ||
625fcfe9 | 1423 | start_mark = parser->mark; |
eb9cceb5 | 1424 | |
625fcfe9 KS |
1425 | SKIP(parser); |
1426 | SKIP(parser); | |
1427 | SKIP(parser); | |
eb9cceb5 | 1428 | |
625fcfe9 | 1429 | end_mark = parser->mark; |
eb9cceb5 KS |
1430 | |
1431 | /* Create the DOCUMENT-START or DOCUMENT-END token. */ | |
1432 | ||
625fcfe9 | 1433 | TOKEN_INIT(token, type, start_mark, end_mark); |
eb9cceb5 KS |
1434 | |
1435 | /* Append the token to the queue. */ | |
1436 | ||
625fcfe9 | 1437 | if (!ENQUEUE(parser, parser->tokens, token)) |
eb9cceb5 | 1438 | return 0; |
eb9cceb5 KS |
1439 | |
1440 | return 1; | |
1441 | } | |
1442 | ||
1443 | /* | |
1444 | * Produce the FLOW-SEQUENCE-START or FLOW-MAPPING-START token. | |
1445 | */ | |
1446 | ||
1447 | static int | |
1448 | yaml_parser_fetch_flow_collection_start(yaml_parser_t *parser, | |
1449 | yaml_token_type_t type) | |
1450 | { | |
1451 | yaml_mark_t start_mark, end_mark; | |
625fcfe9 | 1452 | yaml_token_t token; |
eb9cceb5 KS |
1453 | |
1454 | /* The indicators '[' and '{' may start a simple key. */ | |
1455 | ||
1456 | if (!yaml_parser_save_simple_key(parser)) | |
1457 | return 0; | |
1458 | ||
1459 | /* Increase the flow level. */ | |
1460 | ||
1461 | if (!yaml_parser_increase_flow_level(parser)) | |
1462 | return 0; | |
1463 | ||
1464 | /* A simple key may follow the indicators '[' and '{'. */ | |
1465 | ||
1466 | parser->simple_key_allowed = 1; | |
1467 | ||
1468 | /* Consume the token. */ | |
1469 | ||
625fcfe9 KS |
1470 | start_mark = parser->mark; |
1471 | SKIP(parser); | |
1472 | end_mark = parser->mark; | |
eb9cceb5 KS |
1473 | |
1474 | /* Create the FLOW-SEQUENCE-START of FLOW-MAPPING-START token. */ | |
1475 | ||
625fcfe9 | 1476 | TOKEN_INIT(token, type, start_mark, end_mark); |
eb9cceb5 KS |
1477 | |
1478 | /* Append the token to the queue. */ | |
1479 | ||
625fcfe9 | 1480 | if (!ENQUEUE(parser, parser->tokens, token)) |
eb9cceb5 | 1481 | return 0; |
eb9cceb5 KS |
1482 | |
1483 | return 1; | |
1484 | } | |
1485 | ||
1486 | /* | |
1487 | * Produce the FLOW-SEQUENCE-END or FLOW-MAPPING-END token. | |
1488 | */ | |
1489 | ||
1490 | static int | |
1491 | yaml_parser_fetch_flow_collection_end(yaml_parser_t *parser, | |
1492 | yaml_token_type_t type) | |
1493 | { | |
1494 | yaml_mark_t start_mark, end_mark; | |
625fcfe9 | 1495 | yaml_token_t token; |
eb9cceb5 KS |
1496 | |
1497 | /* Reset any potential simple key on the current flow level. */ | |
1498 | ||
1499 | if (!yaml_parser_remove_simple_key(parser)) | |
1500 | return 0; | |
1501 | ||
1502 | /* Decrease the flow level. */ | |
1503 | ||
1504 | if (!yaml_parser_decrease_flow_level(parser)) | |
1505 | return 0; | |
1506 | ||
1507 | /* No simple keys after the indicators ']' and '}'. */ | |
1508 | ||
1509 | parser->simple_key_allowed = 0; | |
1510 | ||
1511 | /* Consume the token. */ | |
1512 | ||
625fcfe9 KS |
1513 | start_mark = parser->mark; |
1514 | SKIP(parser); | |
1515 | end_mark = parser->mark; | |
eb9cceb5 KS |
1516 | |
1517 | /* Create the FLOW-SEQUENCE-END of FLOW-MAPPING-END token. */ | |
1518 | ||
625fcfe9 | 1519 | TOKEN_INIT(token, type, start_mark, end_mark); |
eb9cceb5 KS |
1520 | |
1521 | /* Append the token to the queue. */ | |
1522 | ||
625fcfe9 | 1523 | if (!ENQUEUE(parser, parser->tokens, token)) |
eb9cceb5 | 1524 | return 0; |
eb9cceb5 KS |
1525 | |
1526 | return 1; | |
1527 | } | |
1528 | ||
1529 | /* | |
1530 | * Produce the FLOW-ENTRY token. | |
1531 | */ | |
1532 | ||
1533 | static int | |
1534 | yaml_parser_fetch_flow_entry(yaml_parser_t *parser) | |
1535 | { | |
1536 | yaml_mark_t start_mark, end_mark; | |
625fcfe9 | 1537 | yaml_token_t token; |
eb9cceb5 KS |
1538 | |
1539 | /* Reset any potential simple keys on the current flow level. */ | |
1540 | ||
1541 | if (!yaml_parser_remove_simple_key(parser)) | |
1542 | return 0; | |
1543 | ||
1544 | /* Simple keys are allowed after ','. */ | |
1545 | ||
1546 | parser->simple_key_allowed = 1; | |
1547 | ||
1548 | /* Consume the token. */ | |
1549 | ||
625fcfe9 KS |
1550 | start_mark = parser->mark; |
1551 | SKIP(parser); | |
1552 | end_mark = parser->mark; | |
eb9cceb5 | 1553 | |
625fcfe9 | 1554 | /* Create the FLOW-ENTRY token and append it to the queue. */ |
eb9cceb5 | 1555 | |
625fcfe9 | 1556 | TOKEN_INIT(token, YAML_FLOW_ENTRY_TOKEN, start_mark, end_mark); |
eb9cceb5 | 1557 | |
625fcfe9 | 1558 | if (!ENQUEUE(parser, parser->tokens, token)) |
eb9cceb5 | 1559 | return 0; |
eb9cceb5 KS |
1560 | |
1561 | return 1; | |
1562 | } | |
1563 | ||
1564 | /* | |
1565 | * Produce the BLOCK-ENTRY token. | |
1566 | */ | |
1567 | ||
1568 | static int | |
1569 | yaml_parser_fetch_block_entry(yaml_parser_t *parser) | |
1570 | { | |
1571 | yaml_mark_t start_mark, end_mark; | |
625fcfe9 | 1572 | yaml_token_t token; |
eb9cceb5 KS |
1573 | |
1574 | /* Check if the scanner is in the block context. */ | |
1575 | ||
1576 | if (!parser->flow_level) | |
1577 | { | |
1578 | /* Check if we are allowed to start a new entry. */ | |
1579 | ||
1580 | if (!parser->simple_key_allowed) { | |
625fcfe9 | 1581 | return yaml_parser_set_scanner_error(parser, NULL, parser->mark, |
eb9cceb5 KS |
1582 | "block sequence entries are not allowed in this context"); |
1583 | } | |
1584 | ||
1585 | /* Add the BLOCK-SEQUENCE-START token if needed. */ | |
1586 | ||
625fcfe9 KS |
1587 | if (!yaml_parser_roll_indent(parser, parser->mark.column, -1, |
1588 | YAML_BLOCK_SEQUENCE_START_TOKEN, parser->mark)) | |
eb9cceb5 KS |
1589 | return 0; |
1590 | } | |
1591 | else | |
1592 | { | |
1593 | /* | |
1594 | * It is an error for the '-' indicator to occur in the flow context, | |
1595 | * but we let the Parser detect and report about it because the Parser | |
1596 | * is able to point to the context. | |
1597 | */ | |
1598 | } | |
1599 | ||
1600 | /* Reset any potential simple keys on the current flow level. */ | |
1601 | ||
1602 | if (!yaml_parser_remove_simple_key(parser)) | |
1603 | return 0; | |
1604 | ||
1605 | /* Simple keys are allowed after '-'. */ | |
1606 | ||
1607 | parser->simple_key_allowed = 1; | |
1608 | ||
1609 | /* Consume the token. */ | |
1610 | ||
625fcfe9 KS |
1611 | start_mark = parser->mark; |
1612 | SKIP(parser); | |
1613 | end_mark = parser->mark; | |
eb9cceb5 | 1614 | |
625fcfe9 | 1615 | /* Create the BLOCK-ENTRY token and append it to the queue. */ |
eb9cceb5 | 1616 | |
625fcfe9 | 1617 | TOKEN_INIT(token, YAML_BLOCK_ENTRY_TOKEN, start_mark, end_mark); |
eb9cceb5 | 1618 | |
625fcfe9 | 1619 | if (!ENQUEUE(parser, parser->tokens, token)) |
eb9cceb5 | 1620 | return 0; |
eb9cceb5 KS |
1621 | |
1622 | return 1; | |
1623 | } | |
1624 | ||
1625 | /* | |
1626 | * Produce the KEY token. | |
1627 | */ | |
1628 | ||
1629 | static int | |
1630 | yaml_parser_fetch_key(yaml_parser_t *parser) | |
1631 | { | |
1632 | yaml_mark_t start_mark, end_mark; | |
625fcfe9 | 1633 | yaml_token_t token; |
eb9cceb5 KS |
1634 | |
1635 | /* In the block context, additional checks are required. */ | |
1636 | ||
1637 | if (!parser->flow_level) | |
1638 | { | |
1639 | /* Check if we are allowed to start a new key (not nessesary simple). */ | |
1640 | ||
1641 | if (!parser->simple_key_allowed) { | |
625fcfe9 | 1642 | return yaml_parser_set_scanner_error(parser, NULL, parser->mark, |
eb9cceb5 KS |
1643 | "mapping keys are not allowed in this context"); |
1644 | } | |
1645 | ||
1646 | /* Add the BLOCK-MAPPING-START token if needed. */ | |
1647 | ||
625fcfe9 KS |
1648 | if (!yaml_parser_roll_indent(parser, parser->mark.column, -1, |
1649 | YAML_BLOCK_MAPPING_START_TOKEN, parser->mark)) | |
eb9cceb5 KS |
1650 | return 0; |
1651 | } | |
1652 | ||
1653 | /* Reset any potential simple keys on the current flow level. */ | |
1654 | ||
1655 | if (!yaml_parser_remove_simple_key(parser)) | |
1656 | return 0; | |
1657 | ||
1658 | /* Simple keys are allowed after '?' in the block context. */ | |
1659 | ||
1660 | parser->simple_key_allowed = (!parser->flow_level); | |
1661 | ||
1662 | /* Consume the token. */ | |
1663 | ||
625fcfe9 KS |
1664 | start_mark = parser->mark; |
1665 | SKIP(parser); | |
1666 | end_mark = parser->mark; | |
eb9cceb5 | 1667 | |
625fcfe9 | 1668 | /* Create the KEY token and append it to the queue. */ |
eb9cceb5 | 1669 | |
625fcfe9 | 1670 | TOKEN_INIT(token, YAML_KEY_TOKEN, start_mark, end_mark); |
eb9cceb5 | 1671 | |
625fcfe9 | 1672 | if (!ENQUEUE(parser, parser->tokens, token)) |
eb9cceb5 | 1673 | return 0; |
eb9cceb5 KS |
1674 | |
1675 | return 1; | |
1676 | } | |
1677 | ||
1678 | /* | |
1679 | * Produce the VALUE token. | |
1680 | */ | |
1681 | ||
1682 | static int | |
1683 | yaml_parser_fetch_value(yaml_parser_t *parser) | |
1684 | { | |
1685 | yaml_mark_t start_mark, end_mark; | |
625fcfe9 KS |
1686 | yaml_token_t token; |
1687 | yaml_simple_key_t *simple_key = parser->simple_keys.top-1; | |
eb9cceb5 KS |
1688 | |
1689 | /* Have we found a simple key? */ | |
1690 | ||
625fcfe9 | 1691 | if (simple_key->possible) |
eb9cceb5 | 1692 | { |
eb9cceb5 | 1693 | |
625fcfe9 | 1694 | /* Create the KEY token and insert it into the queue. */ |
eb9cceb5 | 1695 | |
625fcfe9 | 1696 | TOKEN_INIT(token, YAML_KEY_TOKEN, simple_key->mark, simple_key->mark); |
eb9cceb5 | 1697 | |
625fcfe9 KS |
1698 | if (!QUEUE_INSERT(parser, parser->tokens, |
1699 | simple_key->token_number - parser->tokens_parsed, token)) | |
eb9cceb5 | 1700 | return 0; |
eb9cceb5 KS |
1701 | |
1702 | /* In the block context, we may need to add the BLOCK-MAPPING-START token. */ | |
1703 | ||
625fcfe9 | 1704 | if (!yaml_parser_roll_indent(parser, simple_key->mark.column, |
eb9cceb5 KS |
1705 | simple_key->token_number, |
1706 | YAML_BLOCK_MAPPING_START_TOKEN, simple_key->mark)) | |
1707 | return 0; | |
1708 | ||
625fcfe9 | 1709 | /* Remove the simple key. */ |
eb9cceb5 | 1710 | |
625fcfe9 | 1711 | simple_key->possible = 0; |
eb9cceb5 KS |
1712 | |
1713 | /* A simple key cannot follow another simple key. */ | |
1714 | ||
1715 | parser->simple_key_allowed = 0; | |
1716 | } | |
1717 | else | |
1718 | { | |
1719 | /* The ':' indicator follows a complex key. */ | |
1720 | ||
1721 | /* In the block context, extra checks are required. */ | |
1722 | ||
1723 | if (!parser->flow_level) | |
1724 | { | |
1725 | /* Check if we are allowed to start a complex value. */ | |
1726 | ||
1727 | if (!parser->simple_key_allowed) { | |
625fcfe9 | 1728 | return yaml_parser_set_scanner_error(parser, NULL, parser->mark, |
eb9cceb5 KS |
1729 | "mapping values are not allowed in this context"); |
1730 | } | |
1731 | ||
1732 | /* Add the BLOCK-MAPPING-START token if needed. */ | |
1733 | ||
625fcfe9 KS |
1734 | if (!yaml_parser_roll_indent(parser, parser->mark.column, -1, |
1735 | YAML_BLOCK_MAPPING_START_TOKEN, parser->mark)) | |
eb9cceb5 KS |
1736 | return 0; |
1737 | } | |
1738 | ||
eb9cceb5 KS |
1739 | /* Simple keys after ':' are allowed in the block context. */ |
1740 | ||
1741 | parser->simple_key_allowed = (!parser->flow_level); | |
1742 | } | |
1743 | ||
1744 | /* Consume the token. */ | |
1745 | ||
625fcfe9 KS |
1746 | start_mark = parser->mark; |
1747 | SKIP(parser); | |
1748 | end_mark = parser->mark; | |
eb9cceb5 | 1749 | |
625fcfe9 | 1750 | /* Create the VALUE token and append it to the queue. */ |
eb9cceb5 | 1751 | |
625fcfe9 | 1752 | TOKEN_INIT(token, YAML_VALUE_TOKEN, start_mark, end_mark); |
eb9cceb5 | 1753 | |
625fcfe9 | 1754 | if (!ENQUEUE(parser, parser->tokens, token)) |
eb9cceb5 | 1755 | return 0; |
eb9cceb5 KS |
1756 | |
1757 | return 1; | |
1758 | } | |
1759 | ||
1760 | /* | |
1761 | * Produce the ALIAS or ANCHOR token. | |
1762 | */ | |
1763 | ||
1764 | static int | |
1765 | yaml_parser_fetch_anchor(yaml_parser_t *parser, yaml_token_type_t type) | |
1766 | { | |
625fcfe9 | 1767 | yaml_token_t token; |
eb9cceb5 KS |
1768 | |
1769 | /* An anchor or an alias could be a simple key. */ | |
1770 | ||
1771 | if (!yaml_parser_save_simple_key(parser)) | |
1772 | return 0; | |
1773 | ||
1774 | /* A simple key cannot follow an anchor or an alias. */ | |
1775 | ||
1776 | parser->simple_key_allowed = 0; | |
1777 | ||
625fcfe9 | 1778 | /* Create the ALIAS or ANCHOR token and append it to the queue. */ |
eb9cceb5 | 1779 | |
625fcfe9 KS |
1780 | if (!yaml_parser_scan_anchor(parser, &token, type)) |
1781 | return 0; | |
eb9cceb5 | 1782 | |
625fcfe9 KS |
1783 | if (!ENQUEUE(parser, parser->tokens, token)) { |
1784 | yaml_token_delete(&token); | |
eb9cceb5 KS |
1785 | return 0; |
1786 | } | |
eb9cceb5 KS |
1787 | return 1; |
1788 | } | |
1789 | ||
1790 | /* | |
1791 | * Produce the TAG token. | |
1792 | */ | |
1793 | ||
1794 | static int | |
1795 | yaml_parser_fetch_tag(yaml_parser_t *parser) | |
1796 | { | |
625fcfe9 | 1797 | yaml_token_t token; |
eb9cceb5 KS |
1798 | |
1799 | /* A tag could be a simple key. */ | |
1800 | ||
1801 | if (!yaml_parser_save_simple_key(parser)) | |
1802 | return 0; | |
1803 | ||
1804 | /* A simple key cannot follow a tag. */ | |
1805 | ||
1806 | parser->simple_key_allowed = 0; | |
1807 | ||
625fcfe9 | 1808 | /* Create the TAG token and append it to the queue. */ |
eb9cceb5 | 1809 | |
625fcfe9 KS |
1810 | if (!yaml_parser_scan_tag(parser, &token)) |
1811 | return 0; | |
eb9cceb5 | 1812 | |
625fcfe9 KS |
1813 | if (!ENQUEUE(parser, parser->tokens, token)) { |
1814 | yaml_token_delete(&token); | |
eb9cceb5 KS |
1815 | return 0; |
1816 | } | |
1817 | ||
1818 | return 1; | |
1819 | } | |
1820 | ||
1821 | /* | |
1822 | * Produce the SCALAR(...,literal) or SCALAR(...,folded) tokens. | |
1823 | */ | |
1824 | ||
1825 | static int | |
1826 | yaml_parser_fetch_block_scalar(yaml_parser_t *parser, int literal) | |
1827 | { | |
625fcfe9 | 1828 | yaml_token_t token; |
eb9cceb5 KS |
1829 | |
1830 | /* Remove any potential simple keys. */ | |
1831 | ||
1832 | if (!yaml_parser_remove_simple_key(parser)) | |
1833 | return 0; | |
1834 | ||
1835 | /* A simple key may follow a block scalar. */ | |
1836 | ||
1837 | parser->simple_key_allowed = 1; | |
1838 | ||
625fcfe9 | 1839 | /* Create the SCALAR token and append it to the queue. */ |
eb9cceb5 | 1840 | |
625fcfe9 KS |
1841 | if (!yaml_parser_scan_block_scalar(parser, &token, literal)) |
1842 | return 0; | |
eb9cceb5 | 1843 | |
625fcfe9 KS |
1844 | if (!ENQUEUE(parser, parser->tokens, token)) { |
1845 | yaml_token_delete(&token); | |
eb9cceb5 KS |
1846 | return 0; |
1847 | } | |
1848 | ||
1849 | return 1; | |
1850 | } | |
1851 | ||
1852 | /* | |
1853 | * Produce the SCALAR(...,single-quoted) or SCALAR(...,double-quoted) tokens. | |
1854 | */ | |
1855 | ||
1856 | static int | |
1857 | yaml_parser_fetch_flow_scalar(yaml_parser_t *parser, int single) | |
1858 | { | |
625fcfe9 | 1859 | yaml_token_t token; |
eb9cceb5 KS |
1860 | |
1861 | /* A plain scalar could be a simple key. */ | |
1862 | ||
1863 | if (!yaml_parser_save_simple_key(parser)) | |
1864 | return 0; | |
1865 | ||
1866 | /* A simple key cannot follow a flow scalar. */ | |
1867 | ||
1868 | parser->simple_key_allowed = 0; | |
1869 | ||
625fcfe9 | 1870 | /* Create the SCALAR token and append it to the queue. */ |
eb9cceb5 | 1871 | |
625fcfe9 KS |
1872 | if (!yaml_parser_scan_flow_scalar(parser, &token, single)) |
1873 | return 0; | |
eb9cceb5 | 1874 | |
625fcfe9 KS |
1875 | if (!ENQUEUE(parser, parser->tokens, token)) { |
1876 | yaml_token_delete(&token); | |
eb9cceb5 KS |
1877 | return 0; |
1878 | } | |
1879 | ||
1880 | return 1; | |
1881 | } | |
1882 | ||
1883 | /* | |
1884 | * Produce the SCALAR(...,plain) token. | |
1885 | */ | |
1886 | ||
1887 | static int | |
1888 | yaml_parser_fetch_plain_scalar(yaml_parser_t *parser) | |
1889 | { | |
625fcfe9 | 1890 | yaml_token_t token; |
eb9cceb5 KS |
1891 | |
1892 | /* A plain scalar could be a simple key. */ | |
1893 | ||
1894 | if (!yaml_parser_save_simple_key(parser)) | |
1895 | return 0; | |
1896 | ||
1897 | /* A simple key cannot follow a flow scalar. */ | |
1898 | ||
1899 | parser->simple_key_allowed = 0; | |
1900 | ||
625fcfe9 | 1901 | /* Create the SCALAR token and append it to the queue. */ |
eb9cceb5 | 1902 | |
625fcfe9 KS |
1903 | if (!yaml_parser_scan_plain_scalar(parser, &token)) |
1904 | return 0; | |
eb9cceb5 | 1905 | |
625fcfe9 KS |
1906 | if (!ENQUEUE(parser, parser->tokens, token)) { |
1907 | yaml_token_delete(&token); | |
eb9cceb5 KS |
1908 | return 0; |
1909 | } | |
1910 | ||
1911 | return 1; | |
1912 | } | |
1913 | ||
e71095e3 KS |
1914 | /* |
1915 | * Eat whitespaces and comments until the next token is found. | |
1916 | */ | |
1917 | ||
1918 | static int | |
1919 | yaml_parser_scan_to_next_token(yaml_parser_t *parser) | |
1920 | { | |
1921 | /* Until the next token is not found. */ | |
1922 | ||
1923 | while (1) | |
1924 | { | |
1925 | /* Allow the BOM mark to start a line. */ | |
1926 | ||
625fcfe9 | 1927 | if (!CACHE(parser, 1)) return 0; |
e71095e3 | 1928 | |
e35af832 | 1929 | if (parser->mark.column == 0 && IS_BOM(parser->buffer)) |
625fcfe9 | 1930 | SKIP(parser); |
e71095e3 KS |
1931 | |
1932 | /* | |
1933 | * Eat whitespaces. | |
1934 | * | |
1935 | * Tabs are allowed: | |
1936 | * | |
1937 | * - in the flow context; | |
1938 | * - in the block context, but not at the beginning of the line or | |
986dbde7 | 1939 | * after '-', '?', or ':' (complex value). |
e71095e3 KS |
1940 | */ |
1941 | ||
625fcfe9 | 1942 | if (!CACHE(parser, 1)) return 0; |
e71095e3 | 1943 | |
e35af832 | 1944 | while (CHECK(parser->buffer,' ') || |
e71095e3 | 1945 | ((parser->flow_level || !parser->simple_key_allowed) && |
e35af832 | 1946 | CHECK(parser->buffer, '\t'))) { |
625fcfe9 KS |
1947 | SKIP(parser); |
1948 | if (!CACHE(parser, 1)) return 0; | |
e71095e3 KS |
1949 | } |
1950 | ||
1951 | /* Eat a comment until a line break. */ | |
1952 | ||
e35af832 KS |
1953 | if (CHECK(parser->buffer, '#')) { |
1954 | while (!IS_BREAKZ(parser->buffer)) { | |
625fcfe9 KS |
1955 | SKIP(parser); |
1956 | if (!CACHE(parser, 1)) return 0; | |
e71095e3 KS |
1957 | } |
1958 | } | |
1959 | ||
1960 | /* If it is a line break, eat it. */ | |
1961 | ||
e35af832 | 1962 | if (IS_BREAK(parser->buffer)) |
e71095e3 | 1963 | { |
625fcfe9 KS |
1964 | if (!CACHE(parser, 2)) return 0; |
1965 | SKIP_LINE(parser); | |
e71095e3 KS |
1966 | |
1967 | /* In the block context, a new line may start a simple key. */ | |
1968 | ||
1969 | if (!parser->flow_level) { | |
1970 | parser->simple_key_allowed = 1; | |
1971 | } | |
1972 | } | |
1973 | else | |
1974 | { | |
1975 | /* We have found a token. */ | |
1976 | ||
1977 | break; | |
1978 | } | |
1979 | } | |
1980 | ||
1981 | return 1; | |
1982 | } | |
1983 | ||
1984 | /* | |
1985 | * Scan a YAML-DIRECTIVE or TAG-DIRECTIVE token. | |
1986 | * | |
1987 | * Scope: | |
1988 | * %YAML 1.1 # a comment \n | |
1989 | * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
1990 | * %TAG !yaml! tag:yaml.org,2002: \n | |
1991 | * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
1992 | */ | |
1993 | ||
625fcfe9 KS |
1994 | int |
1995 | yaml_parser_scan_directive(yaml_parser_t *parser, yaml_token_t *token) | |
e71095e3 KS |
1996 | { |
1997 | yaml_mark_t start_mark, end_mark; | |
1998 | yaml_char_t *name = NULL; | |
1999 | int major, minor; | |
2000 | yaml_char_t *handle = NULL, *prefix = NULL; | |
e71095e3 KS |
2001 | |
2002 | /* Eat '%'. */ | |
2003 | ||
625fcfe9 | 2004 | start_mark = parser->mark; |
e71095e3 | 2005 | |
625fcfe9 | 2006 | SKIP(parser); |
e71095e3 KS |
2007 | |
2008 | /* Scan the directive name. */ | |
2009 | ||
2010 | if (!yaml_parser_scan_directive_name(parser, start_mark, &name)) | |
2011 | goto error; | |
2012 | ||
2013 | /* Is it a YAML directive? */ | |
2014 | ||
2015 | if (strcmp((char *)name, "YAML") == 0) | |
2016 | { | |
2017 | /* Scan the VERSION directive value. */ | |
2018 | ||
2019 | if (!yaml_parser_scan_version_directive_value(parser, start_mark, | |
2020 | &major, &minor)) | |
2021 | goto error; | |
2022 | ||
625fcfe9 | 2023 | end_mark = parser->mark; |
e71095e3 KS |
2024 | |
2025 | /* Create a VERSION-DIRECTIVE token. */ | |
2026 | ||
625fcfe9 | 2027 | VERSION_DIRECTIVE_TOKEN_INIT(*token, major, minor, |
e71095e3 | 2028 | start_mark, end_mark); |
e71095e3 KS |
2029 | } |
2030 | ||
2031 | /* Is it a TAG directive? */ | |
2032 | ||
2033 | else if (strcmp((char *)name, "TAG") == 0) | |
2034 | { | |
2035 | /* Scan the TAG directive value. */ | |
2036 | ||
2037 | if (!yaml_parser_scan_tag_directive_value(parser, start_mark, | |
2038 | &handle, &prefix)) | |
2039 | goto error; | |
2040 | ||
625fcfe9 | 2041 | end_mark = parser->mark; |
e71095e3 KS |
2042 | |
2043 | /* Create a TAG-DIRECTIVE token. */ | |
2044 | ||
625fcfe9 | 2045 | TAG_DIRECTIVE_TOKEN_INIT(*token, handle, prefix, |
e71095e3 | 2046 | start_mark, end_mark); |
e71095e3 KS |
2047 | } |
2048 | ||
2049 | /* Unknown directive. */ | |
2050 | ||
2051 | else | |
2052 | { | |
92d41fe1 | 2053 | yaml_parser_set_scanner_error(parser, "while scanning a directive", |
bdf4d192 | 2054 | start_mark, "found unknown directive name"); |
e71095e3 KS |
2055 | goto error; |
2056 | } | |
2057 | ||
2058 | /* Eat the rest of the line including any comments. */ | |
2059 | ||
625fcfe9 KS |
2060 | if (!CACHE(parser, 1)) goto error; |
2061 | ||
e35af832 | 2062 | while (IS_BLANK(parser->buffer)) { |
625fcfe9 KS |
2063 | SKIP(parser); |
2064 | if (!CACHE(parser, 1)) goto error; | |
e71095e3 KS |
2065 | } |
2066 | ||
e35af832 KS |
2067 | if (CHECK(parser->buffer, '#')) { |
2068 | while (!IS_BREAKZ(parser->buffer)) { | |
625fcfe9 KS |
2069 | SKIP(parser); |
2070 | if (!CACHE(parser, 1)) goto error; | |
e71095e3 KS |
2071 | } |
2072 | } | |
2073 | ||
2074 | /* Check if we are at the end of the line. */ | |
2075 | ||
e35af832 | 2076 | if (!IS_BREAKZ(parser->buffer)) { |
92d41fe1 | 2077 | yaml_parser_set_scanner_error(parser, "while scanning a directive", |
6be8109b | 2078 | start_mark, "did not find expected comment or line break"); |
e71095e3 KS |
2079 | goto error; |
2080 | } | |
2081 | ||
2082 | /* Eat a line break. */ | |
2083 | ||
e35af832 | 2084 | if (IS_BREAK(parser->buffer)) { |
625fcfe9 KS |
2085 | if (!CACHE(parser, 2)) goto error; |
2086 | SKIP_LINE(parser); | |
e71095e3 KS |
2087 | } |
2088 | ||
2089 | yaml_free(name); | |
2090 | ||
625fcfe9 | 2091 | return 1; |
e71095e3 KS |
2092 | |
2093 | error: | |
e71095e3 KS |
2094 | yaml_free(prefix); |
2095 | yaml_free(handle); | |
2096 | yaml_free(name); | |
625fcfe9 | 2097 | return 0; |
e71095e3 KS |
2098 | } |
2099 | ||
2100 | /* | |
2101 | * Scan the directive name. | |
2102 | * | |
2103 | * Scope: | |
2104 | * %YAML 1.1 # a comment \n | |
2105 | * ^^^^ | |
2106 | * %TAG !yaml! tag:yaml.org,2002: \n | |
2107 | * ^^^ | |
2108 | */ | |
2109 | ||
2110 | static int | |
2111 | yaml_parser_scan_directive_name(yaml_parser_t *parser, | |
2112 | yaml_mark_t start_mark, yaml_char_t **name) | |
2113 | { | |
625fcfe9 | 2114 | yaml_string_t string = NULL_STRING; |
e71095e3 | 2115 | |
625fcfe9 | 2116 | if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error; |
e71095e3 KS |
2117 | |
2118 | /* Consume the directive name. */ | |
2119 | ||
625fcfe9 | 2120 | if (!CACHE(parser, 1)) goto error; |
e71095e3 | 2121 | |
e35af832 | 2122 | while (IS_ALPHA(parser->buffer)) |
e71095e3 | 2123 | { |
625fcfe9 KS |
2124 | if (!READ(parser, string)) goto error; |
2125 | if (!CACHE(parser, 1)) goto error; | |
e71095e3 KS |
2126 | } |
2127 | ||
2128 | /* Check if the name is empty. */ | |
2129 | ||
625fcfe9 | 2130 | if (string.start == string.pointer) { |
e71095e3 | 2131 | yaml_parser_set_scanner_error(parser, "while scanning a directive", |
6be8109b | 2132 | start_mark, "could not find expected directive name"); |
e71095e3 KS |
2133 | goto error; |
2134 | } | |
2135 | ||
2136 | /* Check for an blank character after the name. */ | |
2137 | ||
e35af832 | 2138 | if (!IS_BLANKZ(parser->buffer)) { |
e71095e3 KS |
2139 | yaml_parser_set_scanner_error(parser, "while scanning a directive", |
2140 | start_mark, "found unexpected non-alphabetical character"); | |
2141 | goto error; | |
2142 | } | |
2143 | ||
625fcfe9 | 2144 | *name = string.start; |
e71095e3 KS |
2145 | |
2146 | return 1; | |
2147 | ||
2148 | error: | |
625fcfe9 | 2149 | STRING_DEL(parser, string); |
e71095e3 KS |
2150 | return 0; |
2151 | } | |
2152 | ||
2153 | /* | |
2154 | * Scan the value of VERSION-DIRECTIVE. | |
2155 | * | |
2156 | * Scope: | |
2157 | * %YAML 1.1 # a comment \n | |
2158 | * ^^^^^^ | |
2159 | */ | |
2160 | ||
2161 | static int | |
2162 | yaml_parser_scan_version_directive_value(yaml_parser_t *parser, | |
2163 | yaml_mark_t start_mark, int *major, int *minor) | |
2164 | { | |
2165 | /* Eat whitespaces. */ | |
2166 | ||
625fcfe9 | 2167 | if (!CACHE(parser, 1)) return 0; |
e71095e3 | 2168 | |
e35af832 | 2169 | while (IS_BLANK(parser->buffer)) { |
625fcfe9 KS |
2170 | SKIP(parser); |
2171 | if (!CACHE(parser, 1)) return 0; | |
e71095e3 KS |
2172 | } |
2173 | ||
2174 | /* Consume the major version number. */ | |
2175 | ||
2176 | if (!yaml_parser_scan_version_directive_number(parser, start_mark, major)) | |
2177 | return 0; | |
2178 | ||
2179 | /* Eat '.'. */ | |
2180 | ||
e35af832 | 2181 | if (!CHECK(parser->buffer, '.')) { |
e71095e3 KS |
2182 | return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive", |
2183 | start_mark, "did not find expected digit or '.' character"); | |
2184 | } | |
2185 | ||
625fcfe9 | 2186 | SKIP(parser); |
e71095e3 KS |
2187 | |
2188 | /* Consume the minor version number. */ | |
2189 | ||
2190 | if (!yaml_parser_scan_version_directive_number(parser, start_mark, minor)) | |
2191 | return 0; | |
ab01bac8 KS |
2192 | |
2193 | return 1; | |
e71095e3 KS |
2194 | } |
2195 | ||
2196 | #define MAX_NUMBER_LENGTH 9 | |
2197 | ||
2198 | /* | |
2199 | * Scan the version number of VERSION-DIRECTIVE. | |
2200 | * | |
2201 | * Scope: | |
2202 | * %YAML 1.1 # a comment \n | |
2203 | * ^ | |
2204 | * %YAML 1.1 # a comment \n | |
2205 | * ^ | |
2206 | */ | |
2207 | ||
2208 | static int | |
2209 | yaml_parser_scan_version_directive_number(yaml_parser_t *parser, | |
2210 | yaml_mark_t start_mark, int *number) | |
2211 | { | |
2212 | int value = 0; | |
2213 | size_t length = 0; | |
2214 | ||
2215 | /* Repeat while the next character is digit. */ | |
2216 | ||
625fcfe9 | 2217 | if (!CACHE(parser, 1)) return 0; |
e71095e3 | 2218 | |
e35af832 | 2219 | while (IS_DIGIT(parser->buffer)) |
e71095e3 KS |
2220 | { |
2221 | /* Check if the number is too long. */ | |
2222 | ||
2223 | if (++length > MAX_NUMBER_LENGTH) { | |
2224 | return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive", | |
2225 | start_mark, "found extremely long version number"); | |
2226 | } | |
2227 | ||
e35af832 | 2228 | value = value*10 + AS_DIGIT(parser->buffer); |
e71095e3 | 2229 | |
625fcfe9 | 2230 | SKIP(parser); |
e71095e3 | 2231 | |
625fcfe9 | 2232 | if (!CACHE(parser, 1)) return 0; |
e71095e3 KS |
2233 | } |
2234 | ||
2235 | /* Check if the number was present. */ | |
2236 | ||
2237 | if (!length) { | |
2238 | return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive", | |
2239 | start_mark, "did not find expected version number"); | |
2240 | } | |
2241 | ||
2242 | *number = value; | |
2243 | ||
2244 | return 1; | |
2245 | } | |
2246 | ||
2247 | /* | |
2248 | * Scan the value of a TAG-DIRECTIVE token. | |
2249 | * | |
2250 | * Scope: | |
2251 | * %TAG !yaml! tag:yaml.org,2002: \n | |
2252 | * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
2253 | */ | |
2254 | ||
2255 | static int | |
2256 | yaml_parser_scan_tag_directive_value(yaml_parser_t *parser, | |
2257 | yaml_mark_t start_mark, yaml_char_t **handle, yaml_char_t **prefix) | |
2258 | { | |
2259 | yaml_char_t *handle_value = NULL; | |
2260 | yaml_char_t *prefix_value = NULL; | |
2261 | ||
2262 | /* Eat whitespaces. */ | |
2263 | ||
625fcfe9 | 2264 | if (!CACHE(parser, 1)) goto error; |
e71095e3 | 2265 | |
e35af832 | 2266 | while (IS_BLANK(parser->buffer)) { |
625fcfe9 KS |
2267 | SKIP(parser); |
2268 | if (!CACHE(parser, 1)) goto error; | |
e71095e3 KS |
2269 | } |
2270 | ||
2271 | /* Scan a handle. */ | |
2272 | ||
2273 | if (!yaml_parser_scan_tag_handle(parser, 1, start_mark, &handle_value)) | |
2274 | goto error; | |
2275 | ||
2276 | /* Expect a whitespace. */ | |
2277 | ||
625fcfe9 | 2278 | if (!CACHE(parser, 1)) goto error; |
e71095e3 | 2279 | |
e35af832 | 2280 | if (!IS_BLANK(parser->buffer)) { |
e71095e3 KS |
2281 | yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive", |
2282 | start_mark, "did not find expected whitespace"); | |
2283 | goto error; | |
2284 | } | |
2285 | ||
2286 | /* Eat whitespaces. */ | |
2287 | ||
e35af832 | 2288 | while (IS_BLANK(parser->buffer)) { |
625fcfe9 KS |
2289 | SKIP(parser); |
2290 | if (!CACHE(parser, 1)) goto error; | |
e71095e3 KS |
2291 | } |
2292 | ||
2293 | /* Scan a prefix. */ | |
2294 | ||
2295 | if (!yaml_parser_scan_tag_uri(parser, 1, NULL, start_mark, &prefix_value)) | |
2296 | goto error; | |
2297 | ||
2298 | /* Expect a whitespace or line break. */ | |
2299 | ||
625fcfe9 | 2300 | if (!CACHE(parser, 1)) goto error; |
e71095e3 | 2301 | |
e35af832 | 2302 | if (!IS_BLANKZ(parser->buffer)) { |
e71095e3 KS |
2303 | yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive", |
2304 | start_mark, "did not find expected whitespace or line break"); | |
2305 | goto error; | |
2306 | } | |
2307 | ||
2308 | *handle = handle_value; | |
2309 | *prefix = prefix_value; | |
2310 | ||
2311 | return 1; | |
2312 | ||
2313 | error: | |
2314 | yaml_free(handle_value); | |
2315 | yaml_free(prefix_value); | |
2316 | return 0; | |
2317 | } | |
2318 | ||
625fcfe9 KS |
2319 | static int |
2320 | yaml_parser_scan_anchor(yaml_parser_t *parser, yaml_token_t *token, | |
e71095e3 KS |
2321 | yaml_token_type_t type) |
2322 | { | |
2323 | int length = 0; | |
2324 | yaml_mark_t start_mark, end_mark; | |
625fcfe9 | 2325 | yaml_string_t string = NULL_STRING; |
e71095e3 | 2326 | |
625fcfe9 | 2327 | if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error; |
e71095e3 KS |
2328 | |
2329 | /* Eat the indicator character. */ | |
2330 | ||
625fcfe9 | 2331 | start_mark = parser->mark; |
e71095e3 | 2332 | |
625fcfe9 | 2333 | SKIP(parser); |
e71095e3 KS |
2334 | |
2335 | /* Consume the value. */ | |
2336 | ||
625fcfe9 | 2337 | if (!CACHE(parser, 1)) goto error; |
e71095e3 | 2338 | |
e35af832 | 2339 | while (IS_ALPHA(parser->buffer)) { |
625fcfe9 KS |
2340 | if (!READ(parser, string)) goto error; |
2341 | if (!CACHE(parser, 1)) goto error; | |
e71095e3 KS |
2342 | length ++; |
2343 | } | |
2344 | ||
625fcfe9 | 2345 | end_mark = parser->mark; |
e71095e3 KS |
2346 | |
2347 | /* | |
2348 | * Check if length of the anchor is greater than 0 and it is followed by | |
2349 | * a whitespace character or one of the indicators: | |
2350 | * | |
2351 | * '?', ':', ',', ']', '}', '%', '@', '`'. | |
2352 | */ | |
2353 | ||
e35af832 KS |
2354 | if (!length || !(IS_BLANKZ(parser->buffer) || CHECK(parser->buffer, '?') |
2355 | || CHECK(parser->buffer, ':') || CHECK(parser->buffer, ',') | |
2356 | || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '}') | |
2357 | || CHECK(parser->buffer, '%') || CHECK(parser->buffer, '@') | |
2358 | || CHECK(parser->buffer, '`'))) { | |
e71095e3 KS |
2359 | yaml_parser_set_scanner_error(parser, type == YAML_ANCHOR_TOKEN ? |
2360 | "while scanning an anchor" : "while scanning an alias", start_mark, | |
2361 | "did not find expected alphabetic or numeric character"); | |
2362 | goto error; | |
2363 | } | |
2364 | ||
2365 | /* Create a token. */ | |
2366 | ||
625fcfe9 KS |
2367 | if (type == YAML_ANCHOR_TOKEN) { |
2368 | ANCHOR_TOKEN_INIT(*token, string.start, start_mark, end_mark); | |
2369 | } | |
2370 | else { | |
2371 | ALIAS_TOKEN_INIT(*token, string.start, start_mark, end_mark); | |
92d41fe1 | 2372 | } |
e71095e3 | 2373 | |
625fcfe9 | 2374 | return 1; |
e71095e3 KS |
2375 | |
2376 | error: | |
625fcfe9 | 2377 | STRING_DEL(parser, string); |
e71095e3 KS |
2378 | return 0; |
2379 | } | |
2380 | ||
2381 | /* | |
2382 | * Scan a TAG token. | |
2383 | */ | |
2384 | ||
625fcfe9 KS |
2385 | static int |
2386 | yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token) | |
e71095e3 KS |
2387 | { |
2388 | yaml_char_t *handle = NULL; | |
2389 | yaml_char_t *suffix = NULL; | |
e71095e3 KS |
2390 | yaml_mark_t start_mark, end_mark; |
2391 | ||
625fcfe9 | 2392 | start_mark = parser->mark; |
e71095e3 KS |
2393 | |
2394 | /* Check if the tag is in the canonical form. */ | |
2395 | ||
625fcfe9 | 2396 | if (!CACHE(parser, 2)) goto error; |
e71095e3 | 2397 | |
e35af832 | 2398 | if (CHECK_AT(parser->buffer, '<', 1)) |
e71095e3 KS |
2399 | { |
2400 | /* Set the handle to '' */ | |
2401 | ||
fc2dd942 | 2402 | handle = YAML_MALLOC(1); |
e71095e3 KS |
2403 | if (!handle) goto error; |
2404 | handle[0] = '\0'; | |
2405 | ||
2406 | /* Eat '!<' */ | |
2407 | ||
625fcfe9 KS |
2408 | SKIP(parser); |
2409 | SKIP(parser); | |
e71095e3 KS |
2410 | |
2411 | /* Consume the tag value. */ | |
2412 | ||
2413 | if (!yaml_parser_scan_tag_uri(parser, 0, NULL, start_mark, &suffix)) | |
2414 | goto error; | |
2415 | ||
2416 | /* Check for '>' and eat it. */ | |
2417 | ||
e35af832 | 2418 | if (!CHECK(parser->buffer, '>')) { |
e71095e3 KS |
2419 | yaml_parser_set_scanner_error(parser, "while scanning a tag", |
2420 | start_mark, "did not find the expected '>'"); | |
2421 | goto error; | |
2422 | } | |
2423 | ||
625fcfe9 | 2424 | SKIP(parser); |
e71095e3 KS |
2425 | } |
2426 | else | |
2427 | { | |
2428 | /* The tag has either the '!suffix' or the '!handle!suffix' form. */ | |
2429 | ||
2430 | /* First, try to scan a handle. */ | |
2431 | ||
2432 | if (!yaml_parser_scan_tag_handle(parser, 0, start_mark, &handle)) | |
2433 | goto error; | |
2434 | ||
2435 | /* Check if it is, indeed, handle. */ | |
2436 | ||
2437 | if (handle[0] == '!' && handle[1] != '\0' && handle[strlen((char *)handle)-1] == '!') | |
2438 | { | |
2439 | /* Scan the suffix now. */ | |
2440 | ||
2441 | if (!yaml_parser_scan_tag_uri(parser, 0, NULL, start_mark, &suffix)) | |
2442 | goto error; | |
2443 | } | |
2444 | else | |
2445 | { | |
2446 | /* It wasn't a handle after all. Scan the rest of the tag. */ | |
2447 | ||
2448 | if (!yaml_parser_scan_tag_uri(parser, 0, handle, start_mark, &suffix)) | |
2449 | goto error; | |
2450 | ||
2451 | /* Set the handle to '!'. */ | |
2452 | ||
2453 | yaml_free(handle); | |
fc2dd942 | 2454 | handle = YAML_MALLOC(2); |
e71095e3 KS |
2455 | if (!handle) goto error; |
2456 | handle[0] = '!'; | |
2457 | handle[1] = '\0'; | |
7e32c194 KS |
2458 | |
2459 | /* | |
625fcfe9 KS |
2460 | * A special case: the '!' tag. Set the handle to '' and the |
2461 | * suffix to '!'. | |
7e32c194 KS |
2462 | */ |
2463 | ||
2464 | if (suffix[0] == '\0') { | |
2465 | yaml_char_t *tmp = handle; | |
2466 | handle = suffix; | |
2467 | suffix = tmp; | |
2468 | } | |
e71095e3 KS |
2469 | } |
2470 | } | |
2471 | ||
2472 | /* Check the character which ends the tag. */ | |
2473 | ||
625fcfe9 | 2474 | if (!CACHE(parser, 1)) goto error; |
e71095e3 | 2475 | |
e35af832 | 2476 | if (!IS_BLANKZ(parser->buffer)) { |
e71095e3 | 2477 | yaml_parser_set_scanner_error(parser, "while scanning a tag", |
6be8109b | 2478 | start_mark, "did not find expected whitespace or line break"); |
e71095e3 KS |
2479 | goto error; |
2480 | } | |
2481 | ||
625fcfe9 | 2482 | end_mark = parser->mark; |
e71095e3 KS |
2483 | |
2484 | /* Create a token. */ | |
2485 | ||
625fcfe9 | 2486 | TAG_TOKEN_INIT(*token, handle, suffix, start_mark, end_mark); |
e71095e3 | 2487 | |
625fcfe9 | 2488 | return 1; |
e71095e3 KS |
2489 | |
2490 | error: | |
2491 | yaml_free(handle); | |
2492 | yaml_free(suffix); | |
625fcfe9 | 2493 | return 0; |
e71095e3 KS |
2494 | } |
2495 | ||
2496 | /* | |
2497 | * Scan a tag handle. | |
2498 | */ | |
2499 | ||
2500 | static int | |
2501 | yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive, | |
2502 | yaml_mark_t start_mark, yaml_char_t **handle) | |
2503 | { | |
625fcfe9 | 2504 | yaml_string_t string = NULL_STRING; |
e71095e3 | 2505 | |
625fcfe9 | 2506 | if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error; |
e71095e3 KS |
2507 | |
2508 | /* Check the initial '!' character. */ | |
2509 | ||
625fcfe9 | 2510 | if (!CACHE(parser, 1)) goto error; |
e71095e3 | 2511 | |
e35af832 | 2512 | if (!CHECK(parser->buffer, '!')) { |
e71095e3 KS |
2513 | yaml_parser_set_scanner_error(parser, directive ? |
2514 | "while scanning a tag directive" : "while scanning a tag", | |
2515 | start_mark, "did not find expected '!'"); | |
2516 | goto error; | |
2517 | } | |
2518 | ||
2519 | /* Copy the '!' character. */ | |
2520 | ||
625fcfe9 | 2521 | if (!READ(parser, string)) goto error; |
e71095e3 KS |
2522 | |
2523 | /* Copy all subsequent alphabetical and numerical characters. */ | |
2524 | ||
625fcfe9 | 2525 | if (!CACHE(parser, 1)) goto error; |
e71095e3 | 2526 | |
e35af832 | 2527 | while (IS_ALPHA(parser->buffer)) |
e71095e3 | 2528 | { |
625fcfe9 KS |
2529 | if (!READ(parser, string)) goto error; |
2530 | if (!CACHE(parser, 1)) goto error; | |
e71095e3 KS |
2531 | } |
2532 | ||
2533 | /* Check if the trailing character is '!' and copy it. */ | |
2534 | ||
e35af832 | 2535 | if (CHECK(parser->buffer, '!')) |
e71095e3 | 2536 | { |
625fcfe9 | 2537 | if (!READ(parser, string)) goto error; |
e71095e3 KS |
2538 | } |
2539 | else | |
2540 | { | |
2541 | /* | |
7e32c194 KS |
2542 | * It's either the '!' tag or not really a tag handle. If it's a %TAG |
2543 | * directive, it's an error. If it's a tag token, it must be a part of | |
2544 | * URI. | |
e71095e3 KS |
2545 | */ |
2546 | ||
625fcfe9 | 2547 | if (directive && !(string.start[0] == '!' && string.start[1] == '\0')) { |
7e32c194 | 2548 | yaml_parser_set_scanner_error(parser, "while parsing a tag directive", |
e71095e3 KS |
2549 | start_mark, "did not find expected '!'"); |
2550 | goto error; | |
2551 | } | |
2552 | } | |
2553 | ||
625fcfe9 | 2554 | *handle = string.start; |
e71095e3 KS |
2555 | |
2556 | return 1; | |
2557 | ||
2558 | error: | |
625fcfe9 | 2559 | STRING_DEL(parser, string); |
e71095e3 KS |
2560 | return 0; |
2561 | } | |
2562 | ||
2563 | /* | |
2564 | * Scan a tag. | |
2565 | */ | |
2566 | ||
2567 | static int | |
2568 | yaml_parser_scan_tag_uri(yaml_parser_t *parser, int directive, | |
2569 | yaml_char_t *head, yaml_mark_t start_mark, yaml_char_t **uri) | |
2570 | { | |
2571 | size_t length = head ? strlen((char *)head) : 0; | |
625fcfe9 | 2572 | yaml_string_t string = NULL_STRING; |
e71095e3 | 2573 | |
625fcfe9 | 2574 | if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error; |
e71095e3 KS |
2575 | |
2576 | /* Resize the string to include the head. */ | |
2577 | ||
f56726b9 | 2578 | while ((size_t)(string.end - string.start) <= length) { |
625fcfe9 KS |
2579 | if (!yaml_string_extend(&string.start, &string.pointer, &string.end)) { |
2580 | parser->error = YAML_MEMORY_ERROR; | |
2581 | goto error; | |
2582 | } | |
e71095e3 KS |
2583 | } |
2584 | ||
7e32c194 KS |
2585 | /* |
2586 | * Copy the head if needed. | |
2587 | * | |
2588 | * Note that we don't copy the leading '!' character. | |
2589 | */ | |
e71095e3 | 2590 | |
7e32c194 | 2591 | if (length > 1) { |
625fcfe9 | 2592 | memcpy(string.start, head+1, length-1); |
7e32c194 | 2593 | string.pointer += length-1; |
e71095e3 KS |
2594 | } |
2595 | ||
2596 | /* Scan the tag. */ | |
2597 | ||
625fcfe9 | 2598 | if (!CACHE(parser, 1)) goto error; |
e71095e3 KS |
2599 | |
2600 | /* | |
2601 | * The set of characters that may appear in URI is as follows: | |
2602 | * | |
2603 | * '0'-'9', 'A'-'Z', 'a'-'z', '_', '-', ';', '/', '?', ':', '@', '&', | |
2604 | * '=', '+', '$', ',', '.', '!', '~', '*', '\'', '(', ')', '[', ']', | |
2605 | * '%'. | |
2606 | */ | |
2607 | ||
e35af832 KS |
2608 | while (IS_ALPHA(parser->buffer) || CHECK(parser->buffer, ';') |
2609 | || CHECK(parser->buffer, '/') || CHECK(parser->buffer, '?') | |
2610 | || CHECK(parser->buffer, ':') || CHECK(parser->buffer, '@') | |
2611 | || CHECK(parser->buffer, '&') || CHECK(parser->buffer, '=') | |
2612 | || CHECK(parser->buffer, '+') || CHECK(parser->buffer, '$') | |
2613 | || CHECK(parser->buffer, ',') || CHECK(parser->buffer, '.') | |
2614 | || CHECK(parser->buffer, '!') || CHECK(parser->buffer, '~') | |
2615 | || CHECK(parser->buffer, '*') || CHECK(parser->buffer, '\'') | |
2616 | || CHECK(parser->buffer, '(') || CHECK(parser->buffer, ')') | |
2617 | || CHECK(parser->buffer, '[') || CHECK(parser->buffer, ']') | |
2618 | || CHECK(parser->buffer, '%')) | |
e71095e3 | 2619 | { |
e71095e3 KS |
2620 | /* Check if it is a URI-escape sequence. */ |
2621 | ||
e35af832 | 2622 | if (CHECK(parser->buffer, '%')) { |
d1003a9d KS |
2623 | if (!STRING_EXTEND(parser, string)) |
2624 | goto error; | |
2625 | ||
e71095e3 KS |
2626 | if (!yaml_parser_scan_uri_escapes(parser, |
2627 | directive, start_mark, &string)) goto error; | |
2628 | } | |
2629 | else { | |
625fcfe9 | 2630 | if (!READ(parser, string)) goto error; |
e71095e3 KS |
2631 | } |
2632 | ||
2633 | length ++; | |
625fcfe9 | 2634 | if (!CACHE(parser, 1)) goto error; |
e71095e3 KS |
2635 | } |
2636 | ||
2637 | /* Check if the tag is non-empty. */ | |
2638 | ||
2639 | if (!length) { | |
625fcfe9 KS |
2640 | if (!STRING_EXTEND(parser, string)) |
2641 | goto error; | |
2642 | ||
e71095e3 KS |
2643 | yaml_parser_set_scanner_error(parser, directive ? |
2644 | "while parsing a %TAG directive" : "while parsing a tag", | |
2645 | start_mark, "did not find expected tag URI"); | |
2646 | goto error; | |
2647 | } | |
2648 | ||
625fcfe9 | 2649 | *uri = string.start; |
e71095e3 KS |
2650 | |
2651 | return 1; | |
2652 | ||
2653 | error: | |
625fcfe9 | 2654 | STRING_DEL(parser, string); |
e71095e3 KS |
2655 | return 0; |
2656 | } | |
2657 | ||
2658 | /* | |
2659 | * Decode an URI-escape sequence corresponding to a single UTF-8 character. | |
2660 | */ | |
2661 | ||
2662 | static int | |
2663 | yaml_parser_scan_uri_escapes(yaml_parser_t *parser, int directive, | |
2664 | yaml_mark_t start_mark, yaml_string_t *string) | |
2665 | { | |
2666 | int width = 0; | |
2667 | ||
2668 | /* Decode the required number of characters. */ | |
2669 | ||
2670 | do { | |
2671 | ||
2672 | unsigned char octet = 0; | |
2673 | ||
2674 | /* Check for a URI-escaped octet. */ | |
2675 | ||
625fcfe9 | 2676 | if (!CACHE(parser, 3)) return 0; |
e71095e3 | 2677 | |
e35af832 KS |
2678 | if (!(CHECK(parser->buffer, '%') |
2679 | && IS_HEX_AT(parser->buffer, 1) | |
2680 | && IS_HEX_AT(parser->buffer, 2))) { | |
e71095e3 KS |
2681 | return yaml_parser_set_scanner_error(parser, directive ? |
2682 | "while parsing a %TAG directive" : "while parsing a tag", | |
2683 | start_mark, "did not find URI escaped octet"); | |
2684 | } | |
2685 | ||
2686 | /* Get the octet. */ | |
2687 | ||
e35af832 | 2688 | octet = (AS_HEX_AT(parser->buffer, 1) << 4) + AS_HEX_AT(parser->buffer, 2); |
e71095e3 KS |
2689 | |
2690 | /* If it is the leading octet, determine the length of the UTF-8 sequence. */ | |
2691 | ||
2692 | if (!width) | |
2693 | { | |
2694 | width = (octet & 0x80) == 0x00 ? 1 : | |
2695 | (octet & 0xE0) == 0xC0 ? 2 : | |
2696 | (octet & 0xF0) == 0xE0 ? 3 : | |
2697 | (octet & 0xF8) == 0xF0 ? 4 : 0; | |
2698 | if (!width) { | |
2699 | return yaml_parser_set_scanner_error(parser, directive ? | |
2700 | "while parsing a %TAG directive" : "while parsing a tag", | |
2701 | start_mark, "found an incorrect leading UTF-8 octet"); | |
2702 | } | |
2703 | } | |
2704 | else | |
2705 | { | |
2706 | /* Check if the trailing octet is correct. */ | |
2707 | ||
2708 | if ((octet & 0xC0) != 0x80) { | |
2709 | return yaml_parser_set_scanner_error(parser, directive ? | |
2710 | "while parsing a %TAG directive" : "while parsing a tag", | |
2711 | start_mark, "found an incorrect trailing UTF-8 octet"); | |
2712 | } | |
2713 | } | |
2714 | ||
2715 | /* Copy the octet and move the pointers. */ | |
2716 | ||
2717 | *(string->pointer++) = octet; | |
625fcfe9 KS |
2718 | SKIP(parser); |
2719 | SKIP(parser); | |
2720 | SKIP(parser); | |
e71095e3 KS |
2721 | |
2722 | } while (--width); | |
2723 | ||
2724 | return 1; | |
2725 | } | |
2726 | ||
92d41fe1 KS |
2727 | /* |
2728 | * Scan a block scalar. | |
2729 | */ | |
2730 | ||
625fcfe9 KS |
2731 | static int |
2732 | yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token, | |
2733 | int literal) | |
92d41fe1 KS |
2734 | { |
2735 | yaml_mark_t start_mark; | |
2736 | yaml_mark_t end_mark; | |
625fcfe9 KS |
2737 | yaml_string_t string = NULL_STRING; |
2738 | yaml_string_t leading_break = NULL_STRING; | |
2739 | yaml_string_t trailing_breaks = NULL_STRING; | |
92d41fe1 KS |
2740 | int chomping = 0; |
2741 | int increment = 0; | |
2742 | int indent = 0; | |
2743 | int leading_blank = 0; | |
2744 | int trailing_blank = 0; | |
2745 | ||
625fcfe9 KS |
2746 | if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error; |
2747 | if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error; | |
2748 | if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error; | |
92d41fe1 KS |
2749 | |
2750 | /* Eat the indicator '|' or '>'. */ | |
2751 | ||
625fcfe9 | 2752 | start_mark = parser->mark; |
92d41fe1 | 2753 | |
625fcfe9 | 2754 | SKIP(parser); |
92d41fe1 KS |
2755 | |
2756 | /* Scan the additional block scalar indicators. */ | |
2757 | ||
625fcfe9 | 2758 | if (!CACHE(parser, 1)) goto error; |
92d41fe1 KS |
2759 | |
2760 | /* Check for a chomping indicator. */ | |
2761 | ||
e35af832 | 2762 | if (CHECK(parser->buffer, '+') || CHECK(parser->buffer, '-')) |
92d41fe1 KS |
2763 | { |
2764 | /* Set the chomping method and eat the indicator. */ | |
2765 | ||
e35af832 | 2766 | chomping = CHECK(parser->buffer, '+') ? +1 : -1; |
92d41fe1 | 2767 | |
625fcfe9 | 2768 | SKIP(parser); |
92d41fe1 KS |
2769 | |
2770 | /* Check for an indentation indicator. */ | |
2771 | ||
625fcfe9 | 2772 | if (!CACHE(parser, 1)) goto error; |
92d41fe1 | 2773 | |
e35af832 | 2774 | if (IS_DIGIT(parser->buffer)) |
92d41fe1 | 2775 | { |
bdf4d192 | 2776 | /* Check that the indentation is greater than 0. */ |
92d41fe1 | 2777 | |
e35af832 | 2778 | if (CHECK(parser->buffer, '0')) { |
92d41fe1 | 2779 | yaml_parser_set_scanner_error(parser, "while scanning a block scalar", |
bdf4d192 | 2780 | start_mark, "found an indentation indicator equal to 0"); |
92d41fe1 KS |
2781 | goto error; |
2782 | } | |
2783 | ||
bdf4d192 | 2784 | /* Get the indentation level and eat the indicator. */ |
92d41fe1 | 2785 | |
e35af832 | 2786 | increment = AS_DIGIT(parser->buffer); |
92d41fe1 | 2787 | |
625fcfe9 | 2788 | SKIP(parser); |
92d41fe1 KS |
2789 | } |
2790 | } | |
2791 | ||
2792 | /* Do the same as above, but in the opposite order. */ | |
2793 | ||
e35af832 | 2794 | else if (IS_DIGIT(parser->buffer)) |
92d41fe1 | 2795 | { |
e35af832 | 2796 | if (CHECK(parser->buffer, '0')) { |
92d41fe1 | 2797 | yaml_parser_set_scanner_error(parser, "while scanning a block scalar", |
bdf4d192 | 2798 | start_mark, "found an indentation indicator equal to 0"); |
92d41fe1 KS |
2799 | goto error; |
2800 | } | |
2801 | ||
e35af832 | 2802 | increment = AS_DIGIT(parser->buffer); |
92d41fe1 | 2803 | |
625fcfe9 | 2804 | SKIP(parser); |
92d41fe1 | 2805 | |
625fcfe9 | 2806 | if (!CACHE(parser, 1)) goto error; |
92d41fe1 | 2807 | |
e35af832 KS |
2808 | if (CHECK(parser->buffer, '+') || CHECK(parser->buffer, '-')) { |
2809 | chomping = CHECK(parser->buffer, '+') ? +1 : -1; | |
625fcfe9 KS |
2810 | |
2811 | SKIP(parser); | |
92d41fe1 KS |
2812 | } |
2813 | } | |
2814 | ||
2815 | /* Eat whitespaces and comments to the end of the line. */ | |
2816 | ||
625fcfe9 | 2817 | if (!CACHE(parser, 1)) goto error; |
92d41fe1 | 2818 | |
e35af832 | 2819 | while (IS_BLANK(parser->buffer)) { |
625fcfe9 KS |
2820 | SKIP(parser); |
2821 | if (!CACHE(parser, 1)) goto error; | |
92d41fe1 KS |
2822 | } |
2823 | ||
e35af832 KS |
2824 | if (CHECK(parser->buffer, '#')) { |
2825 | while (!IS_BREAKZ(parser->buffer)) { | |
625fcfe9 KS |
2826 | SKIP(parser); |
2827 | if (!CACHE(parser, 1)) goto error; | |
92d41fe1 KS |
2828 | } |
2829 | } | |
2830 | ||
2831 | /* Check if we are at the end of the line. */ | |
2832 | ||
e35af832 | 2833 | if (!IS_BREAKZ(parser->buffer)) { |
92d41fe1 | 2834 | yaml_parser_set_scanner_error(parser, "while scanning a block scalar", |
6be8109b | 2835 | start_mark, "did not find expected comment or line break"); |
92d41fe1 KS |
2836 | goto error; |
2837 | } | |
2838 | ||
2839 | /* Eat a line break. */ | |
2840 | ||
e35af832 | 2841 | if (IS_BREAK(parser->buffer)) { |
625fcfe9 KS |
2842 | if (!CACHE(parser, 2)) goto error; |
2843 | SKIP_LINE(parser); | |
92d41fe1 KS |
2844 | } |
2845 | ||
625fcfe9 | 2846 | end_mark = parser->mark; |
92d41fe1 | 2847 | |
bdf4d192 | 2848 | /* Set the indentation level if it was specified. */ |
92d41fe1 KS |
2849 | |
2850 | if (increment) { | |
2851 | indent = parser->indent >= 0 ? parser->indent+increment : increment; | |
2852 | } | |
2853 | ||
2854 | /* Scan the leading line breaks and determine the indentation level if needed. */ | |
2855 | ||
21fbedd4 | 2856 | if (!yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks, |
92d41fe1 KS |
2857 | start_mark, &end_mark)) goto error; |
2858 | ||
2859 | /* Scan the block scalar content. */ | |
2860 | ||
625fcfe9 | 2861 | if (!CACHE(parser, 1)) goto error; |
92d41fe1 | 2862 | |
0174ed6e | 2863 | while ((int)parser->mark.column == indent && !IS_Z(parser->buffer)) |
92d41fe1 KS |
2864 | { |
2865 | /* | |
2866 | * We are at the beginning of a non-empty line. | |
2867 | */ | |
2868 | ||
2869 | /* Is it a trailing whitespace? */ | |
2870 | ||
e35af832 | 2871 | trailing_blank = IS_BLANK(parser->buffer); |
92d41fe1 KS |
2872 | |
2873 | /* Check if we need to fold the leading line break. */ | |
2874 | ||
625fcfe9 | 2875 | if (!literal && (*leading_break.start == '\n') |
92d41fe1 KS |
2876 | && !leading_blank && !trailing_blank) |
2877 | { | |
2878 | /* Do we need to join the lines by space? */ | |
2879 | ||
625fcfe9 KS |
2880 | if (*trailing_breaks.start == '\0') { |
2881 | if (!STRING_EXTEND(parser, string)) goto error; | |
92d41fe1 KS |
2882 | *(string.pointer ++) = ' '; |
2883 | } | |
2884 | ||
625fcfe9 | 2885 | CLEAR(parser, leading_break); |
92d41fe1 KS |
2886 | } |
2887 | else { | |
21fbedd4 | 2888 | if (!JOIN(parser, string, leading_break)) goto error; |
625fcfe9 | 2889 | CLEAR(parser, leading_break); |
92d41fe1 KS |
2890 | } |
2891 | ||
2892 | /* Append the remaining line breaks. */ | |
2893 | ||
21fbedd4 | 2894 | if (!JOIN(parser, string, trailing_breaks)) goto error; |
625fcfe9 | 2895 | CLEAR(parser, trailing_breaks); |
92d41fe1 KS |
2896 | |
2897 | /* Is it a leading whitespace? */ | |
2898 | ||
e35af832 | 2899 | leading_blank = IS_BLANK(parser->buffer); |
92d41fe1 KS |
2900 | |
2901 | /* Consume the current line. */ | |
2902 | ||
e35af832 | 2903 | while (!IS_BREAKZ(parser->buffer)) { |
625fcfe9 KS |
2904 | if (!READ(parser, string)) goto error; |
2905 | if (!CACHE(parser, 1)) goto error; | |
92d41fe1 KS |
2906 | } |
2907 | ||
2908 | /* Consume the line break. */ | |
2909 | ||
625fcfe9 | 2910 | if (!CACHE(parser, 2)) goto error; |
92d41fe1 | 2911 | |
625fcfe9 | 2912 | if (!READ_LINE(parser, leading_break)) goto error; |
92d41fe1 | 2913 | |
bdf4d192 | 2914 | /* Eat the following indentation spaces and line breaks. */ |
92d41fe1 KS |
2915 | |
2916 | if (!yaml_parser_scan_block_scalar_breaks(parser, | |
21fbedd4 | 2917 | &indent, &trailing_breaks, start_mark, &end_mark)) goto error; |
92d41fe1 KS |
2918 | } |
2919 | ||
2920 | /* Chomp the tail. */ | |
2921 | ||
2922 | if (chomping != -1) { | |
21fbedd4 | 2923 | if (!JOIN(parser, string, leading_break)) goto error; |
92d41fe1 KS |
2924 | } |
2925 | if (chomping == 1) { | |
21fbedd4 | 2926 | if (!JOIN(parser, string, trailing_breaks)) goto error; |
92d41fe1 KS |
2927 | } |
2928 | ||
2929 | /* Create a token. */ | |
2930 | ||
625fcfe9 | 2931 | SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start, |
92d41fe1 KS |
2932 | literal ? YAML_LITERAL_SCALAR_STYLE : YAML_FOLDED_SCALAR_STYLE, |
2933 | start_mark, end_mark); | |
92d41fe1 | 2934 | |
625fcfe9 KS |
2935 | STRING_DEL(parser, leading_break); |
2936 | STRING_DEL(parser, trailing_breaks); | |
92d41fe1 | 2937 | |
625fcfe9 | 2938 | return 1; |
92d41fe1 KS |
2939 | |
2940 | error: | |
625fcfe9 KS |
2941 | STRING_DEL(parser, string); |
2942 | STRING_DEL(parser, leading_break); | |
2943 | STRING_DEL(parser, trailing_breaks); | |
92d41fe1 | 2944 | |
625fcfe9 | 2945 | return 0; |
92d41fe1 KS |
2946 | } |
2947 | ||
2948 | /* | |
bdf4d192 SH |
2949 | * Scan indentation spaces and line breaks for a block scalar. Determine the |
2950 | * indentation level if needed. | |
92d41fe1 KS |
2951 | */ |
2952 | ||
2953 | static int | |
2954 | yaml_parser_scan_block_scalar_breaks(yaml_parser_t *parser, | |
2955 | int *indent, yaml_string_t *breaks, | |
2956 | yaml_mark_t start_mark, yaml_mark_t *end_mark) | |
2957 | { | |
2958 | int max_indent = 0; | |
2959 | ||
625fcfe9 | 2960 | *end_mark = parser->mark; |
92d41fe1 | 2961 | |
bdf4d192 | 2962 | /* Eat the indentation spaces and line breaks. */ |
92d41fe1 KS |
2963 | |
2964 | while (1) | |
2965 | { | |
bdf4d192 | 2966 | /* Eat the indentation spaces. */ |
92d41fe1 | 2967 | |
625fcfe9 | 2968 | if (!CACHE(parser, 1)) return 0; |
92d41fe1 | 2969 | |
0174ed6e | 2970 | while ((!*indent || (int)parser->mark.column < *indent) |
e35af832 | 2971 | && IS_SPACE(parser->buffer)) { |
625fcfe9 KS |
2972 | SKIP(parser); |
2973 | if (!CACHE(parser, 1)) return 0; | |
92d41fe1 KS |
2974 | } |
2975 | ||
0174ed6e KS |
2976 | if ((int)parser->mark.column > max_indent) |
2977 | max_indent = (int)parser->mark.column; | |
92d41fe1 | 2978 | |
bdf4d192 | 2979 | /* Check for a tab character messing the indentation. */ |
92d41fe1 | 2980 | |
0174ed6e | 2981 | if ((!*indent || (int)parser->mark.column < *indent) |
e35af832 | 2982 | && IS_TAB(parser->buffer)) { |
92d41fe1 | 2983 | return yaml_parser_set_scanner_error(parser, "while scanning a block scalar", |
bdf4d192 | 2984 | start_mark, "found a tab character where an indentation space is expected"); |
92d41fe1 KS |
2985 | } |
2986 | ||
2987 | /* Have we found a non-empty line? */ | |
2988 | ||
e35af832 | 2989 | if (!IS_BREAK(parser->buffer)) break; |
92d41fe1 KS |
2990 | |
2991 | /* Consume the line break. */ | |
2992 | ||
625fcfe9 KS |
2993 | if (!CACHE(parser, 2)) return 0; |
2994 | if (!READ_LINE(parser, *breaks)) return 0; | |
2995 | *end_mark = parser->mark; | |
92d41fe1 KS |
2996 | } |
2997 | ||
2998 | /* Determine the indentation level if needed. */ | |
2999 | ||
3000 | if (!*indent) { | |
3001 | *indent = max_indent; | |
3002 | if (*indent < parser->indent + 1) | |
3003 | *indent = parser->indent + 1; | |
3004 | if (*indent < 1) | |
3005 | *indent = 1; | |
3006 | } | |
3007 | ||
986dbde7 | 3008 | return 1; |
92d41fe1 KS |
3009 | } |
3010 | ||
21fbedd4 KS |
3011 | /* |
3012 | * Scan a quoted scalar. | |
3013 | */ | |
3014 | ||
625fcfe9 KS |
3015 | static int |
3016 | yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token, | |
3017 | int single) | |
21fbedd4 KS |
3018 | { |
3019 | yaml_mark_t start_mark; | |
3020 | yaml_mark_t end_mark; | |
625fcfe9 KS |
3021 | yaml_string_t string = NULL_STRING; |
3022 | yaml_string_t leading_break = NULL_STRING; | |
3023 | yaml_string_t trailing_breaks = NULL_STRING; | |
3024 | yaml_string_t whitespaces = NULL_STRING; | |
21fbedd4 KS |
3025 | int leading_blanks; |
3026 | ||
625fcfe9 KS |
3027 | if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error; |
3028 | if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error; | |
3029 | if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error; | |
3030 | if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error; | |
21fbedd4 KS |
3031 | |
3032 | /* Eat the left quote. */ | |
3033 | ||
625fcfe9 | 3034 | start_mark = parser->mark; |
21fbedd4 | 3035 | |
625fcfe9 | 3036 | SKIP(parser); |
21fbedd4 KS |
3037 | |
3038 | /* Consume the content of the quoted scalar. */ | |
3039 | ||
3040 | while (1) | |
3041 | { | |
3042 | /* Check that there are no document indicators at the beginning of the line. */ | |
3043 | ||
625fcfe9 | 3044 | if (!CACHE(parser, 4)) goto error; |
21fbedd4 | 3045 | |
625fcfe9 | 3046 | if (parser->mark.column == 0 && |
e35af832 KS |
3047 | ((CHECK_AT(parser->buffer, '-', 0) && |
3048 | CHECK_AT(parser->buffer, '-', 1) && | |
3049 | CHECK_AT(parser->buffer, '-', 2)) || | |
3050 | (CHECK_AT(parser->buffer, '.', 0) && | |
3051 | CHECK_AT(parser->buffer, '.', 1) && | |
3052 | CHECK_AT(parser->buffer, '.', 2))) && | |
3053 | IS_BLANKZ_AT(parser->buffer, 3)) | |
21fbedd4 KS |
3054 | { |
3055 | yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar", | |
3056 | start_mark, "found unexpected document indicator"); | |
3057 | goto error; | |
3058 | } | |
3059 | ||
3060 | /* Check for EOF. */ | |
3061 | ||
e35af832 | 3062 | if (IS_Z(parser->buffer)) { |
21fbedd4 KS |
3063 | yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar", |
3064 | start_mark, "found unexpected end of stream"); | |
3065 | goto error; | |
3066 | } | |
3067 | ||
3068 | /* Consume non-blank characters. */ | |
3069 | ||
625fcfe9 | 3070 | if (!CACHE(parser, 2)) goto error; |
21fbedd4 KS |
3071 | |
3072 | leading_blanks = 0; | |
3073 | ||
e35af832 | 3074 | while (!IS_BLANKZ(parser->buffer)) |
21fbedd4 KS |
3075 | { |
3076 | /* Check for an escaped single quote. */ | |
3077 | ||
e35af832 KS |
3078 | if (single && CHECK_AT(parser->buffer, '\'', 0) |
3079 | && CHECK_AT(parser->buffer, '\'', 1)) | |
21fbedd4 | 3080 | { |
625fcfe9 | 3081 | if (!STRING_EXTEND(parser, string)) goto error; |
21fbedd4 | 3082 | *(string.pointer++) = '\''; |
625fcfe9 KS |
3083 | SKIP(parser); |
3084 | SKIP(parser); | |
21fbedd4 KS |
3085 | } |
3086 | ||
3087 | /* Check for the right quote. */ | |
3088 | ||
e35af832 | 3089 | else if (CHECK(parser->buffer, single ? '\'' : '"')) |
21fbedd4 KS |
3090 | { |
3091 | break; | |
3092 | } | |
3093 | ||
3094 | /* Check for an escaped line break. */ | |
3095 | ||
e35af832 KS |
3096 | else if (!single && CHECK(parser->buffer, '\\') |
3097 | && IS_BREAK_AT(parser->buffer, 1)) | |
21fbedd4 | 3098 | { |
625fcfe9 KS |
3099 | if (!CACHE(parser, 3)) goto error; |
3100 | SKIP(parser); | |
3101 | SKIP_LINE(parser); | |
21fbedd4 KS |
3102 | leading_blanks = 1; |
3103 | break; | |
3104 | } | |
3105 | ||
3106 | /* Check for an escape sequence. */ | |
3107 | ||
e35af832 | 3108 | else if (!single && CHECK(parser->buffer, '\\')) |
21fbedd4 | 3109 | { |
0174ed6e | 3110 | size_t code_length = 0; |
21fbedd4 | 3111 | |
625fcfe9 KS |
3112 | if (!STRING_EXTEND(parser, string)) goto error; |
3113 | ||
21fbedd4 KS |
3114 | /* Check the escape character. */ |
3115 | ||
625fcfe9 | 3116 | switch (parser->buffer.pointer[1]) |
21fbedd4 KS |
3117 | { |
3118 | case '0': | |
3119 | *(string.pointer++) = '\0'; | |
3120 | break; | |
3121 | ||
3122 | case 'a': | |
3123 | *(string.pointer++) = '\x07'; | |
3124 | break; | |
3125 | ||
3126 | case 'b': | |
3127 | *(string.pointer++) = '\x08'; | |
3128 | break; | |
3129 | ||
3130 | case 't': | |
3131 | case '\t': | |
3132 | *(string.pointer++) = '\x09'; | |
3133 | break; | |
3134 | ||
3135 | case 'n': | |
3136 | *(string.pointer++) = '\x0A'; | |
3137 | break; | |
3138 | ||
3139 | case 'v': | |
3140 | *(string.pointer++) = '\x0B'; | |
3141 | break; | |
3142 | ||
3143 | case 'f': | |
3144 | *(string.pointer++) = '\x0C'; | |
3145 | break; | |
3146 | ||
3147 | case 'r': | |
3148 | *(string.pointer++) = '\x0D'; | |
3149 | break; | |
3150 | ||
3151 | case 'e': | |
3152 | *(string.pointer++) = '\x1B'; | |
3153 | break; | |
3154 | ||
3155 | case ' ': | |
3156 | *(string.pointer++) = '\x20'; | |
3157 | break; | |
3158 | ||
3159 | case '"': | |
3160 | *(string.pointer++) = '"'; | |
3161 | break; | |
3162 | ||
6db69c72 IN |
3163 | case '/': |
3164 | *(string.pointer++) = '/'; | |
3165 | break; | |
3166 | ||
7e32c194 KS |
3167 | case '\\': |
3168 | *(string.pointer++) = '\\'; | |
3169 | break; | |
3170 | ||
21fbedd4 KS |
3171 | case 'N': /* NEL (#x85) */ |
3172 | *(string.pointer++) = '\xC2'; | |
3173 | *(string.pointer++) = '\x85'; | |
3174 | break; | |
3175 | ||
3176 | case '_': /* #xA0 */ | |
3177 | *(string.pointer++) = '\xC2'; | |
3178 | *(string.pointer++) = '\xA0'; | |
3179 | break; | |
3180 | ||
3181 | case 'L': /* LS (#x2028) */ | |
3182 | *(string.pointer++) = '\xE2'; | |
3183 | *(string.pointer++) = '\x80'; | |
3184 | *(string.pointer++) = '\xA8'; | |
3185 | break; | |
3186 | ||
3187 | case 'P': /* PS (#x2029) */ | |
3188 | *(string.pointer++) = '\xE2'; | |
3189 | *(string.pointer++) = '\x80'; | |
7e32c194 | 3190 | *(string.pointer++) = '\xA9'; |
21fbedd4 KS |
3191 | break; |
3192 | ||
3193 | case 'x': | |
3194 | code_length = 2; | |
3195 | break; | |
3196 | ||
3197 | case 'u': | |
3198 | code_length = 4; | |
3199 | break; | |
3200 | ||
3201 | case 'U': | |
3202 | code_length = 8; | |
3203 | break; | |
3204 | ||
3205 | default: | |
3206 | yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar", | |
3207 | start_mark, "found unknown escape character"); | |
3208 | goto error; | |
3209 | } | |
3210 | ||
625fcfe9 KS |
3211 | SKIP(parser); |
3212 | SKIP(parser); | |
21fbedd4 KS |
3213 | |
3214 | /* Consume an arbitrary escape code. */ | |
3215 | ||
3216 | if (code_length) | |
3217 | { | |
3218 | unsigned int value = 0; | |
0174ed6e | 3219 | size_t k; |
21fbedd4 KS |
3220 | |
3221 | /* Scan the character value. */ | |
3222 | ||
625fcfe9 | 3223 | if (!CACHE(parser, code_length)) goto error; |
21fbedd4 KS |
3224 | |
3225 | for (k = 0; k < code_length; k ++) { | |
e35af832 | 3226 | if (!IS_HEX_AT(parser->buffer, k)) { |
21fbedd4 KS |
3227 | yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar", |
3228 | start_mark, "did not find expected hexdecimal number"); | |
3229 | goto error; | |
3230 | } | |
e35af832 | 3231 | value = (value << 4) + AS_HEX_AT(parser->buffer, k); |
21fbedd4 KS |
3232 | } |
3233 | ||
3234 | /* Check the value and write the character. */ | |
3235 | ||
3236 | if ((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF) { | |
3237 | yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar", | |
3238 | start_mark, "found invalid Unicode character escape code"); | |
3239 | goto error; | |
3240 | } | |
3241 | ||
3242 | if (value <= 0x7F) { | |
3243 | *(string.pointer++) = value; | |
3244 | } | |
3245 | else if (value <= 0x7FF) { | |
3246 | *(string.pointer++) = 0xC0 + (value >> 6); | |
3247 | *(string.pointer++) = 0x80 + (value & 0x3F); | |
3248 | } | |
3249 | else if (value <= 0xFFFF) { | |
3250 | *(string.pointer++) = 0xE0 + (value >> 12); | |
3251 | *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F); | |
3252 | *(string.pointer++) = 0x80 + (value & 0x3F); | |
3253 | } | |
3254 | else { | |
3255 | *(string.pointer++) = 0xF0 + (value >> 18); | |
3256 | *(string.pointer++) = 0x80 + ((value >> 12) & 0x3F); | |
3257 | *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F); | |
3258 | *(string.pointer++) = 0x80 + (value & 0x3F); | |
3259 | } | |
3260 | ||
3261 | /* Advance the pointer. */ | |
3262 | ||
3263 | for (k = 0; k < code_length; k ++) { | |
625fcfe9 | 3264 | SKIP(parser); |
21fbedd4 KS |
3265 | } |
3266 | } | |
3267 | } | |
3268 | ||
3269 | else | |
3270 | { | |
3271 | /* It is a non-escaped non-blank character. */ | |
3272 | ||
625fcfe9 | 3273 | if (!READ(parser, string)) goto error; |
21fbedd4 KS |
3274 | } |
3275 | ||
625fcfe9 | 3276 | if (!CACHE(parser, 2)) goto error; |
21fbedd4 KS |
3277 | } |
3278 | ||
3279 | /* Check if we are at the end of the scalar. */ | |
3280 | ||
6bbc217f IC |
3281 | /* Fix for crash unitialized value crash |
3282 | * Credit for the bug and input is to OSS Fuzz | |
3283 | * Credit for the fix to Alex Gaynor | |
3284 | */ | |
3285 | if (!CACHE(parser, 1)) goto error; | |
e35af832 | 3286 | if (CHECK(parser->buffer, single ? '\'' : '"')) |
21fbedd4 KS |
3287 | break; |
3288 | ||
3289 | /* Consume blank characters. */ | |
3290 | ||
625fcfe9 | 3291 | if (!CACHE(parser, 1)) goto error; |
21fbedd4 | 3292 | |
e35af832 | 3293 | while (IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer)) |
21fbedd4 | 3294 | { |
e35af832 | 3295 | if (IS_BLANK(parser->buffer)) |
21fbedd4 KS |
3296 | { |
3297 | /* Consume a space or a tab character. */ | |
3298 | ||
3299 | if (!leading_blanks) { | |
625fcfe9 | 3300 | if (!READ(parser, whitespaces)) goto error; |
21fbedd4 | 3301 | } |
7e32c194 | 3302 | else { |
625fcfe9 | 3303 | SKIP(parser); |
7e32c194 | 3304 | } |
21fbedd4 KS |
3305 | } |
3306 | else | |
3307 | { | |
625fcfe9 | 3308 | if (!CACHE(parser, 2)) goto error; |
21fbedd4 KS |
3309 | |
3310 | /* Check if it is a first line break. */ | |
3311 | ||
3312 | if (!leading_blanks) | |
3313 | { | |
625fcfe9 KS |
3314 | CLEAR(parser, whitespaces); |
3315 | if (!READ_LINE(parser, leading_break)) goto error; | |
21fbedd4 KS |
3316 | leading_blanks = 1; |
3317 | } | |
3318 | else | |
3319 | { | |
625fcfe9 | 3320 | if (!READ_LINE(parser, trailing_breaks)) goto error; |
21fbedd4 KS |
3321 | } |
3322 | } | |
625fcfe9 | 3323 | if (!CACHE(parser, 1)) goto error; |
21fbedd4 KS |
3324 | } |
3325 | ||
3326 | /* Join the whitespaces or fold line breaks. */ | |
3327 | ||
21fbedd4 KS |
3328 | if (leading_blanks) |
3329 | { | |
3330 | /* Do we need to fold line breaks? */ | |
3331 | ||
625fcfe9 KS |
3332 | if (leading_break.start[0] == '\n') { |
3333 | if (trailing_breaks.start[0] == '\0') { | |
3334 | if (!STRING_EXTEND(parser, string)) goto error; | |
21fbedd4 KS |
3335 | *(string.pointer++) = ' '; |
3336 | } | |
3337 | else { | |
3338 | if (!JOIN(parser, string, trailing_breaks)) goto error; | |
625fcfe9 | 3339 | CLEAR(parser, trailing_breaks); |
21fbedd4 | 3340 | } |
625fcfe9 | 3341 | CLEAR(parser, leading_break); |
21fbedd4 KS |
3342 | } |
3343 | else { | |
3344 | if (!JOIN(parser, string, leading_break)) goto error; | |
3345 | if (!JOIN(parser, string, trailing_breaks)) goto error; | |
625fcfe9 KS |
3346 | CLEAR(parser, leading_break); |
3347 | CLEAR(parser, trailing_breaks); | |
21fbedd4 KS |
3348 | } |
3349 | } | |
3350 | else | |
3351 | { | |
3352 | if (!JOIN(parser, string, whitespaces)) goto error; | |
625fcfe9 | 3353 | CLEAR(parser, whitespaces); |
21fbedd4 KS |
3354 | } |
3355 | } | |
3356 | ||
3357 | /* Eat the right quote. */ | |
3358 | ||
625fcfe9 | 3359 | SKIP(parser); |
21fbedd4 | 3360 | |
625fcfe9 | 3361 | end_mark = parser->mark; |
21fbedd4 KS |
3362 | |
3363 | /* Create a token. */ | |
3364 | ||
625fcfe9 | 3365 | SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start, |
21fbedd4 KS |
3366 | single ? YAML_SINGLE_QUOTED_SCALAR_STYLE : YAML_DOUBLE_QUOTED_SCALAR_STYLE, |
3367 | start_mark, end_mark); | |
21fbedd4 | 3368 | |
625fcfe9 KS |
3369 | STRING_DEL(parser, leading_break); |
3370 | STRING_DEL(parser, trailing_breaks); | |
3371 | STRING_DEL(parser, whitespaces); | |
21fbedd4 | 3372 | |
625fcfe9 | 3373 | return 1; |
21fbedd4 KS |
3374 | |
3375 | error: | |
625fcfe9 KS |
3376 | STRING_DEL(parser, string); |
3377 | STRING_DEL(parser, leading_break); | |
3378 | STRING_DEL(parser, trailing_breaks); | |
3379 | STRING_DEL(parser, whitespaces); | |
21fbedd4 | 3380 | |
625fcfe9 | 3381 | return 0; |
21fbedd4 KS |
3382 | } |
3383 | ||
3384 | /* | |
3385 | * Scan a plain scalar. | |
3386 | */ | |
3387 | ||
625fcfe9 KS |
3388 | static int |
3389 | yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token) | |
21fbedd4 KS |
3390 | { |
3391 | yaml_mark_t start_mark; | |
3392 | yaml_mark_t end_mark; | |
625fcfe9 KS |
3393 | yaml_string_t string = NULL_STRING; |
3394 | yaml_string_t leading_break = NULL_STRING; | |
3395 | yaml_string_t trailing_breaks = NULL_STRING; | |
3396 | yaml_string_t whitespaces = NULL_STRING; | |
21fbedd4 KS |
3397 | int leading_blanks = 0; |
3398 | int indent = parser->indent+1; | |
3399 | ||
625fcfe9 KS |
3400 | if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error; |
3401 | if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error; | |
3402 | if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error; | |
3403 | if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error; | |
21fbedd4 | 3404 | |
54815ffd | 3405 | start_mark = end_mark = parser->mark; |
21fbedd4 KS |
3406 | |
3407 | /* Consume the content of the plain scalar. */ | |
3408 | ||
3409 | while (1) | |
3410 | { | |
3411 | /* Check for a document indicator. */ | |
3412 | ||
625fcfe9 | 3413 | if (!CACHE(parser, 4)) goto error; |
21fbedd4 | 3414 | |
625fcfe9 | 3415 | if (parser->mark.column == 0 && |
e35af832 KS |
3416 | ((CHECK_AT(parser->buffer, '-', 0) && |
3417 | CHECK_AT(parser->buffer, '-', 1) && | |
3418 | CHECK_AT(parser->buffer, '-', 2)) || | |
3419 | (CHECK_AT(parser->buffer, '.', 0) && | |
3420 | CHECK_AT(parser->buffer, '.', 1) && | |
3421 | CHECK_AT(parser->buffer, '.', 2))) && | |
3422 | IS_BLANKZ_AT(parser->buffer, 3)) break; | |
21fbedd4 KS |
3423 | |
3424 | /* Check for a comment. */ | |
3425 | ||
e35af832 | 3426 | if (CHECK(parser->buffer, '#')) |
21fbedd4 KS |
3427 | break; |
3428 | ||
3429 | /* Consume non-blank characters. */ | |
3430 | ||
e35af832 | 3431 | while (!IS_BLANKZ(parser->buffer)) |
21fbedd4 | 3432 | { |
7e32c194 | 3433 | /* Check for 'x:x' in the flow context. TODO: Fix the test "spec-08-13". */ |
21fbedd4 | 3434 | |
e35af832 KS |
3435 | if (parser->flow_level |
3436 | && CHECK(parser->buffer, ':') | |
3437 | && !IS_BLANKZ_AT(parser->buffer, 1)) { | |
21fbedd4 KS |
3438 | yaml_parser_set_scanner_error(parser, "while scanning a plain scalar", |
3439 | start_mark, "found unexpected ':'"); | |
3440 | goto error; | |
3441 | } | |
3442 | ||
3443 | /* Check for indicators that may end a plain scalar. */ | |
3444 | ||
e35af832 KS |
3445 | if ((CHECK(parser->buffer, ':') && IS_BLANKZ_AT(parser->buffer, 1)) |
3446 | || (parser->flow_level && | |
3447 | (CHECK(parser->buffer, ',') || CHECK(parser->buffer, ':') | |
3448 | || CHECK(parser->buffer, '?') || CHECK(parser->buffer, '[') | |
3449 | || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '{') | |
3450 | || CHECK(parser->buffer, '}')))) | |
21fbedd4 KS |
3451 | break; |
3452 | ||
3453 | /* Check if we need to join whitespaces and breaks. */ | |
3454 | ||
625fcfe9 | 3455 | if (leading_blanks || whitespaces.start != whitespaces.pointer) |
21fbedd4 | 3456 | { |
21fbedd4 KS |
3457 | if (leading_blanks) |
3458 | { | |
3459 | /* Do we need to fold line breaks? */ | |
3460 | ||
625fcfe9 KS |
3461 | if (leading_break.start[0] == '\n') { |
3462 | if (trailing_breaks.start[0] == '\0') { | |
3463 | if (!STRING_EXTEND(parser, string)) goto error; | |
21fbedd4 KS |
3464 | *(string.pointer++) = ' '; |
3465 | } | |
3466 | else { | |
3467 | if (!JOIN(parser, string, trailing_breaks)) goto error; | |
625fcfe9 | 3468 | CLEAR(parser, trailing_breaks); |
21fbedd4 | 3469 | } |
625fcfe9 | 3470 | CLEAR(parser, leading_break); |
21fbedd4 KS |
3471 | } |
3472 | else { | |
3473 | if (!JOIN(parser, string, leading_break)) goto error; | |
3474 | if (!JOIN(parser, string, trailing_breaks)) goto error; | |
625fcfe9 KS |
3475 | CLEAR(parser, leading_break); |
3476 | CLEAR(parser, trailing_breaks); | |
21fbedd4 KS |
3477 | } |
3478 | ||
3479 | leading_blanks = 0; | |
3480 | } | |
3481 | else | |
3482 | { | |
3483 | if (!JOIN(parser, string, whitespaces)) goto error; | |
625fcfe9 | 3484 | CLEAR(parser, whitespaces); |
21fbedd4 KS |
3485 | } |
3486 | } | |
3487 | ||
3488 | /* Copy the character. */ | |
3489 | ||
625fcfe9 | 3490 | if (!READ(parser, string)) goto error; |
21fbedd4 | 3491 | |
625fcfe9 | 3492 | end_mark = parser->mark; |
21fbedd4 | 3493 | |
625fcfe9 | 3494 | if (!CACHE(parser, 2)) goto error; |
21fbedd4 KS |
3495 | } |
3496 | ||
3497 | /* Is it the end? */ | |
3498 | ||
e35af832 | 3499 | if (!(IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer))) |
21fbedd4 KS |
3500 | break; |
3501 | ||
3502 | /* Consume blank characters. */ | |
3503 | ||
625fcfe9 | 3504 | if (!CACHE(parser, 1)) goto error; |
21fbedd4 | 3505 | |
e35af832 | 3506 | while (IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer)) |
21fbedd4 | 3507 | { |
e35af832 | 3508 | if (IS_BLANK(parser->buffer)) |
21fbedd4 | 3509 | { |
bdf4d192 | 3510 | /* Check for tab character that abuse indentation. */ |
21fbedd4 | 3511 | |
0174ed6e | 3512 | if (leading_blanks && (int)parser->mark.column < indent |
e35af832 | 3513 | && IS_TAB(parser->buffer)) { |
21fbedd4 | 3514 | yaml_parser_set_scanner_error(parser, "while scanning a plain scalar", |
bdf4d192 | 3515 | start_mark, "found a tab character that violate indentation"); |
7e32c194 | 3516 | goto error; |
21fbedd4 KS |
3517 | } |
3518 | ||
3519 | /* Consume a space or a tab character. */ | |
3520 | ||
3521 | if (!leading_blanks) { | |
625fcfe9 | 3522 | if (!READ(parser, whitespaces)) goto error; |
21fbedd4 | 3523 | } |
7e32c194 | 3524 | else { |
625fcfe9 | 3525 | SKIP(parser); |
7e32c194 | 3526 | } |
21fbedd4 KS |
3527 | } |
3528 | else | |
3529 | { | |
625fcfe9 | 3530 | if (!CACHE(parser, 2)) goto error; |
21fbedd4 KS |
3531 | |
3532 | /* Check if it is a first line break. */ | |
3533 | ||
3534 | if (!leading_blanks) | |
3535 | { | |
625fcfe9 KS |
3536 | CLEAR(parser, whitespaces); |
3537 | if (!READ_LINE(parser, leading_break)) goto error; | |
21fbedd4 KS |
3538 | leading_blanks = 1; |
3539 | } | |
3540 | else | |
3541 | { | |
625fcfe9 | 3542 | if (!READ_LINE(parser, trailing_breaks)) goto error; |
21fbedd4 KS |
3543 | } |
3544 | } | |
625fcfe9 | 3545 | if (!CACHE(parser, 1)) goto error; |
21fbedd4 KS |
3546 | } |
3547 | ||
bdf4d192 | 3548 | /* Check indentation level. */ |
21fbedd4 | 3549 | |
0174ed6e | 3550 | if (!parser->flow_level && (int)parser->mark.column < indent) |
21fbedd4 KS |
3551 | break; |
3552 | } | |
3553 | ||
3554 | /* Create a token. */ | |
3555 | ||
625fcfe9 | 3556 | SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start, |
21fbedd4 | 3557 | YAML_PLAIN_SCALAR_STYLE, start_mark, end_mark); |
21fbedd4 KS |
3558 | |
3559 | /* Note that we change the 'simple_key_allowed' flag. */ | |
3560 | ||
3561 | if (leading_blanks) { | |
3562 | parser->simple_key_allowed = 1; | |
3563 | } | |
3564 | ||
625fcfe9 KS |
3565 | STRING_DEL(parser, leading_break); |
3566 | STRING_DEL(parser, trailing_breaks); | |
3567 | STRING_DEL(parser, whitespaces); | |
21fbedd4 | 3568 | |
625fcfe9 | 3569 | return 1; |
21fbedd4 KS |
3570 | |
3571 | error: | |
625fcfe9 KS |
3572 | STRING_DEL(parser, string); |
3573 | STRING_DEL(parser, leading_break); | |
3574 | STRING_DEL(parser, trailing_breaks); | |
3575 | STRING_DEL(parser, whitespaces); | |
21fbedd4 | 3576 | |
625fcfe9 | 3577 | return 0; |
21fbedd4 | 3578 | } |