php-parser/main.go

171 lines
3.5 KiB
Go
Raw Normal View History

2017-12-01 13:29:23 +00:00
package main
import (
2017-12-01 16:04:31 +00:00
"flag"
"fmt"
"log"
2017-12-01 13:29:23 +00:00
"os"
2018-01-09 22:03:53 +00:00
"path/filepath"
2018-04-10 17:15:15 +00:00
"sync"
2017-12-01 13:29:23 +00:00
2018-06-21 17:37:34 +00:00
"github.com/pkg/profile"
2017-12-01 16:04:31 +00:00
"github.com/yookoala/realpath"
"github.com/z7zmey/php-parser/parser"
2018-01-26 13:24:56 +00:00
"github.com/z7zmey/php-parser/php5"
2018-02-06 10:39:42 +00:00
"github.com/z7zmey/php-parser/php7"
2018-02-04 19:35:46 +00:00
"github.com/z7zmey/php-parser/visitor"
2017-12-01 16:04:31 +00:00
)
2017-12-01 13:29:23 +00:00
2018-04-10 17:15:15 +00:00
var wg sync.WaitGroup
var usePhp5 *bool
2018-06-19 20:55:12 +00:00
var dumpType string
2018-06-21 17:37:34 +00:00
var profiler string
2018-06-19 20:55:12 +00:00
var showPositions *bool
var showComments *bool
var showResolvedNs *bool
2018-02-06 10:39:42 +00:00
2018-04-10 17:15:15 +00:00
func main() {
2018-06-19 20:55:12 +00:00
usePhp5 = flag.Bool("php5", false, "parse as PHP5")
showPositions = flag.Bool("p", false, "show positions")
showComments = flag.Bool("c", false, "show comments")
showResolvedNs = flag.Bool("r", false, "resolve names")
flag.StringVar(&dumpType, "d", "", "dump format: [custom, go, json, pretty_json]")
2018-06-21 17:37:34 +00:00
flag.StringVar(&profiler, "prof", "", "start profiler: [cpu, mem]")
2018-06-19 20:55:12 +00:00
2017-12-01 16:04:31 +00:00
flag.Parse()
2018-06-21 17:37:34 +00:00
switch profiler {
case "cpu":
defer profile.Start(profile.ProfilePath("."), profile.NoShutdownHook).Stop()
case "mem":
defer profile.Start(profile.MemProfile, profile.ProfilePath("."), profile.NoShutdownHook).Stop()
}
2018-04-10 17:15:15 +00:00
pathCh := make(chan string)
resultCh := make(chan parser.Parser)
2018-04-10 17:15:15 +00:00
// run 4 concurrent parserWorkers
2018-04-10 17:15:15 +00:00
for i := 0; i < 4; i++ {
go parserWorker(pathCh, resultCh)
2018-04-10 17:15:15 +00:00
}
// run printer goroutine
go printer(resultCh)
// process files
processPath(flag.Args(), pathCh)
// wait the all files done
wg.Wait()
close(pathCh)
close(resultCh)
}
func processPath(pathList []string, pathCh chan<- string) {
for _, path := range pathList {
2017-12-01 16:04:31 +00:00
real, err := realpath.Realpath(path)
checkErr(err)
2018-01-09 22:03:53 +00:00
err = filepath.Walk(real, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() && filepath.Ext(path) == ".php" {
2018-04-10 17:15:15 +00:00
wg.Add(1)
pathCh <- path
2018-01-09 22:03:53 +00:00
}
return nil
})
checkErr(err)
2017-12-01 14:04:53 +00:00
}
2017-12-01 16:04:31 +00:00
}
func parserWorker(pathCh <-chan string, result chan<- parser.Parser) {
var parserWorker parser.Parser
2018-04-10 17:15:15 +00:00
for {
2018-06-06 16:47:28 +00:00
path, ok := <-pathCh
if !ok {
return
}
2018-04-10 17:15:15 +00:00
src, _ := os.Open(path)
if *usePhp5 {
parserWorker = php5.NewParser(src, path)
2018-04-10 17:15:15 +00:00
} else {
parserWorker = php7.NewParser(src, path)
2018-04-10 17:15:15 +00:00
}
parserWorker.Parse()
result <- parserWorker
2018-04-10 17:15:15 +00:00
}
}
func printer(result <-chan parser.Parser) {
2018-04-10 17:15:15 +00:00
for {
2018-06-06 16:47:28 +00:00
parserWorker, ok := <-result
if !ok {
return
}
fmt.Printf("==> %s\n", parserWorker.GetPath())
2018-04-10 17:15:15 +00:00
for _, e := range parserWorker.GetErrors() {
2018-04-10 17:15:15 +00:00
fmt.Println(e)
}
2018-06-19 20:55:12 +00:00
var nsResolver *visitor.NamespaceResolver
if *showResolvedNs {
nsResolver = visitor.NewNamespaceResolver()
2018-06-05 09:22:36 +00:00
parserWorker.GetRootNode().Walk(nsResolver)
2018-06-19 20:55:12 +00:00
}
var comments parser.Comments
if *showComments {
comments = parserWorker.GetComments()
}
2018-04-10 17:15:15 +00:00
2018-06-19 20:55:12 +00:00
var positions parser.Positions
if *showPositions {
positions = parserWorker.GetPositions()
}
switch dumpType {
case "custom":
2018-06-18 20:29:52 +00:00
dumper := &visitor.Dumper{
2018-06-05 09:22:36 +00:00
Writer: os.Stdout,
2018-06-19 20:55:12 +00:00
Indent: "| ",
Comments: comments,
Positions: positions,
NsResolver: nsResolver,
}
parserWorker.GetRootNode().Walk(dumper)
case "json":
dumper := &visitor.JsonDumper{
Writer: os.Stdout,
Comments: comments,
Positions: positions,
NsResolver: nsResolver,
}
parserWorker.GetRootNode().Walk(dumper)
case "pretty_json":
dumper := &visitor.PrettyJsonDumper{
Writer: os.Stdout,
Comments: comments,
Positions: positions,
2018-06-05 09:22:36 +00:00
NsResolver: nsResolver,
}
parserWorker.GetRootNode().Walk(dumper)
2018-06-19 20:55:12 +00:00
case "go":
dumper := &visitor.GoDumper{Writer: os.Stdout}
parserWorker.GetRootNode().Walk(dumper)
2018-04-10 17:15:15 +00:00
}
2018-06-19 20:55:12 +00:00
2018-04-10 17:15:15 +00:00
wg.Done()
}
}
2017-12-01 16:04:31 +00:00
func checkErr(err error) {
if err != nil {
log.Fatal(err)
2017-12-01 14:04:53 +00:00
}
}