refactor php7
This commit is contained in:
@@ -189,4 +189,9 @@ type NodeVisitor interface {
|
||||
ScalarLnumber(n *ScalarLnumber)
|
||||
ScalarMagicConstant(n *ScalarMagicConstant)
|
||||
ScalarString(n *ScalarString)
|
||||
|
||||
NameName(n *NameName)
|
||||
NameFullyQualified(n *NameFullyQualified)
|
||||
NameRelative(n *NameRelative)
|
||||
NameNamePart(n *NameNamePart)
|
||||
}
|
||||
|
||||
@@ -53,7 +53,6 @@ type testVisitor struct {
|
||||
depth int
|
||||
}
|
||||
|
||||
|
||||
func (v *testVisitor) Enter(key string, _ bool) {
|
||||
v.depth++
|
||||
fmt.Fprint(os.Stdout, "=>", strings.Repeat(" ", v.depth), key, ":\n")
|
||||
@@ -90,14 +89,14 @@ func (v *testVisitor) Identifier(_ *ast.Identifier) {
|
||||
fmt.Fprintln(os.Stdout, "=>", strings.Repeat(" ", v.depth-1), "*ast.Identifier")
|
||||
}
|
||||
|
||||
func (v *testVisitor) ArgumentList(_ *ast.ArgumentList) {
|
||||
func (v *testVisitor) ArgumentList(_ *ast.ArgumentList) {
|
||||
fmt.Fprintln(os.Stdout, "=>", strings.Repeat(" ", v.depth-1), "*ast.ArgumentList")
|
||||
}
|
||||
|
||||
func (v *testVisitor) Argument(_ *ast.Argument) {
|
||||
func (v *testVisitor) Argument(_ *ast.Argument) {
|
||||
fmt.Fprintln(os.Stdout, "=>", strings.Repeat(" ", v.depth-1), "*ast.Argument")
|
||||
}
|
||||
|
||||
func (v *testVisitor) ScalarDnumber(_ *ast.ScalarDnumber) {
|
||||
func (v *testVisitor) ScalarDnumber(_ *ast.ScalarDnumber) {
|
||||
fmt.Fprintln(os.Stdout, "=>", strings.Repeat(" ", v.depth-1), "*ast.ScalarDnumber")
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
type Node struct {
|
||||
StartTokens []token.Token
|
||||
EndTokens []token.Token
|
||||
Tokens token.Collection
|
||||
Position *position.Position
|
||||
}
|
||||
|
||||
@@ -52,7 +53,7 @@ func (n *Parameter) Accept(v NodeVisitor) {
|
||||
// Identifier node
|
||||
type Identifier struct {
|
||||
Node
|
||||
Value string
|
||||
Value []byte
|
||||
}
|
||||
|
||||
func (n *Identifier) Accept(v NodeVisitor) {
|
||||
@@ -84,7 +85,7 @@ func (n *Argument) Accept(v NodeVisitor) {
|
||||
// ScalarDnumber node
|
||||
type ScalarDnumber struct {
|
||||
Node
|
||||
Value string
|
||||
Value []byte
|
||||
}
|
||||
|
||||
func (n *ScalarDnumber) Accept(v NodeVisitor) {
|
||||
@@ -104,7 +105,7 @@ func (n *ScalarEncapsed) Accept(v NodeVisitor) {
|
||||
// ScalarEncapsedStringPart node
|
||||
type ScalarEncapsedStringPart struct {
|
||||
Node
|
||||
Value string
|
||||
Value []byte
|
||||
}
|
||||
|
||||
func (n *ScalarEncapsedStringPart) Accept(v NodeVisitor) {
|
||||
@@ -114,7 +115,7 @@ func (n *ScalarEncapsedStringPart) Accept(v NodeVisitor) {
|
||||
// ScalarHeredoc node
|
||||
type ScalarHeredoc struct {
|
||||
Node
|
||||
Label string
|
||||
Label []byte
|
||||
Parts []Vertex
|
||||
}
|
||||
|
||||
@@ -125,7 +126,7 @@ func (n *ScalarHeredoc) Accept(v NodeVisitor) {
|
||||
// ScalarLnumber node
|
||||
type ScalarLnumber struct {
|
||||
Node
|
||||
Value string
|
||||
Value []byte
|
||||
}
|
||||
|
||||
func (n *ScalarLnumber) Accept(v NodeVisitor) {
|
||||
@@ -135,7 +136,7 @@ func (n *ScalarLnumber) Accept(v NodeVisitor) {
|
||||
// ScalarMagicConstant node
|
||||
type ScalarMagicConstant struct {
|
||||
Node
|
||||
Value string
|
||||
Value []byte
|
||||
}
|
||||
|
||||
func (n *ScalarMagicConstant) Accept(v NodeVisitor) {
|
||||
@@ -145,7 +146,7 @@ func (n *ScalarMagicConstant) Accept(v NodeVisitor) {
|
||||
// ScalarString node
|
||||
type ScalarString struct {
|
||||
Node
|
||||
Value string
|
||||
Value []byte
|
||||
}
|
||||
|
||||
func (n *ScalarString) Accept(v NodeVisitor) {
|
||||
@@ -550,7 +551,7 @@ func (n *StmtIf) Accept(v NodeVisitor) {
|
||||
// StmtInlineHtml node
|
||||
type StmtInlineHtml struct {
|
||||
Node
|
||||
Value string
|
||||
Value []byte
|
||||
}
|
||||
|
||||
func (n *StmtInlineHtml) Accept(v NodeVisitor) {
|
||||
@@ -1803,3 +1804,39 @@ type ExprBinarySpaceship struct {
|
||||
func (n *ExprBinarySpaceship) Accept(v NodeVisitor) {
|
||||
v.ExprBinarySpaceship(n)
|
||||
}
|
||||
|
||||
type NameName struct {
|
||||
Node
|
||||
Parts []Vertex
|
||||
}
|
||||
|
||||
func (n *NameName) Accept(v NodeVisitor) {
|
||||
v.NameName(n)
|
||||
}
|
||||
|
||||
type NameFullyQualified struct {
|
||||
Node
|
||||
Parts []Vertex
|
||||
}
|
||||
|
||||
func (n *NameFullyQualified) Accept(v NodeVisitor) {
|
||||
v.NameFullyQualified(n)
|
||||
}
|
||||
|
||||
type NameRelative struct {
|
||||
Node
|
||||
Parts []Vertex
|
||||
}
|
||||
|
||||
func (n *NameRelative) Accept(v NodeVisitor) {
|
||||
v.NameRelative(n)
|
||||
}
|
||||
|
||||
type NameNamePart struct {
|
||||
Node
|
||||
Value []byte
|
||||
}
|
||||
|
||||
func (n *NameNamePart) Accept(v NodeVisitor) {
|
||||
v.NameNamePart(n)
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ func (v *Dump) EnterNode(n ast.Vertex) bool {
|
||||
}
|
||||
|
||||
n.Accept(v)
|
||||
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
@@ -13,8 +13,7 @@ func ExampleDump() {
|
||||
&ast.Identifier{},
|
||||
&ast.Parameter{
|
||||
Variadic: true,
|
||||
Var: &ast.ExprVariable{
|
||||
},
|
||||
Var: &ast.ExprVariable{},
|
||||
},
|
||||
&ast.StmtInlineHtml{
|
||||
Value: "foo",
|
||||
|
||||
30
pkg/errors/error.go
Normal file
30
pkg/errors/error.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/z7zmey/php-parser/pkg/position"
|
||||
)
|
||||
|
||||
// Error parsing error
|
||||
type Error struct {
|
||||
Msg string
|
||||
Pos *position.Position
|
||||
}
|
||||
|
||||
// NewError creates and returns new Error
|
||||
func NewError(msg string, p *position.Position) *Error {
|
||||
return &Error{
|
||||
Msg: msg,
|
||||
Pos: p,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Error) String() string {
|
||||
atLine := ""
|
||||
if e.Pos != nil {
|
||||
atLine = fmt.Sprintf(" at line %d", e.Pos.StartLine)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s%s", e.Msg, atLine)
|
||||
}
|
||||
45
pkg/errors/error_test.go
Normal file
45
pkg/errors/error_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package errors_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gotest.tools/assert"
|
||||
|
||||
"github.com/z7zmey/php-parser/errors"
|
||||
"github.com/z7zmey/php-parser/position"
|
||||
)
|
||||
|
||||
func TestConstructor(t *testing.T) {
|
||||
pos := position.NewPosition(1, 2, 3, 4)
|
||||
|
||||
actual := errors.NewError("message", pos)
|
||||
|
||||
expected := &errors.Error{
|
||||
Msg: "message",
|
||||
Pos: pos,
|
||||
}
|
||||
|
||||
assert.DeepEqual(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestPrint(t *testing.T) {
|
||||
pos := position.NewPosition(1, 2, 3, 4)
|
||||
|
||||
Error := errors.NewError("message", pos)
|
||||
|
||||
actual := Error.String()
|
||||
|
||||
expected := "message at line 1"
|
||||
|
||||
assert.DeepEqual(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestPrintWithotPos(t *testing.T) {
|
||||
Error := errors.NewError("message", nil)
|
||||
|
||||
actual := Error.String()
|
||||
|
||||
expected := "message"
|
||||
|
||||
assert.DeepEqual(t, expected, actual)
|
||||
}
|
||||
34
pkg/parser/parser.go
Normal file
34
pkg/parser/parser.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"github.com/z7zmey/php-parser/internal/php5"
|
||||
"github.com/z7zmey/php-parser/internal/php7"
|
||||
"github.com/z7zmey/php-parser/internal/version"
|
||||
"github.com/z7zmey/php-parser/pkg/ast"
|
||||
"github.com/z7zmey/php-parser/pkg/errors"
|
||||
)
|
||||
|
||||
// Parser interface
|
||||
type Parser interface {
|
||||
Parse() int
|
||||
GetRootNode() ast.Vertex
|
||||
GetErrors() []*errors.Error
|
||||
WithTokens()
|
||||
}
|
||||
|
||||
func NewParser(src []byte, v string) (Parser, error) {
|
||||
var parser Parser
|
||||
|
||||
r, err := version.Compare(v, "7.0")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if r == -1 {
|
||||
parser = php5.NewParser(src, v)
|
||||
} else {
|
||||
parser = php7.NewParser(src, v)
|
||||
}
|
||||
|
||||
return parser, nil
|
||||
}
|
||||
88
pkg/token/position.go
Normal file
88
pkg/token/position.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package token
|
||||
|
||||
type Position int
|
||||
|
||||
type Collection map[Position][]Token
|
||||
|
||||
//go:generate stringer -type=Position -output ./position_string.go
|
||||
const (
|
||||
Start Position = iota
|
||||
End
|
||||
Slash
|
||||
Colon
|
||||
SemiColon
|
||||
AltEnd
|
||||
Dollar
|
||||
Ampersand
|
||||
Name
|
||||
Prefix
|
||||
Key
|
||||
Var
|
||||
UseType
|
||||
ReturnType
|
||||
OptionalType
|
||||
CaseSeparator
|
||||
LexicalVars
|
||||
Params
|
||||
Ref
|
||||
Cast
|
||||
Expr
|
||||
InitExpr
|
||||
CondExpr
|
||||
IncExpr
|
||||
True
|
||||
Cond
|
||||
|
||||
HaltCompiller
|
||||
Namespace
|
||||
Static
|
||||
Class
|
||||
Use
|
||||
While
|
||||
For
|
||||
Switch
|
||||
Break
|
||||
Foreach
|
||||
Declare
|
||||
Label
|
||||
Finally
|
||||
List
|
||||
Default
|
||||
If
|
||||
ElseIf
|
||||
Else
|
||||
Variadic
|
||||
Function
|
||||
DoubleArrow
|
||||
Alias
|
||||
As
|
||||
Equal
|
||||
Exit
|
||||
Array
|
||||
Isset
|
||||
Empty
|
||||
Eval
|
||||
Echo
|
||||
Try
|
||||
Catch
|
||||
Unset
|
||||
|
||||
Stmts
|
||||
VarList
|
||||
ConstList
|
||||
NameList
|
||||
ParamList
|
||||
ModifierList
|
||||
ArrayPairList
|
||||
CaseListStart
|
||||
CaseListEnd
|
||||
ArgumentList
|
||||
PropertyList
|
||||
ParameterList
|
||||
AdaptationList
|
||||
LexicalVarList
|
||||
UseDeclarationList
|
||||
|
||||
OpenParenthesisToken
|
||||
CloseParenthesisToken
|
||||
)
|
||||
98
pkg/token/position_string.go
Normal file
98
pkg/token/position_string.go
Normal file
@@ -0,0 +1,98 @@
|
||||
// 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[Slash-2]
|
||||
_ = x[Colon-3]
|
||||
_ = x[SemiColon-4]
|
||||
_ = x[AltEnd-5]
|
||||
_ = x[Dollar-6]
|
||||
_ = x[Ampersand-7]
|
||||
_ = x[Name-8]
|
||||
_ = x[Prefix-9]
|
||||
_ = x[Key-10]
|
||||
_ = x[Var-11]
|
||||
_ = x[UseType-12]
|
||||
_ = x[ReturnType-13]
|
||||
_ = x[OptionalType-14]
|
||||
_ = x[CaseSeparator-15]
|
||||
_ = x[LexicalVars-16]
|
||||
_ = x[Params-17]
|
||||
_ = x[Ref-18]
|
||||
_ = x[Cast-19]
|
||||
_ = x[Expr-20]
|
||||
_ = x[InitExpr-21]
|
||||
_ = x[CondExpr-22]
|
||||
_ = x[IncExpr-23]
|
||||
_ = x[True-24]
|
||||
_ = x[Cond-25]
|
||||
_ = x[HaltCompiller-26]
|
||||
_ = x[Namespace-27]
|
||||
_ = x[Static-28]
|
||||
_ = x[Class-29]
|
||||
_ = x[Use-30]
|
||||
_ = x[While-31]
|
||||
_ = x[For-32]
|
||||
_ = x[Switch-33]
|
||||
_ = x[Break-34]
|
||||
_ = x[Foreach-35]
|
||||
_ = x[Declare-36]
|
||||
_ = x[Label-37]
|
||||
_ = x[Finally-38]
|
||||
_ = x[List-39]
|
||||
_ = x[Default-40]
|
||||
_ = x[If-41]
|
||||
_ = x[ElseIf-42]
|
||||
_ = x[Else-43]
|
||||
_ = x[Variadic-44]
|
||||
_ = x[Function-45]
|
||||
_ = x[DoubleArrow-46]
|
||||
_ = x[Alias-47]
|
||||
_ = x[As-48]
|
||||
_ = x[Equal-49]
|
||||
_ = x[Exit-50]
|
||||
_ = x[Array-51]
|
||||
_ = x[Isset-52]
|
||||
_ = x[Empty-53]
|
||||
_ = x[Eval-54]
|
||||
_ = x[Echo-55]
|
||||
_ = x[Try-56]
|
||||
_ = x[Catch-57]
|
||||
_ = x[Unset-58]
|
||||
_ = x[Stmts-59]
|
||||
_ = x[VarList-60]
|
||||
_ = x[ConstList-61]
|
||||
_ = x[NameList-62]
|
||||
_ = x[ParamList-63]
|
||||
_ = x[ModifierList-64]
|
||||
_ = x[ArrayPairList-65]
|
||||
_ = x[CaseListStart-66]
|
||||
_ = x[CaseListEnd-67]
|
||||
_ = x[ArgumentList-68]
|
||||
_ = x[PropertyList-69]
|
||||
_ = x[ParameterList-70]
|
||||
_ = x[AdaptationList-71]
|
||||
_ = x[LexicalVarList-72]
|
||||
_ = x[UseDeclarationList-73]
|
||||
_ = x[OpenParenthesisToken-74]
|
||||
_ = x[CloseParenthesisToken-75]
|
||||
}
|
||||
|
||||
const _Position_name = "StartEndSlashColonSemiColonAltEndDollarAmpersandNamePrefixKeyVarUseTypeReturnTypeOptionalTypeCaseSeparatorLexicalVarsParamsRefCastExprInitExprCondExprIncExprTrueCondHaltCompillerNamespaceStaticClassUseWhileForSwitchBreakForeachDeclareLabelFinallyListDefaultIfElseIfElseVariadicFunctionDoubleArrowAliasAsEqualExitArrayIssetEmptyEvalEchoTryCatchUnsetStmtsVarListConstListNameListParamListModifierListArrayPairListCaseListStartCaseListEndArgumentListPropertyListParameterListAdaptationListLexicalVarListUseDeclarationListOpenParenthesisTokenCloseParenthesisToken"
|
||||
|
||||
var _Position_index = [...]uint16{0, 5, 8, 13, 18, 27, 33, 39, 48, 52, 58, 61, 64, 71, 81, 93, 106, 117, 123, 126, 130, 134, 142, 150, 157, 161, 165, 178, 187, 193, 198, 201, 206, 209, 215, 220, 227, 234, 239, 246, 250, 257, 259, 265, 269, 277, 285, 296, 301, 303, 308, 312, 317, 322, 327, 331, 335, 338, 343, 348, 353, 360, 369, 377, 386, 398, 411, 424, 435, 447, 459, 472, 486, 500, 518, 538, 559}
|
||||
|
||||
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]]
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
package token
|
||||
|
||||
type TokenID int
|
||||
type ID int
|
||||
|
||||
const (
|
||||
T_INCLUDE TokenID = iota + 57346
|
||||
T_INCLUDE ID = iota + 57346
|
||||
T_INCLUDE_ONCE
|
||||
T_EXIT
|
||||
T_IF
|
||||
@@ -144,6 +144,6 @@ const (
|
||||
)
|
||||
|
||||
type Token struct {
|
||||
ID TokenID
|
||||
ID ID
|
||||
Value []byte
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user