Skip to content

Instantly share code, notes, and snippets.

@haya14busa
Created December 4, 2016 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haya14busa/d84004170caf97ad55162aecb5adfc9e to your computer and use it in GitHub Desktop.
Save haya14busa/d84004170caf97ad55162aecb5adfc9e to your computer and use it in GitHub Desktop.
How go/ast represents syntax tree
// source: https://github.com/golang/go/blob/d54b60a2b2470ea42559b58995e86ff20dd70471/src/go/ast/ast.go
type Node interface {
Pos() token.Pos
End() token.Pos
}
type Expr interface {
Node
exprNode()
}
type Stmt interface {
Node
stmtNode()
}
// Pos and End implementations for expression/type nodes.
func (x *BadExpr) Pos() token.Pos { return x.From }
func (x *Ident) Pos() token.Pos { return x.NamePos }
// ...
// exprNode() ensures that only expression/type nodes can be
// assigned to an Expr.
//
func (*BadExpr) exprNode() {}
func (*Ident) exprNode() {}
// ...
// stmtNode() ensures that only statement nodes can be
// assigned to a Stmt.
//
func (*BadStmt) stmtNode() {}
func (*DeclStmt) stmtNode() {}
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment