d2parser: Error on unquoted strings starting with ...@

This commit is contained in:
Anmol Sethi 2023-06-06 14:47:40 -07:00
parent b4d5bc0be1
commit bc8f180055
No known key found for this signature in database
GPG key ID: 8CEF1878FF10ADEB
5 changed files with 70 additions and 1 deletions

View file

@ -257,6 +257,13 @@ func (p Position) Subtract(r rune, byUTF16 bool) Position {
return p
}
func (p Position) AdvanceString(s string, byUTF16 bool) Position {
for _, r := range s {
p = p.Advance(r, byUTF16)
}
return p
}
func (p Position) SubtractString(s string, byUTF16 bool) Position {
for _, r := range s {
p = p.Subtract(r, byUTF16)

View file

@ -1046,6 +1046,14 @@ func (p *parser) parseUnquotedString(inKey bool) (s *d2ast.UnquotedString) {
s.Value = append(s.Value, d2ast.InterpolationBox{String: &sv, StringRaw: &rawv})
}()
_s, eof := p.peekn(4)
p.rewind()
if !eof {
if _s == "...@" {
p.errorf(p.readerPos, p.pos.AdvanceString("...@", p.utf16), "unquoted strings cannot begin with ...@ as that's import spread syntax")
}
}
for {
r, eof := p.peek()
if eof {

View file

@ -463,6 +463,12 @@ func testImport(t *testing.T) {
assert.ErrorString(t, err, "d2/testdata/d2parser/TestParse/import/#07.d2:1:1: @file is not a valid import, did you mean ...@file?")
},
},
{
text: "meow: ...@file",
assert: func(t testing.TB, ast *d2ast.Map, err error) {
assert.ErrorString(t, err, "d2/testdata/d2parser/TestParse/import/#08.d2:1:10: unquoted strings cannot begin with ...@ as that's import spread syntax")
},
},
}
runa(t, tca)

View file

@ -24,7 +24,7 @@
"primary": {},
"value": {
"array": {
"range": "d2/testdata/d2parser/TestParse/import/#03.d2,0:3:3-0:15:15",
"range": "d2/testdata/d2parser/TestParse/import/#03.d2,0:3:3-0:16:16",
"nodes": [
{
"import": {

48
testdata/d2parser/TestParse/import/#08.exp.json generated vendored Normal file
View file

@ -0,0 +1,48 @@
{
"ast": {
"range": "d2/testdata/d2parser/TestParse/import/#08.d2,0:0:0-0:14:14",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2parser/TestParse/import/#08.d2,0:0:0-0:14:14",
"key": {
"range": "d2/testdata/d2parser/TestParse/import/#08.d2,0:0:0-0:4:4",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2parser/TestParse/import/#08.d2,0:0:0-0:4:4",
"value": [
{
"string": "meow",
"raw_string": "meow"
}
]
}
}
]
},
"primary": {},
"value": {
"unquoted_string": {
"range": "d2/testdata/d2parser/TestParse/import/#08.d2,0:6:6-0:14:14",
"value": [
{
"string": "...@file",
"raw_string": "...@file"
}
]
}
}
}
}
]
},
"err": {
"errs": [
{
"range": "d2/testdata/d2parser/TestParse/import/#08.d2,0:9:9-0:10:10",
"errmsg": "d2/testdata/d2parser/TestParse/import/#08.d2:1:10: unquoted strings cannot begin with ...@ as that's import spread syntax"
}
]
}
}