mirror of
https://github.com/golang/go
synced 2024-11-22 03:04:41 -07:00
- Parse expressions as opposed to statements for gofmt rewrite patterns.
Allows stand-alone types (e.g. []int as patterns) and doesn't require a semicolon at the end (which are now mandatory terminators). - Fix a matcher bug. R=rsc CC=golang-dev https://golang.org/cl/179088
This commit is contained in:
parent
8bf58725b2
commit
59a3cae402
@ -37,21 +37,12 @@ func initRewrite() {
|
|||||||
// but there are problems with preserving formatting and also
|
// but there are problems with preserving formatting and also
|
||||||
// with what a wildcard for a statement looks like.
|
// with what a wildcard for a statement looks like.
|
||||||
func parseExpr(s string, what string) ast.Expr {
|
func parseExpr(s string, what string) ast.Expr {
|
||||||
stmts, err := parser.ParseStmtList("input", s)
|
x, err := parser.ParseExpr("input", s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "parsing %s %s: %s\n", what, s, err)
|
fmt.Fprintf(os.Stderr, "parsing %s %s: %s\n", what, s, err)
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
if len(stmts) != 1 {
|
return x
|
||||||
fmt.Fprintf(os.Stderr, "%s must be single expression\n", what)
|
|
||||||
os.Exit(2)
|
|
||||||
}
|
|
||||||
x, ok := stmts[0].(*ast.ExprStmt)
|
|
||||||
if !ok {
|
|
||||||
fmt.Fprintf(os.Stderr, "%s must be single expression\n", what)
|
|
||||||
os.Exit(2)
|
|
||||||
}
|
|
||||||
return x.X
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -147,6 +138,9 @@ func match(m map[string]reflect.Value, pattern, val reflect.Value) bool {
|
|||||||
switch p := p.(type) {
|
switch p := p.(type) {
|
||||||
case *reflect.SliceValue:
|
case *reflect.SliceValue:
|
||||||
v := v.(*reflect.SliceValue)
|
v := v.(*reflect.SliceValue)
|
||||||
|
if p.Len() != v.Len() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
for i := 0; i < p.Len(); i++ {
|
for i := 0; i < p.Len(); i++ {
|
||||||
if !match(m, p.Elem(i), v.Elem(i)) {
|
if !match(m, p.Elem(i), v.Elem(i)) {
|
||||||
return false
|
return false
|
||||||
@ -156,6 +150,9 @@ func match(m map[string]reflect.Value, pattern, val reflect.Value) bool {
|
|||||||
|
|
||||||
case *reflect.StructValue:
|
case *reflect.StructValue:
|
||||||
v := v.(*reflect.StructValue)
|
v := v.(*reflect.StructValue)
|
||||||
|
if p.NumField() != v.NumField() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
for i := 0; i < p.NumField(); i++ {
|
for i := 0; i < p.NumField(); i++ {
|
||||||
if !match(m, p.Field(i), v.Field(i)) {
|
if !match(m, p.Field(i), v.Field(i)) {
|
||||||
return false
|
return false
|
||||||
|
Loading…
Reference in New Issue
Block a user