| 1 | package scanner |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | |
| 10 | "github.com/goccy/go-yaml/token" |
| 11 | ) |
| 12 | |
| 13 | // IndentState state for indent |
| 14 | type IndentState int |
| 15 | |
| 16 | const ( |
| 17 | // IndentStateEqual equals previous indent |
| 18 | IndentStateEqual IndentState = iota |
| 19 | // IndentStateUp more indent than previous |
| 20 | IndentStateUp |
| 21 | // IndentStateDown less indent than previous |
| 22 | IndentStateDown |
| 23 | // IndentStateKeep uses not indent token |
| 24 | IndentStateKeep |
| 25 | ) |
| 26 | |
| 27 | // Scanner holds the scanner's internal state while processing a given text. |
| 28 | // It can be allocated as part of another data structure but must be initialized via Init before use. |
| 29 | type Scanner struct { |
| 30 | source []rune |
| 31 | sourcePos int |
| 32 | sourceSize int |
| 33 | // line number. This number starts from 1. |
| 34 | line int |
| 35 | // column number. This number starts from 1. |
| 36 | column int |
| 37 | // offset represents the offset from the beginning of the source. |
| 38 | offset int |
| 39 | // lastDelimColumn is the last column needed to compare indent is retained. |
| 40 | lastDelimColumn int |
| 41 | // indentNum indicates the number of spaces used for indentation. |
| 42 | indentNum int |
| 43 | // prevLineIndentNum indicates the number of spaces used for indentation at previous line. |
| 44 | prevLineIndentNum int |
| 45 | // indentLevel indicates the level of indent depth. This value does not match the column value. |
| 46 | indentLevel int |
| 47 | isFirstCharAtLine bool |
| 48 | isAnchor bool |
| 49 | isAlias bool |
| 50 | isDirective bool |
| 51 | startedFlowSequenceNum int |
| 52 | startedFlowMapNum int |
| 53 | indentState IndentState |
| 54 | savedPos *token.Position |
| 55 | } |
| 56 | |
| 57 | func (s *Scanner) pos() *token.Position { |
| 58 | return &token.Position{ |
| 59 | Line: s.line, |
| 60 | Column: s.column, |
| 61 | Offset: s.offset, |
| 62 | IndentNum: s.indentNum, |
| 63 | IndentLevel: s.indentLevel, |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func (s *Scanner) bufferedToken(ctx *Context) *token.Token { |
| 68 | if s.savedPos != nil { |
| 69 | tk := ctx.bufferedToken(s.savedPos) |
| 70 | s.savedPos = nil |
| 71 | return tk |
| 72 | } |
| 73 | line := s.line |
| 74 | column := s.column - len(ctx.buf) |
| 75 | level := s.indentLevel |
| 76 | if ctx.isMultiLine() { |
| 77 | line -= s.newLineCount(ctx.buf) |
| 78 | column = strings.Index(string(ctx.obuf), string(ctx.buf)) + 1 |
| 79 | // Since we are in a literal, folded or raw folded |
| 80 | // we can use the indent level from the last token. |
| 81 | last := ctx.lastToken() |
| 82 | if last != nil { // The last token should never be nil here. |
| 83 | level = last.Position.IndentLevel + 1 |
| 84 | } |
| 85 | } |
| 86 | return ctx.bufferedToken(&token.Position{ |
| 87 | Line: line, |
| 88 | Column: column, |
| 89 | Offset: s.offset - len(ctx.buf), |
| 90 | IndentNum: s.indentNum, |
| 91 | IndentLevel: level, |
| 92 | }) |
| 93 | } |
| 94 | |
| 95 | func (s *Scanner) progressColumn(ctx *Context, num int) { |
| 96 | s.column += num |
| 97 | s.offset += num |
| 98 | s.progress(ctx, num) |
| 99 | } |
| 100 | |
| 101 | func (s *Scanner) progressOnly(ctx *Context, num int) { |
| 102 | s.offset += num |
| 103 | s.progress(ctx, num) |
| 104 | } |
| 105 | |
| 106 | func (s *Scanner) progressLine(ctx *Context) { |
| 107 | s.prevLineIndentNum = s.indentNum |
| 108 | s.column = 1 |
| 109 | s.line++ |
| 110 | s.offset++ |
| 111 | s.indentNum = 0 |
| 112 | s.isFirstCharAtLine = true |
| 113 | s.isAnchor = false |
| 114 | s.isAlias = false |
| 115 | s.isDirective = false |
| 116 | s.progress(ctx, 1) |
| 117 | } |
| 118 | |
| 119 | func (s *Scanner) progress(ctx *Context, num int) { |
| 120 | ctx.progress(num) |
| 121 | s.sourcePos += num |
| 122 | } |
| 123 | |
| 124 | func (s *Scanner) isNewLineChar(c rune) bool { |
| 125 | if c == '\n' { |
| 126 | return true |
| 127 | } |
| 128 | if c == '\r' { |
| 129 | return true |
| 130 | } |
| 131 | return false |
| 132 | } |
| 133 | |
| 134 | func (s *Scanner) newLineCount(src []rune) int { |
| 135 | size := len(src) |
| 136 | cnt := 0 |
| 137 | for i := 0; i < size; i++ { |
| 138 | c := src[i] |
| 139 | switch c { |
| 140 | case '\r': |
| 141 | if i+1 < size && src[i+1] == '\n' { |
| 142 | i++ |
| 143 | } |
| 144 | cnt++ |
| 145 | case '\n': |
| 146 | cnt++ |
| 147 | } |
| 148 | } |
| 149 | return cnt |
| 150 | } |
| 151 | |
| 152 | func (s *Scanner) updateIndentLevel() { |
| 153 | if s.prevLineIndentNum < s.indentNum { |
| 154 | s.indentLevel++ |
| 155 | } else if s.prevLineIndentNum > s.indentNum { |
| 156 | if s.indentLevel > 0 { |
| 157 | s.indentLevel-- |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | func (s *Scanner) updateIndentState(ctx *Context) { |
| 163 | if s.lastDelimColumn == 0 { |
| 164 | return |
| 165 | } |
| 166 | |
| 167 | if s.lastDelimColumn < s.column { |
| 168 | s.indentState = IndentStateUp |
| 169 | } else { |
| 170 | // If lastDelimColumn and s.column are the same, |
| 171 | // treat as Down state since it is the same column as delimiter. |
| 172 | s.indentState = IndentStateDown |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | func (s *Scanner) updateIndent(ctx *Context, c rune) { |
| 177 | if s.isFirstCharAtLine && s.isNewLineChar(c) { |
| 178 | return |
| 179 | } |
| 180 | if s.isFirstCharAtLine && c == ' ' { |
| 181 | s.indentNum++ |
| 182 | return |
| 183 | } |
| 184 | if s.isFirstCharAtLine && c == '\t' { |
| 185 | // found tab indent. |
| 186 | // In this case, scanTab returns error. |
| 187 | return |
| 188 | } |
| 189 | if !s.isFirstCharAtLine { |
| 190 | s.indentState = IndentStateKeep |
| 191 | return |
| 192 | } |
| 193 | s.updateIndentLevel() |
| 194 | s.updateIndentState(ctx) |
| 195 | s.isFirstCharAtLine = false |
| 196 | } |
| 197 | |
| 198 | func (s *Scanner) isChangedToIndentStateDown() bool { |
| 199 | return s.indentState == IndentStateDown |
| 200 | } |
| 201 | |
| 202 | func (s *Scanner) isChangedToIndentStateUp() bool { |
| 203 | return s.indentState == IndentStateUp |
| 204 | } |
| 205 | |
| 206 | func (s *Scanner) addBufferedTokenIfExists(ctx *Context) { |
| 207 | ctx.addToken(s.bufferedToken(ctx)) |
| 208 | } |
| 209 | |
| 210 | func (s *Scanner) breakMultiLine(ctx *Context) { |
| 211 | ctx.breakMultiLine() |
| 212 | } |
| 213 | |
| 214 | func (s *Scanner) scanSingleQuote(ctx *Context) (*token.Token, error) { |
| 215 | ctx.addOriginBuf('\'') |
| 216 | srcpos := s.pos() |
| 217 | startIndex := ctx.idx + 1 |
| 218 | src := ctx.src |
| 219 | size := len(src) |
| 220 | value := []rune{} |
| 221 | isFirstLineChar := false |
| 222 | isNewLine := false |
| 223 | |
| 224 | for idx := startIndex; idx < size; idx++ { |
| 225 | if !isNewLine { |
| 226 | s.progressColumn(ctx, 1) |
| 227 | } else { |
| 228 | isNewLine = false |
| 229 | } |
| 230 | c := src[idx] |
| 231 | ctx.addOriginBuf(c) |
| 232 | if s.isNewLineChar(c) { |
| 233 | notSpaceIdx := -1 |
| 234 | for i := len(value) - 1; i >= 0; i-- { |
| 235 | if value[i] == ' ' { |
| 236 | continue |
| 237 | } |
| 238 | notSpaceIdx = i |
| 239 | break |
| 240 | } |
| 241 | if len(value) > notSpaceIdx { |
| 242 | value = value[:notSpaceIdx+1] |
| 243 | } |
| 244 | if isFirstLineChar { |
| 245 | value = append(value, '\n') |
| 246 | } else { |
| 247 | value = append(value, ' ') |
| 248 | } |
| 249 | isFirstLineChar = true |
| 250 | isNewLine = true |
| 251 | s.progressLine(ctx) |
| 252 | if idx+1 < size { |
| 253 | if err := s.validateDocumentSeparatorMarker(ctx, src[idx+1:]); err != nil { |
| 254 | return nil, err |
| 255 | } |
| 256 | } |
| 257 | continue |
| 258 | } else if isFirstLineChar && c == ' ' { |
| 259 | continue |
| 260 | } else if isFirstLineChar && c == '\t' { |
| 261 | if s.lastDelimColumn >= s.column { |
| 262 | return nil, ErrInvalidToken( |
| 263 | token.Invalid( |
| 264 | "tab character cannot be used for indentation in single-quoted text", |
| 265 | string(ctx.obuf), s.pos(), |
| 266 | ), |
| 267 | ) |
| 268 | } |
| 269 | continue |
| 270 | } else if c != '\'' { |
| 271 | value = append(value, c) |
| 272 | isFirstLineChar = false |
| 273 | continue |
| 274 | } else if idx+1 < len(ctx.src) && ctx.src[idx+1] == '\'' { |
| 275 | // '' handle as ' character |
| 276 | value = append(value, c) |
| 277 | ctx.addOriginBuf(c) |
| 278 | idx++ |
| 279 | s.progressColumn(ctx, 1) |
| 280 | continue |
| 281 | } |
| 282 | s.progressColumn(ctx, 1) |
| 283 | return token.SingleQuote(string(value), string(ctx.obuf), srcpos), nil |
| 284 | } |
| 285 | s.progressColumn(ctx, 1) |
| 286 | return nil, ErrInvalidToken( |
| 287 | token.Invalid( |
| 288 | "could not find end character of single-quoted text", |
| 289 | string(ctx.obuf), srcpos, |
| 290 | ), |
| 291 | ) |
| 292 | } |
| 293 | |
| 294 | func hexToInt(b rune) int { |
| 295 | if b >= 'A' && b <= 'F' { |
| 296 | return int(b) - 'A' + 10 |
| 297 | } |
| 298 | if b >= 'a' && b <= 'f' { |
| 299 | return int(b) - 'a' + 10 |
| 300 | } |
| 301 | return int(b) - '0' |
| 302 | } |
| 303 | |
| 304 | func hexRunesToInt(b []rune) int { |
| 305 | sum := 0 |
| 306 | for i := 0; i < len(b); i++ { |
| 307 | sum += hexToInt(b[i]) << (uint(len(b)-i-1) * 4) |
| 308 | } |
| 309 | return sum |
| 310 | } |
| 311 | |
| 312 | func (s *Scanner) scanDoubleQuote(ctx *Context) (*token.Token, error) { |
| 313 | ctx.addOriginBuf('"') |
| 314 | srcpos := s.pos() |
| 315 | startIndex := ctx.idx + 1 |
| 316 | src := ctx.src |
| 317 | size := len(src) |
| 318 | value := []rune{} |
| 319 | isFirstLineChar := false |
| 320 | isNewLine := false |
| 321 | |
| 322 | for idx := startIndex; idx < size; idx++ { |
| 323 | if !isNewLine { |
| 324 | s.progressColumn(ctx, 1) |
| 325 | } else { |
| 326 | isNewLine = false |
| 327 | } |
| 328 | c := src[idx] |
| 329 | ctx.addOriginBuf(c) |
| 330 | if s.isNewLineChar(c) { |
| 331 | notSpaceIdx := -1 |
| 332 | for i := len(value) - 1; i >= 0; i-- { |
| 333 | if value[i] == ' ' { |
| 334 | continue |
| 335 | } |
| 336 | notSpaceIdx = i |
| 337 | break |
| 338 | } |
| 339 | if len(value) > notSpaceIdx { |
| 340 | value = value[:notSpaceIdx+1] |
| 341 | } |
| 342 | if isFirstLineChar { |
| 343 | value = append(value, '\n') |
| 344 | } else { |
| 345 | value = append(value, ' ') |
| 346 | } |
| 347 | isFirstLineChar = true |
| 348 | isNewLine = true |
| 349 | s.progressLine(ctx) |
| 350 | if idx+1 < size { |
| 351 | if err := s.validateDocumentSeparatorMarker(ctx, src[idx+1:]); err != nil { |
| 352 | return nil, err |
| 353 | } |
| 354 | } |
| 355 | continue |
| 356 | } else if isFirstLineChar && c == ' ' { |
| 357 | continue |
| 358 | } else if isFirstLineChar && c == '\t' { |
| 359 | if s.lastDelimColumn >= s.column { |
| 360 | return nil, ErrInvalidToken( |
| 361 | token.Invalid( |
| 362 | "tab character cannot be used for indentation in double-quoted text", |
| 363 | string(ctx.obuf), s.pos(), |
| 364 | ), |
| 365 | ) |
| 366 | } |
| 367 | continue |
| 368 | } else if c == '\\' { |
| 369 | isFirstLineChar = false |
| 370 | if idx+1 >= size { |
| 371 | value = append(value, c) |
| 372 | continue |
| 373 | } |
| 374 | nextChar := src[idx+1] |
| 375 | progress := 0 |
| 376 | switch nextChar { |
| 377 | case '0': |
| 378 | progress = 1 |
| 379 | ctx.addOriginBuf(nextChar) |
| 380 | value = append(value, 0x00) |
| 381 | case 'a': |
| 382 | progress = 1 |
| 383 | ctx.addOriginBuf(nextChar) |
| 384 | value = append(value, 0x07) |
| 385 | case 'b': |
| 386 | progress = 1 |
| 387 | ctx.addOriginBuf(nextChar) |
| 388 | value = append(value, 0x08) |
| 389 | case 't': |
| 390 | progress = 1 |
| 391 | ctx.addOriginBuf(nextChar) |
| 392 | value = append(value, 0x09) |
| 393 | case 'n': |
| 394 | progress = 1 |
| 395 | ctx.addOriginBuf(nextChar) |
| 396 | value = append(value, 0x0A) |
| 397 | case 'v': |
| 398 | progress = 1 |
| 399 | ctx.addOriginBuf(nextChar) |
| 400 | value = append(value, 0x0B) |
| 401 | case 'f': |
| 402 | progress = 1 |
| 403 | ctx.addOriginBuf(nextChar) |
| 404 | value = append(value, 0x0C) |
| 405 | case 'r': |
| 406 | progress = 1 |
| 407 | ctx.addOriginBuf(nextChar) |
| 408 | value = append(value, 0x0D) |
| 409 | case 'e': |
| 410 | progress = 1 |
| 411 | ctx.addOriginBuf(nextChar) |
| 412 | value = append(value, 0x1B) |
| 413 | case ' ': |
| 414 | progress = 1 |
| 415 | ctx.addOriginBuf(nextChar) |
| 416 | value = append(value, 0x20) |
| 417 | case '"': |
| 418 | progress = 1 |
| 419 | ctx.addOriginBuf(nextChar) |
| 420 | value = append(value, 0x22) |
| 421 | case '/': |
| 422 | progress = 1 |
| 423 | ctx.addOriginBuf(nextChar) |
| 424 | value = append(value, 0x2F) |
| 425 | case '\\': |
| 426 | progress = 1 |
| 427 | ctx.addOriginBuf(nextChar) |
| 428 | value = append(value, 0x5C) |
| 429 | case 'N': |
| 430 | progress = 1 |
| 431 | ctx.addOriginBuf(nextChar) |
| 432 | value = append(value, 0x85) |
| 433 | case '_': |
| 434 | progress = 1 |
| 435 | ctx.addOriginBuf(nextChar) |
| 436 | value = append(value, 0xA0) |
| 437 | case 'L': |
| 438 | progress = 1 |
| 439 | ctx.addOriginBuf(nextChar) |
| 440 | value = append(value, 0x2028) |
| 441 | case 'P': |
| 442 | progress = 1 |
| 443 | ctx.addOriginBuf(nextChar) |
| 444 | value = append(value, 0x2029) |
| 445 | case 'x': |
| 446 | if idx+3 >= size { |
| 447 | progress = 1 |
| 448 | ctx.addOriginBuf(nextChar) |
| 449 | value = append(value, nextChar) |
| 450 | } else { |
| 451 | progress = 3 |
| 452 | codeNum := hexRunesToInt(src[idx+2 : idx+progress+1]) |
| 453 | value = append(value, rune(codeNum)) |
| 454 | } |
| 455 | case 'u': |
| 456 | // \u0000 style must have 5 characters at least. |
| 457 | if idx+5 >= size { |
| 458 | return nil, ErrInvalidToken( |
| 459 | token.Invalid( |
| 460 | "not enough length for escaped UTF-16 character", |
| 461 | string(ctx.obuf), s.pos(), |
| 462 | ), |
| 463 | ) |
| 464 | } |
| 465 | progress = 5 |
| 466 | codeNum := hexRunesToInt(src[idx+2 : idx+6]) |
| 467 | |
| 468 | // handle surrogate pairs. |
| 469 | if codeNum >= 0xD800 && codeNum <= 0xDBFF { |
| 470 | high := codeNum |
| 471 | |
| 472 | // \u0000\u0000 style must have 11 characters at least. |
| 473 | if idx+11 >= size { |
| 474 | return nil, ErrInvalidToken( |
| 475 | token.Invalid( |
| 476 | "not enough length for escaped UTF-16 surrogate pair", |
| 477 | string(ctx.obuf), s.pos(), |
| 478 | ), |
| 479 | ) |
| 480 | } |
| 481 | |
| 482 | if src[idx+6] != '\\' || src[idx+7] != 'u' { |
| 483 | return nil, ErrInvalidToken( |
| 484 | token.Invalid( |
| 485 | "found unexpected character after high surrogate for UTF-16 surrogate pair", |
| 486 | string(ctx.obuf), s.pos(), |
| 487 | ), |
| 488 | ) |
| 489 | } |
| 490 | |
| 491 | low := hexRunesToInt(src[idx+8 : idx+12]) |
| 492 | if low < 0xDC00 || low > 0xDFFF { |
| 493 | return nil, ErrInvalidToken( |
| 494 | token.Invalid( |
| 495 | "found unexpected low surrogate after high surrogate", |
| 496 | string(ctx.obuf), s.pos(), |
| 497 | ), |
| 498 | ) |
| 499 | } |
| 500 | codeNum = ((high - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000 |
| 501 | progress += 6 |
| 502 | } |
| 503 | value = append(value, rune(codeNum)) |
| 504 | case 'U': |
| 505 | // \U00000000 style must have 9 characters at least. |
| 506 | if idx+9 >= size { |
| 507 | return nil, ErrInvalidToken( |
| 508 | token.Invalid( |
| 509 | "not enough length for escaped UTF-32 character", |
| 510 | string(ctx.obuf), s.pos(), |
| 511 | ), |
| 512 | ) |
| 513 | } |
| 514 | progress = 9 |
| 515 | codeNum := hexRunesToInt(src[idx+2 : idx+10]) |
| 516 | value = append(value, rune(codeNum)) |
| 517 | case '\n': |
| 518 | isFirstLineChar = true |
| 519 | isNewLine = true |
| 520 | ctx.addOriginBuf(nextChar) |
| 521 | s.progressColumn(ctx, 1) |
| 522 | s.progressLine(ctx) |
| 523 | idx++ |
| 524 | continue |
| 525 | case '\r': |
| 526 | isFirstLineChar = true |
| 527 | isNewLine = true |
| 528 | ctx.addOriginBuf(nextChar) |
| 529 | s.progressLine(ctx) |
| 530 | progress = 1 |
| 531 | // Skip \n after \r in CRLF sequences |
| 532 | if idx+2 < size && src[idx+2] == '\n' { |
| 533 | ctx.addOriginBuf('\n') |
| 534 | progress = 2 |
| 535 | } |
| 536 | case '\t': |
| 537 | progress = 1 |
| 538 | ctx.addOriginBuf(nextChar) |
| 539 | value = append(value, nextChar) |
| 540 | default: |
| 541 | s.progressColumn(ctx, 1) |
| 542 | return nil, ErrInvalidToken( |
| 543 | token.Invalid( |
| 544 | fmt.Sprintf("found unknown escape character %q", nextChar), |
| 545 | string(ctx.obuf), s.pos(), |
| 546 | ), |
| 547 | ) |
| 548 | } |
| 549 | idx += progress |
| 550 | s.progressColumn(ctx, progress) |
| 551 | continue |
| 552 | } else if c == '\t' { |
| 553 | var ( |
| 554 | foundNotSpaceChar bool |
| 555 | progress int |
| 556 | ) |
| 557 | for i := idx + 1; i < size; i++ { |
| 558 | if src[i] == ' ' || src[i] == '\t' { |
| 559 | progress++ |
| 560 | continue |
| 561 | } |
| 562 | if s.isNewLineChar(src[i]) { |
| 563 | break |
| 564 | } |
| 565 | foundNotSpaceChar = true |
| 566 | } |
| 567 | if foundNotSpaceChar { |
| 568 | value = append(value, c) |
| 569 | if src[idx+1] != '"' { |
| 570 | s.progressColumn(ctx, 1) |
| 571 | } |
| 572 | } else { |
| 573 | idx += progress |
| 574 | s.progressColumn(ctx, progress) |
| 575 | } |
| 576 | continue |
| 577 | } else if c != '"' { |
| 578 | value = append(value, c) |
| 579 | isFirstLineChar = false |
| 580 | continue |
| 581 | } |
| 582 | s.progressColumn(ctx, 1) |
| 583 | return token.DoubleQuote(string(value), string(ctx.obuf), srcpos), nil |
| 584 | } |
| 585 | s.progressColumn(ctx, 1) |
| 586 | return nil, ErrInvalidToken( |
| 587 | token.Invalid( |
| 588 | "could not find end character of double-quoted text", |
| 589 | string(ctx.obuf), srcpos, |
| 590 | ), |
| 591 | ) |
| 592 | } |
| 593 | |
| 594 | func (s *Scanner) validateDocumentSeparatorMarker(ctx *Context, src []rune) error { |
| 595 | if s.foundDocumentSeparatorMarker(src) { |
| 596 | return ErrInvalidToken( |
| 597 | token.Invalid("found unexpected document separator", string(ctx.obuf), s.pos()), |
| 598 | ) |
| 599 | } |
| 600 | return nil |
| 601 | } |
| 602 | |
| 603 | func (s *Scanner) foundDocumentSeparatorMarker(src []rune) bool { |
| 604 | if len(src) < 3 { |
| 605 | return false |
| 606 | } |
| 607 | var marker string |
| 608 | if len(src) == 3 { |
| 609 | marker = string(src) |
| 610 | } else { |
| 611 | marker = strings.TrimRightFunc(string(src[:4]), func(r rune) bool { |
| 612 | return r == ' ' || r == '\t' || r == '\n' || r == '\r' |
| 613 | }) |
| 614 | } |
| 615 | return marker == "---" || marker == "..." |
| 616 | } |
| 617 | |
| 618 | func (s *Scanner) scanQuote(ctx *Context, ch rune) (bool, error) { |
| 619 | if ctx.existsBuffer() { |
| 620 | return false, nil |
| 621 | } |
| 622 | if ch == '\'' { |
| 623 | tk, err := s.scanSingleQuote(ctx) |
| 624 | if err != nil { |
| 625 | return false, err |
| 626 | } |
| 627 | ctx.addToken(tk) |
| 628 | } else { |
| 629 | tk, err := s.scanDoubleQuote(ctx) |
| 630 | if err != nil { |
| 631 | return false, err |
| 632 | } |
| 633 | ctx.addToken(tk) |
| 634 | } |
| 635 | ctx.clear() |
| 636 | return true, nil |
| 637 | } |
| 638 | |
| 639 | func (s *Scanner) scanWhiteSpace(ctx *Context) bool { |
| 640 | if ctx.isMultiLine() { |
| 641 | return false |
| 642 | } |
| 643 | if !s.isAnchor && !s.isDirective && !s.isAlias && !s.isFirstCharAtLine { |
| 644 | return false |
| 645 | } |
| 646 | |
| 647 | if s.isFirstCharAtLine { |
| 648 | s.progressColumn(ctx, 1) |
| 649 | ctx.addOriginBuf(' ') |
| 650 | return true |
| 651 | } |
| 652 | if s.isDirective { |
| 653 | s.addBufferedTokenIfExists(ctx) |
| 654 | s.progressColumn(ctx, 1) |
| 655 | ctx.addOriginBuf(' ') |
| 656 | return true |
| 657 | } |
| 658 | |
| 659 | s.addBufferedTokenIfExists(ctx) |
| 660 | s.isAnchor = false |
| 661 | s.isAlias = false |
| 662 | return true |
| 663 | } |
| 664 | |
| 665 | func (s *Scanner) isMergeKey(ctx *Context) bool { |
| 666 | if ctx.repeatNum('<') != 2 { |
| 667 | return false |
| 668 | } |
| 669 | src := ctx.src |
| 670 | size := len(src) |
| 671 | for idx := ctx.idx + 2; idx < size; idx++ { |
| 672 | c := src[idx] |
| 673 | if c == ' ' { |
| 674 | continue |
| 675 | } |
| 676 | if c != ':' { |
| 677 | return false |
| 678 | } |
| 679 | if idx+1 < size { |
| 680 | nc := src[idx+1] |
| 681 | if nc == ' ' || s.isNewLineChar(nc) { |
| 682 | return true |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | return false |
| 687 | } |
| 688 | |
| 689 | func (s *Scanner) scanTag(ctx *Context) (bool, error) { |
| 690 | if ctx.existsBuffer() || s.isDirective { |
| 691 | return false, nil |
| 692 | } |
| 693 | |
| 694 | ctx.addOriginBuf('!') |
| 695 | s.progress(ctx, 1) // skip '!' character |
| 696 | |
| 697 | var progress int |
| 698 | for idx, c := range ctx.src[ctx.idx:] { |
| 699 | progress = idx + 1 |
| 700 | switch c { |
| 701 | case ' ': |
| 702 | ctx.addOriginBuf(c) |
| 703 | value := ctx.source(ctx.idx-1, ctx.idx+idx) |
| 704 | ctx.addToken(token.Tag(value, string(ctx.obuf), s.pos())) |
| 705 | s.progressColumn(ctx, len([]rune(value))) |
| 706 | ctx.clear() |
| 707 | return true, nil |
| 708 | case ',': |
| 709 | if s.startedFlowSequenceNum > 0 || s.startedFlowMapNum > 0 { |
| 710 | value := ctx.source(ctx.idx-1, ctx.idx+idx) |
| 711 | ctx.addToken(token.Tag(value, string(ctx.obuf), s.pos())) |
| 712 | s.progressColumn(ctx, len([]rune(value))-1) // progress column before collect-entry for scanning it at scanFlowEntry function. |
| 713 | ctx.clear() |
| 714 | return true, nil |
| 715 | } else { |
| 716 | ctx.addOriginBuf(c) |
| 717 | } |
| 718 | case '\n', '\r': |
| 719 | ctx.addOriginBuf(c) |
| 720 | value := ctx.source(ctx.idx-1, ctx.idx+idx) |
| 721 | ctx.addToken(token.Tag(value, string(ctx.obuf), s.pos())) |
| 722 | s.progressColumn(ctx, len([]rune(value))-1) // progress column before new-line-char for scanning new-line-char at scanNewLine function. |
| 723 | ctx.clear() |
| 724 | return true, nil |
| 725 | case '{', '}': |
| 726 | ctx.addOriginBuf(c) |
| 727 | s.progressColumn(ctx, progress) |
| 728 | invalidTk := token.Invalid(fmt.Sprintf("found invalid tag character %q", c), string(ctx.obuf), s.pos()) |
| 729 | return false, ErrInvalidToken(invalidTk) |
| 730 | default: |
| 731 | ctx.addOriginBuf(c) |
| 732 | } |
| 733 | } |
| 734 | s.progressColumn(ctx, progress) |
| 735 | ctx.clear() |
| 736 | return true, nil |
| 737 | } |
| 738 | |
| 739 | func (s *Scanner) scanComment(ctx *Context) bool { |
| 740 | if ctx.existsBuffer() { |
| 741 | c := ctx.previousChar() |
| 742 | if c != ' ' && c != '\t' && !s.isNewLineChar(c) { |
| 743 | return false |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | s.addBufferedTokenIfExists(ctx) |
| 748 | ctx.addOriginBuf('#') |
| 749 | s.progress(ctx, 1) // skip '#' character |
| 750 | |
| 751 | for idx, c := range ctx.src[ctx.idx:] { |
| 752 | ctx.addOriginBuf(c) |
| 753 | if !s.isNewLineChar(c) { |
| 754 | continue |
| 755 | } |
| 756 | if ctx.previousChar() == '\\' { |
| 757 | continue |
| 758 | } |
| 759 | value := ctx.source(ctx.idx, ctx.idx+idx) |
| 760 | progress := len([]rune(value)) |
| 761 | ctx.addToken(token.Comment(value, string(ctx.obuf), s.pos())) |
| 762 | s.progressColumn(ctx, progress) |
| 763 | s.progressLine(ctx) |
| 764 | ctx.clear() |
| 765 | return true |
| 766 | } |
| 767 | // document ends with comment. |
| 768 | value := string(ctx.src[ctx.idx:]) |
| 769 | ctx.addToken(token.Comment(value, string(ctx.obuf), s.pos())) |
| 770 | progress := len([]rune(value)) |
| 771 | s.progressColumn(ctx, progress) |
| 772 | s.progressLine(ctx) |
| 773 | ctx.clear() |
| 774 | return true |
| 775 | } |
| 776 | |
| 777 | func (s *Scanner) scanMultiLine(ctx *Context, c rune) error { |
| 778 | state := ctx.getMultiLineState() |
| 779 | ctx.addOriginBuf(c) |
| 780 | if ctx.isEOS() { |
| 781 | if s.isFirstCharAtLine && c == ' ' { |
| 782 | state.addIndent(ctx, s.column) |
| 783 | } else { |
| 784 | ctx.addBuf(c) |
| 785 | } |
| 786 | state.updateIndentColumn(s.column) |
| 787 | if err := state.validateIndentColumn(); err != nil { |
| 788 | invalidTk := token.Invalid(err.Error(), string(ctx.obuf), s.pos()) |
| 789 | s.progressColumn(ctx, 1) |
| 790 | return ErrInvalidToken(invalidTk) |
| 791 | } |
| 792 | value := ctx.bufferedSrc() |
| 793 | ctx.addToken(token.String(string(value), string(ctx.obuf), s.pos())) |
| 794 | ctx.clear() |
| 795 | s.progressColumn(ctx, 1) |
| 796 | } else if s.isNewLineChar(c) { |
| 797 | ctx.addBuf(c) |
| 798 | state.updateSpaceOnlyIndentColumn(s.column - 1) |
| 799 | state.updateNewLineState() |
| 800 | s.progressLine(ctx) |
| 801 | if ctx.next() { |
| 802 | if s.foundDocumentSeparatorMarker(ctx.src[ctx.idx:]) { |
| 803 | value := ctx.bufferedSrc() |
| 804 | ctx.addToken(token.String(string(value), string(ctx.obuf), s.pos())) |
| 805 | ctx.clear() |
| 806 | s.breakMultiLine(ctx) |
| 807 | } |
| 808 | } |
| 809 | } else if s.isFirstCharAtLine && c == ' ' { |
| 810 | state.addIndent(ctx, s.column) |
| 811 | s.progressColumn(ctx, 1) |
| 812 | } else if s.isFirstCharAtLine && c == '\t' && state.isIndentColumn(s.column) { |
| 813 | err := ErrInvalidToken( |
| 814 | token.Invalid( |
| 815 | "found a tab character where an indentation space is expected", |
| 816 | string(ctx.obuf), s.pos(), |
| 817 | ), |
| 818 | ) |
| 819 | s.progressColumn(ctx, 1) |
| 820 | return err |
| 821 | } else if c == '\t' && !state.isIndentColumn(s.column) { |
| 822 | ctx.addBufWithTab(c) |
| 823 | s.progressColumn(ctx, 1) |
| 824 | } else { |
| 825 | if err := state.validateIndentAfterSpaceOnly(s.column); err != nil { |
| 826 | invalidTk := token.Invalid(err.Error(), string(ctx.obuf), s.pos()) |
| 827 | s.progressColumn(ctx, 1) |
| 828 | return ErrInvalidToken(invalidTk) |
| 829 | } |
| 830 | state.updateIndentColumn(s.column) |
| 831 | if err := state.validateIndentColumn(); err != nil { |
| 832 | invalidTk := token.Invalid(err.Error(), string(ctx.obuf), s.pos()) |
| 833 | s.progressColumn(ctx, 1) |
| 834 | return ErrInvalidToken(invalidTk) |
| 835 | } |
| 836 | if col := state.lastDelimColumn(); col > 0 { |
| 837 | s.lastDelimColumn = col |
| 838 | } |
| 839 | state.updateNewLineInFolded(ctx, s.column) |
| 840 | ctx.addBufWithTab(c) |
| 841 | s.progressColumn(ctx, 1) |
| 842 | } |
| 843 | return nil |
| 844 | } |
| 845 | |
| 846 | func (s *Scanner) scanNewLine(ctx *Context, c rune) { |
| 847 | if len(ctx.buf) > 0 && s.savedPos == nil { |
| 848 | bufLen := len(ctx.bufferedSrc()) |
| 849 | s.savedPos = s.pos() |
| 850 | s.savedPos.Column -= bufLen |
| 851 | s.savedPos.Offset -= bufLen |
| 852 | } |
| 853 | |
| 854 | // if the following case, origin buffer has unnecessary two spaces. |
| 855 | // So, `removeRightSpaceFromOriginBuf` remove them, also fix column number too. |
| 856 | // --- |
| 857 | // a:[space][space] |
| 858 | // b: c |
| 859 | ctx.removeRightSpaceFromBuf() |
| 860 | |
| 861 | // There is no problem that we ignore CR which followed by LF and normalize it to LF, because of following YAML1.2 spec. |
| 862 | // > Line breaks inside scalar content must be normalized by the YAML processor. Each such line break must be parsed into a single line feed character. |
| 863 | // > Outside scalar content, YAML allows any line break to be used to terminate lines. |
| 864 | // > -- https://yaml.org/spec/1.2/spec.html |
| 865 | if c == '\r' && ctx.nextChar() == '\n' { |
| 866 | ctx.addOriginBuf('\r') |
| 867 | s.progress(ctx, 1) |
| 868 | s.offset++ |
| 869 | c = '\n' |
| 870 | } |
| 871 | |
| 872 | if ctx.isEOS() { |
| 873 | s.addBufferedTokenIfExists(ctx) |
| 874 | } else if s.isAnchor || s.isAlias || s.isDirective { |
| 875 | s.addBufferedTokenIfExists(ctx) |
| 876 | } |
| 877 | if ctx.existsBuffer() && s.isFirstCharAtLine { |
| 878 | if ctx.buf[len(ctx.buf)-1] == ' ' { |
| 879 | ctx.buf[len(ctx.buf)-1] = '\n' |
| 880 | } else { |
| 881 | ctx.buf = append(ctx.buf, '\n') |
| 882 | } |
| 883 | } else { |
| 884 | ctx.addBuf(' ') |
| 885 | } |
| 886 | ctx.addOriginBuf(c) |
| 887 | s.progressLine(ctx) |
| 888 | } |
| 889 | |
| 890 | func (s *Scanner) isFlowMode() bool { |
| 891 | if s.startedFlowSequenceNum > 0 { |
| 892 | return true |
| 893 | } |
| 894 | if s.startedFlowMapNum > 0 { |
| 895 | return true |
| 896 | } |
| 897 | return false |
| 898 | } |
| 899 | |
| 900 | func (s *Scanner) scanFlowMapStart(ctx *Context) bool { |
| 901 | if ctx.existsBuffer() && !s.isFlowMode() { |
| 902 | return false |
| 903 | } |
| 904 | |
| 905 | s.addBufferedTokenIfExists(ctx) |
| 906 | ctx.addOriginBuf('{') |
| 907 | ctx.addToken(token.MappingStart(string(ctx.obuf), s.pos())) |
| 908 | s.startedFlowMapNum++ |
| 909 | s.progressColumn(ctx, 1) |
| 910 | ctx.clear() |
| 911 | return true |
| 912 | } |
| 913 | |
| 914 | func (s *Scanner) scanFlowMapEnd(ctx *Context) bool { |
| 915 | if s.startedFlowMapNum <= 0 { |
| 916 | return false |
| 917 | } |
| 918 | |
| 919 | s.addBufferedTokenIfExists(ctx) |
| 920 | ctx.addOriginBuf('}') |
| 921 | ctx.addToken(token.MappingEnd(string(ctx.obuf), s.pos())) |
| 922 | s.startedFlowMapNum-- |
| 923 | s.progressColumn(ctx, 1) |
| 924 | ctx.clear() |
| 925 | return true |
| 926 | } |
| 927 | |
| 928 | func (s *Scanner) scanFlowArrayStart(ctx *Context) bool { |
| 929 | if ctx.existsBuffer() && !s.isFlowMode() { |
| 930 | return false |
| 931 | } |
| 932 | |
| 933 | s.addBufferedTokenIfExists(ctx) |
| 934 | ctx.addOriginBuf('[') |
| 935 | ctx.addToken(token.SequenceStart(string(ctx.obuf), s.pos())) |
| 936 | s.startedFlowSequenceNum++ |
| 937 | s.progressColumn(ctx, 1) |
| 938 | ctx.clear() |
| 939 | return true |
| 940 | } |
| 941 | |
| 942 | func (s *Scanner) scanFlowArrayEnd(ctx *Context) bool { |
| 943 | if ctx.existsBuffer() && s.startedFlowSequenceNum <= 0 { |
| 944 | return false |
| 945 | } |
| 946 | |
| 947 | s.addBufferedTokenIfExists(ctx) |
| 948 | ctx.addOriginBuf(']') |
| 949 | ctx.addToken(token.SequenceEnd(string(ctx.obuf), s.pos())) |
| 950 | s.startedFlowSequenceNum-- |
| 951 | s.progressColumn(ctx, 1) |
| 952 | ctx.clear() |
| 953 | return true |
| 954 | } |
| 955 | |
| 956 | func (s *Scanner) scanFlowEntry(ctx *Context, c rune) bool { |
| 957 | if s.startedFlowSequenceNum <= 0 && s.startedFlowMapNum <= 0 { |
| 958 | return false |
| 959 | } |
| 960 | |
| 961 | s.addBufferedTokenIfExists(ctx) |
| 962 | ctx.addOriginBuf(c) |
| 963 | ctx.addToken(token.CollectEntry(string(ctx.obuf), s.pos())) |
| 964 | s.progressColumn(ctx, 1) |
| 965 | ctx.clear() |
| 966 | return true |
| 967 | } |
| 968 | |
| 969 | func (s *Scanner) scanMapDelim(ctx *Context) (bool, error) { |
| 970 | nc := ctx.nextChar() |
| 971 | if s.isDirective || s.isAnchor || s.isAlias { |
| 972 | return false, nil |
| 973 | } |
| 974 | if s.startedFlowMapNum <= 0 && nc != ' ' && nc != '\t' && !s.isNewLineChar(nc) && !ctx.isNextEOS() { |
| 975 | return false, nil |
| 976 | } |
| 977 | if s.startedFlowMapNum > 0 && nc == '/' { |
| 978 | // like http:// |
| 979 | return false, nil |
| 980 | } |
| 981 | if s.startedFlowMapNum > 0 { |
| 982 | tk := ctx.lastToken() |
| 983 | if tk != nil && tk.Type == token.MappingValueType { |
| 984 | return false, nil |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | if strings.HasPrefix(strings.TrimPrefix(string(ctx.obuf), " "), "\t") && !strings.HasPrefix(string(ctx.buf), "\t") { |
| 989 | invalidTk := token.Invalid("tab character cannot use as a map key directly", string(ctx.obuf), s.pos()) |
| 990 | s.progressColumn(ctx, 1) |
| 991 | return false, ErrInvalidToken(invalidTk) |
| 992 | } |
| 993 | |
| 994 | // mapping value |
| 995 | tk := s.bufferedToken(ctx) |
| 996 | if tk != nil { |
| 997 | s.lastDelimColumn = tk.Position.Column |
| 998 | ctx.addToken(tk) |
| 999 | } else if tk := ctx.lastToken(); tk != nil { |
| 1000 | // If the map key is quote, the buffer does not exist because it has already been cut into tokens. |
| 1001 | // Therefore, we need to check the last token. |
| 1002 | if tk.Indicator == token.QuotedScalarIndicator { |
| 1003 | s.lastDelimColumn = tk.Position.Column |
| 1004 | } |
| 1005 | } |
| 1006 | ctx.addToken(token.MappingValue(s.pos())) |
| 1007 | s.progressColumn(ctx, 1) |
| 1008 | ctx.clear() |
| 1009 | return true, nil |
| 1010 | } |
| 1011 | |
| 1012 | func (s *Scanner) scanDocumentStart(ctx *Context) bool { |
| 1013 | if s.indentNum != 0 { |
| 1014 | return false |
| 1015 | } |
| 1016 | if s.column != 1 { |
| 1017 | return false |
| 1018 | } |
| 1019 | if ctx.repeatNum('-') != 3 { |
| 1020 | return false |
| 1021 | } |
| 1022 | if ctx.size > ctx.idx+3 { |
| 1023 | c := ctx.src[ctx.idx+3] |
| 1024 | if c != ' ' && c != '\t' && c != '\n' && c != '\r' { |
| 1025 | return false |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | s.addBufferedTokenIfExists(ctx) |
| 1030 | ctx.addToken(token.DocumentHeader(string(ctx.obuf)+"---", s.pos())) |
| 1031 | s.progressColumn(ctx, 3) |
| 1032 | ctx.clear() |
| 1033 | s.clearState() |
| 1034 | return true |
| 1035 | } |
| 1036 | |
| 1037 | func (s *Scanner) scanDocumentEnd(ctx *Context) bool { |
| 1038 | if s.indentNum != 0 { |
| 1039 | return false |
| 1040 | } |
| 1041 | if s.column != 1 { |
| 1042 | return false |
| 1043 | } |
| 1044 | if ctx.repeatNum('.') != 3 { |
| 1045 | return false |
| 1046 | } |
| 1047 | |
| 1048 | s.addBufferedTokenIfExists(ctx) |
| 1049 | ctx.addToken(token.DocumentEnd(string(ctx.obuf)+"...", s.pos())) |
| 1050 | s.progressColumn(ctx, 3) |
| 1051 | ctx.clear() |
| 1052 | return true |
| 1053 | } |
| 1054 | |
| 1055 | func (s *Scanner) scanMergeKey(ctx *Context) bool { |
| 1056 | if !s.isMergeKey(ctx) { |
| 1057 | return false |
| 1058 | } |
| 1059 | |
| 1060 | s.lastDelimColumn = s.column |
| 1061 | ctx.addToken(token.MergeKey(string(ctx.obuf)+"<<", s.pos())) |
| 1062 | s.progressColumn(ctx, 2) |
| 1063 | ctx.clear() |
| 1064 | return true |
| 1065 | } |
| 1066 | |
| 1067 | func (s *Scanner) scanRawFoldedChar(ctx *Context) bool { |
| 1068 | if !ctx.existsBuffer() { |
| 1069 | return false |
| 1070 | } |
| 1071 | if !s.isChangedToIndentStateUp() { |
| 1072 | return false |
| 1073 | } |
| 1074 | |
| 1075 | ctx.setRawFolded(s.column) |
| 1076 | ctx.addBuf('-') |
| 1077 | ctx.addOriginBuf('-') |
| 1078 | s.progressColumn(ctx, 1) |
| 1079 | return true |
| 1080 | } |
| 1081 | |
| 1082 | func (s *Scanner) scanSequence(ctx *Context) (bool, error) { |
| 1083 | if ctx.existsBuffer() { |
| 1084 | return false, nil |
| 1085 | } |
| 1086 | |
| 1087 | nc := ctx.nextChar() |
| 1088 | if nc != 0 && nc != ' ' && nc != '\t' && !s.isNewLineChar(nc) { |
| 1089 | return false, nil |
| 1090 | } |
| 1091 | |
| 1092 | if strings.HasPrefix(strings.TrimPrefix(string(ctx.obuf), " "), "\t") { |
| 1093 | invalidTk := token.Invalid("tab character cannot use as a sequence delimiter", string(ctx.obuf), s.pos()) |
| 1094 | s.progressColumn(ctx, 1) |
| 1095 | return false, ErrInvalidToken(invalidTk) |
| 1096 | } |
| 1097 | |
| 1098 | s.addBufferedTokenIfExists(ctx) |
| 1099 | ctx.addOriginBuf('-') |
| 1100 | tk := token.SequenceEntry(string(ctx.obuf), s.pos()) |
| 1101 | s.lastDelimColumn = tk.Position.Column |
| 1102 | ctx.addToken(tk) |
| 1103 | s.progressColumn(ctx, 1) |
| 1104 | ctx.clear() |
| 1105 | return true, nil |
| 1106 | } |
| 1107 | |
| 1108 | func (s *Scanner) scanMultiLineHeader(ctx *Context) (bool, error) { |
| 1109 | if ctx.existsBuffer() { |
| 1110 | return false, nil |
| 1111 | } |
| 1112 | |
| 1113 | if err := s.scanMultiLineHeaderOption(ctx); err != nil { |
| 1114 | return false, err |
| 1115 | } |
| 1116 | s.progressLine(ctx) |
| 1117 | return true, nil |
| 1118 | } |
| 1119 | |
| 1120 | func (s *Scanner) validateMultiLineHeaderOption(opt string) error { |
| 1121 | if len(opt) == 0 { |
| 1122 | return nil |
| 1123 | } |
| 1124 | orgOpt := opt |
| 1125 | opt = strings.TrimPrefix(opt, "-") |
| 1126 | opt = strings.TrimPrefix(opt, "+") |
| 1127 | opt = strings.TrimSuffix(opt, "-") |
| 1128 | opt = strings.TrimSuffix(opt, "+") |
| 1129 | if len(opt) == 0 { |
| 1130 | return nil |
| 1131 | } |
| 1132 | if opt == "0" { |
| 1133 | return fmt.Errorf("invalid header option: %q", orgOpt) |
| 1134 | } |
| 1135 | i, err := strconv.ParseInt(opt, 10, 64) |
| 1136 | if err != nil { |
| 1137 | return fmt.Errorf("invalid header option: %q", orgOpt) |
| 1138 | } |
| 1139 | if i > 9 { |
| 1140 | return fmt.Errorf("invalid header option: %q", orgOpt) |
| 1141 | } |
| 1142 | return nil |
| 1143 | } |
| 1144 | |
| 1145 | func (s *Scanner) scanMultiLineHeaderOption(ctx *Context) error { |
| 1146 | header := ctx.currentChar() |
| 1147 | ctx.addOriginBuf(header) |
| 1148 | s.progress(ctx, 1) // skip '|' or '>' character |
| 1149 | |
| 1150 | var progress int |
| 1151 | for idx, c := range ctx.src[ctx.idx:] { |
| 1152 | progress = idx |
| 1153 | ctx.addOriginBuf(c) |
| 1154 | if s.isNewLineChar(c) { |
| 1155 | break |
| 1156 | } |
| 1157 | } |
| 1158 | value := strings.TrimRight(ctx.source(ctx.idx, ctx.idx+progress), " ") |
| 1159 | commentValueIndex := strings.Index(value, "#") |
| 1160 | opt := value |
| 1161 | if commentValueIndex > 0 { |
| 1162 | opt = value[:commentValueIndex] |
| 1163 | } |
| 1164 | opt = strings.TrimRightFunc(opt, func(r rune) bool { |
| 1165 | return r == ' ' || r == '\t' |
| 1166 | }) |
| 1167 | if len(opt) != 0 { |
| 1168 | if err := s.validateMultiLineHeaderOption(opt); err != nil { |
| 1169 | invalidTk := token.Invalid(err.Error(), string(ctx.obuf), s.pos()) |
| 1170 | s.progressColumn(ctx, progress) |
| 1171 | return ErrInvalidToken(invalidTk) |
| 1172 | } |
| 1173 | } |
| 1174 | if s.column == 1 { |
| 1175 | s.lastDelimColumn = 1 |
| 1176 | } |
| 1177 | |
| 1178 | commentIndex := strings.Index(string(ctx.obuf), "#") |
| 1179 | headerBuf := string(ctx.obuf) |
| 1180 | if commentIndex > 0 { |
| 1181 | headerBuf = headerBuf[:commentIndex] |
| 1182 | } |
| 1183 | switch header { |
| 1184 | case '|': |
| 1185 | ctx.addToken(token.Literal("|"+opt, headerBuf, s.pos())) |
| 1186 | ctx.setLiteral(s.lastDelimColumn, opt) |
| 1187 | case '>': |
| 1188 | ctx.addToken(token.Folded(">"+opt, headerBuf, s.pos())) |
| 1189 | ctx.setFolded(s.lastDelimColumn, opt) |
| 1190 | } |
| 1191 | if commentIndex > 0 { |
| 1192 | comment := string(value[commentValueIndex+1:]) |
| 1193 | s.offset += len(headerBuf) |
| 1194 | s.column += len(headerBuf) |
| 1195 | ctx.addToken(token.Comment(comment, string(ctx.obuf[len(headerBuf):]), s.pos())) |
| 1196 | } |
| 1197 | s.indentState = IndentStateKeep |
| 1198 | ctx.resetBuffer() |
| 1199 | s.progressColumn(ctx, progress) |
| 1200 | return nil |
| 1201 | } |
| 1202 | |
| 1203 | func (s *Scanner) scanMapKey(ctx *Context) bool { |
| 1204 | if ctx.existsBuffer() { |
| 1205 | return false |
| 1206 | } |
| 1207 | |
| 1208 | nc := ctx.nextChar() |
| 1209 | if nc != ' ' && nc != '\t' { |
| 1210 | return false |
| 1211 | } |
| 1212 | |
| 1213 | tk := token.MappingKey(s.pos()) |
| 1214 | s.lastDelimColumn = tk.Position.Column |
| 1215 | ctx.addToken(tk) |
| 1216 | s.progressColumn(ctx, 1) |
| 1217 | ctx.clear() |
| 1218 | return true |
| 1219 | } |
| 1220 | |
| 1221 | func (s *Scanner) scanDirective(ctx *Context) bool { |
| 1222 | if ctx.existsBuffer() { |
| 1223 | return false |
| 1224 | } |
| 1225 | if s.indentNum != 0 { |
| 1226 | return false |
| 1227 | } |
| 1228 | |
| 1229 | s.addBufferedTokenIfExists(ctx) |
| 1230 | ctx.addOriginBuf('%') |
| 1231 | ctx.addToken(token.Directive(string(ctx.obuf), s.pos())) |
| 1232 | s.progressColumn(ctx, 1) |
| 1233 | ctx.clear() |
| 1234 | s.isDirective = true |
| 1235 | return true |
| 1236 | } |
| 1237 | |
| 1238 | func (s *Scanner) scanAnchor(ctx *Context) bool { |
| 1239 | if ctx.existsBuffer() { |
| 1240 | return false |
| 1241 | } |
| 1242 | |
| 1243 | s.addBufferedTokenIfExists(ctx) |
| 1244 | ctx.addOriginBuf('&') |
| 1245 | ctx.addToken(token.Anchor(string(ctx.obuf), s.pos())) |
| 1246 | s.progressColumn(ctx, 1) |
| 1247 | s.isAnchor = true |
| 1248 | ctx.clear() |
| 1249 | return true |
| 1250 | } |
| 1251 | |
| 1252 | func (s *Scanner) scanAlias(ctx *Context) bool { |
| 1253 | if ctx.existsBuffer() { |
| 1254 | return false |
| 1255 | } |
| 1256 | |
| 1257 | s.addBufferedTokenIfExists(ctx) |
| 1258 | ctx.addOriginBuf('*') |
| 1259 | ctx.addToken(token.Alias(string(ctx.obuf), s.pos())) |
| 1260 | s.progressColumn(ctx, 1) |
| 1261 | s.isAlias = true |
| 1262 | ctx.clear() |
| 1263 | return true |
| 1264 | } |
| 1265 | |
| 1266 | func (s *Scanner) scanReservedChar(ctx *Context, c rune) error { |
| 1267 | if ctx.existsBuffer() { |
| 1268 | return nil |
| 1269 | } |
| 1270 | |
| 1271 | ctx.addBuf(c) |
| 1272 | ctx.addOriginBuf(c) |
| 1273 | err := ErrInvalidToken( |
| 1274 | token.Invalid( |
| 1275 | fmt.Sprintf("%q is a reserved character", c), |
| 1276 | string(ctx.obuf), s.pos(), |
| 1277 | ), |
| 1278 | ) |
| 1279 | s.progressColumn(ctx, 1) |
| 1280 | ctx.clear() |
| 1281 | return err |
| 1282 | } |
| 1283 | |
| 1284 | func (s *Scanner) scanTab(ctx *Context, c rune) error { |
| 1285 | if s.startedFlowSequenceNum > 0 || s.startedFlowMapNum > 0 { |
| 1286 | // tabs character is allowed in flow mode. |
| 1287 | return nil |
| 1288 | } |
| 1289 | |
| 1290 | if !s.isFirstCharAtLine { |
| 1291 | return nil |
| 1292 | } |
| 1293 | |
| 1294 | ctx.addBuf(c) |
| 1295 | ctx.addOriginBuf(c) |
| 1296 | err := ErrInvalidToken( |
| 1297 | token.Invalid("found character '\t' that cannot start any token", |
| 1298 | string(ctx.obuf), s.pos(), |
| 1299 | ), |
| 1300 | ) |
| 1301 | s.progressColumn(ctx, 1) |
| 1302 | ctx.clear() |
| 1303 | return err |
| 1304 | } |
| 1305 | |
| 1306 | func (s *Scanner) scan(ctx *Context) error { |
| 1307 | for ctx.next() { |
| 1308 | c := ctx.currentChar() |
| 1309 | // First, change the IndentState. |
| 1310 | // If the target character is the first character in a line, IndentState is Up/Down/Equal state. |
| 1311 | // The second and subsequent letters are Keep. |
| 1312 | s.updateIndent(ctx, c) |
| 1313 | |
| 1314 | // If IndentState is down, tokens are split, so the buffer accumulated until that point needs to be cutted as a token. |
| 1315 | if s.isChangedToIndentStateDown() { |
| 1316 | s.addBufferedTokenIfExists(ctx) |
| 1317 | } |
| 1318 | if ctx.isMultiLine() { |
| 1319 | if s.isChangedToIndentStateDown() { |
| 1320 | if tk := ctx.lastToken(); tk != nil { |
| 1321 | // If literal/folded content is empty, no string token is added. |
| 1322 | // Therefore, add an empty string token. |
| 1323 | // But if literal/folded token column is 1, it is invalid at down state. |
| 1324 | if tk.Position.Column == 1 { |
| 1325 | return ErrInvalidToken( |
| 1326 | token.Invalid( |
| 1327 | "could not find multi-line content", |
| 1328 | string(ctx.obuf), s.pos(), |
| 1329 | ), |
| 1330 | ) |
| 1331 | } |
| 1332 | if tk.Type != token.StringType { |
| 1333 | ctx.addToken(token.String("", "", s.pos())) |
| 1334 | } |
| 1335 | } |
| 1336 | s.breakMultiLine(ctx) |
| 1337 | } else { |
| 1338 | if err := s.scanMultiLine(ctx, c); err != nil { |
| 1339 | return err |
| 1340 | } |
| 1341 | continue |
| 1342 | } |
| 1343 | } |
| 1344 | switch c { |
| 1345 | case '{': |
| 1346 | if s.scanFlowMapStart(ctx) { |
| 1347 | continue |
| 1348 | } |
| 1349 | case '}': |
| 1350 | if s.scanFlowMapEnd(ctx) { |
| 1351 | continue |
| 1352 | } |
| 1353 | case '.': |
| 1354 | if s.scanDocumentEnd(ctx) { |
| 1355 | continue |
| 1356 | } |
| 1357 | case '<': |
| 1358 | if s.scanMergeKey(ctx) { |
| 1359 | continue |
| 1360 | } |
| 1361 | case '-': |
| 1362 | if s.scanDocumentStart(ctx) { |
| 1363 | continue |
| 1364 | } |
| 1365 | if s.scanRawFoldedChar(ctx) { |
| 1366 | continue |
| 1367 | } |
| 1368 | scanned, err := s.scanSequence(ctx) |
| 1369 | if err != nil { |
| 1370 | return err |
| 1371 | } |
| 1372 | if scanned { |
| 1373 | continue |
| 1374 | } |
| 1375 | case '[': |
| 1376 | if s.scanFlowArrayStart(ctx) { |
| 1377 | continue |
| 1378 | } |
| 1379 | case ']': |
| 1380 | if s.scanFlowArrayEnd(ctx) { |
| 1381 | continue |
| 1382 | } |
| 1383 | case ',': |
| 1384 | if s.scanFlowEntry(ctx, c) { |
| 1385 | continue |
| 1386 | } |
| 1387 | case ':': |
| 1388 | scanned, err := s.scanMapDelim(ctx) |
| 1389 | if err != nil { |
| 1390 | return err |
| 1391 | } |
| 1392 | if scanned { |
| 1393 | continue |
| 1394 | } |
| 1395 | case '|', '>': |
| 1396 | scanned, err := s.scanMultiLineHeader(ctx) |
| 1397 | if err != nil { |
| 1398 | return err |
| 1399 | } |
| 1400 | if scanned { |
| 1401 | continue |
| 1402 | } |
| 1403 | case '!': |
| 1404 | scanned, err := s.scanTag(ctx) |
| 1405 | if err != nil { |
| 1406 | return err |
| 1407 | } |
| 1408 | if scanned { |
| 1409 | continue |
| 1410 | } |
| 1411 | case '%': |
| 1412 | if s.scanDirective(ctx) { |
| 1413 | continue |
| 1414 | } |
| 1415 | case '?': |
| 1416 | if s.scanMapKey(ctx) { |
| 1417 | continue |
| 1418 | } |
| 1419 | case '&': |
| 1420 | if s.scanAnchor(ctx) { |
| 1421 | continue |
| 1422 | } |
| 1423 | case '*': |
| 1424 | if s.scanAlias(ctx) { |
| 1425 | continue |
| 1426 | } |
| 1427 | case '#': |
| 1428 | if s.scanComment(ctx) { |
| 1429 | continue |
| 1430 | } |
| 1431 | case '\'', '"': |
| 1432 | scanned, err := s.scanQuote(ctx, c) |
| 1433 | if err != nil { |
| 1434 | return err |
| 1435 | } |
| 1436 | if scanned { |
| 1437 | continue |
| 1438 | } |
| 1439 | case '\r', '\n': |
| 1440 | s.scanNewLine(ctx, c) |
| 1441 | continue |
| 1442 | case ' ': |
| 1443 | if s.scanWhiteSpace(ctx) { |
| 1444 | continue |
| 1445 | } |
| 1446 | case '@', '`': |
| 1447 | if err := s.scanReservedChar(ctx, c); err != nil { |
| 1448 | return err |
| 1449 | } |
| 1450 | case '\t': |
| 1451 | if ctx.existsBuffer() && s.lastDelimColumn == 0 { |
| 1452 | // tab indent for plain text (yaml-test-suite's spec-example-7-12-plain-lines). |
| 1453 | s.indentNum++ |
| 1454 | ctx.addOriginBuf(c) |
| 1455 | s.progressOnly(ctx, 1) |
| 1456 | continue |
| 1457 | } |
| 1458 | if s.lastDelimColumn < s.column { |
| 1459 | s.indentNum++ |
| 1460 | ctx.addOriginBuf(c) |
| 1461 | s.progressOnly(ctx, 1) |
| 1462 | continue |
| 1463 | } |
| 1464 | if err := s.scanTab(ctx, c); err != nil { |
| 1465 | return err |
| 1466 | } |
| 1467 | } |
| 1468 | ctx.addBuf(c) |
| 1469 | ctx.addOriginBuf(c) |
| 1470 | s.progressColumn(ctx, 1) |
| 1471 | } |
| 1472 | s.addBufferedTokenIfExists(ctx) |
| 1473 | return nil |
| 1474 | } |
| 1475 | |
| 1476 | // Init prepares the scanner s to tokenize the text src by setting the scanner at the beginning of src. |
| 1477 | func (s *Scanner) Init(text string) { |
| 1478 | src := []rune(text) |
| 1479 | s.source = src |
| 1480 | s.sourcePos = 0 |
| 1481 | s.sourceSize = len(src) |
| 1482 | s.line = 1 |
| 1483 | s.column = 1 |
| 1484 | s.offset = 1 |
| 1485 | s.isFirstCharAtLine = true |
| 1486 | s.clearState() |
| 1487 | } |
| 1488 | |
| 1489 | func (s *Scanner) clearState() { |
| 1490 | s.prevLineIndentNum = 0 |
| 1491 | s.lastDelimColumn = 0 |
| 1492 | s.indentLevel = 0 |
| 1493 | s.indentNum = 0 |
| 1494 | } |
| 1495 | |
| 1496 | // Scan scans the next token and returns the token collection. The source end is indicated by io.EOF. |
| 1497 | func (s *Scanner) Scan() (token.Tokens, error) { |
| 1498 | if s.sourcePos >= s.sourceSize { |
| 1499 | return nil, io.EOF |
| 1500 | } |
| 1501 | ctx := newContext(s.source[s.sourcePos:]) |
| 1502 | defer ctx.release() |
| 1503 | |
| 1504 | var tokens token.Tokens |
| 1505 | err := s.scan(ctx) |
| 1506 | tokens = append(tokens, ctx.tokens...) |
| 1507 | |
| 1508 | if err != nil { |
| 1509 | var invalidTokenErr *InvalidTokenError |
| 1510 | if errors.As(err, &invalidTokenErr) { |
| 1511 | tokens = append(tokens, invalidTokenErr.Token) |
| 1512 | } |
| 1513 | return tokens, err |
| 1514 | } |
| 1515 | return tokens, nil |
| 1516 | } |