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