Skip to content

Commit 4d6130d

Browse files
authored
Merge pull request z7zmey#117 from z7zmey/refactoring
Refactoring
2 parents 0cd26d7 + 5fd7577 commit 4d6130d

File tree

357 files changed

+163638
-130246
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

357 files changed

+163638
-130246
lines changed

.gitattributes

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
internal/php5/php5.go -diff -merge
2+
internal/php5/php5.go linguist-generated=true
3+
internal/php7/php7.go -diff -merge
4+
internal/php7/php7.go linguist-generated=true
5+
internal/scanner/scanner.go -diff -merge
6+
internal/scanner/scanner.go linguist-generated=true

Makefile

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
PHPFILE=example.php
22

3-
all: compile fmt build run
3+
all: compile fmt build
44

55
fmt:
66
find . -type f -iregex '.*\.go' -exec gofmt -l -s -w '{}' +
77

88
build:
99
go generate ./...
10-
go build
11-
12-
run:
13-
./php-parser -d go $(PHPFILE)
10+
go build ./cmd/...
1411

1512
test:
1613
go test ./...
@@ -19,38 +16,38 @@ cover:
1916
go test ./... --cover
2017

2118
bench:
22-
go test -benchmem -bench=. ./php5
23-
go test -benchmem -bench=. ./php7
24-
25-
compile: ./php5/php5.go ./php7/php7.go ./scanner/scanner.go fmt
26-
sed -i '' -e 's/yyErrorVerbose = false/yyErrorVerbose = true/g' ./php7/php7.go
27-
sed -i '' -e 's/yyErrorVerbose = false/yyErrorVerbose = true/g' ./php5/php5.go
28-
sed -i '' -e 's/\/\/line/\/\/ line/g' ./php5/php5.go
29-
sed -i '' -e 's/\/\/line/\/\/ line/g' ./php7/php7.go
30-
sed -i '' -e 's/\/\/line/\/\/ line/g' ./scanner/scanner.go
19+
go test -benchmem -bench=. ./internal/php5
20+
go test -benchmem -bench=. ./internal/php7
21+
22+
compile: ./internal/php5/php5.go ./internal/php7/php7.go ./internal/scanner/scanner.go
23+
sed -i '' -e 's/yyErrorVerbose = false/yyErrorVerbose = true/g' ./internal/php7/php7.go
24+
sed -i '' -e 's/yyErrorVerbose = false/yyErrorVerbose = true/g' ./internal/php5/php5.go
25+
sed -i '' -e 's/\/\/line/\/\/ line/g' ./internal/php5/php5.go
26+
sed -i '' -e 's/\/\/line/\/\/ line/g' ./internal/php7/php7.go
27+
sed -i '' -e 's/\/\/line/\/\/ line/g' ./internal/scanner/scanner.go
3128
rm -f y.output
3229

33-
./scanner/scanner.go: ./scanner/scanner.rl
30+
./internal/scanner/scanner.go: ./internal/scanner/scanner.rl
3431
ragel -Z -G2 -o $@ $<
3532

36-
./php5/php5.go: ./php5/php5.y
33+
./internal/php5/php5.go: ./internal/php5/php5.y
3734
goyacc -o $@ $<
3835

39-
./php7/php7.go: ./php7/php7.y
36+
./internal/php7/php7.go: ./internal/php7/php7.y
4037
goyacc -o $@ $<
4138

4239
cpu_pprof:
43-
go test -cpuprofile cpu.pprof -bench=. -benchtime=20s ./php7
40+
go test -cpuprofile cpu.pprof -bench=. -benchtime=20s ./internal/php7
4441
go tool pprof ./php7.test cpu.pprof
4542

4643
mem_pprof:
47-
go test -memprofile mem.pprof -bench=. -benchtime=20s -benchmem ./php7
44+
go test -memprofile mem.pprof -bench=. -benchtime=20s -benchmem ./internal/php7
4845
go tool pprof -alloc_objects ./php7.test mem.pprof
4946

5047
cpu_pprof_php5:
51-
go test -cpuprofile cpu.prof -bench=. -benchtime=20s ./php5
48+
go test -cpuprofile cpu.prof -bench=. -benchtime=20s ./internal/php5
5249
go tool pprof ./php5.test cpu.prof
5350

5451
mem_pprof_php5:
55-
go test -memprofile mem.prof -bench=. -benchtime=20s -benchmem ./php5
52+
go test -memprofile mem.prof -bench=. -benchtime=20s -benchmem ./internal/php5
5653
go tool pprof -alloc_objects ./php5.test mem.prof

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

Lines changed: 62 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,73 @@ import (
44
"bytes"
55
"flag"
66
"fmt"
7+
"io"
78
"io/ioutil"
89
"log"
910
"os"
1011
"path/filepath"
1112
"runtime"
13+
"strconv"
1214
"sync"
15+
"time"
1316

1417
"github.com/pkg/profile"
1518
"github.com/yookoala/realpath"
16-
"github.com/z7zmey/php-parser/parser"
17-
"github.com/z7zmey/php-parser/printer"
18-
"github.com/z7zmey/php-parser/visitor"
19+
20+
"github.com/z7zmey/php-parser/pkg/ast"
21+
"github.com/z7zmey/php-parser/pkg/cfg"
22+
"github.com/z7zmey/php-parser/pkg/errors"
23+
"github.com/z7zmey/php-parser/pkg/parser"
24+
"github.com/z7zmey/php-parser/pkg/version"
25+
"github.com/z7zmey/php-parser/pkg/visitor/dumper"
26+
"github.com/z7zmey/php-parser/pkg/visitor/nsresolver"
27+
"github.com/z7zmey/php-parser/pkg/visitor/printer"
28+
"github.com/z7zmey/php-parser/pkg/visitor/traverser"
1929
)
2030

2131
var wg sync.WaitGroup
22-
var phpVersion string
23-
var dumpType string
32+
var phpVersion *version.Version
2433
var profiler string
25-
var withFreeFloating *bool
34+
var dump *bool
2635
var showResolvedNs *bool
2736
var printBack *bool
2837
var printPath *bool
38+
var printErrors *bool
39+
var printExecTime *bool
2940

3041
type file struct {
3142
path string
3243
content []byte
3344
}
3445

3546
type result struct {
36-
path string
37-
parser parser.Parser
47+
path string
48+
rootNode ast.Vertex
49+
errors []*errors.Error
3850
}
3951

4052
func main() {
41-
withFreeFloating = flag.Bool("ff", false, "parse and show free floating strings")
53+
start := time.Now()
54+
var phpVer string
55+
56+
printExecTime = flag.Bool("time", false, "print execution time")
4257
showResolvedNs = flag.Bool("r", false, "resolve names")
4358
printBack = flag.Bool("pb", false, "print AST back into the parsed file")
4459
printPath = flag.Bool("p", false, "print filepath")
45-
flag.StringVar(&dumpType, "d", "", "dump format: [custom, go, json, pretty_json]")
60+
printErrors = flag.Bool("e", false, "print errors")
61+
dump = flag.Bool("d", false, "dump")
4662
flag.StringVar(&profiler, "prof", "", "start profiler: [cpu, mem, trace]")
47-
flag.StringVar(&phpVersion, "phpver", "7.4", "php version")
63+
flag.StringVar(&phpVer, "phpver", "7.4", "php version")
4864

4965
flag.Parse()
5066

67+
var err error
68+
phpVersion, err = version.New(phpVer)
69+
if err != nil {
70+
fmt.Println("Error: " + err.Error())
71+
os.Exit(1)
72+
}
73+
5174
if len(flag.Args()) == 0 {
5275
flag.Usage()
5376
return
@@ -82,6 +105,11 @@ func main() {
82105
wg.Wait()
83106
close(fileCh)
84107
close(resultCh)
108+
109+
elapsed := time.Since(start)
110+
if *printExecTime {
111+
log.Printf("took: %s", elapsed)
112+
}
85113
}
86114

87115
func processPath(pathList []string, fileCh chan<- *file) {
@@ -109,18 +137,19 @@ func parserWorker(fileCh <-chan *file, r chan<- result) {
109137
return
110138
}
111139

112-
parserWorker, err := parser.NewParser(f.content, phpVersion)
140+
var parserErrors []*errors.Error
141+
rootNode, err := parser.Parse(f.content, cfg.Config{
142+
Version: phpVersion,
143+
ErrorHandlerFunc: func(e *errors.Error) {
144+
parserErrors = append(parserErrors, e)
145+
},
146+
})
113147
if err != nil {
114-
panic(err.Error())
148+
fmt.Println("Error:" + err.Error())
149+
os.Exit(1)
115150
}
116151

117-
if *withFreeFloating {
118-
parserWorker.WithFreeFloating()
119-
}
120-
121-
parserWorker.Parse()
122-
123-
r <- result{path: f.path, parser: parserWorker}
152+
r <- result{path: f.path, rootNode: rootNode, errors: parserErrors}
124153
}
125154
}
126155

@@ -136,51 +165,34 @@ func printerWorker(r <-chan result) {
136165
counter++
137166

138167
if *printPath {
139-
fmt.Fprintf(os.Stdout, "==> [%d] %s\n", counter, res.path)
168+
_, _ = io.WriteString(os.Stderr, "==> ["+strconv.Itoa(counter)+"] "+res.path+"\n")
140169
}
141170

142-
for _, e := range res.parser.GetErrors() {
143-
fmt.Fprintf(os.Stdout, "==> %s\n", e)
171+
if *printErrors {
172+
for _, e := range res.errors {
173+
_, _ = io.WriteString(os.Stderr, "==> "+e.String()+"\n")
174+
}
144175
}
145176

146177
if *printBack {
147178
o := bytes.NewBuffer([]byte{})
148179
p := printer.NewPrinter(o)
149-
p.Print(res.parser.GetRootNode())
180+
res.rootNode.Accept(p)
150181

151182
err := ioutil.WriteFile(res.path, o.Bytes(), 0644)
152183
checkErr(err)
153184
}
154185

155-
var nsResolver *visitor.NamespaceResolver
156186
if *showResolvedNs {
157-
nsResolver = visitor.NewNamespaceResolver()
158-
res.parser.GetRootNode().Walk(nsResolver)
187+
v := nsresolver.NewNamespaceResolver()
188+
traverser.NewTraverser(v).Traverse(res.rootNode)
189+
for _, n := range v.ResolvedNames {
190+
_, _ = io.WriteString(os.Stderr, "===> "+n+"\n")
191+
}
159192
}
160193

161-
switch dumpType {
162-
case "custom":
163-
dumper := &visitor.Dumper{
164-
Writer: os.Stdout,
165-
Indent: "| ",
166-
NsResolver: nsResolver,
167-
}
168-
res.parser.GetRootNode().Walk(dumper)
169-
case "json":
170-
dumper := &visitor.JsonDumper{
171-
Writer: os.Stdout,
172-
NsResolver: nsResolver,
173-
}
174-
res.parser.GetRootNode().Walk(dumper)
175-
case "pretty_json":
176-
dumper := &visitor.PrettyJsonDumper{
177-
Writer: os.Stdout,
178-
NsResolver: nsResolver,
179-
}
180-
res.parser.GetRootNode().Walk(dumper)
181-
case "go":
182-
dumper := &visitor.GoDumper{Writer: os.Stdout}
183-
res.parser.GetRootNode().Walk(dumper)
194+
if *dump == true {
195+
dumper.NewDumper(os.Stdout).WithPositions().WithTokens().Dump(res.rootNode)
184196
}
185197

186198
wg.Done()

freefloating/position_string.go

Lines changed: 0 additions & 98 deletions
This file was deleted.

0 commit comments

Comments
 (0)