3
import "github.com/pelletier/go-toml/v2/internal/characters"
5
func scanFollows(b []byte, pattern string) bool {
8
return len(b) >= n && string(b[:n]) == pattern
11
func scanFollowsMultilineBasicStringDelimiter(b []byte) bool {
12
return scanFollows(b, `"""`)
15
func scanFollowsMultilineLiteralStringDelimiter(b []byte) bool {
16
return scanFollows(b, `'''`)
19
func scanFollowsTrue(b []byte) bool {
20
return scanFollows(b, `true`)
23
func scanFollowsFalse(b []byte) bool {
24
return scanFollows(b, `false`)
27
func scanFollowsInf(b []byte) bool {
28
return scanFollows(b, `inf`)
31
func scanFollowsNan(b []byte) bool {
32
return scanFollows(b, `nan`)
35
func scanUnquotedKey(b []byte) ([]byte, []byte) {
36
// unquoted-key = 1*( ALPHA / DIGIT / %x2D / %x5F ) ; A-Z / a-z / 0-9 / - / _
37
for i := 0; i < len(b); i++ {
38
if !isUnquotedKeyChar(b[i]) {
46
func isUnquotedKeyChar(r byte) bool {
47
return (r >= 'A' && r <= 'Z') || (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r == '-' || r == '_'
50
func scanLiteralString(b []byte) ([]byte, []byte, error) {
51
// literal-string = apostrophe *literal-char apostrophe
52
// apostrophe = %x27 ; ' apostrophe
53
// literal-char = %x09 / %x20-26 / %x28-7E / non-ascii
54
for i := 1; i < len(b); {
57
return b[:i+1], b[i+1:], nil
59
return nil, nil, NewParserError(b[i:i+1], "literal strings cannot have new lines")
61
size := characters.Utf8ValidNext(b[i:])
63
return nil, nil, NewParserError(b[i:i+1], "invalid character")
68
return nil, nil, NewParserError(b[len(b):], "unterminated literal string")
71
func scanMultilineLiteralString(b []byte) ([]byte, []byte, error) {
72
// ml-literal-string = ml-literal-string-delim [ newline ] ml-literal-body
73
// ml-literal-string-delim
74
// ml-literal-string-delim = 3apostrophe
75
// ml-literal-body = *mll-content *( mll-quotes 1*mll-content ) [ mll-quotes ]
77
// mll-content = mll-char / newline
78
// mll-char = %x09 / %x20-26 / %x28-7E / non-ascii
79
// mll-quotes = 1*2apostrophe
80
for i := 3; i < len(b); {
83
if scanFollowsMultilineLiteralStringDelimiter(b[i:]) {
86
// At that point we found 3 apostrophe, and i is the
87
// index of the byte after the third one. The scanner
88
// needs to be eager, because there can be an extra 2
89
// apostrophe that can be accepted at the end of the
92
if i >= len(b) || b[i] != '\'' {
93
return b[:i], b[i:], nil
97
if i >= len(b) || b[i] != '\'' {
98
return b[:i], b[i:], nil
102
if i < len(b) && b[i] == '\'' {
103
return nil, nil, NewParserError(b[i-3:i+1], "''' not allowed in multiline literal string")
106
return b[:i], b[i:], nil
110
return nil, nil, NewParserError(b[len(b):], `need a \n after \r`)
113
return nil, nil, NewParserError(b[i:i+2], `need a \n after \r`)
115
i += 2 // skip the \n
118
size := characters.Utf8ValidNext(b[i:])
120
return nil, nil, NewParserError(b[i:i+1], "invalid character")
125
return nil, nil, NewParserError(b[len(b):], `multiline literal string not terminated by '''`)
128
func scanWindowsNewline(b []byte) ([]byte, []byte, error) {
130
if len(b) < lenCRLF {
131
return nil, nil, NewParserError(b, "windows new line expected")
135
return nil, nil, NewParserError(b, `windows new line should be \r\n`)
138
return b[:lenCRLF], b[lenCRLF:], nil
141
func scanWhitespace(b []byte) ([]byte, []byte) {
142
for i := 0; i < len(b); i++ {
154
func scanComment(b []byte) ([]byte, []byte, error) {
155
// comment-start-symbol = %x23 ; #
156
// non-ascii = %x80-D7FF / %xE000-10FFFF
157
// non-eol = %x09 / %x20-7F / non-ascii
159
// comment = comment-start-symbol *non-eol
161
for i := 1; i < len(b); {
163
return b[:i], b[i:], nil
166
if i+1 < len(b) && b[i+1] == '\n' {
167
return b[:i+1], b[i+1:], nil
169
return nil, nil, NewParserError(b[i:i+1], "invalid character in comment")
171
size := characters.Utf8ValidNext(b[i:])
173
return nil, nil, NewParserError(b[i:i+1], "invalid character in comment")
179
return b, b[len(b):], nil
182
func scanBasicString(b []byte) ([]byte, bool, []byte, error) {
183
// basic-string = quotation-mark *basic-char quotation-mark
184
// quotation-mark = %x22 ; "
185
// basic-char = basic-unescaped / escaped
186
// basic-unescaped = wschar / %x21 / %x23-5B / %x5D-7E / non-ascii
187
// escaped = escape escape-seq-char
191
for ; i < len(b); i++ {
194
return b[:i+1], escaped, b[i+1:], nil
196
return nil, escaped, nil, NewParserError(b[i:i+1], "basic strings cannot have new lines")
199
return nil, escaped, nil, NewParserError(b[i:i+1], "need a character after \\")
202
i++ // skip the next character
206
return nil, escaped, nil, NewParserError(b[len(b):], `basic string not terminated by "`)
209
func scanMultilineBasicString(b []byte) ([]byte, bool, []byte, error) {
210
// ml-basic-string = ml-basic-string-delim [ newline ] ml-basic-body
211
// ml-basic-string-delim
212
// ml-basic-string-delim = 3quotation-mark
213
// ml-basic-body = *mlb-content *( mlb-quotes 1*mlb-content ) [ mlb-quotes ]
215
// mlb-content = mlb-char / newline / mlb-escaped-nl
216
// mlb-char = mlb-unescaped / escaped
217
// mlb-quotes = 1*2quotation-mark
218
// mlb-unescaped = wschar / %x21 / %x23-5B / %x5D-7E / non-ascii
219
// mlb-escaped-nl = escape ws newline *( wschar / newline )
224
for ; i < len(b); i++ {
227
if scanFollowsMultilineBasicStringDelimiter(b[i:]) {
230
// At that point we found 3 apostrophe, and i is the
231
// index of the byte after the third one. The scanner
232
// needs to be eager, because there can be an extra 2
233
// apostrophe that can be accepted at the end of the
236
if i >= len(b) || b[i] != '"' {
237
return b[:i], escaped, b[i:], nil
241
if i >= len(b) || b[i] != '"' {
242
return b[:i], escaped, b[i:], nil
246
if i < len(b) && b[i] == '"' {
247
return nil, escaped, nil, NewParserError(b[i-3:i+1], `""" not allowed in multiline basic string`)
250
return b[:i], escaped, b[i:], nil
254
return nil, escaped, nil, NewParserError(b[len(b):], "need a character after \\")
257
i++ // skip the next character
260
return nil, escaped, nil, NewParserError(b[len(b):], `need a \n after \r`)
263
return nil, escaped, nil, NewParserError(b[i:i+2], `need a \n after \r`)
269
return nil, escaped, nil, NewParserError(b[len(b):], `multiline basic string not terminated by """`)