summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/pelletier/go-toml/keysparsing.go
blob: 9707c688405bb6c6bde2b8c7532a1d16fb00adfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Parsing keys handling both bare and quoted keys.

package toml

import (
	"bytes"
	"errors"
	"fmt"
	"strconv"
	"unicode"
)

var escapeSequenceMap = map[rune]rune{
	'b':  '\b',
	't':  '\t',
	'n':  '\n',
	'f':  '\f',
	'r':  '\r',
	'"':  '"',
	'\\': '\\',
}

type parseKeyState int

const (
	BARE parseKeyState = iota
	BASIC
	LITERAL
	ESC
	UNICODE_4
	UNICODE_8
)

func parseKey(key string) ([]string, error) {
	groups := []string{}
	var buffer bytes.Buffer
	var hex bytes.Buffer
	state := BARE
	wasInQuotes := false
	ignoreSpace := true
	expectDot := false

	for _, char := range key {
		if ignoreSpace {
			if char == ' ' {
				continue
			}
			ignoreSpace = false
		}

		if state == ESC {
			if char == 'u' {
				state = UNICODE_4
				hex.Reset()
			} else if char == 'U' {
				state = UNICODE_8
				hex.Reset()
			} else if newChar, ok := escapeSequenceMap[char]; ok {
				buffer.WriteRune(newChar)
				state = BASIC
			} else {
				return nil, fmt.Errorf(`invalid escape sequence \%c`, char)
			}
			continue
		}

		if state == UNICODE_4 || state == UNICODE_8 {
			if isHexDigit(char) {
				hex.WriteRune(char)
			}
			if (state == UNICODE_4 && hex.Len() == 4) || (state == UNICODE_8 && hex.Len() == 8) {
				if value, err := strconv.ParseInt(hex.String(), 16, 32); err == nil {
					buffer.WriteRune(rune(value))
				} else {
					return nil, err
				}
				state = BASIC
			}
			continue
		}

		switch char {
		case '\\':
			if state == BASIC {
				state = ESC
			} else if state == LITERAL {
				buffer.WriteRune(char)
			}
		case '\'':
			if state == BARE {
				state = LITERAL
			} else if state == LITERAL {
				groups = append(groups, buffer.String())
				buffer.Reset()
				wasInQuotes = true
				state = BARE
			}
			expectDot = false
		case '"':
			if state == BARE {
				state = BASIC
			} else if state == BASIC {
				groups = append(groups, buffer.String())
				buffer.Reset()
				state = BARE
				wasInQuotes = true
			}
			expectDot = false
		case '.':
			if state != BARE {
				buffer.WriteRune(char)
			} else {
				if !wasInQuotes {
					if buffer.Len() == 0 {
						return nil, errors.New("empty table key")
					}
					groups = append(groups, buffer.String())
					buffer.Reset()
				}
				ignoreSpace = true
				expectDot = false
				wasInQuotes = false
			}
		case ' ':
			if state == BASIC {
				buffer.WriteRune(char)
			} else {
				expectDot = true
			}
		default:
			if state == BARE {
				if !isValidBareChar(char) {
					return nil, fmt.Errorf("invalid bare character: %c", char)
				} else if expectDot {
					return nil, errors.New("what?")
				}
			}
			buffer.WriteRune(char)
			expectDot = false
		}
	}

	// state must be BARE at the end
	if state == ESC {
		return nil, errors.New("unfinished escape sequence")
	} else if state != BARE {
		return nil, errors.New("mismatched quotes")
	}

	if buffer.Len() > 0 {
		groups = append(groups, buffer.String())
	}
	if len(groups) == 0 {
		return nil, errors.New("empty key")
	}
	return groups, nil
}

func isValidBareChar(r rune) bool {
	return isAlphanumeric(r) || r == '-' || unicode.IsNumber(r)
}