php-parser/cmd/php-parser/main.go

212 lines
4.5 KiB
Go
Raw Normal View History

2017-12-01 13:29:23 +00:00
package main
import (
2020-12-11 11:28:16 +00:00
"bytes"
2017-12-01 16:04:31 +00:00
"flag"
2020-12-29 19:23:22 +00:00
"fmt"
2020-07-04 16:14:54 +00:00
"io"
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"
2020-07-04 16:14:54 +00:00
"strconv"
2018-04-10 17:15:15 +00:00
"sync"
2020-06-29 11:15:58 +00:00
"time"
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"
2020-05-18 19:36:39 +00:00
2023-12-09 21:36:19 +00:00
"git.maride.cc/maride/php-parser/pkg/ast"
"git.maride.cc/maride/php-parser/pkg/conf"
"git.maride.cc/maride/php-parser/pkg/errors"
"git.maride.cc/maride/php-parser/pkg/parser"
"git.maride.cc/maride/php-parser/pkg/version"
"git.maride.cc/maride/php-parser/pkg/visitor/dumper"
"git.maride.cc/maride/php-parser/pkg/visitor/nsresolver"
"git.maride.cc/maride/php-parser/pkg/visitor/printer"
"git.maride.cc/maride/php-parser/pkg/visitor/traverser"
2017-12-01 16:04:31 +00:00
)
2017-12-01 13:29:23 +00:00
2023-03-25 14:15:30 +00:00
var (
wg sync.WaitGroup
phpVersion *version.Version
profiler string
dump *bool
showResolvedNs *bool
printBack *bool
printPath *bool
printErrors *bool
printExecTime *bool
)
2018-02-06 10:39:42 +00:00
2018-07-11 15:59:04 +00:00
type file struct {
path string
content []byte
}
2019-03-10 21:37:01 +00:00
type result struct {
2020-06-29 20:00:56 +00:00
path string
rootNode ast.Vertex
errors []*errors.Error
2019-03-10 21:37:01 +00:00
}
2018-04-10 17:15:15 +00:00
func main() {
2020-06-29 11:15:58 +00:00
start := time.Now()
2020-12-29 19:23:22 +00:00
var phpVer string
2020-06-29 11:15:58 +00:00
printExecTime = flag.Bool("time", false, "print execution time")
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")
2020-03-09 10:44:10 +00:00
printPath = flag.Bool("p", false, "print filepath")
2020-07-04 16:14:54 +00:00
printErrors = flag.Bool("e", false, "print errors")
2020-05-13 18:05:15 +00:00
dump = flag.Bool("d", false, "dump")
2018-07-11 14:01:31 +00:00
flag.StringVar(&profiler, "prof", "", "start profiler: [cpu, mem, trace]")
flag.StringVar(&phpVer, "phpver", "8.0", "php version")
2018-06-19 20:55:12 +00:00
2017-12-01 16:04:31 +00:00
flag.Parse()
2020-12-29 19:23:22 +00:00
var err error
phpVersion, err = version.New(phpVer)
if err != nil {
fmt.Println("Error: " + err.Error())
os.Exit(1)
}
2019-03-10 21:37:01 +00:00
if len(flag.Args()) == 0 {
2019-03-06 20:28:15 +00:00
flag.Usage()
return
}
2018-06-21 17:37:34 +00:00
switch profiler {
case "cpu":
defer profile.Start(profile.ProfilePath("."), profile.NoShutdownHook).Stop()
case "mem":
2023-03-25 14:15:30 +00:00
defer profile.Start(profile.MemProfile, profile.ProfilePath("."), profile.NoShutdownHook).
Stop()
2018-07-11 14:01:31 +00:00
case "trace":
2023-03-25 14:15:30 +00:00
defer profile.Start(profile.TraceProfile, profile.ProfilePath("."), profile.NoShutdownHook).
Stop()
2018-06-21 17:37:34 +00:00
}
2019-03-10 21:37:01 +00:00
numCpu := runtime.GOMAXPROCS(0)
2018-07-11 15:59:04 +00:00
fileCh := make(chan *file, numCpu)
2019-03-10 21:37:01 +00:00
resultCh := make(chan result, 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)
2020-06-29 11:15:58 +00:00
elapsed := time.Since(start)
if *printExecTime {
log.Printf("took: %s", elapsed)
}
2018-04-10 17:15:15 +00:00
}
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)
2019-02-25 14:11:51 +00:00
err = filepath.Walk(real, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() && filepath.Ext(path) == ".php" {
wg.Add(1)
content, err := os.ReadFile(path)
2019-02-25 14:11:51 +00:00
checkErr(err)
fileCh <- &file{path, content}
}
return nil
})
2018-01-09 22:03:53 +00:00
checkErr(err)
2017-12-01 14:04:53 +00:00
}
2017-12-01 16:04:31 +00:00
}
2019-03-10 21:37:01 +00:00
func parserWorker(fileCh <-chan *file, r chan<- result) {
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
}
2020-12-29 19:23:22 +00:00
var parserErrors []*errors.Error
2021-02-13 21:54:34 +00:00
rootNode, err := parser.Parse(f.content, conf.Config{
2020-12-29 19:23:22 +00:00
Version: phpVersion,
2020-06-29 20:00:56 +00:00
ErrorHandlerFunc: func(e *errors.Error) {
parserErrors = append(parserErrors, e)
},
2020-12-29 19:23:22 +00:00
})
2019-12-26 15:57:56 +00:00
if err != nil {
2020-12-29 19:23:22 +00:00
fmt.Println("Error:" + err.Error())
os.Exit(1)
2018-04-10 17:15:15 +00:00
}
2020-06-29 20:00:56 +00:00
r <- result{path: f.path, rootNode: rootNode, errors: parserErrors}
2018-04-10 17:15:15 +00:00
}
}
2019-03-10 21:37:01 +00:00
func printerWorker(r <-chan result) {
2018-06-21 22:00:02 +00:00
var counter int
2018-04-10 17:15:15 +00:00
for {
2019-03-10 21:37:01 +00:00
res, ok := <-r
2018-06-06 16:47:28 +00:00
if !ok {
return
}
2018-06-21 22:00:02 +00:00
counter++
2020-03-09 10:44:10 +00:00
if *printPath {
_, _ = io.WriteString(os.Stderr, "==> ["+strconv.Itoa(counter)+"] "+res.path+"\n")
2020-03-09 10:44:10 +00:00
}
2018-04-10 17:15:15 +00:00
if *printErrors && len(res.errors) > 0 {
_, _ = io.WriteString(os.Stderr, "==> ["+strconv.Itoa(counter)+"] "+res.path+"\n")
2020-07-04 16:14:54 +00:00
for _, e := range res.errors {
_, _ = io.WriteString(os.Stderr, "==> "+e.String()+"\n")
2020-07-04 16:14:54 +00:00
}
_, _ = io.WriteString(os.Stderr, "\n")
2018-04-10 17:15:15 +00:00
}
2020-05-18 19:36:39 +00:00
if *printBack {
2020-12-11 11:28:16 +00:00
o := bytes.NewBuffer([]byte{})
2020-12-28 19:13:08 +00:00
p := printer.NewPrinter(o)
2020-12-11 11:28:16 +00:00
res.rootNode.Accept(p)
err := os.WriteFile(res.path, o.Bytes(), 0644)
2020-12-11 11:28:16 +00:00
checkErr(err)
2020-05-18 19:36:39 +00:00
}
2018-06-19 20:55:12 +00:00
if *showResolvedNs {
2020-12-28 19:13:08 +00:00
v := nsresolver.NewNamespaceResolver()
traverser.NewTraverser(v).Traverse(res.rootNode)
2020-07-04 16:14:54 +00:00
for _, n := range v.ResolvedNames {
_, _ = io.WriteString(os.Stderr, "===> "+n+"\n")
2020-07-04 16:14:54 +00:00
}
2018-06-19 20:55:12 +00:00
}
2023-03-25 14:16:41 +00:00
if *dump {
2020-12-28 19:13:08 +00:00
dumper.NewDumper(os.Stdout).WithPositions().WithTokens().Dump(res.rootNode)
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
}
}