| 1 | package parser |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "strings" |
| 7 | |
| 8 | "github.com/goccy/go-yaml/internal/errors" |
| 9 | "github.com/goccy/go-yaml/token" |
| 10 | ) |
| 11 | |
| 12 | type TokenGroupType int |
| 13 | |
| 14 | const ( |
| 15 | TokenGroupNone TokenGroupType = iota |
| 16 | TokenGroupDirective |
| 17 | TokenGroupDirectiveName |
| 18 | TokenGroupDocument |
| 19 | TokenGroupDocumentBody |
| 20 | TokenGroupAnchor |
| 21 | TokenGroupAnchorName |
| 22 | TokenGroupAlias |
| 23 | TokenGroupLiteral |
| 24 | TokenGroupFolded |
| 25 | TokenGroupScalarTag |
| 26 | TokenGroupMapKey |
| 27 | TokenGroupMapKeyValue |
| 28 | ) |
| 29 | |
| 30 | func (t TokenGroupType) String() string { |
| 31 | switch t { |
| 32 | case TokenGroupNone: |
| 33 | return "none" |
| 34 | case TokenGroupDirective: |
| 35 | return "directive" |
| 36 | case TokenGroupDirectiveName: |
| 37 | return "directive_name" |
| 38 | case TokenGroupDocument: |
| 39 | return "document" |
| 40 | case TokenGroupDocumentBody: |
| 41 | return "document_body" |
| 42 | case TokenGroupAnchor: |
| 43 | return "anchor" |
| 44 | case TokenGroupAnchorName: |
| 45 | return "anchor_name" |
| 46 | case TokenGroupAlias: |
| 47 | return "alias" |
| 48 | case TokenGroupLiteral: |
| 49 | return "literal" |
| 50 | case TokenGroupFolded: |
| 51 | return "folded" |
| 52 | case TokenGroupScalarTag: |
| 53 | return "scalar_tag" |
| 54 | case TokenGroupMapKey: |
| 55 | return "map_key" |
| 56 | case TokenGroupMapKeyValue: |
| 57 | return "map_key_value" |
| 58 | } |
| 59 | return "none" |
| 60 | } |
| 61 | |
| 62 | type Token struct { |
| 63 | Token *token.Token |
| 64 | Group *TokenGroup |
| 65 | LineComment *token.Token |
| 66 | } |
| 67 | |
| 68 | func (t *Token) RawToken() *token.Token { |
| 69 | if t == nil { |
| 70 | return nil |
| 71 | } |
| 72 | if t.Token != nil { |
| 73 | return t.Token |
| 74 | } |
| 75 | return t.Group.RawToken() |
| 76 | } |
| 77 | |
| 78 | func (t *Token) Type() token.Type { |
| 79 | if t == nil { |
| 80 | return 0 |
| 81 | } |
| 82 | if t.Token != nil { |
| 83 | return t.Token.Type |
| 84 | } |
| 85 | return t.Group.TokenType() |
| 86 | } |
| 87 | |
| 88 | func (t *Token) GroupType() TokenGroupType { |
| 89 | if t == nil { |
| 90 | return TokenGroupNone |
| 91 | } |
| 92 | if t.Token != nil { |
| 93 | return TokenGroupNone |
| 94 | } |
| 95 | return t.Group.Type |
| 96 | } |
| 97 | |
| 98 | func (t *Token) Line() int { |
| 99 | if t == nil { |
| 100 | return 0 |
| 101 | } |
| 102 | if t.Token != nil { |
| 103 | return t.Token.Position.Line |
| 104 | } |
| 105 | return t.Group.Line() |
| 106 | } |
| 107 | |
| 108 | func (t *Token) Column() int { |
| 109 | if t == nil { |
| 110 | return 0 |
| 111 | } |
| 112 | if t.Token != nil { |
| 113 | return t.Token.Position.Column |
| 114 | } |
| 115 | return t.Group.Column() |
| 116 | } |
| 117 | |
| 118 | func (t *Token) SetGroupType(typ TokenGroupType) { |
| 119 | if t.Group == nil { |
| 120 | return |
| 121 | } |
| 122 | t.Group.Type = typ |
| 123 | } |
| 124 | |
| 125 | func (t *Token) Dump() { |
| 126 | ctx := new(groupTokenRenderContext) |
| 127 | if t.Token != nil { |
| 128 | fmt.Fprint(os.Stdout, t.Token.Value) |
| 129 | return |
| 130 | } |
| 131 | t.Group.dump(ctx) |
| 132 | fmt.Fprintf(os.Stdout, "\n") |
| 133 | } |
| 134 | |
| 135 | func (t *Token) dump(ctx *groupTokenRenderContext) { |
| 136 | if t.Token != nil { |
| 137 | fmt.Fprint(os.Stdout, t.Token.Value) |
| 138 | return |
| 139 | } |
| 140 | t.Group.dump(ctx) |
| 141 | } |
| 142 | |
| 143 | type groupTokenRenderContext struct { |
| 144 | num int |
| 145 | } |
| 146 | |
| 147 | type TokenGroup struct { |
| 148 | Type TokenGroupType |
| 149 | Tokens []*Token |
| 150 | } |
| 151 | |
| 152 | func (g *TokenGroup) First() *Token { |
| 153 | if len(g.Tokens) == 0 { |
| 154 | return nil |
| 155 | } |
| 156 | return g.Tokens[0] |
| 157 | } |
| 158 | |
| 159 | func (g *TokenGroup) Last() *Token { |
| 160 | if len(g.Tokens) == 0 { |
| 161 | return nil |
| 162 | } |
| 163 | return g.Tokens[len(g.Tokens)-1] |
| 164 | } |
| 165 | |
| 166 | func (g *TokenGroup) dump(ctx *groupTokenRenderContext) { |
| 167 | num := ctx.num |
| 168 | fmt.Fprint(os.Stdout, colorize(num, "(")) |
| 169 | ctx.num++ |
| 170 | for _, tk := range g.Tokens { |
| 171 | tk.dump(ctx) |
| 172 | } |
| 173 | fmt.Fprint(os.Stdout, colorize(num, ")")) |
| 174 | } |
| 175 | |
| 176 | func (g *TokenGroup) RawToken() *token.Token { |
| 177 | if len(g.Tokens) == 0 { |
| 178 | return nil |
| 179 | } |
| 180 | return g.Tokens[0].RawToken() |
| 181 | } |
| 182 | |
| 183 | func (g *TokenGroup) Line() int { |
| 184 | if len(g.Tokens) == 0 { |
| 185 | return 0 |
| 186 | } |
| 187 | return g.Tokens[0].Line() |
| 188 | } |
| 189 | |
| 190 | func (g *TokenGroup) Column() int { |
| 191 | if len(g.Tokens) == 0 { |
| 192 | return 0 |
| 193 | } |
| 194 | return g.Tokens[0].Column() |
| 195 | } |
| 196 | |
| 197 | func (g *TokenGroup) TokenType() token.Type { |
| 198 | if len(g.Tokens) == 0 { |
| 199 | return 0 |
| 200 | } |
| 201 | return g.Tokens[0].Type() |
| 202 | } |
| 203 | |
| 204 | func CreateGroupedTokens(tokens token.Tokens) ([]*Token, error) { |
| 205 | var err error |
| 206 | tks := newTokens(tokens) |
| 207 | tks = createLineCommentTokenGroups(tks) |
| 208 | tks, err = createLiteralAndFoldedTokenGroups(tks) |
| 209 | if err != nil { |
| 210 | return nil, err |
| 211 | } |
| 212 | tks, err = createAnchorAndAliasTokenGroups(tks) |
| 213 | if err != nil { |
| 214 | return nil, err |
| 215 | } |
| 216 | tks, err = createScalarTagTokenGroups(tks) |
| 217 | if err != nil { |
| 218 | return nil, err |
| 219 | } |
| 220 | tks, err = createAnchorWithScalarTagTokenGroups(tks) |
| 221 | if err != nil { |
| 222 | return nil, err |
| 223 | } |
| 224 | tks, err = createMapKeyTokenGroups(tks) |
| 225 | if err != nil { |
| 226 | return nil, err |
| 227 | } |
| 228 | tks = createMapKeyValueTokenGroups(tks) |
| 229 | tks, err = createDirectiveTokenGroups(tks) |
| 230 | if err != nil { |
| 231 | return nil, err |
| 232 | } |
| 233 | tks, err = createDocumentTokens(tks) |
| 234 | if err != nil { |
| 235 | return nil, err |
| 236 | } |
| 237 | return tks, nil |
| 238 | } |
| 239 | |
| 240 | func newTokens(tks token.Tokens) []*Token { |
| 241 | ret := make([]*Token, 0, len(tks)) |
| 242 | for _, tk := range tks { |
| 243 | ret = append(ret, &Token{Token: tk}) |
| 244 | } |
| 245 | return ret |
| 246 | } |
| 247 | |
| 248 | func createLineCommentTokenGroups(tokens []*Token) []*Token { |
| 249 | ret := make([]*Token, 0, len(tokens)) |
| 250 | for i := 0; i < len(tokens); i++ { |
| 251 | tk := tokens[i] |
| 252 | switch tk.Type() { |
| 253 | case token.CommentType: |
| 254 | if i > 0 && tokens[i-1].Line() == tk.Line() { |
| 255 | tokens[i-1].LineComment = tk.RawToken() |
| 256 | } else { |
| 257 | ret = append(ret, tk) |
| 258 | } |
| 259 | default: |
| 260 | ret = append(ret, tk) |
| 261 | } |
| 262 | } |
| 263 | return ret |
| 264 | } |
| 265 | |
| 266 | func createLiteralAndFoldedTokenGroups(tokens []*Token) ([]*Token, error) { |
| 267 | ret := make([]*Token, 0, len(tokens)) |
| 268 | for i := 0; i < len(tokens); i++ { |
| 269 | tk := tokens[i] |
| 270 | switch tk.Type() { |
| 271 | case token.LiteralType: |
| 272 | tks := []*Token{tk} |
| 273 | if i+1 < len(tokens) { |
| 274 | tks = append(tks, tokens[i+1]) |
| 275 | } |
| 276 | ret = append(ret, &Token{ |
| 277 | Group: &TokenGroup{ |
| 278 | Type: TokenGroupLiteral, |
| 279 | Tokens: tks, |
| 280 | }, |
| 281 | }) |
| 282 | i++ |
| 283 | case token.FoldedType: |
| 284 | tks := []*Token{tk} |
| 285 | if i+1 < len(tokens) { |
| 286 | tks = append(tks, tokens[i+1]) |
| 287 | } |
| 288 | ret = append(ret, &Token{ |
| 289 | Group: &TokenGroup{ |
| 290 | Type: TokenGroupFolded, |
| 291 | Tokens: tks, |
| 292 | }, |
| 293 | }) |
| 294 | i++ |
| 295 | default: |
| 296 | ret = append(ret, tk) |
| 297 | } |
| 298 | } |
| 299 | return ret, nil |
| 300 | } |
| 301 | |
| 302 | func createAnchorAndAliasTokenGroups(tokens []*Token) ([]*Token, error) { |
| 303 | ret := make([]*Token, 0, len(tokens)) |
| 304 | for i := 0; i < len(tokens); i++ { |
| 305 | tk := tokens[i] |
| 306 | switch tk.Type() { |
| 307 | case token.AnchorType: |
| 308 | if i+1 >= len(tokens) { |
| 309 | return nil, errors.ErrSyntax("undefined anchor name", tk.RawToken()) |
| 310 | } |
| 311 | if i+2 >= len(tokens) { |
| 312 | return nil, errors.ErrSyntax("undefined anchor value", tk.RawToken()) |
| 313 | } |
| 314 | anchorName := &Token{ |
| 315 | Group: &TokenGroup{ |
| 316 | Type: TokenGroupAnchorName, |
| 317 | Tokens: []*Token{tk, tokens[i+1]}, |
| 318 | }, |
| 319 | } |
| 320 | valueTk := tokens[i+2] |
| 321 | if tk.Line() == valueTk.Line() && valueTk.Type() == token.SequenceEntryType { |
| 322 | return nil, errors.ErrSyntax("sequence entries are not allowed after anchor on the same line", valueTk.RawToken()) |
| 323 | } |
| 324 | if tk.Line() == valueTk.Line() && isScalarType(valueTk) { |
| 325 | ret = append(ret, &Token{ |
| 326 | Group: &TokenGroup{ |
| 327 | Type: TokenGroupAnchor, |
| 328 | Tokens: []*Token{anchorName, valueTk}, |
| 329 | }, |
| 330 | }) |
| 331 | i++ |
| 332 | } else { |
| 333 | ret = append(ret, anchorName) |
| 334 | } |
| 335 | i++ |
| 336 | case token.AliasType: |
| 337 | if i+1 == len(tokens) { |
| 338 | return nil, errors.ErrSyntax("undefined alias name", tk.RawToken()) |
| 339 | } |
| 340 | ret = append(ret, &Token{ |
| 341 | Group: &TokenGroup{ |
| 342 | Type: TokenGroupAlias, |
| 343 | Tokens: []*Token{tk, tokens[i+1]}, |
| 344 | }, |
| 345 | }) |
| 346 | i++ |
| 347 | default: |
| 348 | ret = append(ret, tk) |
| 349 | } |
| 350 | } |
| 351 | return ret, nil |
| 352 | } |
| 353 | |
| 354 | func createScalarTagTokenGroups(tokens []*Token) ([]*Token, error) { |
| 355 | ret := make([]*Token, 0, len(tokens)) |
| 356 | for i := 0; i < len(tokens); i++ { |
| 357 | tk := tokens[i] |
| 358 | if tk.Type() != token.TagType { |
| 359 | ret = append(ret, tk) |
| 360 | continue |
| 361 | } |
| 362 | tag := tk.RawToken() |
| 363 | if strings.HasPrefix(tag.Value, "!!") { |
| 364 | // secondary tag. |
| 365 | switch token.ReservedTagKeyword(tag.Value) { |
| 366 | case token.IntegerTag, token.FloatTag, token.StringTag, token.BinaryTag, token.TimestampTag, token.BooleanTag, token.NullTag: |
| 367 | if len(tokens) <= i+1 { |
| 368 | ret = append(ret, tk) |
| 369 | continue |
| 370 | } |
| 371 | if tk.Line() != tokens[i+1].Line() { |
| 372 | ret = append(ret, tk) |
| 373 | continue |
| 374 | } |
| 375 | if tokens[i+1].GroupType() == TokenGroupAnchorName { |
| 376 | ret = append(ret, tk) |
| 377 | continue |
| 378 | } |
| 379 | if isScalarType(tokens[i+1]) { |
| 380 | ret = append(ret, &Token{ |
| 381 | Group: &TokenGroup{ |
| 382 | Type: TokenGroupScalarTag, |
| 383 | Tokens: []*Token{tk, tokens[i+1]}, |
| 384 | }, |
| 385 | }) |
| 386 | i++ |
| 387 | } else { |
| 388 | ret = append(ret, tk) |
| 389 | } |
| 390 | case token.MergeTag: |
| 391 | if len(tokens) <= i+1 { |
| 392 | ret = append(ret, tk) |
| 393 | continue |
| 394 | } |
| 395 | if tk.Line() != tokens[i+1].Line() { |
| 396 | ret = append(ret, tk) |
| 397 | continue |
| 398 | } |
| 399 | if tokens[i+1].GroupType() == TokenGroupAnchorName { |
| 400 | ret = append(ret, tk) |
| 401 | continue |
| 402 | } |
| 403 | if tokens[i+1].Type() != token.MergeKeyType { |
| 404 | return nil, errors.ErrSyntax("could not find merge key", tokens[i+1].RawToken()) |
| 405 | } |
| 406 | ret = append(ret, &Token{ |
| 407 | Group: &TokenGroup{ |
| 408 | Type: TokenGroupScalarTag, |
| 409 | Tokens: []*Token{tk, tokens[i+1]}, |
| 410 | }, |
| 411 | }) |
| 412 | i++ |
| 413 | default: |
| 414 | ret = append(ret, tk) |
| 415 | } |
| 416 | } else { |
| 417 | if len(tokens) <= i+1 { |
| 418 | ret = append(ret, tk) |
| 419 | continue |
| 420 | } |
| 421 | if tk.Line() != tokens[i+1].Line() { |
| 422 | ret = append(ret, tk) |
| 423 | continue |
| 424 | } |
| 425 | if tokens[i+1].GroupType() == TokenGroupAnchorName { |
| 426 | ret = append(ret, tk) |
| 427 | continue |
| 428 | } |
| 429 | if isFlowType(tokens[i+1]) { |
| 430 | ret = append(ret, tk) |
| 431 | continue |
| 432 | } |
| 433 | ret = append(ret, &Token{ |
| 434 | Group: &TokenGroup{ |
| 435 | Type: TokenGroupScalarTag, |
| 436 | Tokens: []*Token{tk, tokens[i+1]}, |
| 437 | }, |
| 438 | }) |
| 439 | i++ |
| 440 | } |
| 441 | } |
| 442 | return ret, nil |
| 443 | } |
| 444 | |
| 445 | func createAnchorWithScalarTagTokenGroups(tokens []*Token) ([]*Token, error) { |
| 446 | ret := make([]*Token, 0, len(tokens)) |
| 447 | for i := 0; i < len(tokens); i++ { |
| 448 | tk := tokens[i] |
| 449 | switch tk.GroupType() { |
| 450 | case TokenGroupAnchorName: |
| 451 | if i+1 >= len(tokens) { |
| 452 | return nil, errors.ErrSyntax("undefined anchor value", tk.RawToken()) |
| 453 | } |
| 454 | valueTk := tokens[i+1] |
| 455 | if tk.Line() == valueTk.Line() && valueTk.GroupType() == TokenGroupScalarTag { |
| 456 | ret = append(ret, &Token{ |
| 457 | Group: &TokenGroup{ |
| 458 | Type: TokenGroupAnchor, |
| 459 | Tokens: []*Token{tk, tokens[i+1]}, |
| 460 | }, |
| 461 | }) |
| 462 | i++ |
| 463 | } else { |
| 464 | ret = append(ret, tk) |
| 465 | } |
| 466 | default: |
| 467 | ret = append(ret, tk) |
| 468 | } |
| 469 | } |
| 470 | return ret, nil |
| 471 | } |
| 472 | |
| 473 | func createMapKeyTokenGroups(tokens []*Token) ([]*Token, error) { |
| 474 | tks, err := createMapKeyByMappingKey(tokens) |
| 475 | if err != nil { |
| 476 | return nil, err |
| 477 | } |
| 478 | return createMapKeyByMappingValue(tks) |
| 479 | } |
| 480 | |
| 481 | func createMapKeyByMappingKey(tokens []*Token) ([]*Token, error) { |
| 482 | ret := make([]*Token, 0, len(tokens)) |
| 483 | for i := 0; i < len(tokens); i++ { |
| 484 | tk := tokens[i] |
| 485 | switch tk.Type() { |
| 486 | case token.MappingKeyType: |
| 487 | if i+1 >= len(tokens) { |
| 488 | return nil, errors.ErrSyntax("undefined map key", tk.RawToken()) |
| 489 | } |
| 490 | ret = append(ret, &Token{ |
| 491 | Group: &TokenGroup{ |
| 492 | Type: TokenGroupMapKey, |
| 493 | Tokens: []*Token{tk, tokens[i+1]}, |
| 494 | }, |
| 495 | }) |
| 496 | i++ |
| 497 | default: |
| 498 | ret = append(ret, tk) |
| 499 | } |
| 500 | } |
| 501 | return ret, nil |
| 502 | } |
| 503 | |
| 504 | func createMapKeyByMappingValue(tokens []*Token) ([]*Token, error) { |
| 505 | ret := make([]*Token, 0, len(tokens)) |
| 506 | for i := 0; i < len(tokens); i++ { |
| 507 | tk := tokens[i] |
| 508 | switch tk.Type() { |
| 509 | case token.MappingValueType: |
| 510 | if i == 0 { |
| 511 | return nil, errors.ErrSyntax("unexpected key name", tk.RawToken()) |
| 512 | } |
| 513 | mapKeyTk := tokens[i-1] |
| 514 | if isNotMapKeyType(mapKeyTk) { |
| 515 | return nil, errors.ErrSyntax("found an invalid key for this map", tokens[i].RawToken()) |
| 516 | } |
| 517 | newTk := &Token{Token: mapKeyTk.Token, Group: mapKeyTk.Group} |
| 518 | mapKeyTk.Token = nil |
| 519 | mapKeyTk.Group = &TokenGroup{ |
| 520 | Type: TokenGroupMapKey, |
| 521 | Tokens: []*Token{newTk, tk}, |
| 522 | } |
| 523 | default: |
| 524 | ret = append(ret, tk) |
| 525 | } |
| 526 | } |
| 527 | return ret, nil |
| 528 | } |
| 529 | |
| 530 | func createMapKeyValueTokenGroups(tokens []*Token) []*Token { |
| 531 | ret := make([]*Token, 0, len(tokens)) |
| 532 | for i := 0; i < len(tokens); i++ { |
| 533 | tk := tokens[i] |
| 534 | switch tk.GroupType() { |
| 535 | case TokenGroupMapKey: |
| 536 | if len(tokens) <= i+1 { |
| 537 | ret = append(ret, tk) |
| 538 | continue |
| 539 | } |
| 540 | valueTk := tokens[i+1] |
| 541 | if tk.Line() != valueTk.Line() { |
| 542 | ret = append(ret, tk) |
| 543 | continue |
| 544 | } |
| 545 | if valueTk.GroupType() == TokenGroupAnchorName { |
| 546 | ret = append(ret, tk) |
| 547 | continue |
| 548 | } |
| 549 | if valueTk.Type() == token.TagType && valueTk.GroupType() != TokenGroupScalarTag { |
| 550 | ret = append(ret, tk) |
| 551 | continue |
| 552 | } |
| 553 | |
| 554 | if isScalarType(valueTk) || valueTk.Type() == token.TagType { |
| 555 | ret = append(ret, &Token{ |
| 556 | Group: &TokenGroup{ |
| 557 | Type: TokenGroupMapKeyValue, |
| 558 | Tokens: []*Token{tk, valueTk}, |
| 559 | }, |
| 560 | }) |
| 561 | i++ |
| 562 | } else { |
| 563 | ret = append(ret, tk) |
| 564 | continue |
| 565 | } |
| 566 | default: |
| 567 | ret = append(ret, tk) |
| 568 | } |
| 569 | } |
| 570 | return ret |
| 571 | } |
| 572 | |
| 573 | func createDirectiveTokenGroups(tokens []*Token) ([]*Token, error) { |
| 574 | ret := make([]*Token, 0, len(tokens)) |
| 575 | for i := 0; i < len(tokens); i++ { |
| 576 | tk := tokens[i] |
| 577 | switch tk.Type() { |
| 578 | case token.DirectiveType: |
| 579 | if i+1 >= len(tokens) { |
| 580 | return nil, errors.ErrSyntax("undefined directive value", tk.RawToken()) |
| 581 | } |
| 582 | directiveName := &Token{ |
| 583 | Group: &TokenGroup{ |
| 584 | Type: TokenGroupDirectiveName, |
| 585 | Tokens: []*Token{tk, tokens[i+1]}, |
| 586 | }, |
| 587 | } |
| 588 | i++ |
| 589 | var valueTks []*Token |
| 590 | for j := i + 1; j < len(tokens); j++ { |
| 591 | if tokens[j].Line() != tk.Line() { |
| 592 | break |
| 593 | } |
| 594 | valueTks = append(valueTks, tokens[j]) |
| 595 | i++ |
| 596 | } |
| 597 | if i+1 >= len(tokens) || tokens[i+1].Type() != token.DocumentHeaderType { |
| 598 | return nil, errors.ErrSyntax("unexpected directive value. document not started", tk.RawToken()) |
| 599 | } |
| 600 | if len(valueTks) != 0 { |
| 601 | ret = append(ret, &Token{ |
| 602 | Group: &TokenGroup{ |
| 603 | Type: TokenGroupDirective, |
| 604 | Tokens: append([]*Token{directiveName}, valueTks...), |
| 605 | }, |
| 606 | }) |
| 607 | } else { |
| 608 | ret = append(ret, directiveName) |
| 609 | } |
| 610 | default: |
| 611 | ret = append(ret, tk) |
| 612 | } |
| 613 | } |
| 614 | return ret, nil |
| 615 | } |
| 616 | |
| 617 | func createDocumentTokens(tokens []*Token) ([]*Token, error) { |
| 618 | var ret []*Token |
| 619 | for i := 0; i < len(tokens); i++ { |
| 620 | tk := tokens[i] |
| 621 | switch tk.Type() { |
| 622 | case token.DocumentHeaderType: |
| 623 | if i != 0 { |
| 624 | ret = append(ret, &Token{ |
| 625 | Group: &TokenGroup{Tokens: tokens[:i]}, |
| 626 | }) |
| 627 | } |
| 628 | if i+1 == len(tokens) { |
| 629 | // if current token is last token, add DocumentHeader only tokens to ret. |
| 630 | return append(ret, &Token{ |
| 631 | Group: &TokenGroup{ |
| 632 | Type: TokenGroupDocument, |
| 633 | Tokens: []*Token{tk}, |
| 634 | }, |
| 635 | }), nil |
| 636 | } |
| 637 | if tokens[i+1].Type() == token.DocumentHeaderType { |
| 638 | return append(ret, &Token{ |
| 639 | Group: &TokenGroup{ |
| 640 | Type: TokenGroupDocument, |
| 641 | Tokens: []*Token{tk}, |
| 642 | }, |
| 643 | }), nil |
| 644 | } |
| 645 | if tokens[i].Line() == tokens[i+1].Line() { |
| 646 | switch tokens[i+1].GroupType() { |
| 647 | case TokenGroupMapKey, TokenGroupMapKeyValue: |
| 648 | return nil, errors.ErrSyntax("value cannot be placed after document separator", tokens[i+1].RawToken()) |
| 649 | } |
| 650 | switch tokens[i+1].Type() { |
| 651 | case token.SequenceEntryType: |
| 652 | return nil, errors.ErrSyntax("value cannot be placed after document separator", tokens[i+1].RawToken()) |
| 653 | } |
| 654 | } |
| 655 | tks, err := createDocumentTokens(tokens[i+1:]) |
| 656 | if err != nil { |
| 657 | return nil, err |
| 658 | } |
| 659 | if len(tks) != 0 { |
| 660 | tks[0].SetGroupType(TokenGroupDocument) |
| 661 | tks[0].Group.Tokens = append([]*Token{tk}, tks[0].Group.Tokens...) |
| 662 | return append(ret, tks...), nil |
| 663 | } |
| 664 | return append(ret, &Token{ |
| 665 | Group: &TokenGroup{ |
| 666 | Type: TokenGroupDocument, |
| 667 | Tokens: []*Token{tk}, |
| 668 | }, |
| 669 | }), nil |
| 670 | case token.DocumentEndType: |
| 671 | if i != 0 { |
| 672 | ret = append(ret, &Token{ |
| 673 | Group: &TokenGroup{ |
| 674 | Type: TokenGroupDocument, |
| 675 | Tokens: tokens[0 : i+1], |
| 676 | }, |
| 677 | }) |
| 678 | } |
| 679 | if i+1 == len(tokens) { |
| 680 | return ret, nil |
| 681 | } |
| 682 | if isScalarType(tokens[i+1]) { |
| 683 | return nil, errors.ErrSyntax("unexpected end content", tokens[i+1].RawToken()) |
| 684 | } |
| 685 | |
| 686 | tks, err := createDocumentTokens(tokens[i+1:]) |
| 687 | if err != nil { |
| 688 | return nil, err |
| 689 | } |
| 690 | return append(ret, tks...), nil |
| 691 | } |
| 692 | } |
| 693 | return append(ret, &Token{ |
| 694 | Group: &TokenGroup{ |
| 695 | Type: TokenGroupDocument, |
| 696 | Tokens: tokens, |
| 697 | }, |
| 698 | }), nil |
| 699 | } |
| 700 | |
| 701 | func isScalarType(tk *Token) bool { |
| 702 | switch tk.GroupType() { |
| 703 | case TokenGroupMapKey, TokenGroupMapKeyValue: |
| 704 | return false |
| 705 | } |
| 706 | typ := tk.Type() |
| 707 | return typ == token.AnchorType || |
| 708 | typ == token.AliasType || |
| 709 | typ == token.LiteralType || |
| 710 | typ == token.FoldedType || |
| 711 | typ == token.NullType || |
| 712 | typ == token.ImplicitNullType || |
| 713 | typ == token.BoolType || |
| 714 | typ == token.IntegerType || |
| 715 | typ == token.BinaryIntegerType || |
| 716 | typ == token.OctetIntegerType || |
| 717 | typ == token.HexIntegerType || |
| 718 | typ == token.FloatType || |
| 719 | typ == token.InfinityType || |
| 720 | typ == token.NanType || |
| 721 | typ == token.StringType || |
| 722 | typ == token.SingleQuoteType || |
| 723 | typ == token.DoubleQuoteType |
| 724 | } |
| 725 | |
| 726 | func isNotMapKeyType(tk *Token) bool { |
| 727 | typ := tk.Type() |
| 728 | return typ == token.DirectiveType || |
| 729 | typ == token.DocumentHeaderType || |
| 730 | typ == token.DocumentEndType || |
| 731 | typ == token.CollectEntryType || |
| 732 | typ == token.MappingStartType || |
| 733 | typ == token.MappingValueType || |
| 734 | typ == token.MappingEndType || |
| 735 | typ == token.SequenceStartType || |
| 736 | typ == token.SequenceEntryType || |
| 737 | typ == token.SequenceEndType |
| 738 | } |
| 739 | |
| 740 | func isFlowType(tk *Token) bool { |
| 741 | typ := tk.Type() |
| 742 | return typ == token.MappingStartType || |
| 743 | typ == token.MappingEndType || |
| 744 | typ == token.SequenceStartType || |
| 745 | typ == token.SequenceEntryType |
| 746 | } |