[refactoring] update position builder

This commit is contained in:
Vadym Slizov 2020-12-08 02:08:59 +02:00
parent 8064d940f0
commit f3a605aba1
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
9 changed files with 1013 additions and 979 deletions

View File

@ -1,6 +1,6 @@
PHPFILE=example.php PHPFILE=example.php
all: compile fmt build run all: compile fmt build
fmt: fmt:
find . -type f -iregex '.*\.go' -exec gofmt -l -s -w '{}' + find . -type f -iregex '.*\.go' -exec gofmt -l -s -w '{}' +
@ -9,9 +9,6 @@ build:
go generate ./... go generate ./...
go build ./cmd/... go build ./cmd/...
run:
./php-parser -d go $(PHPFILE)
test: test:
go test ./... go test ./...
@ -22,7 +19,7 @@ bench:
go test -benchmem -bench=. ./internal/php5 go test -benchmem -bench=. ./internal/php5
go test -benchmem -bench=. ./internal/php7 go test -benchmem -bench=. ./internal/php7
compile: ./internal/php5/php5.go ./internal/php7/php7.go ./internal/scanner/scanner.go fmt 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/php7/php7.go
sed -i '' -e 's/yyErrorVerbose = false/yyErrorVerbose = true/g' ./internal/php5/php5.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/php5/php5.go

View File

@ -1,6 +1,7 @@
package php5 package php5
import ( import (
builder "github.com/z7zmey/php-parser/internal/position"
"github.com/z7zmey/php-parser/internal/scanner" "github.com/z7zmey/php-parser/internal/scanner"
"github.com/z7zmey/php-parser/pkg/ast" "github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/pkg/errors" "github.com/z7zmey/php-parser/pkg/errors"
@ -13,6 +14,7 @@ type Parser struct {
currentToken *token.Token currentToken *token.Token
rootNode ast.Vertex rootNode ast.Vertex
errHandlerFunc func(*errors.Error) errHandlerFunc func(*errors.Error)
builder *builder.Builder
} }
// NewParser creates and returns new Parser // NewParser creates and returns new Parser
@ -20,6 +22,7 @@ func NewParser(lexer *scanner.Lexer, errHandlerFunc func(*errors.Error)) *Parser
return &Parser{ return &Parser{
Lexer: lexer, Lexer: lexer,
errHandlerFunc: errHandlerFunc, errHandlerFunc: errHandlerFunc,
builder: builder.NewBuilder(),
} }
} }

BIN
internal/php5/php5.go generated

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,7 @@
package php7 package php7
import ( import (
builder "github.com/z7zmey/php-parser/internal/position"
"github.com/z7zmey/php-parser/internal/scanner" "github.com/z7zmey/php-parser/internal/scanner"
"github.com/z7zmey/php-parser/pkg/ast" "github.com/z7zmey/php-parser/pkg/ast"
"github.com/z7zmey/php-parser/pkg/errors" "github.com/z7zmey/php-parser/pkg/errors"
@ -13,6 +14,7 @@ type Parser struct {
currentToken *token.Token currentToken *token.Token
rootNode ast.Vertex rootNode ast.Vertex
errHandlerFunc func(*errors.Error) errHandlerFunc func(*errors.Error)
builder *builder.Builder
} }
// NewParser creates and returns new Parser // NewParser creates and returns new Parser
@ -20,6 +22,7 @@ func NewParser(lexer *scanner.Lexer, errHandlerFunc func(*errors.Error)) *Parser
return &Parser{ return &Parser{
Lexer: lexer, Lexer: lexer,
errHandlerFunc: errHandlerFunc, errHandlerFunc: errHandlerFunc,
builder: builder.NewBuilder(),
} }
} }

BIN
internal/php7/php7.go generated

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -16,6 +16,16 @@ type endPos struct {
endPos int endPos int
} }
type Builder struct {
pool *position.Pool
}
func NewBuilder() *Builder {
return &Builder{
pool: position.NewPool(position.DefaultBlockSize),
}
}
func getListStartPos(l []ast.Vertex) startPos { func getListStartPos(l []ast.Vertex) startPos {
if l == nil { if l == nil {
return startPos{-1, -1} return startPos{-1, -1}
@ -75,130 +85,153 @@ func getNodeEndPos(n ast.Vertex) endPos {
} }
// NewNodeListPosition returns new Position // NewNodeListPosition returns new Position
func NewNodeListPosition(list []ast.Vertex) *position.Position { func (b *Builder) NewNodeListPosition(list []ast.Vertex) *position.Position {
return &position.Position{ pos := b.pool.Get()
StartLine: getListStartPos(list).startLine,
EndLine: getListEndPos(list).endLine, pos.StartLine = getListStartPos(list).startLine
StartPos: getListStartPos(list).startPos, pos.EndLine = getListEndPos(list).endLine
EndPos: getListEndPos(list).endPos, pos.StartPos = getListStartPos(list).startPos
} pos.EndPos = getListEndPos(list).endPos
return pos
} }
// NewNodePosition returns new Position // NewNodePosition returns new Position
func NewNodePosition(n ast.Vertex) *position.Position { func (b *Builder) NewNodePosition(n ast.Vertex) *position.Position {
return &position.Position{ pos := b.pool.Get()
StartLine: getNodeStartPos(n).startLine,
EndLine: getNodeEndPos(n).endLine, pos.StartLine = getNodeStartPos(n).startLine
StartPos: getNodeStartPos(n).startPos, pos.EndLine = getNodeEndPos(n).endLine
EndPos: getNodeEndPos(n).endPos, pos.StartPos = getNodeStartPos(n).startPos
} pos.EndPos = getNodeEndPos(n).endPos
return pos
} }
// NewTokenPosition returns new Position // NewTokenPosition returns new Position
func NewTokenPosition(t *token.Token) *position.Position { func (b *Builder) NewTokenPosition(t *token.Token) *position.Position {
return &position.Position{ pos := b.pool.Get()
StartLine: t.Position.StartLine,
EndLine: t.Position.EndLine, pos.StartLine = t.Position.StartLine
StartPos: t.Position.StartPos, pos.EndLine = t.Position.EndLine
EndPos: t.Position.EndPos, pos.StartPos = t.Position.StartPos
} pos.EndPos = t.Position.EndPos
return pos
} }
// NewTokensPosition returns new Position // NewTokensPosition returns new Position
func NewTokensPosition(startToken *token.Token, endToken *token.Token) *position.Position { func (b *Builder) NewTokensPosition(startToken *token.Token, endToken *token.Token) *position.Position {
return &position.Position{ pos := b.pool.Get()
StartLine: startToken.Position.StartLine,
EndLine: endToken.Position.EndLine, pos.StartLine = startToken.Position.StartLine
StartPos: startToken.Position.StartPos, pos.EndLine = endToken.Position.EndLine
EndPos: endToken.Position.EndPos, pos.StartPos = startToken.Position.StartPos
} pos.EndPos = endToken.Position.EndPos
return pos
} }
// NewTokenNodePosition returns new Position // NewTokenNodePosition returns new Position
func NewTokenNodePosition(t *token.Token, n ast.Vertex) *position.Position { func (b *Builder) NewTokenNodePosition(t *token.Token, n ast.Vertex) *position.Position {
return &position.Position{ pos := b.pool.Get()
StartLine: t.Position.StartLine,
EndLine: getNodeEndPos(n).endLine, pos.StartLine = t.Position.StartLine
StartPos: t.Position.StartPos, pos.EndLine = getNodeEndPos(n).endLine
EndPos: getNodeEndPos(n).endPos, pos.StartPos = t.Position.StartPos
} pos.EndPos = getNodeEndPos(n).endPos
return pos
} }
// NewNodeTokenPosition returns new Position // NewNodeTokenPosition returns new Position
func NewNodeTokenPosition(n ast.Vertex, t *token.Token) *position.Position { func (b *Builder) NewNodeTokenPosition(n ast.Vertex, t *token.Token) *position.Position {
return &position.Position{ pos := b.pool.Get()
StartLine: getNodeStartPos(n).startLine,
EndLine: t.Position.EndLine, pos.StartLine = getNodeStartPos(n).startLine
StartPos: getNodeStartPos(n).startPos, pos.EndLine = t.Position.EndLine
EndPos: t.Position.EndPos, pos.StartPos = getNodeStartPos(n).startPos
} pos.EndPos = t.Position.EndPos
return pos
} }
// NewNodesPosition returns new Position // NewNodesPosition returns new Position
func NewNodesPosition(startNode ast.Vertex, endNode ast.Vertex) *position.Position { func (b *Builder) NewNodesPosition(startNode ast.Vertex, endNode ast.Vertex) *position.Position {
return &position.Position{ pos := b.pool.Get()
StartLine: getNodeStartPos(startNode).startLine,
EndLine: getNodeEndPos(endNode).endLine, pos.StartLine = getNodeStartPos(startNode).startLine
StartPos: getNodeStartPos(startNode).startPos, pos.EndLine = getNodeEndPos(endNode).endLine
EndPos: getNodeEndPos(endNode).endPos, pos.StartPos = getNodeStartPos(startNode).startPos
} pos.EndPos = getNodeEndPos(endNode).endPos
return pos
} }
// NewNodeListTokenPosition returns new Position // NewNodeListTokenPosition returns new Position
func NewNodeListTokenPosition(list []ast.Vertex, t *token.Token) *position.Position { func (b *Builder) NewNodeListTokenPosition(list []ast.Vertex, t *token.Token) *position.Position {
return &position.Position{ pos := b.pool.Get()
StartLine: getListStartPos(list).startLine,
EndLine: t.Position.EndLine, pos.StartLine = getListStartPos(list).startLine
StartPos: getListStartPos(list).startPos, pos.EndLine = t.Position.EndLine
EndPos: t.Position.EndPos, pos.StartPos = getListStartPos(list).startPos
} pos.EndPos = t.Position.EndPos
return pos
} }
// NewTokenNodeListPosition returns new Position // NewTokenNodeListPosition returns new Position
func NewTokenNodeListPosition(t *token.Token, list []ast.Vertex) *position.Position { func (b *Builder) NewTokenNodeListPosition(t *token.Token, list []ast.Vertex) *position.Position {
return &position.Position{ pos := b.pool.Get()
StartLine: t.Position.StartLine,
EndLine: getListEndPos(list).endLine, pos.StartLine = t.Position.StartLine
StartPos: t.Position.StartPos, pos.EndLine = getListEndPos(list).endLine
EndPos: getListEndPos(list).endPos, pos.StartPos = t.Position.StartPos
} pos.EndPos = getListEndPos(list).endPos
return pos
} }
// NewNodeNodeListPosition returns new Position // NewNodeNodeListPosition returns new Position
func NewNodeNodeListPosition(n ast.Vertex, list []ast.Vertex) *position.Position { func (b *Builder) NewNodeNodeListPosition(n ast.Vertex, list []ast.Vertex) *position.Position {
return &position.Position{ pos := b.pool.Get()
StartLine: getNodeStartPos(n).startLine,
EndLine: getListEndPos(list).endLine, pos.StartLine = getNodeStartPos(n).startLine
StartPos: getNodeStartPos(n).startPos, pos.EndLine = getListEndPos(list).endLine
EndPos: getListEndPos(list).endPos, pos.StartPos = getNodeStartPos(n).startPos
} pos.EndPos = getListEndPos(list).endPos
return pos
} }
// NewNodeListNodePosition returns new Position // NewNodeListNodePosition returns new Position
func NewNodeListNodePosition(list []ast.Vertex, n ast.Vertex) *position.Position { func (b *Builder) NewNodeListNodePosition(list []ast.Vertex, n ast.Vertex) *position.Position {
return &position.Position{ pos := b.pool.Get()
StartLine: getListStartPos(list).startLine,
EndLine: getNodeEndPos(n).endLine, pos.StartLine = getListStartPos(list).startLine
StartPos: getListStartPos(list).startPos, pos.EndLine = getNodeEndPos(n).endLine
EndPos: getNodeEndPos(n).endPos, pos.StartPos = getListStartPos(list).startPos
} pos.EndPos = getNodeEndPos(n).endPos
return pos
} }
// NewOptionalListTokensPosition returns new Position // NewOptionalListTokensPosition returns new Position
func NewOptionalListTokensPosition(list []ast.Vertex, t *token.Token, endToken *token.Token) *position.Position { func (b *Builder) NewOptionalListTokensPosition(list []ast.Vertex, t *token.Token, endToken *token.Token) *position.Position {
if list == nil { pos := b.pool.Get()
return &position.Position{
StartLine: t.Position.StartLine,
EndLine: endToken.Position.EndLine,
StartPos: t.Position.StartPos,
EndPos: endToken.Position.EndPos,
}
}
return &position.Position{ if list == nil {
StartLine: getListStartPos(list).startLine, pos.StartLine = t.Position.StartLine
EndLine: endToken.Position.EndLine, pos.EndLine = endToken.Position.EndLine
StartPos: getListStartPos(list).startPos, pos.StartPos = t.Position.StartPos
EndPos: endToken.Position.EndPos, pos.EndPos = endToken.Position.EndPos
return pos
} }
pos.StartLine = getListStartPos(list).startLine
pos.EndLine = endToken.Position.EndLine
pos.StartPos = getListStartPos(list).startPos
pos.EndPos = endToken.Position.EndPos
return pos
} }

View File

@ -36,7 +36,7 @@ func NewLexer(data []byte, phpVersion string, errHandlerFunc func(*errors.Error)
stack: make([]int, 0), stack: make([]int, 0),
tokenPool: token.NewPool(position.DefaultBlockSize), tokenPool: token.NewPool(position.DefaultBlockSize),
positionPool: position.NewPool(position.DefaultBlockSize), positionPool: position.NewPool(token.DefaultBlockSize),
newLines: NewLines{make([]int, 0, 128)}, newLines: NewLines{make([]int, 0, 128)},
} }