| 1 | package token |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | // Character type for character |
| 12 | type Character byte |
| 13 | |
| 14 | const ( |
| 15 | // SequenceEntryCharacter character for sequence entry |
| 16 | SequenceEntryCharacter Character = '-' |
| 17 | // MappingKeyCharacter character for mapping key |
| 18 | MappingKeyCharacter Character = '?' |
| 19 | // MappingValueCharacter character for mapping value |
| 20 | MappingValueCharacter Character = ':' |
| 21 | // CollectEntryCharacter character for collect entry |
| 22 | CollectEntryCharacter Character = ',' |
| 23 | // SequenceStartCharacter character for sequence start |
| 24 | SequenceStartCharacter Character = '[' |
| 25 | // SequenceEndCharacter character for sequence end |
| 26 | SequenceEndCharacter Character = ']' |
| 27 | // MappingStartCharacter character for mapping start |
| 28 | MappingStartCharacter Character = '{' |
| 29 | // MappingEndCharacter character for mapping end |
| 30 | MappingEndCharacter Character = '}' |
| 31 | // CommentCharacter character for comment |
| 32 | CommentCharacter Character = '#' |
| 33 | // AnchorCharacter character for anchor |
| 34 | AnchorCharacter Character = '&' |
| 35 | // AliasCharacter character for alias |
| 36 | AliasCharacter Character = '*' |
| 37 | // TagCharacter character for tag |
| 38 | TagCharacter Character = '!' |
| 39 | // LiteralCharacter character for literal |
| 40 | LiteralCharacter Character = '|' |
| 41 | // FoldedCharacter character for folded |
| 42 | FoldedCharacter Character = '>' |
| 43 | // SingleQuoteCharacter character for single quote |
| 44 | SingleQuoteCharacter Character = '\'' |
| 45 | // DoubleQuoteCharacter character for double quote |
| 46 | DoubleQuoteCharacter Character = '"' |
| 47 | // DirectiveCharacter character for directive |
| 48 | DirectiveCharacter Character = '%' |
| 49 | // SpaceCharacter character for space |
| 50 | SpaceCharacter Character = ' ' |
| 51 | // LineBreakCharacter character for line break |
| 52 | LineBreakCharacter Character = '\n' |
| 53 | ) |
| 54 | |
| 55 | // Type type identifier for token |
| 56 | type Type int |
| 57 | |
| 58 | const ( |
| 59 | // UnknownType reserve for invalid type |
| 60 | UnknownType Type = iota |
| 61 | // DocumentHeaderType type for DocumentHeader token |
| 62 | DocumentHeaderType |
| 63 | // DocumentEndType type for DocumentEnd token |
| 64 | DocumentEndType |
| 65 | // SequenceEntryType type for SequenceEntry token |
| 66 | SequenceEntryType |
| 67 | // MappingKeyType type for MappingKey token |
| 68 | MappingKeyType |
| 69 | // MappingValueType type for MappingValue token |
| 70 | MappingValueType |
| 71 | // MergeKeyType type for MergeKey token |
| 72 | MergeKeyType |
| 73 | // CollectEntryType type for CollectEntry token |
| 74 | CollectEntryType |
| 75 | // SequenceStartType type for SequenceStart token |
| 76 | SequenceStartType |
| 77 | // SequenceEndType type for SequenceEnd token |
| 78 | SequenceEndType |
| 79 | // MappingStartType type for MappingStart token |
| 80 | MappingStartType |
| 81 | // MappingEndType type for MappingEnd token |
| 82 | MappingEndType |
| 83 | // CommentType type for Comment token |
| 84 | CommentType |
| 85 | // AnchorType type for Anchor token |
| 86 | AnchorType |
| 87 | // AliasType type for Alias token |
| 88 | AliasType |
| 89 | // TagType type for Tag token |
| 90 | TagType |
| 91 | // LiteralType type for Literal token |
| 92 | LiteralType |
| 93 | // FoldedType type for Folded token |
| 94 | FoldedType |
| 95 | // SingleQuoteType type for SingleQuote token |
| 96 | SingleQuoteType |
| 97 | // DoubleQuoteType type for DoubleQuote token |
| 98 | DoubleQuoteType |
| 99 | // DirectiveType type for Directive token |
| 100 | DirectiveType |
| 101 | // SpaceType type for Space token |
| 102 | SpaceType |
| 103 | // NullType type for Null token |
| 104 | NullType |
| 105 | // ImplicitNullType type for implicit Null token. |
| 106 | // This is used when explicit keywords such as null or ~ are not specified. |
| 107 | // It is distinguished during encoding and output as an empty string. |
| 108 | ImplicitNullType |
| 109 | // InfinityType type for Infinity token |
| 110 | InfinityType |
| 111 | // NanType type for Nan token |
| 112 | NanType |
| 113 | // IntegerType type for Integer token |
| 114 | IntegerType |
| 115 | // BinaryIntegerType type for BinaryInteger token |
| 116 | BinaryIntegerType |
| 117 | // OctetIntegerType type for OctetInteger token |
| 118 | OctetIntegerType |
| 119 | // HexIntegerType type for HexInteger token |
| 120 | HexIntegerType |
| 121 | // FloatType type for Float token |
| 122 | FloatType |
| 123 | // StringType type for String token |
| 124 | StringType |
| 125 | // BoolType type for Bool token |
| 126 | BoolType |
| 127 | // InvalidType type for invalid token |
| 128 | InvalidType |
| 129 | ) |
| 130 | |
| 131 | // String type identifier to text |
| 132 | func (t Type) String() string { |
| 133 | switch t { |
| 134 | case UnknownType: |
| 135 | return "Unknown" |
| 136 | case DocumentHeaderType: |
| 137 | return "DocumentHeader" |
| 138 | case DocumentEndType: |
| 139 | return "DocumentEnd" |
| 140 | case SequenceEntryType: |
| 141 | return "SequenceEntry" |
| 142 | case MappingKeyType: |
| 143 | return "MappingKey" |
| 144 | case MappingValueType: |
| 145 | return "MappingValue" |
| 146 | case MergeKeyType: |
| 147 | return "MergeKey" |
| 148 | case CollectEntryType: |
| 149 | return "CollectEntry" |
| 150 | case SequenceStartType: |
| 151 | return "SequenceStart" |
| 152 | case SequenceEndType: |
| 153 | return "SequenceEnd" |
| 154 | case MappingStartType: |
| 155 | return "MappingStart" |
| 156 | case MappingEndType: |
| 157 | return "MappingEnd" |
| 158 | case CommentType: |
| 159 | return "Comment" |
| 160 | case AnchorType: |
| 161 | return "Anchor" |
| 162 | case AliasType: |
| 163 | return "Alias" |
| 164 | case TagType: |
| 165 | return "Tag" |
| 166 | case LiteralType: |
| 167 | return "Literal" |
| 168 | case FoldedType: |
| 169 | return "Folded" |
| 170 | case SingleQuoteType: |
| 171 | return "SingleQuote" |
| 172 | case DoubleQuoteType: |
| 173 | return "DoubleQuote" |
| 174 | case DirectiveType: |
| 175 | return "Directive" |
| 176 | case SpaceType: |
| 177 | return "Space" |
| 178 | case StringType: |
| 179 | return "String" |
| 180 | case BoolType: |
| 181 | return "Bool" |
| 182 | case IntegerType: |
| 183 | return "Integer" |
| 184 | case BinaryIntegerType: |
| 185 | return "BinaryInteger" |
| 186 | case OctetIntegerType: |
| 187 | return "OctetInteger" |
| 188 | case HexIntegerType: |
| 189 | return "HexInteger" |
| 190 | case FloatType: |
| 191 | return "Float" |
| 192 | case NullType: |
| 193 | return "Null" |
| 194 | case ImplicitNullType: |
| 195 | return "ImplicitNull" |
| 196 | case InfinityType: |
| 197 | return "Infinity" |
| 198 | case NanType: |
| 199 | return "Nan" |
| 200 | case InvalidType: |
| 201 | return "Invalid" |
| 202 | } |
| 203 | return "" |
| 204 | } |
| 205 | |
| 206 | // CharacterType type for character category |
| 207 | type CharacterType int |
| 208 | |
| 209 | const ( |
| 210 | // CharacterTypeIndicator type of indicator character |
| 211 | CharacterTypeIndicator CharacterType = iota |
| 212 | // CharacterTypeWhiteSpace type of white space character |
| 213 | CharacterTypeWhiteSpace |
| 214 | // CharacterTypeMiscellaneous type of miscellaneous character |
| 215 | CharacterTypeMiscellaneous |
| 216 | // CharacterTypeEscaped type of escaped character |
| 217 | CharacterTypeEscaped |
| 218 | // CharacterTypeInvalid type for a invalid token. |
| 219 | CharacterTypeInvalid |
| 220 | ) |
| 221 | |
| 222 | // String character type identifier to text |
| 223 | func (c CharacterType) String() string { |
| 224 | switch c { |
| 225 | case CharacterTypeIndicator: |
| 226 | return "Indicator" |
| 227 | case CharacterTypeWhiteSpace: |
| 228 | return "WhiteSpace" |
| 229 | case CharacterTypeMiscellaneous: |
| 230 | return "Miscellaneous" |
| 231 | case CharacterTypeEscaped: |
| 232 | return "Escaped" |
| 233 | } |
| 234 | return "" |
| 235 | } |
| 236 | |
| 237 | // Indicator type for indicator |
| 238 | type Indicator int |
| 239 | |
| 240 | const ( |
| 241 | // NotIndicator not indicator |
| 242 | NotIndicator Indicator = iota |
| 243 | // BlockStructureIndicator indicator for block structure ( '-', '?', ':' ) |
| 244 | BlockStructureIndicator |
| 245 | // FlowCollectionIndicator indicator for flow collection ( '[', ']', '{', '}', ',' ) |
| 246 | FlowCollectionIndicator |
| 247 | // CommentIndicator indicator for comment ( '#' ) |
| 248 | CommentIndicator |
| 249 | // NodePropertyIndicator indicator for node property ( '!', '&', '*' ) |
| 250 | NodePropertyIndicator |
| 251 | // BlockScalarIndicator indicator for block scalar ( '|', '>' ) |
| 252 | BlockScalarIndicator |
| 253 | // QuotedScalarIndicator indicator for quoted scalar ( ''', '"' ) |
| 254 | QuotedScalarIndicator |
| 255 | // DirectiveIndicator indicator for directive ( '%' ) |
| 256 | DirectiveIndicator |
| 257 | // InvalidUseOfReservedIndicator indicator for invalid use of reserved keyword ( '@', '`' ) |
| 258 | InvalidUseOfReservedIndicator |
| 259 | ) |
| 260 | |
| 261 | // String indicator to text |
| 262 | func (i Indicator) String() string { |
| 263 | switch i { |
| 264 | case NotIndicator: |
| 265 | return "NotIndicator" |
| 266 | case BlockStructureIndicator: |
| 267 | return "BlockStructure" |
| 268 | case FlowCollectionIndicator: |
| 269 | return "FlowCollection" |
| 270 | case CommentIndicator: |
| 271 | return "Comment" |
| 272 | case NodePropertyIndicator: |
| 273 | return "NodeProperty" |
| 274 | case BlockScalarIndicator: |
| 275 | return "BlockScalar" |
| 276 | case QuotedScalarIndicator: |
| 277 | return "QuotedScalar" |
| 278 | case DirectiveIndicator: |
| 279 | return "Directive" |
| 280 | case InvalidUseOfReservedIndicator: |
| 281 | return "InvalidUseOfReserved" |
| 282 | } |
| 283 | return "" |
| 284 | } |
| 285 | |
| 286 | var ( |
| 287 | reservedNullKeywords = []string{ |
| 288 | "null", |
| 289 | "Null", |
| 290 | "NULL", |
| 291 | "~", |
| 292 | } |
| 293 | reservedBoolKeywords = []string{ |
| 294 | "true", |
| 295 | "True", |
| 296 | "TRUE", |
| 297 | "false", |
| 298 | "False", |
| 299 | "FALSE", |
| 300 | } |
| 301 | // For compatibility with other YAML 1.1 parsers |
| 302 | // Note that we use these solely for encoding the bool value with quotes. |
| 303 | // go-yaml should not treat these as reserved keywords at parsing time. |
| 304 | // as go-yaml is supposed to be compliant only with YAML 1.2. |
| 305 | reservedLegacyBoolKeywords = []string{ |
| 306 | "y", |
| 307 | "Y", |
| 308 | "yes", |
| 309 | "Yes", |
| 310 | "YES", |
| 311 | "n", |
| 312 | "N", |
| 313 | "no", |
| 314 | "No", |
| 315 | "NO", |
| 316 | "on", |
| 317 | "On", |
| 318 | "ON", |
| 319 | "off", |
| 320 | "Off", |
| 321 | "OFF", |
| 322 | } |
| 323 | reservedInfKeywords = []string{ |
| 324 | ".inf", |
| 325 | ".Inf", |
| 326 | ".INF", |
| 327 | "-.inf", |
| 328 | "-.Inf", |
| 329 | "-.INF", |
| 330 | } |
| 331 | reservedNanKeywords = []string{ |
| 332 | ".nan", |
| 333 | ".NaN", |
| 334 | ".NAN", |
| 335 | } |
| 336 | reservedKeywordMap = map[string]func(string, string, *Position) *Token{} |
| 337 | // reservedEncKeywordMap contains is the keyword map used at encoding time. |
| 338 | // This is supposed to be a superset of reservedKeywordMap, |
| 339 | // and used to quote legacy keywords present in YAML 1.1 or lesser for compatibility reasons, |
| 340 | // even though this library is supposed to be YAML 1.2-compliant. |
| 341 | reservedEncKeywordMap = map[string]func(string, string, *Position) *Token{} |
| 342 | ) |
| 343 | |
| 344 | func reservedKeywordToken(typ Type, value, org string, pos *Position) *Token { |
| 345 | return &Token{ |
| 346 | Type: typ, |
| 347 | CharacterType: CharacterTypeMiscellaneous, |
| 348 | Indicator: NotIndicator, |
| 349 | Value: value, |
| 350 | Origin: org, |
| 351 | Position: pos, |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | func init() { |
| 356 | for _, keyword := range reservedNullKeywords { |
| 357 | f := func(value, org string, pos *Position) *Token { |
| 358 | return reservedKeywordToken(NullType, value, org, pos) |
| 359 | } |
| 360 | |
| 361 | reservedKeywordMap[keyword] = f |
| 362 | reservedEncKeywordMap[keyword] = f |
| 363 | } |
| 364 | for _, keyword := range reservedBoolKeywords { |
| 365 | f := func(value, org string, pos *Position) *Token { |
| 366 | return reservedKeywordToken(BoolType, value, org, pos) |
| 367 | } |
| 368 | reservedKeywordMap[keyword] = f |
| 369 | reservedEncKeywordMap[keyword] = f |
| 370 | } |
| 371 | for _, keyword := range reservedLegacyBoolKeywords { |
| 372 | reservedEncKeywordMap[keyword] = func(value, org string, pos *Position) *Token { |
| 373 | return reservedKeywordToken(BoolType, value, org, pos) |
| 374 | } |
| 375 | } |
| 376 | for _, keyword := range reservedInfKeywords { |
| 377 | reservedKeywordMap[keyword] = func(value, org string, pos *Position) *Token { |
| 378 | return reservedKeywordToken(InfinityType, value, org, pos) |
| 379 | } |
| 380 | } |
| 381 | for _, keyword := range reservedNanKeywords { |
| 382 | reservedKeywordMap[keyword] = func(value, org string, pos *Position) *Token { |
| 383 | return reservedKeywordToken(NanType, value, org, pos) |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | // ReservedTagKeyword type of reserved tag keyword |
| 389 | type ReservedTagKeyword string |
| 390 | |
| 391 | const ( |
| 392 | // IntegerTag `!!int` tag |
| 393 | IntegerTag ReservedTagKeyword = "!!int" |
| 394 | // FloatTag `!!float` tag |
| 395 | FloatTag ReservedTagKeyword = "!!float" |
| 396 | // NullTag `!!null` tag |
| 397 | NullTag ReservedTagKeyword = "!!null" |
| 398 | // SequenceTag `!!seq` tag |
| 399 | SequenceTag ReservedTagKeyword = "!!seq" |
| 400 | // MappingTag `!!map` tag |
| 401 | MappingTag ReservedTagKeyword = "!!map" |
| 402 | // StringTag `!!str` tag |
| 403 | StringTag ReservedTagKeyword = "!!str" |
| 404 | // BinaryTag `!!binary` tag |
| 405 | BinaryTag ReservedTagKeyword = "!!binary" |
| 406 | // OrderedMapTag `!!omap` tag |
| 407 | OrderedMapTag ReservedTagKeyword = "!!omap" |
| 408 | // SetTag `!!set` tag |
| 409 | SetTag ReservedTagKeyword = "!!set" |
| 410 | // TimestampTag `!!timestamp` tag |
| 411 | TimestampTag ReservedTagKeyword = "!!timestamp" |
| 412 | // BooleanTag `!!bool` tag |
| 413 | BooleanTag ReservedTagKeyword = "!!bool" |
| 414 | // MergeTag `!!merge` tag |
| 415 | MergeTag ReservedTagKeyword = "!!merge" |
| 416 | ) |
| 417 | |
| 418 | var ( |
| 419 | // ReservedTagKeywordMap map for reserved tag keywords |
| 420 | ReservedTagKeywordMap = map[ReservedTagKeyword]func(string, string, *Position) *Token{ |
| 421 | IntegerTag: func(value, org string, pos *Position) *Token { |
| 422 | return &Token{ |
| 423 | Type: TagType, |
| 424 | CharacterType: CharacterTypeIndicator, |
| 425 | Indicator: NodePropertyIndicator, |
| 426 | Value: value, |
| 427 | Origin: org, |
| 428 | Position: pos, |
| 429 | } |
| 430 | }, |
| 431 | FloatTag: func(value, org string, pos *Position) *Token { |
| 432 | return &Token{ |
| 433 | Type: TagType, |
| 434 | CharacterType: CharacterTypeIndicator, |
| 435 | Indicator: NodePropertyIndicator, |
| 436 | Value: value, |
| 437 | Origin: org, |
| 438 | Position: pos, |
| 439 | } |
| 440 | }, |
| 441 | NullTag: func(value, org string, pos *Position) *Token { |
| 442 | return &Token{ |
| 443 | Type: TagType, |
| 444 | CharacterType: CharacterTypeIndicator, |
| 445 | Indicator: NodePropertyIndicator, |
| 446 | Value: value, |
| 447 | Origin: org, |
| 448 | Position: pos, |
| 449 | } |
| 450 | }, |
| 451 | SequenceTag: func(value, org string, pos *Position) *Token { |
| 452 | return &Token{ |
| 453 | Type: TagType, |
| 454 | CharacterType: CharacterTypeIndicator, |
| 455 | Indicator: NodePropertyIndicator, |
| 456 | Value: value, |
| 457 | Origin: org, |
| 458 | Position: pos, |
| 459 | } |
| 460 | }, |
| 461 | MappingTag: func(value, org string, pos *Position) *Token { |
| 462 | return &Token{ |
| 463 | Type: TagType, |
| 464 | CharacterType: CharacterTypeIndicator, |
| 465 | Indicator: NodePropertyIndicator, |
| 466 | Value: value, |
| 467 | Origin: org, |
| 468 | Position: pos, |
| 469 | } |
| 470 | }, |
| 471 | StringTag: func(value, org string, pos *Position) *Token { |
| 472 | return &Token{ |
| 473 | Type: TagType, |
| 474 | CharacterType: CharacterTypeIndicator, |
| 475 | Indicator: NodePropertyIndicator, |
| 476 | Value: value, |
| 477 | Origin: org, |
| 478 | Position: pos, |
| 479 | } |
| 480 | }, |
| 481 | BinaryTag: func(value, org string, pos *Position) *Token { |
| 482 | return &Token{ |
| 483 | Type: TagType, |
| 484 | CharacterType: CharacterTypeIndicator, |
| 485 | Indicator: NodePropertyIndicator, |
| 486 | Value: value, |
| 487 | Origin: org, |
| 488 | Position: pos, |
| 489 | } |
| 490 | }, |
| 491 | OrderedMapTag: func(value, org string, pos *Position) *Token { |
| 492 | return &Token{ |
| 493 | Type: TagType, |
| 494 | CharacterType: CharacterTypeIndicator, |
| 495 | Indicator: NodePropertyIndicator, |
| 496 | Value: value, |
| 497 | Origin: org, |
| 498 | Position: pos, |
| 499 | } |
| 500 | }, |
| 501 | SetTag: func(value, org string, pos *Position) *Token { |
| 502 | return &Token{ |
| 503 | Type: TagType, |
| 504 | CharacterType: CharacterTypeIndicator, |
| 505 | Indicator: NodePropertyIndicator, |
| 506 | Value: value, |
| 507 | Origin: org, |
| 508 | Position: pos, |
| 509 | } |
| 510 | }, |
| 511 | TimestampTag: func(value, org string, pos *Position) *Token { |
| 512 | return &Token{ |
| 513 | Type: TagType, |
| 514 | CharacterType: CharacterTypeIndicator, |
| 515 | Indicator: NodePropertyIndicator, |
| 516 | Value: value, |
| 517 | Origin: org, |
| 518 | Position: pos, |
| 519 | } |
| 520 | }, |
| 521 | BooleanTag: func(value, org string, pos *Position) *Token { |
| 522 | return &Token{ |
| 523 | Type: TagType, |
| 524 | CharacterType: CharacterTypeIndicator, |
| 525 | Indicator: NodePropertyIndicator, |
| 526 | Value: value, |
| 527 | Origin: org, |
| 528 | Position: pos, |
| 529 | } |
| 530 | }, |
| 531 | MergeTag: func(value, org string, pos *Position) *Token { |
| 532 | return &Token{ |
| 533 | Type: TagType, |
| 534 | CharacterType: CharacterTypeIndicator, |
| 535 | Indicator: NodePropertyIndicator, |
| 536 | Value: value, |
| 537 | Origin: org, |
| 538 | Position: pos, |
| 539 | } |
| 540 | }, |
| 541 | } |
| 542 | ) |
| 543 | |
| 544 | type NumberType string |
| 545 | |
| 546 | const ( |
| 547 | NumberTypeDecimal NumberType = "decimal" |
| 548 | NumberTypeBinary NumberType = "binary" |
| 549 | NumberTypeOctet NumberType = "octet" |
| 550 | NumberTypeHex NumberType = "hex" |
| 551 | NumberTypeFloat NumberType = "float" |
| 552 | ) |
| 553 | |
| 554 | type NumberValue struct { |
| 555 | Type NumberType |
| 556 | Value any |
| 557 | Text string |
| 558 | } |
| 559 | |
| 560 | func ToNumber(value string) *NumberValue { |
| 561 | num, err := toNumber(value) |
| 562 | if err != nil { |
| 563 | return nil |
| 564 | } |
| 565 | return num |
| 566 | } |
| 567 | |
| 568 | func isNumber(value string) bool { |
| 569 | num, err := toNumber(value) |
| 570 | if err != nil { |
| 571 | var numErr *strconv.NumError |
| 572 | if errors.As(err, &numErr) && errors.Is(numErr.Err, strconv.ErrRange) { |
| 573 | return true |
| 574 | } |
| 575 | return false |
| 576 | } |
| 577 | return num != nil |
| 578 | } |
| 579 | |
| 580 | func toNumber(value string) (*NumberValue, error) { |
| 581 | if len(value) == 0 { |
| 582 | return nil, nil |
| 583 | } |
| 584 | if strings.HasPrefix(value, "_") { |
| 585 | return nil, nil |
| 586 | } |
| 587 | dotCount := strings.Count(value, ".") |
| 588 | if dotCount > 1 { |
| 589 | return nil, nil |
| 590 | } |
| 591 | |
| 592 | isNegative := strings.HasPrefix(value, "-") |
| 593 | normalized := strings.ReplaceAll(strings.TrimPrefix(strings.TrimPrefix(value, "+"), "-"), "_", "") |
| 594 | |
| 595 | var ( |
| 596 | typ NumberType |
| 597 | base int |
| 598 | ) |
| 599 | switch { |
| 600 | case strings.HasPrefix(normalized, "0x"): |
| 601 | normalized = strings.TrimPrefix(normalized, "0x") |
| 602 | base = 16 |
| 603 | typ = NumberTypeHex |
| 604 | case strings.HasPrefix(normalized, "0o"): |
| 605 | normalized = strings.TrimPrefix(normalized, "0o") |
| 606 | base = 8 |
| 607 | typ = NumberTypeOctet |
| 608 | case strings.HasPrefix(normalized, "0b"): |
| 609 | normalized = strings.TrimPrefix(normalized, "0b") |
| 610 | base = 2 |
| 611 | typ = NumberTypeBinary |
| 612 | case strings.HasPrefix(normalized, "0") && len(normalized) > 1 && dotCount == 0: |
| 613 | base = 8 |
| 614 | typ = NumberTypeOctet |
| 615 | case dotCount == 1: |
| 616 | typ = NumberTypeFloat |
| 617 | default: |
| 618 | typ = NumberTypeDecimal |
| 619 | base = 10 |
| 620 | } |
| 621 | |
| 622 | text := normalized |
| 623 | if isNegative { |
| 624 | text = "-" + text |
| 625 | } |
| 626 | |
| 627 | var v any |
| 628 | if typ == NumberTypeFloat { |
| 629 | f, err := strconv.ParseFloat(text, 64) |
| 630 | if err != nil { |
| 631 | return nil, err |
| 632 | } |
| 633 | v = f |
| 634 | } else if isNegative { |
| 635 | i, err := strconv.ParseInt(text, base, 64) |
| 636 | if err != nil { |
| 637 | return nil, err |
| 638 | } |
| 639 | v = i |
| 640 | } else { |
| 641 | u, err := strconv.ParseUint(text, base, 64) |
| 642 | if err != nil { |
| 643 | return nil, err |
| 644 | } |
| 645 | v = u |
| 646 | } |
| 647 | |
| 648 | return &NumberValue{ |
| 649 | Type: typ, |
| 650 | Value: v, |
| 651 | Text: text, |
| 652 | }, nil |
| 653 | } |
| 654 | |
| 655 | // This is a subset of the formats permitted by the regular expression |
| 656 | // defined at http://yaml.org/type/timestamp.html. Note that time.Parse |
| 657 | // cannot handle: "2001-12-14 21:59:43.10 -5" from the examples. |
| 658 | var timestampFormats = []string{ |
| 659 | time.RFC3339Nano, |
| 660 | "2006-01-02t15:04:05.999999999Z07:00", // RFC3339Nano with lower-case "t". |
| 661 | time.DateTime, |
| 662 | time.DateOnly, |
| 663 | |
| 664 | // Not in examples, but to preserve backward compatibility by quoting time values. |
| 665 | "15:4", |
| 666 | } |
| 667 | |
| 668 | func isTimestamp(value string) bool { |
| 669 | for _, format := range timestampFormats { |
| 670 | if _, err := time.Parse(format, value); err == nil { |
| 671 | return true |
| 672 | } |
| 673 | } |
| 674 | return false |
| 675 | } |
| 676 | |
| 677 | // IsNeedQuoted checks whether the value needs quote for passed string or not |
| 678 | func IsNeedQuoted(value string) bool { |
| 679 | if value == "" { |
| 680 | return true |
| 681 | } |
| 682 | if _, exists := reservedEncKeywordMap[value]; exists { |
| 683 | return true |
| 684 | } |
| 685 | if isNumber(value) { |
| 686 | return true |
| 687 | } |
| 688 | if value == "-" { |
| 689 | return true |
| 690 | } |
| 691 | first := value[0] |
| 692 | switch first { |
| 693 | case '*', '&', '[', '{', '}', ']', ',', '!', '|', '>', '%', '\'', '"', '@', ' ', '`': |
| 694 | return true |
| 695 | } |
| 696 | last := value[len(value)-1] |
| 697 | switch last { |
| 698 | case ':', ' ': |
| 699 | return true |
| 700 | } |
| 701 | if isTimestamp(value) { |
| 702 | return true |
| 703 | } |
| 704 | for i, c := range value { |
| 705 | switch c { |
| 706 | case '#', '\\': |
| 707 | return true |
| 708 | case ':', '-': |
| 709 | if i+1 < len(value) && value[i+1] == ' ' { |
| 710 | return true |
| 711 | } |
| 712 | } |
| 713 | } |
| 714 | return false |
| 715 | } |
| 716 | |
| 717 | // LiteralBlockHeader detect literal block scalar header |
| 718 | func LiteralBlockHeader(value string) string { |
| 719 | lbc := DetectLineBreakCharacter(value) |
| 720 | |
| 721 | switch { |
| 722 | case !strings.Contains(value, lbc): |
| 723 | return "" |
| 724 | case strings.HasSuffix(value, fmt.Sprintf("%s%s", lbc, lbc)): |
| 725 | return "|+" |
| 726 | case strings.HasSuffix(value, lbc): |
| 727 | return "|" |
| 728 | default: |
| 729 | return "|-" |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | // New create reserved keyword token or number token and other string token. |
| 734 | func New(value string, org string, pos *Position) *Token { |
| 735 | fn := reservedKeywordMap[value] |
| 736 | if fn != nil { |
| 737 | return fn(value, org, pos) |
| 738 | } |
| 739 | if num := ToNumber(value); num != nil { |
| 740 | tk := &Token{ |
| 741 | Type: IntegerType, |
| 742 | CharacterType: CharacterTypeMiscellaneous, |
| 743 | Indicator: NotIndicator, |
| 744 | Value: value, |
| 745 | Origin: org, |
| 746 | Position: pos, |
| 747 | } |
| 748 | switch num.Type { |
| 749 | case NumberTypeFloat: |
| 750 | tk.Type = FloatType |
| 751 | case NumberTypeBinary: |
| 752 | tk.Type = BinaryIntegerType |
| 753 | case NumberTypeOctet: |
| 754 | tk.Type = OctetIntegerType |
| 755 | case NumberTypeHex: |
| 756 | tk.Type = HexIntegerType |
| 757 | } |
| 758 | return tk |
| 759 | } |
| 760 | return String(value, org, pos) |
| 761 | } |
| 762 | |
| 763 | // Position type for position in YAML document |
| 764 | type Position struct { |
| 765 | Line int |
| 766 | Column int |
| 767 | Offset int |
| 768 | IndentNum int |
| 769 | IndentLevel int |
| 770 | } |
| 771 | |
| 772 | // String position to text |
| 773 | func (p *Position) String() string { |
| 774 | return fmt.Sprintf("[level:%d,line:%d,column:%d,offset:%d]", p.IndentLevel, p.Line, p.Column, p.Offset) |
| 775 | } |
| 776 | |
| 777 | // Token type for token |
| 778 | type Token struct { |
| 779 | // Type is a token type. |
| 780 | Type Type |
| 781 | // CharacterType is a character type. |
| 782 | CharacterType CharacterType |
| 783 | // Indicator is a indicator type. |
| 784 | Indicator Indicator |
| 785 | // Value is a string extracted with only meaningful characters, with spaces and such removed. |
| 786 | Value string |
| 787 | // Origin is a string that stores the original text as-is. |
| 788 | Origin string |
| 789 | // Error keeps error message for InvalidToken. |
| 790 | Error string |
| 791 | // Position is a token position. |
| 792 | Position *Position |
| 793 | // Next is a next token reference. |
| 794 | Next *Token |
| 795 | // Prev is a previous token reference. |
| 796 | Prev *Token |
| 797 | } |
| 798 | |
| 799 | // PreviousType previous token type |
| 800 | func (t *Token) PreviousType() Type { |
| 801 | if t.Prev != nil { |
| 802 | return t.Prev.Type |
| 803 | } |
| 804 | return UnknownType |
| 805 | } |
| 806 | |
| 807 | // NextType next token type |
| 808 | func (t *Token) NextType() Type { |
| 809 | if t.Next != nil { |
| 810 | return t.Next.Type |
| 811 | } |
| 812 | return UnknownType |
| 813 | } |
| 814 | |
| 815 | // AddColumn append column number to current position of column |
| 816 | func (t *Token) AddColumn(col int) { |
| 817 | if t == nil { |
| 818 | return |
| 819 | } |
| 820 | t.Position.Column += col |
| 821 | } |
| 822 | |
| 823 | // Clone copy token ( preserve Prev/Next reference ) |
| 824 | func (t *Token) Clone() *Token { |
| 825 | if t == nil { |
| 826 | return nil |
| 827 | } |
| 828 | copied := *t |
| 829 | if t.Position != nil { |
| 830 | pos := *(t.Position) |
| 831 | copied.Position = &pos |
| 832 | } |
| 833 | return &copied |
| 834 | } |
| 835 | |
| 836 | // Dump outputs token information to stdout for debugging. |
| 837 | func (t *Token) Dump() { |
| 838 | fmt.Printf( |
| 839 | "[TYPE]:%q [CHARTYPE]:%q [INDICATOR]:%q [VALUE]:%q [ORG]:%q [POS(line:column:level:offset)]: %d:%d:%d:%d\n", |
| 840 | t.Type, t.CharacterType, t.Indicator, t.Value, t.Origin, t.Position.Line, t.Position.Column, t.Position.IndentLevel, t.Position.Offset, |
| 841 | ) |
| 842 | } |
| 843 | |
| 844 | // Tokens type of token collection |
| 845 | type Tokens []*Token |
| 846 | |
| 847 | func (t Tokens) InvalidToken() *Token { |
| 848 | for _, tt := range t { |
| 849 | if tt.Type == InvalidType { |
| 850 | return tt |
| 851 | } |
| 852 | } |
| 853 | return nil |
| 854 | } |
| 855 | |
| 856 | func (t *Tokens) add(tk *Token) { |
| 857 | tokens := *t |
| 858 | if len(tokens) == 0 { |
| 859 | tokens = append(tokens, tk) |
| 860 | } else { |
| 861 | last := tokens[len(tokens)-1] |
| 862 | last.Next = tk |
| 863 | tk.Prev = last |
| 864 | tokens = append(tokens, tk) |
| 865 | } |
| 866 | *t = tokens |
| 867 | } |
| 868 | |
| 869 | // Add append new some tokens |
| 870 | func (t *Tokens) Add(tks ...*Token) { |
| 871 | for _, tk := range tks { |
| 872 | t.add(tk) |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | // Dump dump all token structures for debugging |
| 877 | func (t Tokens) Dump() { |
| 878 | for _, tk := range t { |
| 879 | fmt.Print("- ") |
| 880 | tk.Dump() |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | // String create token for String |
| 885 | func String(value string, org string, pos *Position) *Token { |
| 886 | return &Token{ |
| 887 | Type: StringType, |
| 888 | CharacterType: CharacterTypeMiscellaneous, |
| 889 | Indicator: NotIndicator, |
| 890 | Value: value, |
| 891 | Origin: org, |
| 892 | Position: pos, |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | // SequenceEntry create token for SequenceEntry |
| 897 | func SequenceEntry(org string, pos *Position) *Token { |
| 898 | return &Token{ |
| 899 | Type: SequenceEntryType, |
| 900 | CharacterType: CharacterTypeIndicator, |
| 901 | Indicator: BlockStructureIndicator, |
| 902 | Value: string(SequenceEntryCharacter), |
| 903 | Origin: org, |
| 904 | Position: pos, |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | // MappingKey create token for MappingKey |
| 909 | func MappingKey(pos *Position) *Token { |
| 910 | return &Token{ |
| 911 | Type: MappingKeyType, |
| 912 | CharacterType: CharacterTypeIndicator, |
| 913 | Indicator: BlockStructureIndicator, |
| 914 | Value: string(MappingKeyCharacter), |
| 915 | Origin: string(MappingKeyCharacter), |
| 916 | Position: pos, |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | // MappingValue create token for MappingValue |
| 921 | func MappingValue(pos *Position) *Token { |
| 922 | return &Token{ |
| 923 | Type: MappingValueType, |
| 924 | CharacterType: CharacterTypeIndicator, |
| 925 | Indicator: BlockStructureIndicator, |
| 926 | Value: string(MappingValueCharacter), |
| 927 | Origin: string(MappingValueCharacter), |
| 928 | Position: pos, |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | // CollectEntry create token for CollectEntry |
| 933 | func CollectEntry(org string, pos *Position) *Token { |
| 934 | return &Token{ |
| 935 | Type: CollectEntryType, |
| 936 | CharacterType: CharacterTypeIndicator, |
| 937 | Indicator: FlowCollectionIndicator, |
| 938 | Value: string(CollectEntryCharacter), |
| 939 | Origin: org, |
| 940 | Position: pos, |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | // SequenceStart create token for SequenceStart |
| 945 | func SequenceStart(org string, pos *Position) *Token { |
| 946 | return &Token{ |
| 947 | Type: SequenceStartType, |
| 948 | CharacterType: CharacterTypeIndicator, |
| 949 | Indicator: FlowCollectionIndicator, |
| 950 | Value: string(SequenceStartCharacter), |
| 951 | Origin: org, |
| 952 | Position: pos, |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | // SequenceEnd create token for SequenceEnd |
| 957 | func SequenceEnd(org string, pos *Position) *Token { |
| 958 | return &Token{ |
| 959 | Type: SequenceEndType, |
| 960 | CharacterType: CharacterTypeIndicator, |
| 961 | Indicator: FlowCollectionIndicator, |
| 962 | Value: string(SequenceEndCharacter), |
| 963 | Origin: org, |
| 964 | Position: pos, |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | // MappingStart create token for MappingStart |
| 969 | func MappingStart(org string, pos *Position) *Token { |
| 970 | return &Token{ |
| 971 | Type: MappingStartType, |
| 972 | CharacterType: CharacterTypeIndicator, |
| 973 | Indicator: FlowCollectionIndicator, |
| 974 | Value: string(MappingStartCharacter), |
| 975 | Origin: org, |
| 976 | Position: pos, |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | // MappingEnd create token for MappingEnd |
| 981 | func MappingEnd(org string, pos *Position) *Token { |
| 982 | return &Token{ |
| 983 | Type: MappingEndType, |
| 984 | CharacterType: CharacterTypeIndicator, |
| 985 | Indicator: FlowCollectionIndicator, |
| 986 | Value: string(MappingEndCharacter), |
| 987 | Origin: org, |
| 988 | Position: pos, |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | // Comment create token for Comment |
| 993 | func Comment(value string, org string, pos *Position) *Token { |
| 994 | return &Token{ |
| 995 | Type: CommentType, |
| 996 | CharacterType: CharacterTypeIndicator, |
| 997 | Indicator: CommentIndicator, |
| 998 | Value: value, |
| 999 | Origin: org, |
| 1000 | Position: pos, |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | // Anchor create token for Anchor |
| 1005 | func Anchor(org string, pos *Position) *Token { |
| 1006 | return &Token{ |
| 1007 | Type: AnchorType, |
| 1008 | CharacterType: CharacterTypeIndicator, |
| 1009 | Indicator: NodePropertyIndicator, |
| 1010 | Value: string(AnchorCharacter), |
| 1011 | Origin: org, |
| 1012 | Position: pos, |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | // Alias create token for Alias |
| 1017 | func Alias(org string, pos *Position) *Token { |
| 1018 | return &Token{ |
| 1019 | Type: AliasType, |
| 1020 | CharacterType: CharacterTypeIndicator, |
| 1021 | Indicator: NodePropertyIndicator, |
| 1022 | Value: string(AliasCharacter), |
| 1023 | Origin: org, |
| 1024 | Position: pos, |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | // Tag create token for Tag |
| 1029 | func Tag(value string, org string, pos *Position) *Token { |
| 1030 | fn := ReservedTagKeywordMap[ReservedTagKeyword(value)] |
| 1031 | if fn != nil { |
| 1032 | return fn(value, org, pos) |
| 1033 | } |
| 1034 | return &Token{ |
| 1035 | Type: TagType, |
| 1036 | CharacterType: CharacterTypeIndicator, |
| 1037 | Indicator: NodePropertyIndicator, |
| 1038 | Value: value, |
| 1039 | Origin: org, |
| 1040 | Position: pos, |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | // Literal create token for Literal |
| 1045 | func Literal(value string, org string, pos *Position) *Token { |
| 1046 | return &Token{ |
| 1047 | Type: LiteralType, |
| 1048 | CharacterType: CharacterTypeIndicator, |
| 1049 | Indicator: BlockScalarIndicator, |
| 1050 | Value: value, |
| 1051 | Origin: org, |
| 1052 | Position: pos, |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | // Folded create token for Folded |
| 1057 | func Folded(value string, org string, pos *Position) *Token { |
| 1058 | return &Token{ |
| 1059 | Type: FoldedType, |
| 1060 | CharacterType: CharacterTypeIndicator, |
| 1061 | Indicator: BlockScalarIndicator, |
| 1062 | Value: value, |
| 1063 | Origin: org, |
| 1064 | Position: pos, |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | // SingleQuote create token for SingleQuote |
| 1069 | func SingleQuote(value string, org string, pos *Position) *Token { |
| 1070 | return &Token{ |
| 1071 | Type: SingleQuoteType, |
| 1072 | CharacterType: CharacterTypeIndicator, |
| 1073 | Indicator: QuotedScalarIndicator, |
| 1074 | Value: value, |
| 1075 | Origin: org, |
| 1076 | Position: pos, |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | // DoubleQuote create token for DoubleQuote |
| 1081 | func DoubleQuote(value string, org string, pos *Position) *Token { |
| 1082 | return &Token{ |
| 1083 | Type: DoubleQuoteType, |
| 1084 | CharacterType: CharacterTypeIndicator, |
| 1085 | Indicator: QuotedScalarIndicator, |
| 1086 | Value: value, |
| 1087 | Origin: org, |
| 1088 | Position: pos, |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | // Directive create token for Directive |
| 1093 | func Directive(org string, pos *Position) *Token { |
| 1094 | return &Token{ |
| 1095 | Type: DirectiveType, |
| 1096 | CharacterType: CharacterTypeIndicator, |
| 1097 | Indicator: DirectiveIndicator, |
| 1098 | Value: string(DirectiveCharacter), |
| 1099 | Origin: org, |
| 1100 | Position: pos, |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | // Space create token for Space |
| 1105 | func Space(pos *Position) *Token { |
| 1106 | return &Token{ |
| 1107 | Type: SpaceType, |
| 1108 | CharacterType: CharacterTypeWhiteSpace, |
| 1109 | Indicator: NotIndicator, |
| 1110 | Value: string(SpaceCharacter), |
| 1111 | Origin: string(SpaceCharacter), |
| 1112 | Position: pos, |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | // MergeKey create token for MergeKey |
| 1117 | func MergeKey(org string, pos *Position) *Token { |
| 1118 | return &Token{ |
| 1119 | Type: MergeKeyType, |
| 1120 | CharacterType: CharacterTypeMiscellaneous, |
| 1121 | Indicator: NotIndicator, |
| 1122 | Value: "<<", |
| 1123 | Origin: org, |
| 1124 | Position: pos, |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | // DocumentHeader create token for DocumentHeader |
| 1129 | func DocumentHeader(org string, pos *Position) *Token { |
| 1130 | return &Token{ |
| 1131 | Type: DocumentHeaderType, |
| 1132 | CharacterType: CharacterTypeMiscellaneous, |
| 1133 | Indicator: NotIndicator, |
| 1134 | Value: "---", |
| 1135 | Origin: org, |
| 1136 | Position: pos, |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | // DocumentEnd create token for DocumentEnd |
| 1141 | func DocumentEnd(org string, pos *Position) *Token { |
| 1142 | return &Token{ |
| 1143 | Type: DocumentEndType, |
| 1144 | CharacterType: CharacterTypeMiscellaneous, |
| 1145 | Indicator: NotIndicator, |
| 1146 | Value: "...", |
| 1147 | Origin: org, |
| 1148 | Position: pos, |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | func Invalid(err string, org string, pos *Position) *Token { |
| 1153 | return &Token{ |
| 1154 | Type: InvalidType, |
| 1155 | CharacterType: CharacterTypeInvalid, |
| 1156 | Indicator: NotIndicator, |
| 1157 | Value: org, |
| 1158 | Origin: org, |
| 1159 | Error: err, |
| 1160 | Position: pos, |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | // DetectLineBreakCharacter detect line break character in only one inside scalar content scope. |
| 1165 | func DetectLineBreakCharacter(src string) string { |
| 1166 | nc := strings.Count(src, "\n") |
| 1167 | rc := strings.Count(src, "\r") |
| 1168 | rnc := strings.Count(src, "\r\n") |
| 1169 | switch { |
| 1170 | case nc == rnc && rc == rnc: |
| 1171 | return "\r\n" |
| 1172 | case rc > nc: |
| 1173 | return "\r" |
| 1174 | default: |
| 1175 | return "\n" |
| 1176 | } |
| 1177 | } |