Skip to content

Commit

Permalink
d2parser: Add not ampersand support
Browse files Browse the repository at this point in the history
  • Loading branch information
nhooyr committed Aug 30, 2023
1 parent c670987 commit 3181c9a
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 4 deletions.
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
74 changes: 74 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.

0 comments on commit 3181c9a

Please sign in to comment.