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

ng/stmt,ng/expr: introduce a Walk function #93

Open
sbinet opened this issue Nov 17, 2017 · 3 comments
Open

ng/stmt,ng/expr: introduce a Walk function #93

sbinet opened this issue Nov 17, 2017 · 3 comments
Assignees

Comments

@sbinet
Copy link
Collaborator

sbinet commented Nov 17, 2017

to fix #92 I think we'd need some kind of a visitor for stmt.Stmt, expr.Expr, etc...

the go/ast has ast.Walk:

$> go doc go/ast.Walk
func Walk(v Visitor, node Node)
    Walk traverses an AST in depth-first order: It starts by calling
    v.Visit(node); node must not be nil. If the visitor w returned by
    v.Visit(node) is not nil, Walk is invoked recursively with visitor w for
    each of the non-nil children of node, followed by a call of w.Visit(nil).

$> go doc go/ast.Visitor
type Visitor interface {
	Visit(node Node) (w Visitor)
}
    A Visitor's Visit method is invoked for each node encountered by Walk. If
    the result visitor w is not nil, Walk visits each of the children of node
    with the visitor w, followed by a call of w.Visit(nil).

$> go doc go/ast.Node
type Node interface {
	Pos() token.Pos // position of first character belonging to the node
	End() token.Pos // position of first character immediately after the node
}
    All node types implement the Node interface.

what do you think?

@crawshaw
Copy link
Member

I wonder if we could jump straight to an Apply interface. Something like the fallthrough detection doesn't actually need to modify the AST, but Apply can be used without making any changes. (And it would be super-simple to build a Walk on top of it.)

https://go-review.googlesource.com/c/tools/+/77811/4/go/ast/astutil/rewrite.go

We will need a generic Node interface. Right now there's no position information to expose by it (this is a long-standing TODO I keep not getting to). I'll file a separate issue about that.

@sbinet
Copy link
Collaborator Author

sbinet commented Nov 17, 2017

Apply sounds good to me.

crawshaw added a commit that referenced this issue Nov 19, 2017
Introduce a syntax.Node interface, which can represent any node in
the syntax tree.

Introduce a src.Pos object that represents a source file position,
and put src.Pos on all the syntax tree nodes.

Move the stmt, expr, and tipe packages under syntax to make their
purpose clear.

Start wiring source positions through the parser. Much more to do.

Updates #95
Updates #93
@crawshaw
Copy link
Member

I need this for #5, so I'm going to take a look at it. (Though I'm back at work for a week, so it may have to wait until next weekend.)

@crawshaw crawshaw self-assigned this Nov 26, 2017
crawshaw added a commit that referenced this issue Dec 3, 2017
This syntax tree walker interface and design are deeply inspired by
the Go syntax tree Apply function in golang.org/x/tools/go/ast/astutil.
It will one day be extended to allow editing syntax trees.

For #93
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants