[refactoring] remove general Node struct

This commit is contained in:
Vadym Slizov 2020-12-08 00:04:12 +02:00
parent f6f86bf99b
commit 20a42da7c9
No known key found for this signature in database
GPG Key ID: AEA2A9388EF42A4A
13 changed files with 1932 additions and 3327 deletions

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"bytes"
"flag" "flag"
"io" "io"
"io/ioutil" "io/ioutil"
@ -21,7 +20,6 @@ import (
"github.com/z7zmey/php-parser/pkg/ast/visitor" "github.com/z7zmey/php-parser/pkg/ast/visitor"
"github.com/z7zmey/php-parser/pkg/errors" "github.com/z7zmey/php-parser/pkg/errors"
"github.com/z7zmey/php-parser/pkg/parser" "github.com/z7zmey/php-parser/pkg/parser"
"github.com/z7zmey/php-parser/pkg/printer"
) )
var wg sync.WaitGroup var wg sync.WaitGroup
@ -165,12 +163,12 @@ func printerWorker(r <-chan result) {
} }
if *printBack { if *printBack {
o := bytes.NewBuffer([]byte{}) //o := bytes.NewBuffer([]byte{})
p := printer.NewPrinter(o) //p := printer.NewPrinter(o)
p.Print(res.rootNode) //p.Print(res.rootNode)
//
err := ioutil.WriteFile(res.path, o.Bytes(), 0644) //err := ioutil.WriteFile(res.path, o.Bytes(), 0644)
checkErr(err) //checkErr(err)
} }
if *showResolvedNs { if *showResolvedNs {

BIN
internal/php5/php5.go generated

Binary file not shown.

File diff suppressed because it is too large Load Diff

BIN
internal/php7/php7.go generated

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -36,7 +36,7 @@ func getNodeStartPos(n ast.Vertex) startPos {
return startPos{-1, -1} return startPos{-1, -1}
} }
p := n.GetNode().Position p := n.GetPosition()
if p != nil { if p != nil {
sl = p.StartLine sl = p.StartLine
sp = p.StartPos sp = p.StartPos
@ -65,7 +65,7 @@ func getNodeEndPos(n ast.Vertex) endPos {
return endPos{-1, -1} return endPos{-1, -1}
} }
p := n.GetNode().Position p := n.GetPosition()
if p != nil { if p != nil {
el = p.EndLine el = p.EndLine
ep = p.EndPos ep = p.EndPos

View File

@ -23,9 +23,7 @@ func TestNewTokenPosition(t *testing.T) {
pos := builder.NewTokenPosition(tkn) pos := builder.NewTokenPosition(tkn)
assert.DeepEqual(t, &position.Position{1, 1, 0, 3}, pos) assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 1, EndPos: 3}, pos)
assert.DeepEqual(t, &position.Position{1, 1, 0, 3}, pos)
} }
func TestNewTokensPosition(t *testing.T) { func TestNewTokensPosition(t *testing.T) {
@ -50,24 +48,22 @@ func TestNewTokensPosition(t *testing.T) {
pos := builder.NewTokensPosition(token1, token2) pos := builder.NewTokensPosition(token1, token2)
assert.DeepEqual(t, &position.Position{1, 2, 0, 6}, pos) assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 2, EndPos: 6}, pos)
} }
func TestNewNodePosition(t *testing.T) { func TestNewNodePosition(t *testing.T) {
n := &ast.Identifier{ n := &ast.Identifier{
Node: ast.Node{ Position: &position.Position{
Position: &position.Position{ StartLine: 1,
StartLine: 1, EndLine: 1,
EndLine: 1, StartPos: 0,
StartPos: 0, EndPos: 3,
EndPos: 3,
},
}, },
} }
pos := builder.NewNodePosition(n) pos := builder.NewNodePosition(n)
assert.DeepEqual(t, &position.Position{1, 1, 0, 3}, pos) assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 1, EndPos: 3}, pos)
} }
func TestNewTokenNodePosition(t *testing.T) { func TestNewTokenNodePosition(t *testing.T) {
@ -81,30 +77,26 @@ func TestNewTokenNodePosition(t *testing.T) {
}, },
} }
n := &ast.Identifier{ n := &ast.Identifier{
Node: ast.Node{ Position: &position.Position{
Position: &position.Position{ StartLine: 2,
StartLine: 2, EndLine: 2,
EndLine: 2, StartPos: 4,
StartPos: 4, EndPos: 12,
EndPos: 12,
},
}, },
} }
pos := builder.NewTokenNodePosition(tkn, n) pos := builder.NewTokenNodePosition(tkn, n)
assert.DeepEqual(t, &position.Position{1, 2, 0, 12}, pos) assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 2, EndPos: 12}, pos)
} }
func TestNewNodeTokenPosition(t *testing.T) { func TestNewNodeTokenPosition(t *testing.T) {
n := &ast.Identifier{ n := &ast.Identifier{
Node: ast.Node{ Position: &position.Position{
Position: &position.Position{ StartLine: 1,
StartLine: 1, EndLine: 1,
EndLine: 1, StartPos: 0,
StartPos: 0, EndPos: 9,
EndPos: 9,
},
}, },
} }
@ -120,85 +112,73 @@ func TestNewNodeTokenPosition(t *testing.T) {
pos := builder.NewNodeTokenPosition(n, tkn) pos := builder.NewNodeTokenPosition(n, tkn)
assert.DeepEqual(t, &position.Position{1, 2, 0, 12}, pos) assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 2, EndPos: 12}, pos)
} }
func TestNewNodeListPosition(t *testing.T) { func TestNewNodeListPosition(t *testing.T) {
n1 := &ast.Identifier{ n1 := &ast.Identifier{
Node: ast.Node{ Position: &position.Position{
Position: &position.Position{ StartLine: 1,
StartLine: 1, EndLine: 1,
EndLine: 1, StartPos: 0,
StartPos: 0, EndPos: 9,
EndPos: 9,
},
}, },
} }
n2 := &ast.Identifier{ n2 := &ast.Identifier{
Node: ast.Node{ Position: &position.Position{
Position: &position.Position{ StartLine: 2,
StartLine: 2, EndLine: 2,
EndLine: 2, StartPos: 10,
StartPos: 10, EndPos: 19,
EndPos: 19,
},
}, },
} }
pos := builder.NewNodeListPosition([]ast.Vertex{n1, n2}) pos := builder.NewNodeListPosition([]ast.Vertex{n1, n2})
assert.DeepEqual(t, &position.Position{1, 2, 0, 19}, pos) assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 2, EndPos: 19}, pos)
} }
func TestNewNodesPosition(t *testing.T) { func TestNewNodesPosition(t *testing.T) {
n1 := &ast.Identifier{ n1 := &ast.Identifier{
Node: ast.Node{ Position: &position.Position{
Position: &position.Position{ StartLine: 1,
StartLine: 1, EndLine: 1,
EndLine: 1, StartPos: 0,
StartPos: 0, EndPos: 9,
EndPos: 9,
},
}, },
} }
n2 := &ast.Identifier{ n2 := &ast.Identifier{
Node: ast.Node{ Position: &position.Position{
Position: &position.Position{ StartLine: 2,
StartLine: 2, EndLine: 2,
EndLine: 2, StartPos: 10,
StartPos: 10, EndPos: 19,
EndPos: 19,
},
}, },
} }
pos := builder.NewNodesPosition(n1, n2) pos := builder.NewNodesPosition(n1, n2)
assert.DeepEqual(t, &position.Position{1, 2, 0, 19}, pos) assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 2, EndPos: 19}, pos)
} }
func TestNewNodeListTokenPosition(t *testing.T) { func TestNewNodeListTokenPosition(t *testing.T) {
n1 := &ast.Identifier{ n1 := &ast.Identifier{
Node: ast.Node{ Position: &position.Position{
Position: &position.Position{ StartLine: 1,
StartLine: 1, EndLine: 1,
EndLine: 1, StartPos: 0,
StartPos: 0, EndPos: 9,
EndPos: 9,
},
}, },
} }
n2 := &ast.Identifier{ n2 := &ast.Identifier{
Node: ast.Node{ Position: &position.Position{
Position: &position.Position{ StartLine: 2,
StartLine: 2, EndLine: 2,
EndLine: 2, StartPos: 10,
StartPos: 10, EndPos: 19,
EndPos: 19,
},
}, },
} }
@ -214,7 +194,7 @@ func TestNewNodeListTokenPosition(t *testing.T) {
pos := builder.NewNodeListTokenPosition([]ast.Vertex{n1, n2}, tkn) pos := builder.NewNodeListTokenPosition([]ast.Vertex{n1, n2}, tkn)
assert.DeepEqual(t, &position.Position{1, 3, 0, 22}, pos) assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 3, EndPos: 22}, pos)
} }
func TestNewTokenNodeListPosition(t *testing.T) { func TestNewTokenNodeListPosition(t *testing.T) {
@ -229,106 +209,90 @@ func TestNewTokenNodeListPosition(t *testing.T) {
} }
n1 := &ast.Identifier{ n1 := &ast.Identifier{
Node: ast.Node{ Position: &position.Position{
Position: &position.Position{ StartLine: 2,
StartLine: 2, EndLine: 2,
EndLine: 2, StartPos: 3,
StartPos: 3, EndPos: 10,
EndPos: 10,
},
}, },
} }
n2 := &ast.Identifier{ n2 := &ast.Identifier{
Node: ast.Node{ Position: &position.Position{
Position: &position.Position{ StartLine: 3,
StartLine: 3, EndLine: 3,
EndLine: 3, StartPos: 11,
StartPos: 11, EndPos: 20,
EndPos: 20,
},
}, },
} }
pos := builder.NewTokenNodeListPosition(tkn, []ast.Vertex{n1, n2}) pos := builder.NewTokenNodeListPosition(tkn, []ast.Vertex{n1, n2})
assert.DeepEqual(t, &position.Position{1, 3, 0, 20}, pos) assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 3, EndPos: 20}, pos)
} }
func TestNewNodeNodeListPosition(t *testing.T) { func TestNewNodeNodeListPosition(t *testing.T) {
n1 := &ast.Identifier{ n1 := &ast.Identifier{
Node: ast.Node{ Position: &position.Position{
Position: &position.Position{ StartLine: 1,
StartLine: 1, EndLine: 1,
EndLine: 1, StartPos: 0,
StartPos: 0, EndPos: 8,
EndPos: 8,
},
}, },
} }
n2 := &ast.Identifier{ n2 := &ast.Identifier{
Node: ast.Node{ Position: &position.Position{
Position: &position.Position{ StartLine: 2,
StartLine: 2, EndLine: 2,
EndLine: 2, StartPos: 9,
StartPos: 9, EndPos: 17,
EndPos: 17,
},
}, },
} }
n3 := &ast.Identifier{ n3 := &ast.Identifier{
Node: ast.Node{ Position: &position.Position{
Position: &position.Position{ StartLine: 3,
StartLine: 3, EndLine: 3,
EndLine: 3, StartPos: 18,
StartPos: 18, EndPos: 26,
EndPos: 26,
},
}, },
} }
pos := builder.NewNodeNodeListPosition(n1, []ast.Vertex{n2, n3}) pos := builder.NewNodeNodeListPosition(n1, []ast.Vertex{n2, n3})
assert.DeepEqual(t, &position.Position{1, 3, 0, 26}, pos) assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 3, EndPos: 26}, pos)
} }
func TestNewNodeListNodePosition(t *testing.T) { func TestNewNodeListNodePosition(t *testing.T) {
n1 := &ast.Identifier{ n1 := &ast.Identifier{
Node: ast.Node{ Position: &position.Position{
Position: &position.Position{ StartLine: 1,
StartLine: 1, EndLine: 1,
EndLine: 1, StartPos: 0,
StartPos: 0, EndPos: 8,
EndPos: 8,
},
}, },
} }
n2 := &ast.Identifier{ n2 := &ast.Identifier{
Node: ast.Node{ Position: &position.Position{
Position: &position.Position{ StartLine: 2,
StartLine: 2, EndLine: 2,
EndLine: 2, StartPos: 9,
StartPos: 9, EndPos: 17,
EndPos: 17,
},
}, },
} }
n3 := &ast.Identifier{ n3 := &ast.Identifier{
Node: ast.Node{ Position: &position.Position{
Position: &position.Position{ StartLine: 3,
StartLine: 3, EndLine: 3,
EndLine: 3, StartPos: 18,
StartPos: 18, EndPos: 26,
EndPos: 26,
},
}, },
} }
pos := builder.NewNodeListNodePosition([]ast.Vertex{n1, n2}, n3) pos := builder.NewNodeListNodePosition([]ast.Vertex{n1, n2}, n3)
assert.DeepEqual(t, &position.Position{1, 3, 0, 26}, pos) assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 3, EndPos: 26}, pos)
} }
func TestNewOptionalListTokensPosition(t *testing.T) { func TestNewOptionalListTokensPosition(t *testing.T) {
@ -353,28 +317,24 @@ func TestNewOptionalListTokensPosition(t *testing.T) {
pos := builder.NewOptionalListTokensPosition(nil, token1, token2) pos := builder.NewOptionalListTokensPosition(nil, token1, token2)
assert.DeepEqual(t, &position.Position{1, 2, 0, 6}, pos) assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: 2, EndPos: 6}, pos)
} }
func TestNewOptionalListTokensPosition2(t *testing.T) { func TestNewOptionalListTokensPosition2(t *testing.T) {
n2 := &ast.Identifier{ n2 := &ast.Identifier{
Node: ast.Node{ Position: &position.Position{
Position: &position.Position{ StartLine: 2,
StartLine: 2, EndLine: 2,
EndLine: 2, StartPos: 9,
StartPos: 9, EndPos: 17,
EndPos: 17,
},
}, },
} }
n3 := &ast.Identifier{ n3 := &ast.Identifier{
Node: ast.Node{ Position: &position.Position{
Position: &position.Position{ StartLine: 3,
StartLine: 3, EndLine: 3,
EndLine: 3, StartPos: 18,
StartPos: 18, EndPos: 26,
EndPos: 26,
},
}, },
} }
@ -399,34 +359,32 @@ func TestNewOptionalListTokensPosition2(t *testing.T) {
pos := builder.NewOptionalListTokensPosition([]ast.Vertex{n2, n3}, token1, token2) pos := builder.NewOptionalListTokensPosition([]ast.Vertex{n2, n3}, token1, token2)
assert.DeepEqual(t, &position.Position{2, 5, 9, 32}, pos) assert.DeepEqual(t, &position.Position{StartLine: 2, EndLine: 5, StartPos: 9, EndPos: 32}, pos)
} }
func TestNilNodePos(t *testing.T) { func TestNilNodePos(t *testing.T) {
pos := builder.NewNodesPosition(nil, nil) pos := builder.NewNodesPosition(nil, nil)
assert.DeepEqual(t, &position.Position{-1, -1, -1, -1}, pos) assert.DeepEqual(t, &position.Position{StartLine: -1, EndLine: -1, StartPos: -1, EndPos: -1}, pos)
} }
func TestNilNodeListPos(t *testing.T) { func TestNilNodeListPos(t *testing.T) {
n1 := &ast.Identifier{ n1 := &ast.Identifier{
Node: ast.Node{ Position: &position.Position{
Position: &position.Position{ StartLine: 1,
StartLine: 1, EndLine: 1,
EndLine: 1, StartPos: 0,
StartPos: 0, EndPos: 8,
EndPos: 8,
},
}, },
} }
pos := builder.NewNodeNodeListPosition(n1, nil) pos := builder.NewNodeNodeListPosition(n1, nil)
assert.DeepEqual(t, &position.Position{1, -1, 0, -1}, pos) assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: -1, EndPos: -1}, pos)
} }
func TestNilNodeListTokenPos(t *testing.T) { func TestNilNodeListTokenPos(t *testing.T) {
token := &token.Token{ tkn := &token.Token{
Value: []byte(`foo`), Value: []byte(`foo`),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -436,30 +394,28 @@ func TestNilNodeListTokenPos(t *testing.T) {
}, },
} }
pos := builder.NewNodeListTokenPosition(nil, token) pos := builder.NewNodeListTokenPosition(nil, tkn)
assert.DeepEqual(t, &position.Position{-1, 1, -1, 3}, pos) assert.DeepEqual(t, &position.Position{StartLine: -1, EndLine: 1, StartPos: -1, EndPos: 3}, pos)
} }
func TestEmptyNodeListPos(t *testing.T) { func TestEmptyNodeListPos(t *testing.T) {
n1 := &ast.Identifier{ n1 := &ast.Identifier{
Node: ast.Node{ Position: &position.Position{
Position: &position.Position{ StartLine: 1,
StartLine: 1, EndLine: 1,
EndLine: 1, StartPos: 0,
StartPos: 0, EndPos: 8,
EndPos: 8,
},
}, },
} }
pos := builder.NewNodeNodeListPosition(n1, []ast.Vertex{}) pos := builder.NewNodeNodeListPosition(n1, []ast.Vertex{})
assert.DeepEqual(t, &position.Position{1, -1, 0, -1}, pos) assert.DeepEqual(t, &position.Position{StartLine: 1, EndLine: -1, EndPos: -1}, pos)
} }
func TestEmptyNodeListTokenPos(t *testing.T) { func TestEmptyNodeListTokenPos(t *testing.T) {
token := &token.Token{ tkn := &token.Token{
Value: []byte(`foo`), Value: []byte(`foo`),
Position: &position.Position{ Position: &position.Position{
StartLine: 1, StartLine: 1,
@ -469,7 +425,7 @@ func TestEmptyNodeListTokenPos(t *testing.T) {
}, },
} }
pos := builder.NewNodeListTokenPosition([]ast.Vertex{}, token) pos := builder.NewNodeListTokenPosition([]ast.Vertex{}, tkn)
assert.DeepEqual(t, &position.Position{-1, 1, -1, 3}, pos) assert.DeepEqual(t, &position.Position{StartLine: -1, EndLine: 1, StartPos: -1, EndPos: 3}, pos)
} }

View File

@ -1,8 +1,10 @@
package ast package ast
import "github.com/z7zmey/php-parser/pkg/position"
type Vertex interface { type Vertex interface {
Accept(v NodeVisitor) Accept(v NodeVisitor)
GetNode() *Node GetPosition() *position.Position
} }
type Traverser interface { type Traverser interface {

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -20,12 +20,6 @@ func ExampleDump() {
}, },
}, },
}, },
Position: &position.Position{
StartLine: 1,
EndLine: 1,
StartPos: 0,
EndPos: 1,
},
}, },
Stmts: []ast.Vertex{ Stmts: []ast.Vertex{
&ast.Identifier{}, &ast.Identifier{},

View File

@ -1,74 +0,0 @@
package token
type Position int
//go:generate stringer -type=Position -output ./position_string.go
const (
Start Position = iota
End
SemiColon
AltEnd
Ampersand
Name
Key
Var
ReturnType
CaseSeparator
LexicalVars
Params
Ref
Cast
Expr
InitExpr
CondExpr
IncExpr
True
Cond
Namespace
Static
Use
For
Foreach
Declare
Label
Finally
List
Default
Function
Alias
Equal
Array
Isset
Echo
Try
Catch
Unset
Stmts
VarList
ConstList
NameList
ParamList
ModifierList
ArrayPairList
CaseListStart
CaseListEnd
PropertyList
ParameterList
AdaptationList
LexicalVarList
CloseParenthesisToken
)
type Collection map[Position][]*Token
func (c Collection) IsEmpty() bool {
for _, v := range c {
if len(v) > 0 {
return false
}
}
return true
}

View File

@ -1,75 +0,0 @@
// Code generated by "stringer -type=Position -output ./position_string.go"; DO NOT EDIT.
package token
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[Start-0]
_ = x[End-1]
_ = x[SemiColon-2]
_ = x[AltEnd-3]
_ = x[Ampersand-4]
_ = x[Name-5]
_ = x[Key-6]
_ = x[Var-7]
_ = x[ReturnType-8]
_ = x[CaseSeparator-9]
_ = x[LexicalVars-10]
_ = x[Params-11]
_ = x[Ref-12]
_ = x[Cast-13]
_ = x[Expr-14]
_ = x[InitExpr-15]
_ = x[CondExpr-16]
_ = x[IncExpr-17]
_ = x[True-18]
_ = x[Cond-19]
_ = x[Namespace-20]
_ = x[Static-21]
_ = x[Use-22]
_ = x[For-23]
_ = x[Foreach-24]
_ = x[Declare-25]
_ = x[Label-26]
_ = x[Finally-27]
_ = x[List-28]
_ = x[Default-29]
_ = x[Function-30]
_ = x[Alias-31]
_ = x[Equal-32]
_ = x[Array-33]
_ = x[Isset-34]
_ = x[Echo-35]
_ = x[Try-36]
_ = x[Catch-37]
_ = x[Unset-38]
_ = x[Stmts-39]
_ = x[VarList-40]
_ = x[ConstList-41]
_ = x[NameList-42]
_ = x[ParamList-43]
_ = x[ModifierList-44]
_ = x[ArrayPairList-45]
_ = x[CaseListStart-46]
_ = x[CaseListEnd-47]
_ = x[PropertyList-48]
_ = x[ParameterList-49]
_ = x[AdaptationList-50]
_ = x[LexicalVarList-51]
_ = x[CloseParenthesisToken-52]
}
const _Position_name = "StartEndSemiColonAltEndAmpersandNameKeyVarReturnTypeCaseSeparatorLexicalVarsParamsRefCastExprInitExprCondExprIncExprTrueCondNamespaceStaticUseForForeachDeclareLabelFinallyListDefaultFunctionAliasEqualArrayIssetEchoTryCatchUnsetStmtsVarListConstListNameListParamListModifierListArrayPairListCaseListStartCaseListEndPropertyListParameterListAdaptationListLexicalVarListCloseParenthesisToken"
var _Position_index = [...]uint16{0, 5, 8, 17, 23, 32, 36, 39, 42, 52, 65, 76, 82, 85, 89, 93, 101, 109, 116, 120, 124, 133, 139, 142, 145, 152, 159, 164, 171, 175, 182, 190, 195, 200, 205, 210, 214, 217, 222, 227, 232, 239, 248, 256, 265, 277, 290, 303, 314, 326, 339, 353, 367, 388}
func (i Position) String() string {
if i < 0 || i >= Position(len(_Position_index)-1) {
return "Position(" + strconv.FormatInt(int64(i), 10) + ")"
}
return _Position_name[_Position_index[i]:_Position_index[i+1]]
}