Parsing Go source code
This commit is contained in:
parent
8ba8b75b48
commit
3f8ec92869
1 changed files with 29 additions and 0 deletions
|
|
@ -2,10 +2,39 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
type visitor int
|
||||
|
||||
func (v visitor) Visit(n ast.Node) ast.Visitor {
|
||||
if n == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
fmt.Printf("%s%T\n", strings.Repeat("\t", int(v)), n)
|
||||
return v + 1
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) == 1 {
|
||||
fmt.Println("Not enough arguments!")
|
||||
return
|
||||
}
|
||||
|
||||
for _, file := range os.Args[1:] {
|
||||
fmt.Println("Processing:", file)
|
||||
one := token.NewFileSet()
|
||||
var v visitor
|
||||
f, err := parser.ParseFile(one, file, nil, parser.AllErrors)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
ast.Walk(v, f)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue