Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d2parser: Add not ampersand support #1568

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions d2ast/d2ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,9 @@ type Key struct {
// Indicates this MapKey is a filter selector.
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.
// The following are all valid MapKeys:
// Key:
Expand Down
19 changes: 15 additions & 4 deletions d2parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,16 +660,27 @@ func (p *parser) parseMapKey() (mk *d2ast.Key) {
}
}()

// Check for ampersand/@.
// Check for not ampersand/@.
r, eof := p.peek()
if eof {
return mk
}
if r != '&' {
p.rewind()
} else {
if r == '!' {
r, eof := p.peek()
if eof {
return mk
}
if r == '&' {
p.commit()
mk.NotAmpersand = true
} else {
p.rewind()
}
} else if r == '&' {
p.commit()
mk.Ampersand = true
} else {
p.rewind()
}

r, eof = p.peek()
Expand Down
12 changes: 12 additions & 0 deletions d2parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,18 @@ b-
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",
text: ` a -> b -> c `,
Expand Down
76 changes: 76 additions & 0 deletions testdata/d2parser/TestParse/not-amper.exp.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.