Skip to content

Commit

Permalink
handle IndexListExpr node (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
quasilyte authored Aug 28, 2022
1 parent 5e2753e commit 86e4605
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 223 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"runs-on": "ubuntu-latest",
steps: [
{
name: "Set up Go 1.17",
name: "Set up Go 1.18",
uses: "actions/setup-go@v1",
"with": {"go-version": 1.17},
"with": {"go-version": 1.18},
id: "go",
},
{name: "Check out code into the Go module directory", uses: "actions/checkout@v1"},
Expand Down
11 changes: 11 additions & 0 deletions compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ func (c *compiler) compileExpr(n ast.Expr) {
c.compileBinaryExpr(n)
case *ast.IndexExpr:
c.compileIndexExpr(n)
case *typeparams.IndexListExpr:
c.compileIndexListExpr(n)
case *ast.Ident:
c.compileIdent(n)
case *ast.CallExpr:
Expand Down Expand Up @@ -452,6 +454,15 @@ func (c *compiler) compileIndexExpr(n *ast.IndexExpr) {
c.compileExpr(n.Index)
}

func (c *compiler) compileIndexListExpr(n *typeparams.IndexListExpr) {
c.emitInstOp(opIndexListExpr)
c.compileExpr(n.X)
for _, x := range n.Indices {
c.compileExpr(x)
}
c.emitInstOp(opEnd)
}

func (c *compiler) compileWildIdent(n *ast.Ident, optional bool) {
info := decodeWildName(n.Name)
var inst instruction
Expand Down
8 changes: 8 additions & 0 deletions compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,14 @@ func TestCompileWithTypes(t *testing.T) {
` • BasicLit 0`,
},

`x[int, uint]`: {
`IndexListExpr`,
` • Ident x`,
` • Ident int`,
` • Ident uint`,
` • End`,
},

`s[:]`: {
`SliceExpr`,
` • Ident s`,
Expand Down
2 changes: 2 additions & 0 deletions gen_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ var opPrototypes = []operationProto{

{name: "IndexExpr", tag: "IndexExpr", args: "x expr"},

{name: "IndexListExpr", tag: "IndexListExpr", args: "x exprs..."},

{name: "SliceExpr", tag: "SliceExpr", args: "x"},
{name: "SliceFromExpr", tag: "SliceExpr", args: "x from", example: "x[from:]"},
{name: "SliceToExpr", tag: "SliceExpr", args: "x to", example: "x[:to]"},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/quasilyte/gogrep
go 1.16

require (
github.com/go-toolsmith/astequal v1.0.2
github.com/go-toolsmith/astequal v1.0.3
github.com/google/go-cmp v0.5.8
golang.org/x/exp/typeparams v0.0.0-20220428152302-39d4317da171
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/go-toolsmith/astequal v1.0.2 h1:+XvaV8zNxua+9+Oa4AHmgmpo4RYAbwr/qjNppLfX2yM=
github.com/go-toolsmith/astequal v1.0.2/go.mod h1:9Ai4UglvtR+4up+bAD4+hCj7iTo4m/OXVTSLnCyTAx4=
github.com/go-toolsmith/astequal v1.0.3 h1:+LVdyRatFS+XO78SGV4I3TCEA0AC7fKEGma+fH+674o=
github.com/go-toolsmith/astequal v1.0.3/go.mod h1:9Ai4UglvtR+4up+bAD4+hCj7iTo4m/OXVTSLnCyTAx4=
github.com/go-toolsmith/strparse v1.0.0 h1:Vcw78DnpCAKlM20kSbAyO4mPfJn/lyYA4BJUDxe2Jb4=
github.com/go-toolsmith/strparse v1.0.0/go.mod h1:YI2nUKP9YGZnL/L1/DLFBfixrcjslWct4wyljWhSRy8=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
Expand Down
4 changes: 4 additions & 0 deletions match.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ func (m *matcher) matchNodeWithInst(state *MatcherState, inst instruction, n ast
n, ok := n.(*ast.IndexExpr)
return ok && m.matchNode(state, n.X) && m.matchNode(state, n.Index)

case opIndexListExpr:
n, ok := n.(*typeparams.IndexListExpr)
return ok && m.matchNode(state, n.X) && m.matchExprSlice(state, n.Indices)

case opKeyValueExpr:
n, ok := n.(*ast.KeyValueExpr)
return ok && m.matchNode(state, n.Key) && m.matchNode(state, n.Value)
Expand Down
4 changes: 4 additions & 0 deletions match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,10 @@ func TestMatch(t *testing.T) {
{`$_[$t]($*_)`, 1, `f[int](10)`},
{`$_[$t]($*_)`, 0, `f(10)`},
{`$_[$t]($*_)`, 0, `f(10, 20)`},
{`$_[$t, int]($*_)`, 0, `f(10, 20)`},
{`$_[$t, int]($*_)`, 1, `f[int, int](10, 20)`},
{`$_[$t, $t]($*_)`, 1, `f[int, int](10, 20)`},
{`$_[$t, $t]($*_)`, 0, `f[int, uint](10, 20)`},
}...)
}

Expand Down
7 changes: 7 additions & 0 deletions nodetag/nodetag.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package nodetag

import (
"go/ast"

"golang.org/x/exp/typeparams"
)

type Value int
Expand Down Expand Up @@ -37,6 +39,7 @@ const (
ImportSpec
IncDecStmt
IndexExpr
IndexListExpr
InterfaceType
KeyValueExpr
LabeledStmt
Expand Down Expand Up @@ -126,6 +129,8 @@ func FromNode(n ast.Node) Value {
return IncDecStmt
case *ast.IndexExpr:
return IndexExpr
case *typeparams.IndexListExpr:
return IndexListExpr
case *ast.InterfaceType:
return InterfaceType
case *ast.KeyValueExpr:
Expand Down Expand Up @@ -236,6 +241,8 @@ func FromString(s string) Value {
return IncDecStmt
case "IndexExpr":
return IndexExpr
case "IndexListExpr":
return IndexListExpr
case "InterfaceType":
return InterfaceType
case "KeyValueExpr":
Expand Down
223 changes: 112 additions & 111 deletions operation_string.go

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

Loading

0 comments on commit 86e4605

Please sign in to comment.