summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/pelletier/go-toml/keysparsing.go
blob: 0da938b0335b03fe5cf010ea0d65f93dac3a0344 (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
	unicode4
	unicode8
)

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 = unicode4
				hex.Reset()
			} else if char == 'U' {
				state = unicode8
				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 == unicode4 || state == unicode8 {
			if isHexDigit(char) {
				hex.WriteRune(char)
			}
			if (state == unicode4 && hex.Len() == 4) || (state == unicode8 && 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)
}