You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

keysparsing.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Parsing keys handling both bare and quoted keys.
  2. package toml
  3. import (
  4. "errors"
  5. "fmt"
  6. )
  7. // Convert the bare key group string to an array.
  8. // The input supports double quotation and single quotation,
  9. // but escape sequences are not supported. Lexers must unescape them beforehand.
  10. func parseKey(key string) ([]string, error) {
  11. runes := []rune(key)
  12. var groups []string
  13. if len(key) == 0 {
  14. return nil, errors.New("empty key")
  15. }
  16. idx := 0
  17. for idx < len(runes) {
  18. for ; idx < len(runes) && isSpace(runes[idx]); idx++ {
  19. // skip leading whitespace
  20. }
  21. if idx >= len(runes) {
  22. break
  23. }
  24. r := runes[idx]
  25. if isValidBareChar(r) {
  26. // parse bare key
  27. startIdx := idx
  28. endIdx := -1
  29. idx++
  30. for idx < len(runes) {
  31. r = runes[idx]
  32. if isValidBareChar(r) {
  33. idx++
  34. } else if r == '.' {
  35. endIdx = idx
  36. break
  37. } else if isSpace(r) {
  38. endIdx = idx
  39. for ; idx < len(runes) && isSpace(runes[idx]); idx++ {
  40. // skip trailing whitespace
  41. }
  42. if idx < len(runes) && runes[idx] != '.' {
  43. return nil, fmt.Errorf("invalid key character after whitespace: %c", runes[idx])
  44. }
  45. break
  46. } else {
  47. return nil, fmt.Errorf("invalid bare key character: %c", r)
  48. }
  49. }
  50. if endIdx == -1 {
  51. endIdx = idx
  52. }
  53. groups = append(groups, string(runes[startIdx:endIdx]))
  54. } else if r == '\'' {
  55. // parse single quoted key
  56. idx++
  57. startIdx := idx
  58. for {
  59. if idx >= len(runes) {
  60. return nil, fmt.Errorf("unclosed single-quoted key")
  61. }
  62. r = runes[idx]
  63. if r == '\'' {
  64. groups = append(groups, string(runes[startIdx:idx]))
  65. idx++
  66. break
  67. }
  68. idx++
  69. }
  70. } else if r == '"' {
  71. // parse double quoted key
  72. idx++
  73. startIdx := idx
  74. for {
  75. if idx >= len(runes) {
  76. return nil, fmt.Errorf("unclosed double-quoted key")
  77. }
  78. r = runes[idx]
  79. if r == '"' {
  80. groups = append(groups, string(runes[startIdx:idx]))
  81. idx++
  82. break
  83. }
  84. idx++
  85. }
  86. } else if r == '.' {
  87. idx++
  88. if idx >= len(runes) {
  89. return nil, fmt.Errorf("unexpected end of key")
  90. }
  91. r = runes[idx]
  92. if !isValidBareChar(r) && r != '\'' && r != '"' && r != ' ' {
  93. return nil, fmt.Errorf("expecting key part after dot")
  94. }
  95. } else {
  96. return nil, fmt.Errorf("invalid key character: %c", r)
  97. }
  98. }
  99. if len(groups) == 0 {
  100. return nil, fmt.Errorf("empty key")
  101. }
  102. return groups, nil
  103. }
  104. func isValidBareChar(r rune) bool {
  105. return isAlphanumeric(r) || r == '-' || isDigit(r)
  106. }