Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
143 commits
Select commit Hold shift + click to select a range
10e2a3f
New AST structure
Mar 12, 2020
60f171b
refactor token structure
May 10, 2020
3b9ef2b
refactor position package
May 10, 2020
aab9da0
refactor position package
May 10, 2020
6a84d58
refactor php7
May 12, 2020
7e07f1c
refactor php5
May 13, 2020
12086ef
refactor cmd
May 13, 2020
50f8a47
update tests
May 14, 2020
4971309
refactor printer
May 17, 2020
f4c15f4
[refactoring] fix tests
May 17, 2020
e98607f
[refactoring] use position.Position to store token position
May 17, 2020
5620697
[refactoring] rename struct field scanner.Token.Hidden
May 17, 2020
8a50bd2
[refactoring] remove scanner.Lexer.PhpDocComment
May 17, 2020
3bee91c
[refactoring] remove scanner.Lval interface
May 17, 2020
4f79d47
[refactoring] remove unused scanner.Lexer.lastToken field
May 17, 2020
291dc7e
[refactoring] move errors from scanner to parser
May 18, 2020
d9a7d20
[refactoring] scanner.Lexer.withHiddenTokens
May 18, 2020
436287b
[refactoring] scanner DefaultConfig
May 18, 2020
a636a14
[refactoring] remove scanner interface
May 18, 2020
21f3d88
[refactoring] update makefile
May 18, 2020
009d638
[refactoring] version testing
May 18, 2020
ee673e5
[refactoring] update internal parser
Jun 29, 2020
7eff836
[refactoring] internal position package
Jun 29, 2020
424f7a1
[refactoring] remove lexer config struct
Jun 29, 2020
d7652b1
[refactoring] parsing error handler
Jun 29, 2020
ee3fe3b
[refactoring] keep $ in variable identifier
Jun 29, 2020
06d202e
[refactoring] remove param withTokens from parser
Jul 2, 2020
476e842
[refactoring] add .gitattributes to mark autogenerated files
Jul 2, 2020
84fe088
[refactoring] allow nil parser.errHandlerFunc
Jul 4, 2020
f12218d
Merge branch 'master' into refactoring
Jul 4, 2020
7fc23cc
[refactoring] cli: add -e flag
Jul 4, 2020
8417e7d
[refactoring] dumper: print node position and tokens
Jul 4, 2020
7a87466
[refactoring] dumper: fix dumping node value
Jul 4, 2020
5cc3b50
[refactoring] dumper: print Node first
Jul 4, 2020
b5e29fc
[refactoring] fix dfs traverser
Jul 5, 2020
ce18c23
[refactoring] parameters: new node structure
Jul 5, 2020
88dfd32
Merge branch 'master' into refactoring
Jul 29, 2020
feebb01
[refactoring] update `use` ast structure
Aug 3, 2020
48e0996
[refactoring] do not inherit free-floating tokens in `name` nodes
Aug 3, 2020
b7d32b0
[refactoring] store `colon` as free-floating token
Aug 3, 2020
a70e34d
[refactoring] remove unused token positions
Aug 3, 2020
4546465
[refactoring] store halt_compiler tokens
Aug 3, 2020
fd5d891
[refactoring] add Parser nodes for `use`
Aug 8, 2020
4aae540
[refactoring] store argument list parenthesis
Aug 8, 2020
b8b4439
[refactoring] store `alt_if` node brackets
Aug 9, 2020
3940922
[refactoring] store expr parenthesis
Aug 9, 2020
97747c5
[refactoring] remove scanner token
Aug 17, 2020
767187f
[refactoring] update ast structure for "Use" and "GroupUse" nodes
Aug 22, 2020
c1b3e6f
[refactoring] update ast structure of "name" nodes
Aug 24, 2020
0285900
[refactoring] update ast structure of "HaltCompiler" node
Aug 24, 2020
c632136
[refactoring] update ast structure of "Constant" nodes
Aug 24, 2020
9542085
[refactoring] update ast structure of "StmtList" nodes
Sep 2, 2020
6976388
[refactoring] update ast structure of "If", "ElseIf", "Else" nodes
Sep 3, 2020
3b85f5e
[refactoring] update ast structure of "While" node
Sep 4, 2020
c274c4f
[refactoring] update ast structure of "Do" node
Sep 4, 2020
0e73cd8
[refactoring] update ast structure of "For" node
Sep 4, 2020
f6cb2bf
[refactoring] update ast structure of "Switch", "Case", "Default" nod…
Sep 6, 2020
e817f8d
[refactoring] update ast structure of "Break", "Continue" nodes
Sep 6, 2020
59ef622
[refactoring] update ast structure of "Return" node
Sep 6, 2020
3f12ada
[refactoring] update ast structure of "Static", "Global" and "StaticV…
Sep 9, 2020
7678303
[refactoring] update ast structure of "Echo" node
Sep 10, 2020
8b4d65a
[refactoring] update ast structure of "InlinHtml" node
Sep 14, 2020
62fc16d
[refactoring] update ast structure of "Unset" node
Sep 14, 2020
69bc0af
[refactoring] update ast structure of "Foreach" node
Sep 14, 2020
e78f0dc
[refactoring] update ast structure of "Declare" and "ConstList" nodes
Sep 14, 2020
33af1df
[refactoring] update ast structure of "Nop" node
Sep 14, 2020
80aa328
[refactoring] update ast structure of "Try", "Catch" and "Finally" nodes
Sep 14, 2020
48aaa7c
[refactoring] update ast structure of "Throw" node
Sep 17, 2020
94aa9cf
[refactoring] update ast structure of "Goto" and "Label" nodes
Sep 17, 2020
bf3ae74
[refactoring] update ast structure of "Identifier"
z7zmey Nov 20, 2020
43ace92
[refactoring] update ast structure of "Nullable"
z7zmey Nov 20, 2020
ddb9f3b
[refactoring] update ast structure of parameter "Variadic" and "Refer…
z7zmey Nov 20, 2020
73d819e
[refactoring] update ast structure of parameter "Parameter" node
z7zmey Nov 20, 2020
7e2965f
[refactoring] update ast structure of "Argument" and "ArgumentList" n…
z7zmey Nov 20, 2020
e3ad974
[refactoring] update ast structure of "Lnumber" and "Dnumber" nodes
z7zmey Nov 20, 2020
e6a23df
[refactoring] update ast structure of "Encapsed", "EncapsedStringPart…
z7zmey Nov 20, 2020
3bda40e
[refactoring] update ast structure of "MagicConstant" and "String" nodes
z7zmey Nov 20, 2020
fe2e097
[refactoring] update ast structure of "Class" and "Trait" nodes
z7zmey Nov 22, 2020
4c54c56
[refactoring] update ast structure of "ClassExtends", "ClassImplement…
z7zmey Nov 22, 2020
2d6ae3a
[refactoring] update ast structure of "ClassMethod", "Function", "Arr…
z7zmey Nov 22, 2020
5bd63d9
[refactoring] update ast structure of "StmtExpression", "StmtInterfac…
z7zmey Nov 22, 2020
43f6fab
[refactoring] update ast structure of "StmtPropertyList", "StmtTraitA…
z7zmey Nov 29, 2020
47b974a
[refactoring] update ast structure of "array" and "list" nodes
z7zmey Nov 30, 2020
4b8d431
[refactoring] update ast structure of "ArrayDimFetch" and "ArrayItem"…
z7zmey Nov 30, 2020
b90400d
[refactoring] update ast structure of "BitwiseNot", "BooleanNot", "Cl…
z7zmey Dec 1, 2020
2d240e9
[refactoring] update ast structure of "ClosureUse", "ConstFetch", "Em…
z7zmey Dec 1, 2020
9b122de
[refactoring] update ast structure of "Eval", "Exit", "FunctionCall",…
z7zmey Dec 1, 2020
b5ef30e
[refactoring] update ast structure of "InstanceOf" and "Isset" nodes
z7zmey Dec 1, 2020
b1e9f5e
[refactoring] update ast structure of "ExprMethodCall" and "ExprPrope…
z7zmey Dec 3, 2020
45e9590
[refactoring] update ast structure of "PostDec", "PostInc", "PreDec",…
z7zmey Dec 3, 2020
592c9b9
[refactoring] update ast structure of "Print", "Reference", "Require"…
z7zmey Dec 3, 2020
13436b8
[refactoring] update ast structure of "ShellExec", "StaticCall", "Sta…
z7zmey Dec 3, 2020
01d695c
[refactoring] update ast structure of "ArrayDimFetch", "UnaryMinus", …
z7zmey Dec 3, 2020
5a6418e
[refactoring] update ast structure of "Yield", "YieldFrom" and "Cast"…
z7zmey Dec 4, 2020
d19b3f6
[refactoring] update ast structure of "Assign" and "Binary" nodes
z7zmey Dec 4, 2020
df1626b
[refactoring] update ast structure of "Root" and "Class" nodes
z7zmey Dec 4, 2020
f3203c0
[refactoring] update ast structure of "Parameter", "Class", "Function…
z7zmey Dec 4, 2020
132db0e
[refactoring] update ast structure of "ClassMethod", "Function", "Arr…
z7zmey Dec 4, 2020
ea3c529
[refactoring] update ast structure of "For", "TraitUseAlias" and "Tra…
z7zmey Dec 4, 2020
af968a7
[refactoring] update "ParserBrackets" nodes
z7zmey Dec 4, 2020
94897d8
[refactoring] remove *FreeFloating* functions
z7zmey Dec 4, 2020
40b944a
[refactoring] update ast structure of "ClassConstList", "Constant", "…
z7zmey Dec 4, 2020
997f7bc
refactoring: fix panic when empty return type
z7zmey Dec 4, 2020
dfe8982
refactoring: fix panic when missed arguments list
z7zmey Dec 4, 2020
f6f86bf
refactoring: fix panic when missed arguments list
z7zmey Dec 4, 2020
20a42da
[refactoring] remove general Node struct
z7zmey Dec 7, 2020
8064d94
[refactoring] update Token structure
z7zmey Dec 7, 2020
f3a605a
[refactoring] update position builder
z7zmey Dec 8, 2020
497e7f8
[refactoring] update printer
z7zmey Dec 11, 2020
6043361
[refactoring] fix namespaceResolver tests
z7zmey Dec 11, 2020
632146f
[refactoring] update dumper
z7zmey Dec 11, 2020
45ded32
[refactoring] remove ParserSeparatedList from visitor
z7zmey Dec 11, 2020
5291529
[refactoring] fix typo
z7zmey Dec 11, 2020
c32f5bd
[refactoring] remove Alt flags
z7zmey Dec 11, 2020
6941f0f
[refactoring] rename dumper
z7zmey Dec 12, 2020
7b8b1ce
refactoring: update formatter
z7zmey Dec 19, 2020
fd984fe
refactoring: update php7 tests
z7zmey Dec 20, 2020
90aca88
refactoring: update php5 tests
z7zmey Dec 20, 2020
286dd50
refactoring: update position builder tests
z7zmey Dec 20, 2020
eda7ae1
refactoring: update scanner tests
z7zmey Dec 20, 2020
74b0949
refactoring: test variable formatter
z7zmey Dec 20, 2020
03c7979
refactoring: remove StmtTraitAdaptationList node
z7zmey Dec 20, 2020
b85bae2
refactoring: update ast structure of "Closure" and "ClosureUse" nodes
z7zmey Dec 26, 2020
8bf1fa8
refactoring: update ast structure of "Foreach" node
z7zmey Dec 26, 2020
0f5f5e7
refactoring: update ast structure of "ArrayItem" node
z7zmey Dec 26, 2020
a593760
refactoring: remove ExprReference node
z7zmey Dec 26, 2020
616fd44
refactoring: rename DieTkn to ExitTkn
z7zmey Dec 26, 2020
115d481
refactoring: update ast structure of "IncludeOnce" node
z7zmey Dec 26, 2020
6991959
refactoring: short int cast
z7zmey Dec 26, 2020
2c09138
refactoring: update ast structure of "Variable" node
z7zmey Dec 27, 2020
0f2341b
refactoring: update ast structure of "MethodCall" and "PropertyFetch"…
z7zmey Dec 27, 2020
ad884c9
refactoring: update ast structure of "StaticCall" node
z7zmey Dec 27, 2020
e4321b5
refactoring: create "ExprBrackets" node
z7zmey Dec 27, 2020
38141ce
refactoring: create "ScalarEncapsedStringVar" node
z7zmey Dec 27, 2020
2990f0c
refactoring: create "ScalarEncapsedStringBrackets" node
z7zmey Dec 27, 2020
c29e8ec
refactoring: update ast structure of "Class", "Interface" and "Trait"…
z7zmey Dec 28, 2020
c0465f9
refactoring: remove "StmtTraitMethodRef" node
z7zmey Dec 28, 2020
bd47900
refactoring: update traverser
z7zmey Dec 28, 2020
07f49a4
refactoring: move internal nodes
z7zmey Dec 28, 2020
0701b35
refactoring: fix scanning double dollar in template string
z7zmey Dec 28, 2020
cb4b4e6
refactoring: fix naming
z7zmey Dec 28, 2020
e3b133f
refactoring: update api
z7zmey Dec 29, 2020
5fd7577
Merge branch 'master' into refactoring
z7zmey Dec 29, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
internal/php5/php5.go -diff -merge
internal/php5/php5.go linguist-generated=true
internal/php7/php7.go -diff -merge
internal/php7/php7.go linguist-generated=true
internal/scanner/scanner.go -diff -merge
internal/scanner/scanner.go linguist-generated=true
39 changes: 18 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
PHPFILE=example.php

all: compile fmt build run
all: compile fmt build

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

build:
go generate ./...
go build

run:
./php-parser -d go $(PHPFILE)
go build ./cmd/...

test:
go test ./...
Expand All @@ -19,38 +16,38 @@ cover:
go test ./... --cover

bench:
go test -benchmem -bench=. ./php5
go test -benchmem -bench=. ./php7

compile: ./php5/php5.go ./php7/php7.go ./scanner/scanner.go fmt
sed -i '' -e 's/yyErrorVerbose = false/yyErrorVerbose = true/g' ./php7/php7.go
sed -i '' -e 's/yyErrorVerbose = false/yyErrorVerbose = true/g' ./php5/php5.go
sed -i '' -e 's/\/\/line/\/\/ line/g' ./php5/php5.go
sed -i '' -e 's/\/\/line/\/\/ line/g' ./php7/php7.go
sed -i '' -e 's/\/\/line/\/\/ line/g' ./scanner/scanner.go
go test -benchmem -bench=. ./internal/php5
go test -benchmem -bench=. ./internal/php7

compile: ./internal/php5/php5.go ./internal/php7/php7.go ./internal/scanner/scanner.go
sed -i '' -e 's/yyErrorVerbose = false/yyErrorVerbose = true/g' ./internal/php7/php7.go
sed -i '' -e 's/yyErrorVerbose = false/yyErrorVerbose = true/g' ./internal/php5/php5.go
sed -i '' -e 's/\/\/line/\/\/ line/g' ./internal/php5/php5.go
sed -i '' -e 's/\/\/line/\/\/ line/g' ./internal/php7/php7.go
sed -i '' -e 's/\/\/line/\/\/ line/g' ./internal/scanner/scanner.go
rm -f y.output

./scanner/scanner.go: ./scanner/scanner.rl
./internal/scanner/scanner.go: ./internal/scanner/scanner.rl
ragel -Z -G2 -o $@ $<

./php5/php5.go: ./php5/php5.y
./internal/php5/php5.go: ./internal/php5/php5.y
goyacc -o $@ $<

./php7/php7.go: ./php7/php7.y
./internal/php7/php7.go: ./internal/php7/php7.y
goyacc -o $@ $<

cpu_pprof:
go test -cpuprofile cpu.pprof -bench=. -benchtime=20s ./php7
go test -cpuprofile cpu.pprof -bench=. -benchtime=20s ./internal/php7
go tool pprof ./php7.test cpu.pprof

mem_pprof:
go test -memprofile mem.pprof -bench=. -benchtime=20s -benchmem ./php7
go test -memprofile mem.pprof -bench=. -benchtime=20s -benchmem ./internal/php7
go tool pprof -alloc_objects ./php7.test mem.pprof

cpu_pprof_php5:
go test -cpuprofile cpu.prof -bench=. -benchtime=20s ./php5
go test -cpuprofile cpu.prof -bench=. -benchtime=20s ./internal/php5
go tool pprof ./php5.test cpu.prof

mem_pprof_php5:
go test -memprofile mem.prof -bench=. -benchtime=20s -benchmem ./php5
go test -memprofile mem.prof -bench=. -benchtime=20s -benchmem ./internal/php5
go tool pprof -alloc_objects ./php5.test mem.prof
112 changes: 62 additions & 50 deletions main.go → cmd/php-parser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,73 @@ import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"strconv"
"sync"
"time"

"github.com/pkg/profile"
"github.com/yookoala/realpath"
"github.com/z7zmey/php-parser/parser"
"github.com/z7zmey/php-parser/printer"
"github.com/z7zmey/php-parser/visitor"

"github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/pkg/cfg"
"github.com/z7zmey/php-parser/pkg/errors"
"github.com/z7zmey/php-parser/pkg/parser"
"github.com/z7zmey/php-parser/pkg/version"
"github.com/z7zmey/php-parser/pkg/visitor/dumper"
"github.com/z7zmey/php-parser/pkg/visitor/nsresolver"
"github.com/z7zmey/php-parser/pkg/visitor/printer"
"github.com/z7zmey/php-parser/pkg/visitor/traverser"
)

var wg sync.WaitGroup
var phpVersion string
var dumpType string
var phpVersion *version.Version
var profiler string
var withFreeFloating *bool
var dump *bool
var showResolvedNs *bool
var printBack *bool
var printPath *bool
var printErrors *bool
var printExecTime *bool

type file struct {
path string
content []byte
}

type result struct {
path string
parser parser.Parser
path string
rootNode ast.Vertex
errors []*errors.Error
}

func main() {
withFreeFloating = flag.Bool("ff", false, "parse and show free floating strings")
start := time.Now()
var phpVer string

printExecTime = flag.Bool("time", false, "print execution time")
showResolvedNs = flag.Bool("r", false, "resolve names")
printBack = flag.Bool("pb", false, "print AST back into the parsed file")
printPath = flag.Bool("p", false, "print filepath")
flag.StringVar(&dumpType, "d", "", "dump format: [custom, go, json, pretty_json]")
printErrors = flag.Bool("e", false, "print errors")
dump = flag.Bool("d", false, "dump")
flag.StringVar(&profiler, "prof", "", "start profiler: [cpu, mem, trace]")
flag.StringVar(&phpVersion, "phpver", "7.4", "php version")
flag.StringVar(&phpVer, "phpver", "7.4", "php version")

flag.Parse()

var err error
phpVersion, err = version.New(phpVer)
if err != nil {
fmt.Println("Error: " + err.Error())
os.Exit(1)
}

if len(flag.Args()) == 0 {
flag.Usage()
return
Expand Down Expand Up @@ -82,6 +105,11 @@ func main() {
wg.Wait()
close(fileCh)
close(resultCh)

elapsed := time.Since(start)
if *printExecTime {
log.Printf("took: %s", elapsed)
}
}

func processPath(pathList []string, fileCh chan<- *file) {
Expand Down Expand Up @@ -109,18 +137,19 @@ func parserWorker(fileCh <-chan *file, r chan<- result) {
return
}

parserWorker, err := parser.NewParser(f.content, phpVersion)
var parserErrors []*errors.Error
rootNode, err := parser.Parse(f.content, cfg.Config{
Version: phpVersion,
ErrorHandlerFunc: func(e *errors.Error) {
parserErrors = append(parserErrors, e)
},
})
if err != nil {
panic(err.Error())
fmt.Println("Error:" + err.Error())
os.Exit(1)
}

if *withFreeFloating {
parserWorker.WithFreeFloating()
}

parserWorker.Parse()

r <- result{path: f.path, parser: parserWorker}
r <- result{path: f.path, rootNode: rootNode, errors: parserErrors}
}
}

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

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

for _, e := range res.parser.GetErrors() {
fmt.Fprintf(os.Stdout, "==> %s\n", e)
if *printErrors {
for _, e := range res.errors {
_, _ = io.WriteString(os.Stderr, "==> "+e.String()+"\n")
}
}

if *printBack {
o := bytes.NewBuffer([]byte{})
p := printer.NewPrinter(o)
p.Print(res.parser.GetRootNode())
res.rootNode.Accept(p)

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

var nsResolver *visitor.NamespaceResolver
if *showResolvedNs {
nsResolver = visitor.NewNamespaceResolver()
res.parser.GetRootNode().Walk(nsResolver)
v := nsresolver.NewNamespaceResolver()
traverser.NewTraverser(v).Traverse(res.rootNode)
for _, n := range v.ResolvedNames {
_, _ = io.WriteString(os.Stderr, "===> "+n+"\n")
}
}

switch dumpType {
case "custom":
dumper := &visitor.Dumper{
Writer: os.Stdout,
Indent: "| ",
NsResolver: nsResolver,
}
res.parser.GetRootNode().Walk(dumper)
case "json":
dumper := &visitor.JsonDumper{
Writer: os.Stdout,
NsResolver: nsResolver,
}
res.parser.GetRootNode().Walk(dumper)
case "pretty_json":
dumper := &visitor.PrettyJsonDumper{
Writer: os.Stdout,
NsResolver: nsResolver,
}
res.parser.GetRootNode().Walk(dumper)
case "go":
dumper := &visitor.GoDumper{Writer: os.Stdout}
res.parser.GetRootNode().Walk(dumper)
if *dump == true {
dumper.NewDumper(os.Stdout).WithPositions().WithTokens().Dump(res.rootNode)
}

wg.Done()
Expand Down
98 changes: 0 additions & 98 deletions freefloating/position_string.go

This file was deleted.

Loading