func (c *astConverter) maybeReplace(node ast.Node, ann *annotations.Annotation, replace func(e ast.Expr)) bool { if replace == nil { return false } if typ, ok := ann.Type(); ok { e, err := parser.ParseExpr(typ) if err == nil { replace(e) return true } } n := reflect.ValueOf(node) if n.Elem().Type().Kind() != reflect.Struct { return false } doc := n.Elem().FieldByName("Doc") if !doc.IsValid() { return false } cg := doc.Interface().(*ast.CommentGroup) if cg == nil { return false } for _, l := range cg.List { s := l.Text s = strings.TrimPrefix(s, "//") s = strings.TrimPrefix(s, "/*") s = strings.TrimSpace(s) if !strings.HasPrefix(s, "For SGo: ") { continue } ann := s[len("For SGo: "):] e, err := parser.ParseExpr(ann) if err != nil { return false } replace(e) return true } return false }
// parseExpr parses s as an expression. // It might make sense to expand this to allow statement patterns, // but there are problems with preserving formatting and also // with what a wildcard for a statement looks like. func parseExpr(s, what string) ast.Expr { x, err := parser.ParseExpr(s) if err != nil { fmt.Fprintf(os.Stderr, "parsing %s %s at %s\n", what, s, err) os.Exit(2) } return x }
func TestExprString(t *testing.T) { for _, test := range testExprs { x, err := parser.ParseExpr(test.src) if err != nil { t.Errorf("%s: %s", test.src, err) continue } if got := ExprString(x); got != test.str { t.Errorf("%s: got %s, want %s", test.src, got, test.str) } } }