issue #8: cli - parse concurrently

This commit is contained in:
z7zmey 2018-04-10 20:15:15 +03:00
parent 0cf164c11c
commit 316ed6e851
4 changed files with 106 additions and 49 deletions

116
main.go
View File

@ -6,66 +6,50 @@ import (
"log"
"os"
"path/filepath"
"sync"
"github.com/yookoala/realpath"
"github.com/z7zmey/php-parser/comment"
"github.com/z7zmey/php-parser/errors"
"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
var errors []*errors.Error
var wg sync.WaitGroup
var usePhp5 *bool
usePhp5 := flag.Bool("php5", false, "use PHP5 parser")
func main() {
usePhp5 = flag.Bool("php5", false, "use PHP5 parser")
flag.Parse()
for _, path := range flag.Args() {
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 {
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 {
parser := php5.NewParser(src, path)
parser.Parse()
nodes = parser.GetRootNode()
errors = parser.GetErrors()
comments = parser.GetComments()
positions = parser.GetPositions()
} else {
parser := php7.NewParser(src, path)
parser.Parse()
nodes = parser.GetRootNode()
errors = parser.GetErrors()
comments = parser.GetComments()
positions = parser.GetPositions()
}
for _, e := range errors {
fmt.Println(e)
}
nsResolver := visitor.NewNamespaceResolver()
nodes.Walk(nsResolver)
dumper := visitor.Dumper{
Writer: os.Stdout,
Indent: " | ",
Comments: comments,
Positions: positions,
NsResolver: nsResolver,
}
nodes.Walk(dumper)
wg.Add(1)
pathCh <- path
}
return nil
})
@ -73,6 +57,48 @@ func main() {
}
}
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()
}
}
func checkErr(err error) {
if err != nil {
log.Fatal(err)

17
parser.go Normal file
View File

@ -0,0 +1,17 @@
package main
import (
"github.com/z7zmey/php-parser/comment"
"github.com/z7zmey/php-parser/errors"
"github.com/z7zmey/php-parser/node"
"github.com/z7zmey/php-parser/position"
)
type Parser interface {
Parse() int
GetPath() string
GetRootNode() node.Node
GetErrors() []*errors.Error
GetComments() comment.Comments
GetPositions() position.Positions
}

View File

@ -18,6 +18,7 @@ func (lval *yySymType) Token(t token.Token) {
// Parser structure
type Parser struct {
*scanner.Lexer
path string
lastToken *token.Token
positionBuilder *position.Builder
errors []*errors.Error
@ -27,11 +28,12 @@ type Parser struct {
}
// NewParser creates and returns new Parser
func NewParser(src io.Reader, fName string) *Parser {
lexer := scanner.NewLexer(src, fName)
func NewParser(src io.Reader, path string) *Parser {
lexer := scanner.NewLexer(src, path)
return &Parser{
lexer,
path,
nil,
nil,
nil,
@ -81,6 +83,11 @@ func (l *Parser) listGetFirstNodeComments(list []node.Node) []comment.Comment {
return l.comments[node]
}
// GetPath return path to file
func (l *Parser) GetPath() string {
return l.path
}
// GetRootNode returns root node
func (l *Parser) GetRootNode() node.Node {
return l.rootNode

View File

@ -18,6 +18,7 @@ func (lval *yySymType) Token(t token.Token) {
// Parser structure
type Parser struct {
*scanner.Lexer
path string
lastToken *token.Token
positionBuilder *position.Builder
errors []*errors.Error
@ -27,11 +28,12 @@ type Parser struct {
}
// NewParser creates and returns new Parser
func NewParser(src io.Reader, fName string) *Parser {
lexer := scanner.NewLexer(src, fName)
func NewParser(src io.Reader, path string) *Parser {
lexer := scanner.NewLexer(src, path)
return &Parser{
lexer,
path,
nil,
nil,
nil,
@ -81,6 +83,11 @@ func (l *Parser) listGetFirstNodeComments(list []node.Node) []comment.Comment {
return l.comments[node]
}
// GetPath return path to file
func (l *Parser) GetPath() string {
return l.path
}
// GetRootNode returns root node
func (l *Parser) GetRootNode() node.Node {
return l.rootNode