php-parser/main.go

107 lines
1.8 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
2017-12-01 16:04:31 +00:00
"github.com/yookoala/realpath"
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-02-06 10:39:42 +00:00
2018-04-10 17:15:15 +00:00
func main() {
usePhp5 = flag.Bool("php5", false, "use PHP5 parser")
2017-12-01 16:04:31 +00:00
flag.Parse()
2018-04-10 17:15:15 +00:00
pathCh := make(chan string)
resultCh := make(chan Parser)
// run 4 concurrent parsers
for i := 0; i < 4; i++ {
go parser(pathCh, resultCh)
}
// 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
}
2018-04-10 17:15:15 +00:00
func parser(pathCh <-chan string, result chan<- Parser) {
var parser Parser
for {
path := <-pathCh
src, _ := os.Open(path)
if *usePhp5 {
parser = php5.NewParser(src, path)
} else {
parser = php7.NewParser(src, path)
}
parser.Parse()
result <- parser
}
}
func printer(result <-chan Parser) {
for {
parser := <-result
fmt.Printf("==> %s\n", parser.GetPath())
for _, e := range parser.GetErrors() {
fmt.Println(e)
}
nsResolver := visitor.NewNamespaceResolver()
parser.GetRootNode().Walk(nsResolver)
dumper := visitor.Dumper{
Writer: os.Stdout,
Indent: " | ",
Comments: parser.GetComments(),
Positions: parser.GetPositions(),
NsResolver: nsResolver,
}
parser.GetRootNode().Walk(dumper)
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
}
}