Skip to content

Commit df5bf7f

Browse files
authored
Merge pull request #303 from BurntSushi/ml-end
Multiline strings ending in 6 or more quotes should fail
2 parents 8485579 + 6300ada commit df5bf7f

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module github.com/BurntSushi/toml
22

33
go 1.13
44

5-
require github.com/BurntSushi/toml-test v0.1.1-0.20210621044449-d531a008d555
5+
require github.com/BurntSushi/toml-test v0.1.1-0.20210624055653-1f6389604dc6

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
github.com/BurntSushi/toml v0.3.2-0.20210614224209-34d990aa228d/go.mod h1:2QZjSXA5e+XyFeCAxxtL8Z4StYUsTquL8ODGPR3C3MA=
22
github.com/BurntSushi/toml v0.3.2-0.20210621044154-20a94d639b8e/go.mod h1:t4zg8TkHfP16Vb3x4WKIw7zVYMit5QFtPEO8lOWxzTg=
33
github.com/BurntSushi/toml-test v0.1.1-0.20210620192437-de01089bbf76/go.mod h1:P/PrhmZ37t5llHfDuiouWXtFgqOoQ12SAh9j6EjrBR4=
4-
github.com/BurntSushi/toml-test v0.1.1-0.20210621044449-d531a008d555 h1:oNF2vOuH05dfefEw1E/x5wf6LRu98Wb9dzITyLYfrhc=
5-
github.com/BurntSushi/toml-test v0.1.1-0.20210621044449-d531a008d555/go.mod h1:UAIt+Eo8itMZAAgImXkPGDMYsT1SsJkVdB5TuONl86A=
4+
github.com/BurntSushi/toml-test v0.1.1-0.20210624055653-1f6389604dc6 h1:hRkQ1B9Jtdssyzo0Cr3EgS2WMwPscGNrCDNCeYshAQA=
5+
github.com/BurntSushi/toml-test v0.1.1-0.20210624055653-1f6389604dc6/go.mod h1:UAIt+Eo8itMZAAgImXkPGDMYsT1SsJkVdB5TuONl86A=
66
zgo.at/zli v0.0.0-20210619044753-e7020a328e59/go.mod h1:HLAc12TjNGT+VRXr76JnsNE3pbooQtwKWhX+RlDjQ2Y=

lex.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func (lx *lexer) ignore() {
174174
lx.start = lx.pos
175175
}
176176

177-
// backup steps back one rune. Can be called only twice between calls to next.
177+
// backup steps back one rune. Can be called 4 times between calls to next.
178178
func (lx *lexer) backup() {
179179
if lx.atEOF {
180180
lx.atEOF = false
@@ -670,20 +670,28 @@ func lexMultilineString(lx *lexer) stateFn {
670670
case '\\':
671671
return lexMultilineStringEscape
672672
case stringEnd:
673+
/// Found " → try to read two more "".
673674
if lx.accept(stringEnd) {
674675
if lx.accept(stringEnd) {
675-
// Can end in quote: """str""""
676+
/// Peek ahead: the string can contain " and "", including at the
677+
/// end: """str"""""
678+
/// 6 or more at the end, however, is an error.
676679
if lx.peek() == stringEnd {
680+
/// Check if we already lexed 5 's; if so we have 6 now, and
681+
/// that's just too many man!
682+
if strings.HasSuffix(lx.current(), `"""""`) {
683+
return lx.errorf(`unexpected '""""""'`)
684+
}
677685
lx.backup()
678686
lx.backup()
679687
return lexMultilineString
680688
}
681689

682-
lx.backup()
690+
lx.backup() /// backup: don't include the """ in the item.
683691
lx.backup()
684692
lx.backup()
685693
lx.emit(itemMultilineString)
686-
lx.next()
694+
lx.next() /// Read over ''' again and discard it.
687695
lx.next()
688696
lx.next()
689697
lx.ignore()
@@ -734,19 +742,28 @@ func lexMultilineRawString(lx *lexer) stateFn {
734742
}
735743
return lexMultilineRawString
736744
case rawStringEnd:
745+
/// Found ' → try to read two more ''.
737746
if lx.accept(rawStringEnd) {
738747
if lx.accept(rawStringEnd) {
739-
// Can end in quote: '''str''''
748+
/// Peek ahead: the string can contain ' and '', including at the
749+
/// end: '''str'''''
750+
/// 6 or more at the end, however, is an error.
740751
if lx.peek() == rawStringEnd {
752+
/// Check if we already lexed 5 's; if so we have 6 now, and
753+
/// that's just too many man!
754+
if strings.HasSuffix(lx.current(), "'''''") {
755+
return lx.errorf(`unexpected "''''''"`)
756+
}
741757
lx.backup()
742758
lx.backup()
743759
return lexMultilineRawString
744760
}
745-
lx.backup()
761+
762+
lx.backup() /// backup: don't include the ''' in the item.
746763
lx.backup()
747764
lx.backup()
748765
lx.emit(itemRawMultilineString)
749-
lx.next()
766+
lx.next() /// Read over ''' again and discard it.
750767
lx.next()
751768
lx.next()
752769
lx.ignore()

toml_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ func TestToml(t *testing.T) {
5757
"valid/datetime-local",
5858
"valid/array-mix-string-table",
5959
"valid/inline-table-nest",
60-
61-
"invalid/string-literal-multiline-quotes",
6260
},
6361
}
6462

0 commit comments

Comments
 (0)