move Parser interface to parser package

This commit is contained in:
z7zmey 2018-04-10 23:59:57 +03:00
parent 04bd98eec2
commit 8d6affdd68
2 changed files with 21 additions and 19 deletions

37
main.go
View File

@ -9,6 +9,7 @@ import (
"sync" "sync"
"github.com/yookoala/realpath" "github.com/yookoala/realpath"
"github.com/z7zmey/php-parser/parser"
"github.com/z7zmey/php-parser/php5" "github.com/z7zmey/php-parser/php5"
"github.com/z7zmey/php-parser/php7" "github.com/z7zmey/php-parser/php7"
"github.com/z7zmey/php-parser/visitor" "github.com/z7zmey/php-parser/visitor"
@ -18,15 +19,15 @@ var wg sync.WaitGroup
var usePhp5 *bool var usePhp5 *bool
func main() { func main() {
usePhp5 = flag.Bool("php5", false, "use PHP5 parser") usePhp5 = flag.Bool("php5", false, "use PHP5 parserWorker")
flag.Parse() flag.Parse()
pathCh := make(chan string) pathCh := make(chan string)
resultCh := make(chan Parser) resultCh := make(chan parser.Parser)
// run 4 concurrent parsers // run 4 concurrent parserWorkers
for i := 0; i < 4; i++ { for i := 0; i < 4; i++ {
go parser(pathCh, resultCh) go parserWorker(pathCh, resultCh)
} }
// run printer goroutine // run printer goroutine
@ -57,44 +58,44 @@ func processPath(pathList []string, pathCh chan<- string) {
} }
} }
func parser(pathCh <-chan string, result chan<- Parser) { func parserWorker(pathCh <-chan string, result chan<- parser.Parser) {
var parser Parser var parserWorker parser.Parser
for { for {
path := <-pathCh path := <-pathCh
src, _ := os.Open(path) src, _ := os.Open(path)
if *usePhp5 { if *usePhp5 {
parser = php5.NewParser(src, path) parserWorker = php5.NewParser(src, path)
} else { } else {
parser = php7.NewParser(src, path) parserWorker = php7.NewParser(src, path)
} }
parser.Parse() parserWorker.Parse()
result <- parser result <- parserWorker
} }
} }
func printer(result <-chan Parser) { func printer(result <-chan parser.Parser) {
for { for {
parser := <-result parserWorker := <-result
fmt.Printf("==> %s\n", parser.GetPath()) fmt.Printf("==> %s\n", parserWorker.GetPath())
for _, e := range parser.GetErrors() { for _, e := range parserWorker.GetErrors() {
fmt.Println(e) fmt.Println(e)
} }
nsResolver := visitor.NewNamespaceResolver() nsResolver := visitor.NewNamespaceResolver()
parser.GetRootNode().Walk(nsResolver) parserWorker.GetRootNode().Walk(nsResolver)
dumper := visitor.Dumper{ dumper := visitor.Dumper{
Writer: os.Stdout, Writer: os.Stdout,
Indent: " | ", Indent: " | ",
Comments: parser.GetComments(), Comments: parserWorker.GetComments(),
Positions: parser.GetPositions(), Positions: parserWorker.GetPositions(),
NsResolver: nsResolver, NsResolver: nsResolver,
} }
parser.GetRootNode().Walk(dumper) parserWorker.GetRootNode().Walk(dumper)
wg.Done() wg.Done()
} }
} }

View File

@ -1,4 +1,4 @@
package main package parser
import ( import (
"github.com/z7zmey/php-parser/comment" "github.com/z7zmey/php-parser/comment"
@ -7,6 +7,7 @@ import (
"github.com/z7zmey/php-parser/position" "github.com/z7zmey/php-parser/position"
) )
// Parser interface
type Parser interface { type Parser interface {
Parse() int Parse() int
GetPath() string GetPath() string