forked from z7zmey/php-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (54 loc) · 1.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"github.com/yookoala/realpath"
"github.com/z7zmey/php-parser/comment"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/php5"
"github.com/z7zmey/php-parser/php7"
"github.com/z7zmey/php-parser/position"
"github.com/z7zmey/php-parser/visitor"
)
func main() {
var nodes node.Node
var comments comment.Comments
var positions position.Positions
usePhp5 := flag.Bool("php5", false, "use PHP5 parser")
flag.Parse()
for _, path := range flag.Args() {
real, err := realpath.Realpath(path)
checkErr(err)
err = filepath.Walk(real, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() && filepath.Ext(path) == ".php" {
fmt.Printf("==> %s\n", path)
src, _ := os.Open(string(path))
if *usePhp5 {
nodes, comments, positions = php5.Parse(src, path)
} else {
nodes, comments, positions = php7.Parse(src, path)
}
nsResolver := visitor.NewNamespaceResolver()
nodes.Walk(nsResolver)
dumper := visitor.Dumper{
Writer: os.Stdout,
Indent: " | ",
Comments: comments,
Positions: positions,
NsResolver: nsResolver,
}
nodes.Walk(dumper)
}
return nil
})
checkErr(err)
}
}
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}