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

gopbuiltingen; gop/ast/gopq #2117

Merged
merged 5 commits into from
Feb 23, 2025
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
73 changes: 68 additions & 5 deletions ast/gopq/gop_dom.go → ast/gopq/dom.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,82 @@ func (p *astSpec) Obj() interface{} {

// -----------------------------------------------------------------------------

func visitStmt(stmt ast.Stmt, filter func(node Node) error) error {
if stmt != nil {
return filter(&astStmt{stmt})
}
return nil
}

type astStmt struct {
ast.Stmt
}

func (p *astStmt) ForEach(filter func(node Node) error) error {
switch stmt := p.Stmt.(type) {
case *ast.IfStmt:
if err := visitStmt(stmt.Init, filter); err == ErrBreak {
return err
}
if err := filter(&astStmt{stmt.Body}); err == ErrBreak {
return err
}
if err := visitStmt(stmt.Else, filter); err == ErrBreak {
return err
}
case *ast.BlockStmt:
for _, stmt := range stmt.List {
node := &astStmt{stmt}
if err := filter(node); err == ErrBreak {
return err
}
}
}
// TODO(xsw): visit other stmts
return nil
}

func (p *astStmt) Obj() interface{} {
return p.Stmt
}

// -----------------------------------------------------------------------------

type astExpr struct {
ast.Expr
}

func (p *astExpr) ForEach(filter func(node Node) error) error {
return nil
}

func (p *astExpr) Obj() interface{} {
return p.Expr
}

// -----------------------------------------------------------------------------

// NameOf returns name of an ast node.
func NameOf(node Node) string {
switch v := node.Obj().(type) {
return getName(node.Obj())
}

func getName(v interface{}) string {
switch v := v.(type) {
case *ast.FuncDecl:
return v.Name.Name
case *ast.ImportSpec:
if v.Name == nil {
n := v.Name
if n == nil {
return ""
}
return v.Name.Name
default:
panic("node doesn't contain the `name` property")
return n.Name
case *ast.Ident:
return v.Name
case *ast.SelectorExpr:
return getName(v.X) + "." + v.Sel.Name
}
panic("node doesn't contain the `name` property")
}

// -----------------------------------------------------------------------------
Loading