toml: eat first nl if present in multiline strings (#12496)

pull/12501/head
Larpon 2021-11-17 22:48:29 +01:00 committed by GitHub
parent 81455acd29
commit 3b612899bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 6 deletions

View File

@ -1127,6 +1127,13 @@ pub fn (mut p Parser) quoted() ast.Quoted {
mut lit := p.tok.lit[1..p.tok.lit.len - 1]
if is_multiline {
lit = p.tok.lit[3..p.tok.lit.len - 3]
// From https://toml.io/en/v1.0.0#string
// "Multi-line literal strings [...] A newline immediately following the opening
// delimiter will be trimmed. All other content between the delimiters
// is interpreted as-is without modification."
if lit.len > 0 && lit[0] == `\n` {
lit = lit[1..]
}
}
return ast.Quoted{
text: lit

View File

@ -18,7 +18,8 @@ const (
valid_value_exceptions = [
// String
'string/raw-multiline.toml', // This test is not correct. Our parser *correctly* includes the newline at the start of the raw multiline.
'string/double-quote-escape.toml',
'string/unicode-escape.toml',
'string/escapes.toml',
'string/escape-tricky.toml',
'string/multiline.toml',

View File

@ -40,9 +40,9 @@ fn test_multiline_strings() {
value = toml_doc.value('multi2')
assert value.string() == 'one\ntwo'
value = toml_doc.value('multi3')
assert value.string() == '\none\ntwo\nthree'
assert value.string() == 'one\ntwo\nthree'
value = toml_doc.value('multi4')
assert value.string() == '\none\ntwo\nthree\nfour\n'
assert value.string() == 'one\ntwo\nthree\nfour\n'
toml_doc = toml.parse(toml_multiline_text_2) or { panic(err) }
value = toml_doc.value('multi1')
@ -50,9 +50,9 @@ fn test_multiline_strings() {
value = toml_doc.value('multi2')
assert value.string() == 'one\ntwo'
value = toml_doc.value('multi3')
assert value.string() == '\none\ntwo\nthree'
assert value.string() == 'one\ntwo\nthree'
value = toml_doc.value('multi4')
assert value.string() == '\none\ntwo\nthree\nfour\n'
assert value.string() == 'one\ntwo\nthree\nfour\n'
toml_file :=
os.real_path(os.join_path(os.dir(@FILE), 'testdata', os.file_name(@FILE).all_before_last('.'))) +
@ -91,7 +91,7 @@ fn test_literal_strings() {
// See `.gitattributes` in the project root for the rule in action.
// These lines would look like this on Windows:
// assert toml_doc.value('ml_lit1').string() == '\r\n\\'
assert toml_doc.value('ml_lit1').string() == '\n\\'
assert toml_doc.value('ml_lit1').string() == '\\'
assert toml_doc.value('ml_lit2').string() == '\\\n\\'
assert toml_doc.value('ml_lit3').string() == '\\\ntricky\\\n'
}