d2parser: Add not ampersand support

See #1567
This commit is contained in:
Anmol Sethi 2023-08-29 21:21:26 -07:00
parent c670987516
commit 387c33660f
No known key found for this signature in database
GPG key ID: 8CEF1878FF10ADEB
4 changed files with 106 additions and 4 deletions

View file

@ -613,6 +613,9 @@ type Key struct {
// Indicates this MapKey is a filter selector. // Indicates this MapKey is a filter selector.
Ampersand bool `json:"ampersand,omitempty"` Ampersand bool `json:"ampersand,omitempty"`
// Indicates this MapKey is a not filter selector.
NotAmpersand bool `json:"not_ampersand,omitempty"`
// At least one of Key and Edges will be set but all four can also be set. // At least one of Key and Edges will be set but all four can also be set.
// The following are all valid MapKeys: // The following are all valid MapKeys:
// Key: // Key:

View file

@ -660,16 +660,27 @@ func (p *parser) parseMapKey() (mk *d2ast.Key) {
} }
}() }()
// Check for ampersand/@. // Check for not ampersand/@.
r, eof := p.peek() r, eof := p.peek()
if eof { if eof {
return mk return mk
} }
if r != '&' { if r == '!' {
p.rewind() r, eof := p.peek()
} else { if eof {
return mk
}
if r == '&' {
p.commit()
mk.NotAmpersand = true
} else {
p.rewind()
}
} else if r == '&' {
p.commit() p.commit()
mk.Ampersand = true mk.Ampersand = true
} else {
p.rewind()
} }
r, eof = p.peek() r, eof = p.peek()

View file

@ -380,6 +380,18 @@ b-
c- c-
`, `,
}, },
{
name: "not-amper",
text: `
&k: amper
!&k: not amper
`,
assert: func(t testing.TB, ast *d2ast.Map, err error) {
assert.Success(t, err)
assert.True(t, ast.Nodes[0].MapKey.Ampersand)
assert.True(t, ast.Nodes[1].MapKey.NotAmpersand)
},
},
{ {
name: "whitespace_range", name: "whitespace_range",
text: ` a -> b -> c `, text: ` a -> b -> c `,

76
testdata/d2parser/TestParse/not-amper.exp.json generated vendored Normal file
View file

@ -0,0 +1,76 @@
{
"ast": {
"range": "d2/testdata/d2parser/TestParse/not-amper.d2,0:0:0-3:0:26",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2parser/TestParse/not-amper.d2,1:0:1-1:9:10",
"ampersand": true,
"key": {
"range": "d2/testdata/d2parser/TestParse/not-amper.d2,1:1:2-1:2:3",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2parser/TestParse/not-amper.d2,1:1:2-1:2:3",
"value": [
{
"string": "k",
"raw_string": "k"
}
]
}
}
]
},
"primary": {},
"value": {
"unquoted_string": {
"range": "d2/testdata/d2parser/TestParse/not-amper.d2,1:4:5-1:9:10",
"value": [
{
"string": "amper",
"raw_string": "amper"
}
]
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2parser/TestParse/not-amper.d2,2:0:11-2:14:25",
"not_ampersand": true,
"key": {
"range": "d2/testdata/d2parser/TestParse/not-amper.d2,2:2:13-2:3:14",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2parser/TestParse/not-amper.d2,2:2:13-2:3:14",
"value": [
{
"string": "k",
"raw_string": "k"
}
]
}
}
]
},
"primary": {},
"value": {
"unquoted_string": {
"range": "d2/testdata/d2parser/TestParse/not-amper.d2,2:5:16-2:14:25",
"value": [
{
"string": "not amper",
"raw_string": "not amper"
}
]
}
}
}
}
]
},
"err": null
}