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