php-parser/main.go

208 lines
4.3 KiB
Go
Raw Normal View History

2017-12-01 13:29:23 +00:00
package main
import (
2018-07-11 15:59:04 +00:00
"bufio"
"bytes"
2017-12-01 16:04:31 +00:00
"flag"
"fmt"
2018-07-11 15:59:04 +00:00
"io/ioutil"
2017-12-01 16:04:31 +00:00
"log"
2017-12-01 13:29:23 +00:00
"os"
2018-01-09 22:03:53 +00:00
"path/filepath"
2018-07-11 15:59:04 +00:00
"runtime"
2018-04-10 17:15:15 +00:00
"sync"
2017-12-01 13:29:23 +00:00
2018-06-25 15:20:16 +00:00
"github.com/karrick/godirwalk"
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"
"github.com/z7zmey/php-parser/printer"
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
var withMeta *bool
2018-06-19 20:55:12 +00:00
var showResolvedNs *bool
var printBack *bool
2018-02-06 10:39:42 +00:00
2018-07-11 15:59:04 +00:00
type file struct {
path string
content []byte
}
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")
withMeta = flag.Bool("meta", false, "show meta")
2018-06-19 20:55:12 +00:00
showResolvedNs = flag.Bool("r", false, "resolve names")
printBack = flag.Bool("pb", false, "print AST back into the parsed file")
2018-06-19 20:55:12 +00:00
flag.StringVar(&dumpType, "d", "", "dump format: [custom, go, json, pretty_json]")
2018-07-11 14:01:31 +00:00
flag.StringVar(&profiler, "prof", "", "start profiler: [cpu, mem, trace]")
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-07-11 14:01:31 +00:00
case "trace":
defer profile.Start(profile.TraceProfile, profile.ProfilePath("."), profile.NoShutdownHook).Stop()
2018-06-21 17:37:34 +00:00
}
2018-07-11 15:59:04 +00:00
numCpu := runtime.NumCPU()
fileCh := make(chan *file, numCpu)
resultCh := make(chan parser.Parser, numCpu)
2018-04-10 17:15:15 +00:00
// run 4 concurrent parserWorkers
2018-07-11 15:59:04 +00:00
for i := 0; i < numCpu; i++ {
go parserWorker(fileCh, resultCh)
2018-04-10 17:15:15 +00:00
}
// run printer goroutine
go printerWorker(resultCh)
2018-04-10 17:15:15 +00:00
// process files
2018-07-11 15:59:04 +00:00
processPath(flag.Args(), fileCh)
2018-04-10 17:15:15 +00:00
// wait the all files done
wg.Wait()
2018-07-11 15:59:04 +00:00
close(fileCh)
2018-04-10 17:15:15 +00:00
close(resultCh)
}
2018-07-11 15:59:04 +00:00
func processPath(pathList []string, fileCh chan<- *file) {
2018-04-10 17:15:15 +00:00
for _, path := range pathList {
2017-12-01 16:04:31 +00:00
real, err := realpath.Realpath(path)
checkErr(err)
2018-06-25 15:20:16 +00:00
s, err := os.Stat(real)
2018-01-09 22:03:53 +00:00
checkErr(err)
2018-06-25 15:20:16 +00:00
if !s.IsDir() {
wg.Add(1)
2018-07-11 15:59:04 +00:00
content, err := ioutil.ReadFile(real)
checkErr(err)
fileCh <- &file{real, content}
2018-06-25 15:20:16 +00:00
} else {
godirwalk.Walk(real, &godirwalk.Options{
Unsorted: true,
Callback: func(osPathname string, de *godirwalk.Dirent) error {
if !de.IsDir() && filepath.Ext(osPathname) == ".php" {
wg.Add(1)
2018-07-11 15:59:04 +00:00
content, err := ioutil.ReadFile(osPathname)
checkErr(err)
fileCh <- &file{osPathname, content}
2018-06-25 15:20:16 +00:00
}
return nil
},
ErrorCallback: func(osPathname string, err error) godirwalk.ErrorAction {
return godirwalk.SkipNode
},
})
}
2017-12-01 14:04:53 +00:00
}
2017-12-01 16:04:31 +00:00
}
2018-07-11 15:59:04 +00:00
func parserWorker(fileCh <-chan *file, result chan<- parser.Parser) {
var parserWorker parser.Parser
2018-04-10 17:15:15 +00:00
for {
2018-07-11 15:59:04 +00:00
f, ok := <-fileCh
2018-06-06 16:47:28 +00:00
if !ok {
return
}
2018-07-11 15:59:04 +00:00
src := bytes.NewReader(f.content)
2018-04-10 17:15:15 +00:00
if *usePhp5 {
2018-07-11 15:59:04 +00:00
parserWorker = php5.NewParser(src, f.path)
2018-04-10 17:15:15 +00:00
} else {
2018-07-11 15:59:04 +00:00
parserWorker = php7.NewParser(src, f.path)
2018-04-10 17:15:15 +00:00
}
if *withMeta {
parserWorker.WithMeta()
}
parserWorker.Parse()
2018-06-25 17:14:36 +00:00
result <- parserWorker
2018-04-10 17:15:15 +00:00
}
}
func printerWorker(result <-chan parser.Parser) {
2018-06-21 22:00:02 +00:00
var counter int
2018-07-11 15:59:04 +00:00
w := bufio.NewWriter(os.Stdout)
2018-04-10 17:15:15 +00:00
for {
2018-06-06 16:47:28 +00:00
parserWorker, ok := <-result
if !ok {
2018-07-11 15:59:04 +00:00
w.Flush()
2018-06-06 16:47:28 +00:00
return
}
2018-06-21 22:00:02 +00:00
counter++
2018-07-11 15:59:04 +00:00
fmt.Fprintf(w, "==> [%d] %s\n", counter, parserWorker.GetPath())
2018-04-10 17:15:15 +00:00
for _, e := range parserWorker.GetErrors() {
2018-07-11 15:59:04 +00:00
fmt.Fprintln(w, e)
2018-04-10 17:15:15 +00:00
}
if *printBack {
o := bytes.NewBuffer([]byte{})
p := printer.NewPrinter(o)
p.Print(parserWorker.GetRootNode())
err := ioutil.WriteFile(parserWorker.GetPath(), o.Bytes(), 0644)
checkErr(err)
}
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
}
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: "| ",
NsResolver: nsResolver,
}
parserWorker.GetRootNode().Walk(dumper)
case "json":
dumper := &visitor.JsonDumper{
Writer: os.Stdout,
NsResolver: nsResolver,
}
parserWorker.GetRootNode().Walk(dumper)
case "pretty_json":
dumper := &visitor.PrettyJsonDumper{
Writer: os.Stdout,
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
}
}