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