Last active
December 25, 2016 15:53
-
-
Save tenntenn/69ab226b794410d87e389239efcf87ce to your computer and use it in GitHub Desktop.
ASTを取得する方法を調べる #golang ref: http://qiita.com/tenntenn/items/13340f2845316532b55a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func ParseDir(fset *token.FileSet, path string, filter func(os.FileInfo) bool, mode Mode) (pkgs map[string]*ast.Package, first error) | |
func ParseFile(fset *token.FileSet, filename string, src interface{}, mode Mode) (f *ast.File, err error) | |
func ParseExpr(x string) (ast.Expr, error) | |
func ParseExprFrom(fset *token.FileSet, filename string, src interface{}, mode Mode) (ast.Expr, error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fset := token.NewFileSet() | |
_, err := parser.ParseFile(fset, "sample.go", []byte(src), parser.DeclarationErrors|parser.AllErrors) | |
if err != nil { | |
switch err := err.(type) { | |
case scanner.ErrorList: | |
for _, e := range err { | |
fmt.Printf("%s:%d:%d %s\n", e.Pos.Filename, e.Pos.Line, e.Pos.Column, e.Msg) | |
} | |
default: | |
fmt.Println(err) | |
} | |
return | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func ParseExpr(x string) (ast.Expr, error) { | |
return ParseExprFrom(token.NewFileSet(), "", []byte(x), 0) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (p *parser) init(fset *token.FileSet, filename string, src []byte, mode Mode) { | |
p.file = fset.AddFile(filename, -1, len(src)) | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (s *FileSet) AddFile(filename string, base, size int) *File { | |
s.mutex.Lock() | |
defer s.mutex.Unlock() | |
if base < 0 { | |
base = s.base | |
} | |
if base < s.base || size < 0 { | |
panic("illegal base or size") | |
} | |
// base >= s.base && size >= 0 | |
f := &File{s, filename, base, size, []int{0}, nil} | |
base += size + 1 // +1 because EOF also has a position | |
if base < 0 { | |
panic("token.Pos offset overflow (> 2G of source code in file set)") | |
} | |
// add the file to the file set | |
s.base = base | |
s.files = append(s.files, f) | |
s.last = f | |
return f | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func ParseDir(fset *token.FileSet, path string, filter func(os.FileInfo) bool, mode Mode) (pkgs map[string]*ast.Package, first error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Package struct { | |
Name string // package name | |
Scope *Scope // package scope across all files | |
Imports map[string]*Object // map of package id -> package object | |
Files map[string]*File // Go source files by filename | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func ParseFile(fset *token.FileSet, filename string, src interface{}, mode Mode) (f *ast.File, err error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type File struct { | |
Doc *CommentGroup // associated documentation; or nil | |
Package token.Pos // position of "package" keyword | |
Name *Ident // package name | |
Decls []Decl // top-level declarations; or nil | |
Scope *Scope // package scope (this file only) | |
Imports []*ImportSpec // imports in this file | |
Unresolved []*Ident // unresolved identifiers in this file | |
Comments []*CommentGroup // list of all comments in the source file | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Mode uint | |
const ( | |
PackageClauseOnly Mode = 1 << iota // stop parsing after package clause | |
ImportsOnly // stop parsing after import declarations | |
ParseComments // parse comments and add them to AST | |
Trace // print a trace of parsed productions | |
DeclarationErrors // report declaration errors | |
SpuriousErrors // same as AllErrors, for backward-compatibility | |
AllErrors = SpuriousErrors // report all errors (not just the first 10 on different lines) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment