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