| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.yaml.snakeyaml.scanner; |
| 18 | |
|
| 19 | |
import java.nio.ByteBuffer; |
| 20 | |
import java.nio.charset.CharacterCodingException; |
| 21 | |
import java.util.ArrayList; |
| 22 | |
import java.util.HashMap; |
| 23 | |
import java.util.Iterator; |
| 24 | |
import java.util.LinkedHashMap; |
| 25 | |
import java.util.List; |
| 26 | |
import java.util.Map; |
| 27 | |
import java.util.regex.Pattern; |
| 28 | |
|
| 29 | |
import org.yaml.snakeyaml.error.Mark; |
| 30 | |
import org.yaml.snakeyaml.error.YAMLException; |
| 31 | |
import org.yaml.snakeyaml.reader.StreamReader; |
| 32 | |
import org.yaml.snakeyaml.tokens.AliasToken; |
| 33 | |
import org.yaml.snakeyaml.tokens.AnchorToken; |
| 34 | |
import org.yaml.snakeyaml.tokens.BlockEndToken; |
| 35 | |
import org.yaml.snakeyaml.tokens.BlockEntryToken; |
| 36 | |
import org.yaml.snakeyaml.tokens.BlockMappingStartToken; |
| 37 | |
import org.yaml.snakeyaml.tokens.BlockSequenceStartToken; |
| 38 | |
import org.yaml.snakeyaml.tokens.DirectiveToken; |
| 39 | |
import org.yaml.snakeyaml.tokens.DocumentEndToken; |
| 40 | |
import org.yaml.snakeyaml.tokens.DocumentStartToken; |
| 41 | |
import org.yaml.snakeyaml.tokens.FlowEntryToken; |
| 42 | |
import org.yaml.snakeyaml.tokens.FlowMappingEndToken; |
| 43 | |
import org.yaml.snakeyaml.tokens.FlowMappingStartToken; |
| 44 | |
import org.yaml.snakeyaml.tokens.FlowSequenceEndToken; |
| 45 | |
import org.yaml.snakeyaml.tokens.FlowSequenceStartToken; |
| 46 | |
import org.yaml.snakeyaml.tokens.KeyToken; |
| 47 | |
import org.yaml.snakeyaml.tokens.ScalarToken; |
| 48 | |
import org.yaml.snakeyaml.tokens.StreamEndToken; |
| 49 | |
import org.yaml.snakeyaml.tokens.StreamStartToken; |
| 50 | |
import org.yaml.snakeyaml.tokens.TagToken; |
| 51 | |
import org.yaml.snakeyaml.tokens.TagTuple; |
| 52 | |
import org.yaml.snakeyaml.tokens.Token; |
| 53 | |
import org.yaml.snakeyaml.tokens.ValueToken; |
| 54 | |
import org.yaml.snakeyaml.util.ArrayStack; |
| 55 | |
import org.yaml.snakeyaml.util.UriEncoder; |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
public final class ScannerImpl implements Scanner { |
| 84 | 1 | private final static Pattern NOT_HEXA = Pattern.compile("[^0-9A-Fa-f]"); |
| 85 | 1 | public final static Map<Character, String> ESCAPE_REPLACEMENTS = new HashMap<Character, String>(); |
| 86 | 1 | public final static Map<Character, Integer> ESCAPE_CODES = new HashMap<Character, Integer>(); |
| 87 | |
|
| 88 | |
static { |
| 89 | 1 | ESCAPE_REPLACEMENTS.put(new Character('0'), "\0"); |
| 90 | 1 | ESCAPE_REPLACEMENTS.put(new Character('a'), "\u0007"); |
| 91 | 1 | ESCAPE_REPLACEMENTS.put(new Character('b'), "\u0008"); |
| 92 | 1 | ESCAPE_REPLACEMENTS.put(new Character('t'), "\u0009"); |
| 93 | 1 | ESCAPE_REPLACEMENTS.put(new Character('n'), "\n"); |
| 94 | 1 | ESCAPE_REPLACEMENTS.put(new Character('v'), "\u000B"); |
| 95 | 1 | ESCAPE_REPLACEMENTS.put(new Character('f'), "\u000C"); |
| 96 | 1 | ESCAPE_REPLACEMENTS.put(new Character('r'), "\r"); |
| 97 | 1 | ESCAPE_REPLACEMENTS.put(new Character('e'), "\u001B"); |
| 98 | 1 | ESCAPE_REPLACEMENTS.put(new Character(' '), "\u0020"); |
| 99 | 1 | ESCAPE_REPLACEMENTS.put(new Character('"'), "\""); |
| 100 | 1 | ESCAPE_REPLACEMENTS.put(new Character('\\'), "\\"); |
| 101 | 1 | ESCAPE_REPLACEMENTS.put(new Character('N'), "\u0085"); |
| 102 | 1 | ESCAPE_REPLACEMENTS.put(new Character('_'), "\u00A0"); |
| 103 | 1 | ESCAPE_REPLACEMENTS.put(new Character('L'), "\u2028"); |
| 104 | 1 | ESCAPE_REPLACEMENTS.put(new Character('P'), "\u2029"); |
| 105 | |
|
| 106 | 1 | ESCAPE_CODES.put(new Character('x'), 2); |
| 107 | 1 | ESCAPE_CODES.put(new Character('u'), 4); |
| 108 | 1 | ESCAPE_CODES.put(new Character('U'), 8); |
| 109 | 1 | } |
| 110 | |
private final StreamReader reader; |
| 111 | |
|
| 112 | 3692 | private boolean done = false; |
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | 3692 | private int flowLevel = 0; |
| 117 | |
|
| 118 | |
|
| 119 | |
private List<Token> tokens; |
| 120 | |
|
| 121 | |
|
| 122 | 3692 | private int tokensTaken = 0; |
| 123 | |
|
| 124 | |
|
| 125 | 3692 | private int indent = -1; |
| 126 | |
|
| 127 | |
|
| 128 | |
private ArrayStack<Integer> indents; |
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | 3692 | private boolean allowSimpleKey = true; |
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
private Map<Integer, SimpleKey> possibleSimpleKeys; |
| 164 | |
|
| 165 | 3692 | public ScannerImpl(StreamReader reader) { |
| 166 | 3692 | this.reader = reader; |
| 167 | 3692 | this.tokens = new ArrayList<Token>(100); |
| 168 | 3692 | this.indents = new ArrayStack<Integer>(10); |
| 169 | |
|
| 170 | 3692 | this.possibleSimpleKeys = new LinkedHashMap<Integer, SimpleKey>(); |
| 171 | 3692 | fetchStreamStart(); |
| 172 | 3692 | } |
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
public boolean checkToken(Token.ID... choices) { |
| 178 | 3510680 | while (needMoreTokens()) { |
| 179 | 649823 | fetchMoreTokens(); |
| 180 | |
} |
| 181 | 2860855 | if (!this.tokens.isEmpty()) { |
| 182 | 2860735 | if (choices.length == 0) { |
| 183 | 1962 | return true; |
| 184 | |
} |
| 185 | |
|
| 186 | |
|
| 187 | 2858773 | Token.ID first = this.tokens.get(0).getTokenId(); |
| 188 | 5751008 | for (int i = 0; i < choices.length; i++) { |
| 189 | 3739635 | if (first == choices[i]) { |
| 190 | 847400 | return true; |
| 191 | |
} |
| 192 | |
} |
| 193 | |
} |
| 194 | 2011493 | return false; |
| 195 | |
} |
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
public Token peekToken() { |
| 201 | 444330 | while (needMoreTokens()) { |
| 202 | 1908 | fetchMoreTokens(); |
| 203 | |
} |
| 204 | 442422 | return this.tokens.get(0); |
| 205 | |
} |
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
public Token getToken() { |
| 211 | 840588 | if (!this.tokens.isEmpty()) { |
| 212 | 840587 | this.tokensTaken++; |
| 213 | 840587 | return this.tokens.remove(0); |
| 214 | |
} |
| 215 | 1 | return null; |
| 216 | |
} |
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
private boolean needMoreTokens() { |
| 221 | 3955010 | if (this.done) { |
| 222 | 36237 | return false; |
| 223 | |
} |
| 224 | 3918773 | if (this.tokens.isEmpty()) { |
| 225 | 390241 | return true; |
| 226 | |
} |
| 227 | |
|
| 228 | |
|
| 229 | 3528532 | stalePossibleSimpleKeys(); |
| 230 | 3528530 | return nextPossibleSimpleKey() == this.tokensTaken; |
| 231 | |
} |
| 232 | |
|
| 233 | |
private void fetchMoreTokens() { |
| 234 | |
|
| 235 | 651731 | scanToNextToken(); |
| 236 | |
|
| 237 | 651731 | stalePossibleSimpleKeys(); |
| 238 | |
|
| 239 | |
|
| 240 | 651731 | unwindIndent(reader.getColumn()); |
| 241 | |
|
| 242 | 651731 | char ch = reader.peek(); |
| 243 | 651731 | switch (ch) { |
| 244 | |
case '\0': |
| 245 | |
|
| 246 | 3582 | fetchStreamEnd(); |
| 247 | 3580 | return; |
| 248 | |
case '%': |
| 249 | |
|
| 250 | 1810 | if (checkDirective()) { |
| 251 | 1810 | fetchDirective(); |
| 252 | 1784 | return; |
| 253 | |
} |
| 254 | |
break; |
| 255 | |
case '-': |
| 256 | |
|
| 257 | 117036 | if (checkDocumentStart()) { |
| 258 | 2336 | fetchDocumentStart(); |
| 259 | 2336 | return; |
| 260 | |
|
| 261 | 114700 | } else if (checkBlockEntry()) { |
| 262 | 114646 | fetchBlockEntry(); |
| 263 | 114644 | return; |
| 264 | |
} |
| 265 | |
break; |
| 266 | |
case '.': |
| 267 | |
|
| 268 | 156 | if (checkDocumentEnd()) { |
| 269 | 136 | fetchDocumentEnd(); |
| 270 | 136 | return; |
| 271 | |
} |
| 272 | |
break; |
| 273 | |
|
| 274 | |
case '[': |
| 275 | |
|
| 276 | 1092 | fetchFlowSequenceStart(); |
| 277 | 1092 | return; |
| 278 | |
case '{': |
| 279 | |
|
| 280 | 2788 | fetchFlowMappingStart(); |
| 281 | 2788 | return; |
| 282 | |
case ']': |
| 283 | |
|
| 284 | 1089 | fetchFlowSequenceEnd(); |
| 285 | 1087 | return; |
| 286 | |
case '}': |
| 287 | |
|
| 288 | 2786 | fetchFlowMappingEnd(); |
| 289 | 2786 | return; |
| 290 | |
case ',': |
| 291 | |
|
| 292 | 3569 | fetchFlowEntry(); |
| 293 | 3569 | return; |
| 294 | |
|
| 295 | |
case '?': |
| 296 | |
|
| 297 | 971 | if (checkKey()) { |
| 298 | 971 | fetchKey(); |
| 299 | 969 | return; |
| 300 | |
} |
| 301 | |
break; |
| 302 | |
case ':': |
| 303 | |
|
| 304 | 140221 | if (checkValue()) { |
| 305 | 139915 | fetchValue(); |
| 306 | 139913 | return; |
| 307 | |
} |
| 308 | |
break; |
| 309 | |
case '*': |
| 310 | |
|
| 311 | 1425 | fetchAlias(); |
| 312 | 1425 | return; |
| 313 | |
case '&': |
| 314 | |
|
| 315 | 1372 | fetchAnchor(); |
| 316 | 1368 | return; |
| 317 | |
case '!': |
| 318 | |
|
| 319 | 12164 | fetchTag(); |
| 320 | 12149 | return; |
| 321 | |
case '|': |
| 322 | |
|
| 323 | 634 | if (this.flowLevel == 0) { |
| 324 | 634 | fetchLiteral(); |
| 325 | 634 | return; |
| 326 | |
} |
| 327 | |
break; |
| 328 | |
case '>': |
| 329 | |
|
| 330 | 554 | if (this.flowLevel == 0) { |
| 331 | 554 | fetchFolded(); |
| 332 | 548 | return; |
| 333 | |
} |
| 334 | |
break; |
| 335 | |
case '\'': |
| 336 | |
|
| 337 | 203279 | fetchSingle(); |
| 338 | 203277 | return; |
| 339 | |
case '"': |
| 340 | |
|
| 341 | 6067 | fetchDouble(); |
| 342 | 6061 | return; |
| 343 | |
} |
| 344 | |
|
| 345 | 151516 | if (checkPlain()) { |
| 346 | 151513 | fetchPlain(); |
| 347 | 151511 | return; |
| 348 | |
} |
| 349 | |
|
| 350 | 3 | String chRepresentation = String.valueOf(ch); |
| 351 | 3 | for (Character s : ESCAPE_REPLACEMENTS.keySet()) { |
| 352 | 45 | String v = ESCAPE_REPLACEMENTS.get(s); |
| 353 | 45 | if (v.equals(chRepresentation)) { |
| 354 | 1 | chRepresentation = "\\" + s; |
| 355 | 1 | break; |
| 356 | |
} |
| 357 | 44 | } |
| 358 | 3 | throw new ScannerException("while scanning for the next token", null, "found character " |
| 359 | |
+ ch + "'" + chRepresentation + "' that cannot start any token", reader.getMark()); |
| 360 | |
} |
| 361 | |
|
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
|
| 366 | |
|
| 367 | |
|
| 368 | |
private int nextPossibleSimpleKey() { |
| 369 | |
|
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | 3528530 | if (!this.possibleSimpleKeys.isEmpty()) { |
| 374 | 328867 | return this.possibleSimpleKeys.values().iterator().next().getTokenNumber(); |
| 375 | |
} |
| 376 | 3199663 | return -1; |
| 377 | |
} |
| 378 | |
|
| 379 | |
|
| 380 | |
|
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | |
|
| 385 | |
|
| 386 | |
|
| 387 | |
|
| 388 | |
|
| 389 | |
private void stalePossibleSimpleKeys() { |
| 390 | |
|
| 391 | 4180263 | if (!this.possibleSimpleKeys.isEmpty()) { |
| 392 | 591207 | for (Iterator<SimpleKey> iterator = this.possibleSimpleKeys.values().iterator(); iterator |
| 393 | 1190612 | .hasNext();) { |
| 394 | 599407 | SimpleKey key = iterator.next(); |
| 395 | 599407 | if ((key.getLine() != reader.getLine()) |
| 396 | |
|| (reader.getIndex() - key.getIndex() > 1024)) { |
| 397 | 105778 | if (key.isRequired()) { |
| 398 | 2 | throw new ScannerException("while scanning a simple key", key.getMark(), |
| 399 | |
"could not found expected ':'", reader.getMark()); |
| 400 | |
} |
| 401 | 105776 | iterator.remove(); |
| 402 | |
} |
| 403 | 599405 | } |
| 404 | |
} |
| 405 | 4180261 | } |
| 406 | |
|
| 407 | |
|
| 408 | |
|
| 409 | |
|
| 410 | |
|
| 411 | |
|
| 412 | |
private void savePossibleSimpleKey() { |
| 413 | |
|
| 414 | |
|
| 415 | |
|
| 416 | |
|
| 417 | |
|
| 418 | 379700 | boolean required = ((this.flowLevel == 0) && (this.indent == this.reader.getColumn())); |
| 419 | |
|
| 420 | 379700 | if (allowSimpleKey || !required) { |
| 421 | |
|
| 422 | |
|
| 423 | |
|
| 424 | |
} else { |
| 425 | 0 | throw new YAMLException( |
| 426 | |
"A simple key is required only if it is the first token in the current line"); |
| 427 | |
} |
| 428 | |
|
| 429 | |
|
| 430 | |
|
| 431 | 379700 | if (this.allowSimpleKey) { |
| 432 | 247553 | removePossibleSimpleKey(); |
| 433 | 247553 | int tokenNumber = this.tokensTaken + this.tokens.size(); |
| 434 | 247553 | SimpleKey key = new SimpleKey(tokenNumber, required, reader.getIndex(), |
| 435 | |
reader.getLine(), this.reader.getColumn(), this.reader.getMark()); |
| 436 | 247553 | this.possibleSimpleKeys.put(this.flowLevel, key); |
| 437 | |
} |
| 438 | 379700 | } |
| 439 | |
|
| 440 | |
|
| 441 | |
|
| 442 | |
|
| 443 | |
private void removePossibleSimpleKey() { |
| 444 | 380606 | SimpleKey key = possibleSimpleKeys.remove(flowLevel); |
| 445 | 380606 | if (key != null && key.isRequired()) { |
| 446 | 4 | throw new ScannerException("while scanning a simple key", key.getMark(), |
| 447 | |
"could not found expected ':'", reader.getMark()); |
| 448 | |
} |
| 449 | 380602 | } |
| 450 | |
|
| 451 | |
|
| 452 | |
|
| 453 | |
|
| 454 | |
|
| 455 | |
|
| 456 | |
|
| 457 | |
|
| 458 | |
|
| 459 | |
|
| 460 | |
|
| 461 | |
|
| 462 | |
|
| 463 | |
private void unwindIndent(int col) { |
| 464 | |
|
| 465 | |
|
| 466 | 659595 | if (this.flowLevel != 0) { |
| 467 | 28706 | return; |
| 468 | |
} |
| 469 | |
|
| 470 | |
|
| 471 | 654062 | while (this.indent > col) { |
| 472 | 23173 | Mark mark = reader.getMark(); |
| 473 | 23173 | this.indent = this.indents.pop(); |
| 474 | 23173 | this.tokens.add(new BlockEndToken(mark, mark)); |
| 475 | 23173 | } |
| 476 | 630889 | } |
| 477 | |
|
| 478 | |
|
| 479 | |
|
| 480 | |
|
| 481 | |
private boolean addIndent(int column) { |
| 482 | 250488 | if (this.indent < column) { |
| 483 | 23213 | this.indents.push(this.indent); |
| 484 | 23213 | this.indent = column; |
| 485 | 23213 | return true; |
| 486 | |
} |
| 487 | 227275 | return false; |
| 488 | |
} |
| 489 | |
|
| 490 | |
|
| 491 | |
|
| 492 | |
|
| 493 | |
|
| 494 | |
|
| 495 | |
|
| 496 | |
private void fetchStreamStart() { |
| 497 | |
|
| 498 | 3692 | Mark mark = reader.getMark(); |
| 499 | |
|
| 500 | |
|
| 501 | 3692 | Token token = new StreamStartToken(mark, mark); |
| 502 | 3692 | this.tokens.add(token); |
| 503 | 3692 | } |
| 504 | |
|
| 505 | |
private void fetchStreamEnd() { |
| 506 | |
|
| 507 | 3582 | unwindIndent(-1); |
| 508 | |
|
| 509 | |
|
| 510 | 3582 | removePossibleSimpleKey(); |
| 511 | 3580 | this.allowSimpleKey = false; |
| 512 | 3580 | this.possibleSimpleKeys.clear(); |
| 513 | |
|
| 514 | |
|
| 515 | 3580 | Mark mark = reader.getMark(); |
| 516 | |
|
| 517 | |
|
| 518 | 3580 | Token token = new StreamEndToken(mark, mark); |
| 519 | 3580 | this.tokens.add(token); |
| 520 | |
|
| 521 | |
|
| 522 | 3580 | this.done = true; |
| 523 | 3580 | } |
| 524 | |
|
| 525 | |
private void fetchDirective() { |
| 526 | |
|
| 527 | 1810 | unwindIndent(-1); |
| 528 | |
|
| 529 | |
|
| 530 | 1810 | removePossibleSimpleKey(); |
| 531 | 1810 | this.allowSimpleKey = false; |
| 532 | |
|
| 533 | |
|
| 534 | 1810 | Token tok = scanDirective(); |
| 535 | 1784 | this.tokens.add(tok); |
| 536 | 1784 | } |
| 537 | |
|
| 538 | |
private void fetchDocumentStart() { |
| 539 | 2336 | fetchDocumentIndicator(true); |
| 540 | 2336 | } |
| 541 | |
|
| 542 | |
private void fetchDocumentEnd() { |
| 543 | 136 | fetchDocumentIndicator(false); |
| 544 | 136 | } |
| 545 | |
|
| 546 | |
private void fetchDocumentIndicator(boolean isDocumentStart) { |
| 547 | |
|
| 548 | 2472 | unwindIndent(-1); |
| 549 | |
|
| 550 | |
|
| 551 | |
|
| 552 | 2472 | removePossibleSimpleKey(); |
| 553 | 2472 | this.allowSimpleKey = false; |
| 554 | |
|
| 555 | |
|
| 556 | 2472 | Mark startMark = reader.getMark(); |
| 557 | 2472 | reader.forward(3); |
| 558 | 2472 | Mark endMark = reader.getMark(); |
| 559 | |
Token token; |
| 560 | 2472 | if (isDocumentStart) { |
| 561 | 2336 | token = new DocumentStartToken(startMark, endMark); |
| 562 | |
} else { |
| 563 | 136 | token = new DocumentEndToken(startMark, endMark); |
| 564 | |
} |
| 565 | 2472 | this.tokens.add(token); |
| 566 | 2472 | } |
| 567 | |
|
| 568 | |
private void fetchFlowSequenceStart() { |
| 569 | 1092 | fetchFlowCollectionStart(false); |
| 570 | 1092 | } |
| 571 | |
|
| 572 | |
private void fetchFlowMappingStart() { |
| 573 | 2788 | fetchFlowCollectionStart(true); |
| 574 | 2788 | } |
| 575 | |
|
| 576 | |
private void fetchFlowCollectionStart(boolean isMappingStart) { |
| 577 | |
|
| 578 | 3880 | savePossibleSimpleKey(); |
| 579 | |
|
| 580 | |
|
| 581 | 3880 | this.flowLevel++; |
| 582 | |
|
| 583 | |
|
| 584 | 3880 | this.allowSimpleKey = true; |
| 585 | |
|
| 586 | |
|
| 587 | 3880 | Mark startMark = reader.getMark(); |
| 588 | 3880 | reader.forward(1); |
| 589 | 3880 | Mark endMark = reader.getMark(); |
| 590 | |
Token token; |
| 591 | 3880 | if (isMappingStart) { |
| 592 | 2788 | token = new FlowMappingStartToken(startMark, endMark); |
| 593 | |
} else { |
| 594 | 1092 | token = new FlowSequenceStartToken(startMark, endMark); |
| 595 | |
} |
| 596 | 3880 | this.tokens.add(token); |
| 597 | 3880 | } |
| 598 | |
|
| 599 | |
private void fetchFlowSequenceEnd() { |
| 600 | 1089 | fetchFlowCollectionEnd(false); |
| 601 | 1087 | } |
| 602 | |
|
| 603 | |
private void fetchFlowMappingEnd() { |
| 604 | 2786 | fetchFlowCollectionEnd(true); |
| 605 | 2786 | } |
| 606 | |
|
| 607 | |
private void fetchFlowCollectionEnd(boolean isMappingEnd) { |
| 608 | |
|
| 609 | 3875 | removePossibleSimpleKey(); |
| 610 | |
|
| 611 | |
|
| 612 | 3873 | this.flowLevel--; |
| 613 | |
|
| 614 | |
|
| 615 | 3873 | this.allowSimpleKey = false; |
| 616 | |
|
| 617 | |
|
| 618 | 3873 | Mark startMark = reader.getMark(); |
| 619 | 3873 | reader.forward(); |
| 620 | 3873 | Mark endMark = reader.getMark(); |
| 621 | |
Token token; |
| 622 | 3873 | if (isMappingEnd) { |
| 623 | 2786 | token = new FlowMappingEndToken(startMark, endMark); |
| 624 | |
} else { |
| 625 | 1087 | token = new FlowSequenceEndToken(startMark, endMark); |
| 626 | |
} |
| 627 | 3873 | this.tokens.add(token); |
| 628 | 3873 | } |
| 629 | |
|
| 630 | |
private void fetchFlowEntry() { |
| 631 | |
|
| 632 | 3569 | this.allowSimpleKey = true; |
| 633 | |
|
| 634 | |
|
| 635 | 3569 | removePossibleSimpleKey(); |
| 636 | |
|
| 637 | |
|
| 638 | 3569 | Mark startMark = reader.getMark(); |
| 639 | 3569 | reader.forward(); |
| 640 | 3569 | Mark endMark = reader.getMark(); |
| 641 | 3569 | Token token = new FlowEntryToken(startMark, endMark); |
| 642 | 3569 | this.tokens.add(token); |
| 643 | 3569 | } |
| 644 | |
|
| 645 | |
private void fetchBlockEntry() { |
| 646 | |
|
| 647 | 114646 | if (this.flowLevel == 0) { |
| 648 | |
|
| 649 | 114646 | if (!this.allowSimpleKey) { |
| 650 | 2 | throw new ScannerException(null, null, "sequence entries are not allowed here", |
| 651 | |
reader.getMark()); |
| 652 | |
} |
| 653 | |
|
| 654 | |
|
| 655 | 114644 | if (addIndent(this.reader.getColumn())) { |
| 656 | 603 | Mark mark = reader.getMark(); |
| 657 | 603 | this.tokens.add(new BlockSequenceStartToken(mark, mark)); |
| 658 | |
} |
| 659 | |
} else { |
| 660 | |
|
| 661 | |
|
| 662 | |
} |
| 663 | |
|
| 664 | 114644 | this.allowSimpleKey = true; |
| 665 | |
|
| 666 | |
|
| 667 | 114644 | removePossibleSimpleKey(); |
| 668 | |
|
| 669 | |
|
| 670 | 114644 | Mark startMark = reader.getMark(); |
| 671 | 114644 | reader.forward(); |
| 672 | 114644 | Mark endMark = reader.getMark(); |
| 673 | 114644 | Token token = new BlockEntryToken(startMark, endMark); |
| 674 | 114644 | this.tokens.add(token); |
| 675 | 114644 | } |
| 676 | |
|
| 677 | |
private void fetchKey() { |
| 678 | |
|
| 679 | 971 | if (this.flowLevel == 0) { |
| 680 | |
|
| 681 | 179 | if (!this.allowSimpleKey) { |
| 682 | 2 | throw new ScannerException(null, null, "mapping keys are not allowed here", |
| 683 | |
reader.getMark()); |
| 684 | |
} |
| 685 | |
|
| 686 | 177 | if (addIndent(this.reader.getColumn())) { |
| 687 | 79 | Mark mark = reader.getMark(); |
| 688 | 79 | this.tokens.add(new BlockMappingStartToken(mark, mark)); |
| 689 | |
} |
| 690 | |
} |
| 691 | |
|
| 692 | 969 | this.allowSimpleKey = this.flowLevel == 0; |
| 693 | |
|
| 694 | |
|
| 695 | 969 | removePossibleSimpleKey(); |
| 696 | |
|
| 697 | |
|
| 698 | 969 | Mark startMark = reader.getMark(); |
| 699 | 969 | reader.forward(); |
| 700 | 969 | Mark endMark = reader.getMark(); |
| 701 | 969 | Token token = new KeyToken(startMark, endMark); |
| 702 | 969 | this.tokens.add(token); |
| 703 | 969 | } |
| 704 | |
|
| 705 | |
private void fetchValue() { |
| 706 | |
|
| 707 | 139915 | SimpleKey key = this.possibleSimpleKeys.remove(this.flowLevel); |
| 708 | 139915 | if (key != null) { |
| 709 | |
|
| 710 | 138969 | this.tokens.add(key.getTokenNumber() - this.tokensTaken, new KeyToken(key.getMark(), |
| 711 | |
key.getMark())); |
| 712 | |
|
| 713 | |
|
| 714 | |
|
| 715 | 138969 | if (this.flowLevel == 0) { |
| 716 | 135507 | if (addIndent(key.getColumn())) { |
| 717 | 22529 | this.tokens.add(key.getTokenNumber() - this.tokensTaken, |
| 718 | |
new BlockMappingStartToken(key.getMark(), key.getMark())); |
| 719 | |
} |
| 720 | |
} |
| 721 | |
|
| 722 | 138969 | this.allowSimpleKey = false; |
| 723 | |
|
| 724 | |
} else { |
| 725 | |
|
| 726 | |
|
| 727 | |
|
| 728 | 946 | if (this.flowLevel == 0) { |
| 729 | |
|
| 730 | |
|
| 731 | |
|
| 732 | 162 | if (!this.allowSimpleKey) { |
| 733 | 2 | throw new ScannerException(null, null, "mapping values are not allowed here", |
| 734 | |
reader.getMark()); |
| 735 | |
} |
| 736 | |
} |
| 737 | |
|
| 738 | |
|
| 739 | |
|
| 740 | |
|
| 741 | 944 | if (flowLevel == 0) { |
| 742 | 160 | if (addIndent(reader.getColumn())) { |
| 743 | 2 | Mark mark = reader.getMark(); |
| 744 | 2 | this.tokens.add(new BlockMappingStartToken(mark, mark)); |
| 745 | |
} |
| 746 | |
} |
| 747 | |
|
| 748 | |
|
| 749 | 944 | allowSimpleKey = (flowLevel == 0); |
| 750 | |
|
| 751 | |
|
| 752 | 944 | removePossibleSimpleKey(); |
| 753 | |
} |
| 754 | |
|
| 755 | 139913 | Mark startMark = reader.getMark(); |
| 756 | 139913 | reader.forward(); |
| 757 | 139913 | Mark endMark = reader.getMark(); |
| 758 | 139913 | Token token = new ValueToken(startMark, endMark); |
| 759 | 139913 | this.tokens.add(token); |
| 760 | 139913 | } |
| 761 | |
|
| 762 | |
private void fetchAlias() { |
| 763 | |
|
| 764 | 1425 | savePossibleSimpleKey(); |
| 765 | |
|
| 766 | |
|
| 767 | 1425 | this.allowSimpleKey = false; |
| 768 | |
|
| 769 | |
|
| 770 | 1425 | Token tok = scanAnchor(false); |
| 771 | 1425 | this.tokens.add(tok); |
| 772 | 1425 | } |
| 773 | |
|
| 774 | |
private void fetchAnchor() { |
| 775 | |
|
| 776 | 1372 | savePossibleSimpleKey(); |
| 777 | |
|
| 778 | |
|
| 779 | 1372 | this.allowSimpleKey = false; |
| 780 | |
|
| 781 | |
|
| 782 | 1372 | Token tok = scanAnchor(true); |
| 783 | 1368 | this.tokens.add(tok); |
| 784 | 1368 | } |
| 785 | |
|
| 786 | |
private void fetchTag() { |
| 787 | |
|
| 788 | 12164 | savePossibleSimpleKey(); |
| 789 | |
|
| 790 | |
|
| 791 | 12164 | this.allowSimpleKey = false; |
| 792 | |
|
| 793 | |
|
| 794 | 12164 | Token tok = scanTag(); |
| 795 | 12149 | this.tokens.add(tok); |
| 796 | 12149 | } |
| 797 | |
|
| 798 | |
private void fetchLiteral() { |
| 799 | 634 | fetchBlockScalar('|'); |
| 800 | 634 | } |
| 801 | |
|
| 802 | |
private void fetchFolded() { |
| 803 | 554 | fetchBlockScalar('>'); |
| 804 | 548 | } |
| 805 | |
|
| 806 | |
private void fetchBlockScalar(char style) { |
| 807 | |
|
| 808 | 1188 | this.allowSimpleKey = true; |
| 809 | |
|
| 810 | |
|
| 811 | 1188 | removePossibleSimpleKey(); |
| 812 | |
|
| 813 | |
|
| 814 | 1188 | Token tok = scanBlockScalar(style); |
| 815 | 1182 | this.tokens.add(tok); |
| 816 | 1182 | } |
| 817 | |
|
| 818 | |
private void fetchSingle() { |
| 819 | 203279 | fetchFlowScalar('\''); |
| 820 | 203277 | } |
| 821 | |
|
| 822 | |
private void fetchDouble() { |
| 823 | 6067 | fetchFlowScalar('"'); |
| 824 | 6061 | } |
| 825 | |
|
| 826 | |
private void fetchFlowScalar(char style) { |
| 827 | |
|
| 828 | 209346 | savePossibleSimpleKey(); |
| 829 | |
|
| 830 | |
|
| 831 | 209346 | this.allowSimpleKey = false; |
| 832 | |
|
| 833 | |
|
| 834 | 209346 | Token tok = scanFlowScalar(style); |
| 835 | 209338 | this.tokens.add(tok); |
| 836 | 209338 | } |
| 837 | |
|
| 838 | |
private void fetchPlain() { |
| 839 | |
|
| 840 | 151513 | savePossibleSimpleKey(); |
| 841 | |
|
| 842 | |
|
| 843 | |
|
| 844 | |
|
| 845 | 151513 | this.allowSimpleKey = false; |
| 846 | |
|
| 847 | |
|
| 848 | 151513 | Token tok = scanPlain(); |
| 849 | 151511 | this.tokens.add(tok); |
| 850 | 151511 | } |
| 851 | |
|
| 852 | |
|
| 853 | |
|
| 854 | |
private boolean checkDirective() { |
| 855 | |
|
| 856 | |
|
| 857 | 1810 | return reader.getColumn() == 0; |
| 858 | |
} |
| 859 | |
|
| 860 | |
private boolean checkDocumentStart() { |
| 861 | |
|
| 862 | 117036 | if (reader.getColumn() == 0) { |
| 863 | 16278 | if ("---".equals(reader.prefix(3)) && Constant.NULL_BL_T_LINEBR.has(reader.peek(3))) { |
| 864 | 2336 | return true; |
| 865 | |
} |
| 866 | |
} |
| 867 | 114700 | return false; |
| 868 | |
} |
| 869 | |
|
| 870 | |
private boolean checkDocumentEnd() { |
| 871 | |
|
| 872 | 156 | if (reader.getColumn() == 0) { |
| 873 | 147 | if ("...".equals(reader.prefix(3)) && Constant.NULL_BL_T_LINEBR.has(reader.peek(3))) { |
| 874 | 136 | return true; |
| 875 | |
} |
| 876 | |
} |
| 877 | 20 | return false; |
| 878 | |
} |
| 879 | |
|
| 880 | |
private boolean checkBlockEntry() { |
| 881 | |
|
| 882 | 114700 | return Constant.NULL_BL_T_LINEBR.has(reader.peek(1)); |
| 883 | |
} |
| 884 | |
|
| 885 | |
private boolean checkKey() { |
| 886 | |
|
| 887 | 971 | if (this.flowLevel != 0) { |
| 888 | 792 | return true; |
| 889 | |
} else { |
| 890 | |
|
| 891 | 179 | return Constant.NULL_BL_T_LINEBR.has(reader.peek(1)); |
| 892 | |
} |
| 893 | |
} |
| 894 | |
|
| 895 | |
private boolean checkValue() { |
| 896 | |
|
| 897 | 140221 | if (flowLevel != 0) { |
| 898 | 4246 | return true; |
| 899 | |
} else { |
| 900 | |
|
| 901 | 135975 | return Constant.NULL_BL_T_LINEBR.has(reader.peek(1)); |
| 902 | |
} |
| 903 | |
} |
| 904 | |
|
| 905 | |
private boolean checkPlain() { |
| 906 | |
|
| 907 | |
|
| 908 | |
|
| 909 | |
|
| 910 | |
|
| 911 | |
|
| 912 | |
|
| 913 | |
|
| 914 | |
|
| 915 | |
|
| 916 | |
|
| 917 | |
|
| 918 | |
|
| 919 | |
|
| 920 | |
|
| 921 | |
|
| 922 | 151516 | char ch = reader.peek(); |
| 923 | 151516 | return Constant.NULL_BL_T_LINEBR.hasNo(ch, "-?:,[]{}#&*!|>\'\"%@`") |
| 924 | |
|| (Constant.NULL_BL_T_LINEBR.hasNo(reader.peek(1)) && (ch == '-' || (this.flowLevel == 0 && "?:" |
| 925 | |
.indexOf(ch) != -1))); |
| 926 | |
} |
| 927 | |
|
| 928 | |
|
| 929 | |
|
| 930 | |
|
| 931 | |
|
| 932 | |
|
| 933 | |
|
| 934 | |
|
| 935 | |
|
| 936 | |
|
| 937 | |
|
| 938 | |
|
| 939 | |
|
| 940 | |
|
| 941 | |
|
| 942 | |
|
| 943 | |
|
| 944 | |
|
| 945 | |
|
| 946 | |
|
| 947 | |
|
| 948 | |
|
| 949 | |
|
| 950 | |
|
| 951 | |
private void scanToNextToken() { |
| 952 | 651731 | if (reader.getIndex() == 0 && reader.peek() == '\uFEFF') { |
| 953 | 0 | reader.forward(); |
| 954 | |
} |
| 955 | 651731 | boolean found = false; |
| 956 | 1446818 | while (!found) { |
| 957 | 795087 | int ff = 0; |
| 958 | 1335502 | while (reader.peek(ff) == ' ') { |
| 959 | 540415 | ff++; |
| 960 | |
} |
| 961 | 795087 | if (ff > 0) { |
| 962 | 376974 | reader.forward(ff); |
| 963 | |
} |
| 964 | |
|
| 965 | 795087 | if (reader.peek() == '#') { |
| 966 | 557 | ff = 0; |
| 967 | 10345 | while (Constant.NULL_OR_LINEBR.hasNo(reader.peek(ff))) { |
| 968 | 9788 | ff++; |
| 969 | |
} |
| 970 | 557 | if (ff > 0) { |
| 971 | 557 | reader.forward(ff); |
| 972 | |
} |
| 973 | |
} |
| 974 | 795087 | if (scanLineBreak().length() != 0) { |
| 975 | 143356 | if (this.flowLevel == 0) { |
| 976 | 140509 | this.allowSimpleKey = true; |
| 977 | |
} |
| 978 | |
} else { |
| 979 | 651731 | found = true; |
| 980 | |
} |
| 981 | 795087 | } |
| 982 | 651731 | } |
| 983 | |
|
| 984 | |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
| 985 | |
private Token scanDirective() { |
| 986 | |
|
| 987 | 1810 | Mark startMark = reader.getMark(); |
| 988 | |
Mark endMark; |
| 989 | 1810 | reader.forward(); |
| 990 | 1810 | String name = scanDirectiveName(startMark); |
| 991 | 1806 | List<?> value = null; |
| 992 | 1806 | if ("YAML".equals(name)) { |
| 993 | 1640 | value = scanYamlDirectiveValue(startMark); |
| 994 | 1628 | endMark = reader.getMark(); |
| 995 | 166 | } else if ("TAG".equals(name)) { |
| 996 | 161 | value = scanTagDirectiveValue(startMark); |
| 997 | 153 | endMark = reader.getMark(); |
| 998 | |
} else { |
| 999 | 5 | endMark = reader.getMark(); |
| 1000 | 5 | int ff = 0; |
| 1001 | 150 | while (Constant.NULL_OR_LINEBR.hasNo(reader.peek(ff))) { |
| 1002 | 145 | ff++; |
| 1003 | |
} |
| 1004 | 5 | if (ff > 0) { |
| 1005 | 5 | reader.forward(ff); |
| 1006 | |
} |
| 1007 | |
} |
| 1008 | 1786 | scanDirectiveIgnoredLine(startMark); |
| 1009 | 1784 | return new DirectiveToken(name, value, startMark, endMark); |
| 1010 | |
} |
| 1011 | |
|
| 1012 | |
private String scanDirectiveName(Mark startMark) { |
| 1013 | |
|
| 1014 | 1810 | int length = 0; |
| 1015 | 1810 | char ch = reader.peek(length); |
| 1016 | 8904 | while (Constant.ALPHA.has(ch)) { |
| 1017 | 7094 | length++; |
| 1018 | 7094 | ch = reader.peek(length); |
| 1019 | |
} |
| 1020 | 1810 | if (length == 0) { |
| 1021 | 2 | throw new ScannerException("while scanning a directive", startMark, |
| 1022 | |
"expected alphabetic or numeric character, but found " + ch + "(" + ((int) ch) |
| 1023 | |
+ ")", reader.getMark()); |
| 1024 | |
} |
| 1025 | 1808 | String value = reader.prefixForward(length); |
| 1026 | 1808 | ch = reader.peek(); |
| 1027 | 1808 | if (Constant.NULL_BL_LINEBR.hasNo(ch)) { |
| 1028 | 2 | throw new ScannerException("while scanning a directive", startMark, |
| 1029 | |
"expected alphabetic or numeric character, but found " + ch + "(" + ((int) ch) |
| 1030 | |
+ ")", reader.getMark()); |
| 1031 | |
} |
| 1032 | 1806 | return value; |
| 1033 | |
} |
| 1034 | |
|
| 1035 | |
private List<Integer> scanYamlDirectiveValue(Mark startMark) { |
| 1036 | |
|
| 1037 | 3302 | while (reader.peek() == ' ') { |
| 1038 | 1662 | reader.forward(); |
| 1039 | |
} |
| 1040 | 1640 | Integer major = scanYamlDirectiveNumber(startMark); |
| 1041 | 1636 | if (reader.peek() != '.') { |
| 1042 | 2 | throw new ScannerException("while scanning a directive", startMark, |
| 1043 | |
"expected a digit or '.', but found " + reader.peek() + "(" |
| 1044 | |
+ ((int) reader.peek()) + ")", reader.getMark()); |
| 1045 | |
} |
| 1046 | 1634 | reader.forward(); |
| 1047 | 1634 | Integer minor = scanYamlDirectiveNumber(startMark); |
| 1048 | 1630 | if (Constant.NULL_BL_LINEBR.hasNo(reader.peek())) { |
| 1049 | 2 | throw new ScannerException("while scanning a directive", startMark, |
| 1050 | |
"expected a digit or ' ', but found " + reader.peek() + "(" |
| 1051 | |
+ ((int) reader.peek()) + ")", reader.getMark()); |
| 1052 | |
} |
| 1053 | 1628 | List<Integer> result = new ArrayList<Integer>(2); |
| 1054 | 1628 | result.add(major); |
| 1055 | 1628 | result.add(minor); |
| 1056 | 1628 | return result; |
| 1057 | |
} |
| 1058 | |
|
| 1059 | |
private Integer scanYamlDirectiveNumber(Mark startMark) { |
| 1060 | |
|
| 1061 | 3274 | char ch = reader.peek(); |
| 1062 | 3274 | if (!Character.isDigit(ch)) { |
| 1063 | 8 | throw new ScannerException("while scanning a directive", startMark, |
| 1064 | |
"expected a digit, but found " + ch + "(" + ((int) ch) + ")", reader.getMark()); |
| 1065 | |
} |
| 1066 | 3266 | int length = 0; |
| 1067 | 6540 | while (Character.isDigit(reader.peek(length))) { |
| 1068 | 3274 | length++; |
| 1069 | |
} |
| 1070 | 3266 | Integer value = new Integer(reader.prefixForward(length)); |
| 1071 | 3266 | return value; |
| 1072 | |
} |
| 1073 | |
|
| 1074 | |
private List<String> scanTagDirectiveValue(Mark startMark) { |
| 1075 | |
|
| 1076 | 352 | while (reader.peek() == ' ') { |
| 1077 | 191 | reader.forward(); |
| 1078 | |
} |
| 1079 | 161 | String handle = scanTagDirectiveHandle(startMark); |
| 1080 | 382 | while (reader.peek() == ' ') { |
| 1081 | 227 | reader.forward(); |
| 1082 | |
} |
| 1083 | 155 | String prefix = scanTagDirectivePrefix(startMark); |
| 1084 | 153 | List<String> result = new ArrayList<String>(2); |
| 1085 | 153 | result.add(handle); |
| 1086 | 153 | result.add(prefix); |
| 1087 | 153 | return result; |
| 1088 | |
} |
| 1089 | |
|
| 1090 | |
private String scanTagDirectiveHandle(Mark startMark) { |
| 1091 | |
|
| 1092 | 161 | String value = scanTagHandle("directive", startMark); |
| 1093 | 157 | char ch = reader.peek(); |
| 1094 | 157 | if (ch != ' ') { |
| 1095 | 2 | throw new ScannerException("while scanning a directive", startMark, |
| 1096 | |
"expected ' ', but found " + reader.peek() + "(" + ch + ")", reader.getMark()); |
| 1097 | |
} |
| 1098 | 155 | return value; |
| 1099 | |
} |
| 1100 | |
|
| 1101 | |
private String scanTagDirectivePrefix(Mark startMark) { |
| 1102 | |
|
| 1103 | 155 | String value = scanTagUri("directive", startMark); |
| 1104 | 155 | if (Constant.NULL_BL_LINEBR.hasNo(reader.peek())) { |
| 1105 | 2 | throw new ScannerException("while scanning a directive", startMark, |
| 1106 | |
"expected ' ', but found " + reader.peek() + "(" + ((int) reader.peek()) + ")", |
| 1107 | |
reader.getMark()); |
| 1108 | |
} |
| 1109 | 153 | return value; |
| 1110 | |
} |
| 1111 | |
|
| 1112 | |
private String scanDirectiveIgnoredLine(Mark startMark) { |
| 1113 | |
|
| 1114 | 1786 | int ff = 0; |
| 1115 | 1793 | while (reader.peek(ff) == ' ') { |
| 1116 | 7 | ff++; |
| 1117 | |
} |
| 1118 | 1786 | if (ff > 0) { |
| 1119 | 7 | reader.forward(ff); |
| 1120 | |
} |
| 1121 | 1786 | if (reader.peek() == '#') { |
| 1122 | 5 | ff = 0; |
| 1123 | 90 | while (Constant.NULL_OR_LINEBR.hasNo(reader.peek(ff))) { |
| 1124 | 85 | ff++; |
| 1125 | |
} |
| 1126 | 5 | reader.forward(ff); |
| 1127 | |
} |
| 1128 | 1786 | char ch = reader.peek(); |
| 1129 | 1786 | String lineBreak = scanLineBreak(); |
| 1130 | 1786 | if (lineBreak.length() == 0 && ch != '\0') { |
| 1131 | 2 | throw new ScannerException("while scanning a directive", startMark, |
| 1132 | |
"expected a comment or a line break, but found " + ch + "(" + ((int) ch) + ")", |
| 1133 | |
reader.getMark()); |
| 1134 | |
} |
| 1135 | 1784 | return lineBreak; |
| 1136 | |
} |
| 1137 | |
|
| 1138 | |
|
| 1139 | |
|
| 1140 | |
|
| 1141 | |
|
| 1142 | |
|
| 1143 | |
|
| 1144 | |
|
| 1145 | |
|
| 1146 | |
|
| 1147 | |
|
| 1148 | |
|
| 1149 | |
|
| 1150 | |
private Token scanAnchor(boolean isAnchor) { |
| 1151 | 2797 | Mark startMark = reader.getMark(); |
| 1152 | 2797 | char indicator = reader.peek(); |
| 1153 | 2797 | String name = indicator == '*' ? "alias" : "anchor"; |
| 1154 | 2797 | reader.forward(); |
| 1155 | 2797 | int length = 0; |
| 1156 | 2797 | char ch = reader.peek(length); |
| 1157 | 16116 | while (Constant.ALPHA.has(ch)) { |
| 1158 | 13319 | length++; |
| 1159 | 13319 | ch = reader.peek(length); |
| 1160 | |
} |
| 1161 | 2797 | if (length == 0) { |
| 1162 | 2 | throw new ScannerException("while scanning an " + name, startMark, |
| 1163 | |
"expected alphabetic or numeric character, but found but found " + ch, |
| 1164 | |
reader.getMark()); |
| 1165 | |
} |
| 1166 | 2795 | String value = reader.prefixForward(length); |
| 1167 | 2795 | ch = reader.peek(); |
| 1168 | 2795 | if (Constant.NULL_BL_T_LINEBR.hasNo(ch, "?:,]}%@`")) { |
| 1169 | 2 | throw new ScannerException("while scanning an " + name, startMark, |
| 1170 | |
"expected alphabetic or numeric character, but found " + ch + "(" |
| 1171 | |
+ ((int) reader.peek()) + ")", reader.getMark()); |
| 1172 | |
} |
| 1173 | 2793 | Mark endMark = reader.getMark(); |
| 1174 | |
Token tok; |
| 1175 | 2793 | if (isAnchor) { |
| 1176 | 1368 | tok = new AnchorToken(value, startMark, endMark); |
| 1177 | |
} else { |
| 1178 | 1425 | tok = new AliasToken(value, startMark, endMark); |
| 1179 | |
} |
| 1180 | 2793 | return tok; |
| 1181 | |
} |
| 1182 | |
|
| 1183 | |
private Token scanTag() { |
| 1184 | |
|
| 1185 | 12164 | Mark startMark = reader.getMark(); |
| 1186 | 12164 | char ch = reader.peek(1); |
| 1187 | 12164 | String handle = null; |
| 1188 | 12164 | String suffix = null; |
| 1189 | 12164 | if (ch == '<') { |
| 1190 | 137 | reader.forward(2); |
| 1191 | 137 | suffix = scanTagUri("tag", startMark); |
| 1192 | 131 | if (reader.peek() != '>') { |
| 1193 | 2 | throw new ScannerException("while scanning a tag", startMark, |
| 1194 | |
"expected '>', but found '" + reader.peek() + "' (" + ((int) reader.peek()) |
| 1195 | |
+ ")", reader.getMark()); |
| 1196 | |
} |
| 1197 | 129 | reader.forward(); |
| 1198 | 12027 | } else if (Constant.NULL_BL_T_LINEBR.has(ch)) { |
| 1199 | 2053 | suffix = "!"; |
| 1200 | 2053 | reader.forward(); |
| 1201 | |
} else { |
| 1202 | 9974 | int length = 1; |
| 1203 | 9974 | boolean useHandle = false; |
| 1204 | 37167 | while (Constant.NULL_BL_LINEBR.hasNo(ch)) { |
| 1205 | 36035 | if (ch == '!') { |
| 1206 | 8842 | useHandle = true; |
| 1207 | 8842 | break; |
| 1208 | |
} |
| 1209 | 27193 | length++; |
| 1210 | 27193 | ch = reader.peek(length); |
| 1211 | |
} |
| 1212 | 9974 | handle = "!"; |
| 1213 | 9974 | if (useHandle) { |
| 1214 | 8842 | handle = scanTagHandle("tag", startMark); |
| 1215 | |
} else { |
| 1216 | 1132 | handle = "!"; |
| 1217 | 1132 | reader.forward(); |
| 1218 | |
} |
| 1219 | 9974 | suffix = scanTagUri("tag", startMark); |
| 1220 | |
} |
| 1221 | 12151 | ch = reader.peek(); |
| 1222 | 12151 | if (Constant.NULL_BL_LINEBR.hasNo(ch)) { |
| 1223 | 2 | throw new ScannerException("while scanning a tag", startMark, |
| 1224 | |
"expected ' ', but found '" + ch + "' (" + ((int) ch) + ")", reader.getMark()); |
| 1225 | |
} |
| 1226 | 12149 | TagTuple value = new TagTuple(handle, suffix); |
| 1227 | 12149 | Mark endMark = reader.getMark(); |
| 1228 | 12149 | return new TagToken(value, startMark, endMark); |
| 1229 | |
} |
| 1230 | |
|
| 1231 | |
private Token scanBlockScalar(char style) { |
| 1232 | |
|
| 1233 | |
boolean folded; |
| 1234 | 1188 | if (style == '>') { |
| 1235 | 554 | folded = true; |
| 1236 | |
} else { |
| 1237 | 634 | folded = false; |
| 1238 | |
} |
| 1239 | 1188 | StringBuilder chunks = new StringBuilder(); |
| 1240 | 1188 | Mark startMark = reader.getMark(); |
| 1241 | |
|
| 1242 | 1188 | reader.forward(); |
| 1243 | 1188 | Chomping chompi = scanBlockScalarIndicators(startMark); |
| 1244 | 1184 | int increment = chompi.getIncrement(); |
| 1245 | 1184 | scanBlockScalarIgnoredLine(startMark); |
| 1246 | |
|
| 1247 | |
|
| 1248 | 1182 | int minIndent = this.indent + 1; |
| 1249 | 1182 | if (minIndent < 1) { |
| 1250 | 375 | minIndent = 1; |
| 1251 | |
} |
| 1252 | 1182 | String breaks = null; |
| 1253 | 1182 | int maxIndent = 0; |
| 1254 | 1182 | int indent = 0; |
| 1255 | |
Mark endMark; |
| 1256 | 1182 | if (increment == -1) { |
| 1257 | 1099 | Object[] brme = scanBlockScalarIndentation(); |
| 1258 | 1099 | breaks = (String) brme[0]; |
| 1259 | 1099 | maxIndent = ((Integer) brme[1]).intValue(); |
| 1260 | 1099 | endMark = (Mark) brme[2]; |
| 1261 | 1099 | indent = Math.max(minIndent, maxIndent); |
| 1262 | 1099 | } else { |
| 1263 | 83 | indent = minIndent + increment - 1; |
| 1264 | 83 | Object[] brme = scanBlockScalarBreaks(indent); |
| 1265 | 83 | breaks = (String) brme[0]; |
| 1266 | 83 | endMark = (Mark) brme[1]; |
| 1267 | |
} |
| 1268 | |
|
| 1269 | 1182 | String lineBreak = ""; |
| 1270 | |
|
| 1271 | |
|
| 1272 | 1966 | while (this.reader.getColumn() == indent && reader.peek() != '\0') { |
| 1273 | 1946 | chunks.append(breaks); |
| 1274 | 1946 | boolean leadingNonSpace = " \t".indexOf(reader.peek()) == -1; |
| 1275 | 1946 | int length = 0; |
| 1276 | 32069 | while (Constant.NULL_OR_LINEBR.hasNo(reader.peek(length))) { |
| 1277 | 30123 | length++; |
| 1278 | |
} |
| 1279 | 1946 | chunks.append(reader.prefixForward(length)); |
| 1280 | 1946 | lineBreak = scanLineBreak(); |
| 1281 | 1946 | Object[] brme = scanBlockScalarBreaks(indent); |
| 1282 | 1946 | breaks = (String) brme[0]; |
| 1283 | 1946 | endMark = (Mark) brme[1]; |
| 1284 | 1946 | if (this.reader.getColumn() == indent && reader.peek() != '\0') { |
| 1285 | |
|
| 1286 | |
|
| 1287 | |
|
| 1288 | |
|
| 1289 | 784 | if (folded && "\n".equals(lineBreak) && leadingNonSpace |
| 1290 | |
&& " \t".indexOf(reader.peek()) == -1) { |
| 1291 | 184 | if (breaks.length() == 0) { |
| 1292 | 92 | chunks.append(" "); |
| 1293 | |
} |
| 1294 | |
} else { |
| 1295 | 600 | chunks.append(lineBreak); |
| 1296 | |
} |
| 1297 | |
|
| 1298 | |
|
| 1299 | |
} else { |
| 1300 | |
break; |
| 1301 | |
} |
| 1302 | 784 | } |
| 1303 | |
|
| 1304 | 1182 | if (chompi.chompTailIsNotFalse()) { |
| 1305 | 413 | chunks.append(lineBreak); |
| 1306 | |
} |
| 1307 | 1182 | if (chompi.chompTailIsTrue()) { |
| 1308 | 35 | chunks.append(breaks); |
| 1309 | |
} |
| 1310 | |
|
| 1311 | 1182 | return new ScalarToken(chunks.toString(), false, startMark, endMark, style); |
| 1312 | |
} |
| 1313 | |
|
| 1314 | |
private Chomping scanBlockScalarIndicators(Mark startMark) { |
| 1315 | |
|
| 1316 | 1188 | Boolean chomping = null; |
| 1317 | 1188 | int increment = -1; |
| 1318 | 1188 | char ch = reader.peek(); |
| 1319 | 1188 | if (ch == '-' || ch == '+') { |
| 1320 | 780 | if (ch == '+') { |
| 1321 | 30 | chomping = Boolean.TRUE; |
| 1322 | |
} else { |
| 1323 | 750 | chomping = Boolean.FALSE; |
| 1324 | |
} |
| 1325 | 780 | reader.forward(); |
| 1326 | 780 | ch = reader.peek(); |
| 1327 | 780 | if (Character.isDigit(ch)) { |
| 1328 | 7 | increment = Integer.parseInt(String.valueOf(ch)); |
| 1329 | 7 | if (increment == 0) { |
| 1330 | 2 | throw new ScannerException("while scanning a block scalar", startMark, |
| 1331 | |
"expected indentation indicator in the range 1-9, but found 0", |
| 1332 | |
reader.getMark()); |
| 1333 | |
} |
| 1334 | 5 | reader.forward(); |
| 1335 | |
} |
| 1336 | 408 | } else if (Character.isDigit(ch)) { |
| 1337 | 80 | increment = Integer.parseInt(String.valueOf(ch)); |
| 1338 | 80 | if (increment == 0) { |
| 1339 | 2 | throw new ScannerException("while scanning a block scalar", startMark, |
| 1340 | |
"expected indentation indicator in the range 1-9, but found 0", |
| 1341 | |
reader.getMark()); |
| 1342 | |
} |
| 1343 | 78 | reader.forward(); |
| 1344 | 78 | ch = reader.peek(); |
| 1345 | 78 | if (ch == '-' || ch == '+') { |
| 1346 | 26 | if (ch == '+') { |
| 1347 | 5 | chomping = Boolean.TRUE; |
| 1348 | |
} else { |
| 1349 | 21 | chomping = Boolean.FALSE; |
| 1350 | |
} |
| 1351 | 26 | reader.forward(); |
| 1352 | |
} |
| 1353 | |
} |
| 1354 | 1184 | ch = reader.peek(); |
| 1355 | 1184 | if (Constant.NULL_BL_LINEBR.hasNo(ch)) { |
| 1356 | 0 | throw new ScannerException("while scanning a block scalar", startMark, |
| 1357 | |
"expected chomping or indentation indicators, but found " + ch, |
| 1358 | |
reader.getMark()); |
| 1359 | |
} |
| 1360 | 1184 | return new Chomping(chomping, increment); |
| 1361 | |
} |
| 1362 | |
|
| 1363 | |
private String scanBlockScalarIgnoredLine(Mark startMark) { |
| 1364 | |
|
| 1365 | 1184 | int ff = 0; |
| 1366 | 1216 | while (reader.peek(ff) == ' ') { |
| 1367 | 32 | ff++; |
| 1368 | |
} |
| 1369 | 1184 | if (ff > 0) { |
| 1370 | 32 | reader.forward(ff); |
| 1371 | |
} |
| 1372 | |
|
| 1373 | 1184 | if (reader.peek() == '#') { |
| 1374 | 30 | ff = 0; |
| 1375 | 625 | while (Constant.NULL_OR_LINEBR.hasNo(reader.peek(ff))) { |
| 1376 | 595 | ff++; |
| 1377 | |
} |
| 1378 | 30 | if (ff > 0) { |
| 1379 | 30 | reader.forward(ff); |
| 1380 | |
} |
| 1381 | |
} |
| 1382 | 1184 | char ch = reader.peek(); |
| 1383 | 1184 | String lineBreak = scanLineBreak(); |
| 1384 | 1184 | if (lineBreak.length() == 0 && ch != '\0') { |
| 1385 | 2 | throw new ScannerException("while scanning a block scalar", startMark, |
| 1386 | |
"expected a comment or a line break, but found " + ch, reader.getMark()); |
| 1387 | |
} |
| 1388 | 1182 | return lineBreak; |
| 1389 | |
} |
| 1390 | |
|
| 1391 | |
private Object[] scanBlockScalarIndentation() { |
| 1392 | |
|
| 1393 | 1099 | StringBuilder chunks = new StringBuilder(); |
| 1394 | 1099 | int maxIndent = 0; |
| 1395 | 1099 | Mark endMark = reader.getMark(); |
| 1396 | 4045 | while (Constant.LINEBR.has(reader.peek(), " \r")) { |
| 1397 | 2946 | if (reader.peek() != ' ') { |
| 1398 | 55 | chunks.append(scanLineBreak()); |
| 1399 | 55 | endMark = reader.getMark(); |
| 1400 | |
} else { |
| 1401 | 2891 | reader.forward(); |
| 1402 | 2891 | if (this.reader.getColumn() > maxIndent) { |
| 1403 | 2831 | maxIndent = reader.getColumn(); |
| 1404 | |
} |
| 1405 | |
} |
| 1406 | |
} |
| 1407 | 1099 | return new Object[] { chunks.toString(), maxIndent, endMark }; |
| 1408 | |
} |
| 1409 | |
|
| 1410 | |
private Object[] scanBlockScalarBreaks(int indent) { |
| 1411 | |
|
| 1412 | 2029 | StringBuilder chunks = new StringBuilder(); |
| 1413 | 2029 | Mark endMark = reader.getMark(); |
| 1414 | 2029 | int ff = 0; |
| 1415 | 2029 | int col = this.reader.getColumn(); |
| 1416 | 4038 | while (col < indent && reader.peek(ff) == ' ') { |
| 1417 | 2009 | ff++; |
| 1418 | 2009 | col++; |
| 1419 | |
} |
| 1420 | 2029 | if (ff > 0) { |
| 1421 | 742 | reader.forward(ff); |
| 1422 | |
} |
| 1423 | |
|
| 1424 | 2029 | String lineBreak = null; |
| 1425 | 2487 | while ((lineBreak = scanLineBreak()).length() != 0) { |
| 1426 | 458 | chunks.append(lineBreak); |
| 1427 | 458 | endMark = reader.getMark(); |
| 1428 | 458 | ff = 0; |
| 1429 | 458 | col = this.reader.getColumn(); |
| 1430 | 1046 | while (col < indent && reader.peek(ff) == ' ') { |
| 1431 | 588 | ff++; |
| 1432 | 588 | col++; |
| 1433 | |
} |
| 1434 | 458 | if (ff > 0) { |
| 1435 | 337 | reader.forward(ff); |
| 1436 | |
} |
| 1437 | |
} |
| 1438 | 2029 | return new Object[] { chunks.toString(), endMark }; |
| 1439 | |
} |
| 1440 | |
|
| 1441 | |
|
| 1442 | |
|
| 1443 | |
|
| 1444 | |
|
| 1445 | |
|
| 1446 | |
|
| 1447 | |
|
| 1448 | |
|
| 1449 | |
|
| 1450 | |
|
| 1451 | |
private Token scanFlowScalar(char style) { |
| 1452 | |
boolean _double; |
| 1453 | 209346 | if (style == '"') { |
| 1454 | 6067 | _double = true; |
| 1455 | |
} else { |
| 1456 | 203279 | _double = false; |
| 1457 | |
} |
| 1458 | 209346 | StringBuilder chunks = new StringBuilder(); |
| 1459 | 209346 | Mark startMark = reader.getMark(); |
| 1460 | 209346 | char quote = reader.peek(); |
| 1461 | 209346 | reader.forward(); |
| 1462 | 209346 | chunks.append(scanFlowScalarNonSpaces(_double, startMark)); |
| 1463 | 215684 | while (reader.peek() != quote) { |
| 1464 | 6346 | chunks.append(scanFlowScalarSpaces(startMark)); |
| 1465 | 6342 | chunks.append(scanFlowScalarNonSpaces(_double, startMark)); |
| 1466 | |
} |
| 1467 | 209338 | reader.forward(); |
| 1468 | 209338 | Mark endMark = reader.getMark(); |
| 1469 | 209338 | return new ScalarToken(chunks.toString(), false, startMark, endMark, style); |
| 1470 | |
} |
| 1471 | |
|
| 1472 | |
private String scanFlowScalarNonSpaces(boolean _double, Mark startMark) { |
| 1473 | |
|
| 1474 | 215688 | StringBuilder chunks = new StringBuilder(); |
| 1475 | |
while (true) { |
| 1476 | 218753 | int length = 0; |
| 1477 | 768436 | while (Constant.NULL_BL_T_LINEBR.hasNo(reader.peek(length), "\'\"\\")) { |
| 1478 | 549683 | length++; |
| 1479 | |
} |
| 1480 | 218753 | if (length != 0) { |
| 1481 | 214660 | chunks.append(reader.prefixForward(length)); |
| 1482 | |
} |
| 1483 | 218753 | char ch = reader.peek(); |
| 1484 | 218753 | if (!_double && ch == '\'' && reader.peek(1) == '\'') { |
| 1485 | 24 | chunks.append("'"); |
| 1486 | 24 | reader.forward(2); |
| 1487 | 218729 | } else if ((_double && ch == '\'') || (!_double && "\"\\".indexOf(ch) != -1)) { |
| 1488 | 85 | chunks.append(ch); |
| 1489 | 85 | reader.forward(); |
| 1490 | 218644 | } else if (_double && ch == '\\') { |
| 1491 | 2960 | reader.forward(); |
| 1492 | 2960 | ch = reader.peek(); |
| 1493 | 2960 | if (ESCAPE_REPLACEMENTS.containsKey(new Character(ch))) { |
| 1494 | 2487 | chunks.append(ESCAPE_REPLACEMENTS.get(new Character(ch))); |
| 1495 | 2487 | reader.forward(); |
| 1496 | 473 | } else if (ESCAPE_CODES.containsKey(new Character(ch))) { |
| 1497 | 132 | length = (ESCAPE_CODES.get(new Character(ch))).intValue(); |
| 1498 | 132 | reader.forward(); |
| 1499 | 132 | String hex = reader.prefix(length); |
| 1500 | 132 | if (NOT_HEXA.matcher(hex).find()) { |
| 1501 | 2 | throw new ScannerException("while scanning a double-quoted scalar", |
| 1502 | |
startMark, "expected escape sequence of " + length |
| 1503 | |
+ " hexadecimal numbers, but found: " + hex, |
| 1504 | |
reader.getMark()); |
| 1505 | |
} |
| 1506 | 130 | char unicode = (char) Integer.parseInt(hex, 16); |
| 1507 | 130 | chunks.append(unicode); |
| 1508 | 130 | reader.forward(length); |
| 1509 | 130 | } else if (scanLineBreak().length() != 0) { |
| 1510 | 339 | chunks.append(scanFlowScalarBreaks(startMark)); |
| 1511 | |
} else { |
| 1512 | 2 | throw new ScannerException("while scanning a double-quoted scalar", startMark, |
| 1513 | |
"found unknown escape character " + ch + "(" + ((int) ch) + ")", |
| 1514 | |
reader.getMark()); |
| 1515 | |
} |
| 1516 | |
} else { |
| 1517 | 215684 | return chunks.toString(); |
| 1518 | |
} |
| 1519 | 3065 | } |
| 1520 | |
} |
| 1521 | |
|
| 1522 | |
private String scanFlowScalarSpaces(Mark startMark) { |
| 1523 | |
|
| 1524 | 6346 | StringBuilder chunks = new StringBuilder(); |
| 1525 | 6346 | int length = 0; |
| 1526 | 12383 | while (" \t".indexOf(reader.peek(length)) != -1) { |
| 1527 | 6037 | length++; |
| 1528 | |
} |
| 1529 | 6346 | String whitespaces = reader.prefixForward(length); |
| 1530 | 6346 | char ch = reader.peek(); |
| 1531 | 6346 | if (ch == '\0') { |
| 1532 | 2 | throw new ScannerException("while scanning a quoted scalar", startMark, |
| 1533 | |
"found unexpected end of stream", reader.getMark()); |
| 1534 | |
} |
| 1535 | 6344 | String lineBreak = scanLineBreak(); |
| 1536 | 6344 | if (lineBreak.length() != 0) { |
| 1537 | 637 | String breaks = scanFlowScalarBreaks(startMark); |
| 1538 | 635 | if (!"\n".equals(lineBreak)) { |
| 1539 | 67 | chunks.append(lineBreak); |
| 1540 | 568 | } else if (breaks.length() == 0) { |
| 1541 | 203 | chunks.append(" "); |
| 1542 | |
} |
| 1543 | 635 | chunks.append(breaks); |
| 1544 | 635 | } else { |
| 1545 | 5707 | chunks.append(whitespaces); |
| 1546 | |
} |
| 1547 | 6342 | return chunks.toString(); |
| 1548 | |
} |
| 1549 | |
|
| 1550 | |
private String scanFlowScalarBreaks(Mark startMark) { |
| 1551 | |
|
| 1552 | 976 | StringBuilder chunks = new StringBuilder(); |
| 1553 | |
while (true) { |
| 1554 | |
|
| 1555 | |
|
| 1556 | 1469 | String prefix = reader.prefix(3); |
| 1557 | 1469 | if (("---".equals(prefix) || "...".equals(prefix)) |
| 1558 | |
&& Constant.NULL_BL_T_LINEBR.has(reader.peek(3))) { |
| 1559 | 2 | throw new ScannerException("while scanning a quoted scalar", startMark, |
| 1560 | |
"found unexpected document separator", reader.getMark()); |
| 1561 | |
} |
| 1562 | 4413 | while (" \t".indexOf(reader.peek()) != -1) { |
| 1563 | 2946 | reader.forward(); |
| 1564 | |
} |
| 1565 | 1467 | String lineBreak = scanLineBreak(); |
| 1566 | 1467 | if (lineBreak.length() != 0) { |
| 1567 | 493 | chunks.append(lineBreak); |
| 1568 | |
} else { |
| 1569 | 974 | return chunks.toString(); |
| 1570 | |
} |
| 1571 | 493 | } |
| 1572 | |
} |
| 1573 | |
|
| 1574 | |
|
| 1575 | |
|
| 1576 | |
|
| 1577 | |
|
| 1578 | |
|
| 1579 | |
|
| 1580 | |
|
| 1581 | |
|
| 1582 | |
|
| 1583 | |
private Token scanPlain() { |
| 1584 | 151513 | StringBuilder chunks = new StringBuilder(); |
| 1585 | 151513 | Mark startMark = reader.getMark(); |
| 1586 | 151513 | Mark endMark = startMark; |
| 1587 | 151513 | int indent = this.indent + 1; |
| 1588 | 151513 | String spaces = ""; |
| 1589 | |
while (true) { |
| 1590 | |
char ch; |
| 1591 | 153245 | int length = 0; |
| 1592 | 153245 | if (reader.peek() == '#') { |
| 1593 | 0 | break; |
| 1594 | |
} |
| 1595 | |
while (true) { |
| 1596 | 749004 | ch = reader.peek(length); |
| 1597 | 749004 | if (Constant.NULL_BL_T_LINEBR.has(ch) |
| 1598 | |
|| (this.flowLevel == 0 && ch == ':' && Constant.NULL_BL_T_LINEBR |
| 1599 | |
.has(reader.peek(length + 1))) |
| 1600 | |
|| (this.flowLevel != 0 && ",:?[]{}".indexOf(ch) != -1)) { |
| 1601 | 4347 | break; |
| 1602 | |
} |
| 1603 | 595759 | length++; |
| 1604 | |
} |
| 1605 | |
|
| 1606 | 153245 | if (this.flowLevel != 0 && ch == ':' |
| 1607 | |
&& Constant.NULL_BL_T_LINEBR.hasNo(reader.peek(length + 1), ",[]{}")) { |
| 1608 | 2 | reader.forward(length); |
| 1609 | 2 | throw new ScannerException("while scanning a plain scalar", startMark, |
| 1610 | |
"found unexpected ':'", reader.getMark(), |
| 1611 | |
"Please check http://pyyaml.org/wiki/YAMLColonInFlowContext for details."); |
| 1612 | |
} |
| 1613 | 153243 | if (length == 0) { |
| 1614 | 380 | break; |
| 1615 | |
} |
| 1616 | 152863 | this.allowSimpleKey = false; |
| 1617 | 152863 | chunks.append(spaces); |
| 1618 | 152863 | chunks.append(reader.prefixForward(length)); |
| 1619 | 152863 | endMark = reader.getMark(); |
| 1620 | 152863 | spaces = scanPlainSpaces(); |
| 1621 | |
|
| 1622 | 152863 | if (spaces.length() == 0 || reader.peek() == '#' |
| 1623 | |
|| (this.flowLevel == 0 && this.reader.getColumn() < indent)) { |
| 1624 | 112545 | break; |
| 1625 | |
} |
| 1626 | 1732 | } |
| 1627 | 151511 | return new ScalarToken(chunks.toString(), startMark, endMark, true); |
| 1628 | |
} |
| 1629 | |
|
| 1630 | |
|
| 1631 | |
|
| 1632 | |
|
| 1633 | |
|
| 1634 | |
|
| 1635 | |
|
| 1636 | |
|
| 1637 | |
private String scanPlainSpaces() { |
| 1638 | 152863 | int length = 0; |
| 1639 | 154758 | while (reader.peek(length) == ' ') { |
| 1640 | 1895 | length++; |
| 1641 | |
} |
| 1642 | 152863 | String whitespaces = reader.prefixForward(length); |
| 1643 | 152863 | String lineBreak = scanLineBreak(); |
| 1644 | 152863 | if (lineBreak.length() != 0) { |
| 1645 | 112794 | this.allowSimpleKey = true; |
| 1646 | 112794 | String prefix = reader.prefix(3); |
| 1647 | 112794 | if ("---".equals(prefix) || "...".equals(prefix) |
| 1648 | |
&& Constant.NULL_BL_T_LINEBR.has(reader.peek(3))) { |
| 1649 | 85 | return ""; |
| 1650 | |
} |
| 1651 | 112709 | StringBuilder breaks = new StringBuilder(); |
| 1652 | |
while (true) { |
| 1653 | 500951 | if (reader.peek() == ' ') { |
| 1654 | 388190 | reader.forward(); |
| 1655 | |
} else { |
| 1656 | 112761 | String lb = scanLineBreak(); |
| 1657 | 112761 | if (lb.length() != 0) { |
| 1658 | 52 | breaks.append(lb); |
| 1659 | 52 | prefix = reader.prefix(3); |
| 1660 | 52 | if ("---".equals(prefix) || "...".equals(prefix) |
| 1661 | |
&& Constant.NULL_BL_T_LINEBR.has(reader.peek(3))) { |
| 1662 | 0 | return ""; |
| 1663 | |
} |
| 1664 | |
} else { |
| 1665 | |
break; |
| 1666 | |
} |
| 1667 | 52 | } |
| 1668 | |
} |
| 1669 | 112709 | if (!"\n".equals(lineBreak)) { |
| 1670 | 5 | return lineBreak + breaks; |
| 1671 | 112704 | } else if (breaks.length() == 0) { |
| 1672 | 112657 | return " "; |
| 1673 | |
} |
| 1674 | 47 | return breaks.toString(); |
| 1675 | |
} |
| 1676 | 40069 | return whitespaces; |
| 1677 | |
} |
| 1678 | |
|
| 1679 | |
|
| 1680 | |
|
| 1681 | |
|
| 1682 | |
|
| 1683 | |
|
| 1684 | |
|
| 1685 | |
|
| 1686 | |
private String scanTagHandle(String name, Mark startMark) { |
| 1687 | 9003 | char ch = reader.peek(); |
| 1688 | 9003 | if (ch != '!') { |
| 1689 | 2 | throw new ScannerException("while scanning a " + name, startMark, |
| 1690 | |
"expected '!', but found " + ch + "(" + ((int) ch) + ")", reader.getMark()); |
| 1691 | |
} |
| 1692 | 9001 | int length = 1; |
| 1693 | 9001 | ch = reader.peek(length); |
| 1694 | 9001 | if (ch != ' ') { |
| 1695 | 9302 | while (Constant.ALPHA.has(ch)) { |
| 1696 | 370 | length++; |
| 1697 | 370 | ch = reader.peek(length); |
| 1698 | |
} |
| 1699 | 8932 | if (ch != '!') { |
| 1700 | 2 | reader.forward(length); |
| 1701 | 2 | throw new ScannerException("while scanning a " + name, startMark, |
| 1702 | |
"expected '!', but found " + ch + "(" + ((int) ch) + ")", reader.getMark()); |
| 1703 | |
} |
| 1704 | 8930 | length++; |
| 1705 | |
} |
| 1706 | 8999 | String value = reader.prefixForward(length); |
| 1707 | 8999 | return value; |
| 1708 | |
} |
| 1709 | |
|
| 1710 | |
private String scanTagUri(String name, Mark startMark) { |
| 1711 | |
|
| 1712 | |
|
| 1713 | 10266 | StringBuilder chunks = new StringBuilder(); |
| 1714 | 10266 | int length = 0; |
| 1715 | 10266 | char ch = reader.peek(length); |
| 1716 | 111701 | while (Constant.URI_CHARS.has(ch)) { |
| 1717 | 101444 | if (ch == '%') { |
| 1718 | 11 | chunks.append(reader.prefixForward(length)); |
| 1719 | 11 | length = 0; |
| 1720 | 11 | chunks.append(scanUriEscapes(name, startMark)); |
| 1721 | |
} else { |
| 1722 | 101433 | length++; |
| 1723 | |
} |
| 1724 | 101435 | ch = reader.peek(length); |
| 1725 | |
} |
| 1726 | 10257 | if (length != 0) { |
| 1727 | 10254 | chunks.append(reader.prefixForward(length)); |
| 1728 | 10254 | length = 0; |
| 1729 | |
} |
| 1730 | 10257 | if (chunks.length() == 0) { |
| 1731 | 2 | throw new ScannerException("while scanning a " + name, startMark, |
| 1732 | |
"expected URI, but found " + ch + "(" + ((int) ch) + ")", reader.getMark()); |
| 1733 | |
} |
| 1734 | 10255 | return chunks.toString(); |
| 1735 | |
} |
| 1736 | |
|
| 1737 | |
private String scanUriEscapes(String name, Mark startMark) { |
| 1738 | |
|
| 1739 | |
|
| 1740 | 11 | int length = 1; |
| 1741 | 1044 | while (reader.peek(length * 3) == '%') { |
| 1742 | 1033 | length++; |
| 1743 | |
} |
| 1744 | |
|
| 1745 | |
|
| 1746 | |
|
| 1747 | |
|
| 1748 | 11 | Mark beginningMark = reader.getMark(); |
| 1749 | 11 | ByteBuffer buff = ByteBuffer.allocate(length); |
| 1750 | 1050 | while (reader.peek() == '%') { |
| 1751 | 1043 | reader.forward(); |
| 1752 | |
try { |
| 1753 | 1043 | byte code = (byte) Integer.parseInt(reader.prefix(2), 16); |
| 1754 | 1039 | buff.put(code); |
| 1755 | 4 | } catch (NumberFormatException nfe) { |
| 1756 | 4 | throw new ScannerException("while scanning a " + name, startMark, |
| 1757 | |
"expected URI escape sequence of 2 hexadecimal numbers, but found " |
| 1758 | |
+ reader.peek() + "(" + ((int) reader.peek()) + ") and " |
| 1759 | |
+ reader.peek(1) + "(" + ((int) reader.peek(1)) + ")", |
| 1760 | |
reader.getMark()); |
| 1761 | 1039 | } |
| 1762 | 1039 | reader.forward(2); |
| 1763 | |
} |
| 1764 | 7 | buff.flip(); |
| 1765 | |
try { |
| 1766 | 7 | return UriEncoder.decode(buff); |
| 1767 | 5 | } catch (CharacterCodingException e) { |
| 1768 | 5 | throw new ScannerException("while scanning a " + name, startMark, |
| 1769 | |
"expected URI in UTF-8: " + e.getMessage(), beginningMark); |
| 1770 | |
} |
| 1771 | |
} |
| 1772 | |
|
| 1773 | |
private String scanLineBreak() { |
| 1774 | |
|
| 1775 | |
|
| 1776 | |
|
| 1777 | |
|
| 1778 | |
|
| 1779 | |
|
| 1780 | 1076321 | char ch = reader.peek(); |
| 1781 | 1076321 | if (ch == '\r' || ch == '\n' || ch == '\u0085') { |
| 1782 | 262910 | if (ch == '\r' && '\n' == reader.peek(1)) { |
| 1783 | 540 | reader.forward(2); |
| 1784 | |
} else { |
| 1785 | 262370 | reader.forward(); |
| 1786 | |
} |
| 1787 | 262910 | return "\n"; |
| 1788 | 813411 | } else if (ch == '\u2028' || ch == '\u2029') { |
| 1789 | 185 | reader.forward(); |
| 1790 | 185 | return String.valueOf(ch); |
| 1791 | |
} |
| 1792 | 813226 | return ""; |
| 1793 | |
} |
| 1794 | |
|
| 1795 | |
|
| 1796 | |
|
| 1797 | |
|
| 1798 | |
private class Chomping { |
| 1799 | |
private final Boolean value; |
| 1800 | |
private final int increment; |
| 1801 | |
|
| 1802 | 1184 | public Chomping(Boolean value, int increment) { |
| 1803 | 1184 | this.value = value; |
| 1804 | 1184 | this.increment = increment; |
| 1805 | 1184 | } |
| 1806 | |
|
| 1807 | |
public boolean chompTailIsNotFalse() { |
| 1808 | 1182 | return value == null || value; |
| 1809 | |
} |
| 1810 | |
|
| 1811 | |
public boolean chompTailIsTrue() { |
| 1812 | 1182 | return value != null && value; |
| 1813 | |
} |
| 1814 | |
|
| 1815 | |
public int getIncrement() { |
| 1816 | 1184 | return increment; |
| 1817 | |
} |
| 1818 | |
} |
| 1819 | |
} |