Merge branch 'master' into refactoring
This commit is contained in:
commit
88dfd32d9e
@ -1049,8 +1049,7 @@ func TestPhp5ParameterNode(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Stmts: []ast.Vertex{
|
Stmts: []ast.Vertex{},
|
||||||
},
|
|
||||||
},
|
},
|
||||||
&ast.StmtClass{
|
&ast.StmtClass{
|
||||||
Node: ast.Node{
|
Node: ast.Node{
|
||||||
@ -1278,8 +1277,7 @@ func TestPhp5ParameterNode(t *testing.T) {
|
|||||||
EndPos: 114,
|
EndPos: 114,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Stmts: []ast.Vertex{
|
Stmts: []ast.Vertex{},
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -1465,8 +1463,7 @@ func TestPhp5ParameterNode(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Stmts: []ast.Vertex{
|
Stmts: []ast.Vertex{},
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
&ast.StmtExpression{
|
&ast.StmtExpression{
|
||||||
@ -1651,14 +1648,12 @@ func TestPhp5ParameterNode(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Stmts: []ast.Vertex{
|
Stmts: []ast.Vertex{},
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
lexer := scanner.NewLexer([]byte(src), "5.6", false, nil)
|
lexer := scanner.NewLexer([]byte(src), "5.6", false, nil)
|
||||||
php5parser := php5.NewParser(lexer, nil)
|
php5parser := php5.NewParser(lexer, nil)
|
||||||
php5parser.Parse()
|
php5parser.Parse()
|
||||||
|
@ -1170,8 +1170,7 @@ func TestPhp7ParameterNode(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Stmts: []ast.Vertex{
|
Stmts: []ast.Vertex{},
|
||||||
},
|
|
||||||
},
|
},
|
||||||
&ast.StmtClass{
|
&ast.StmtClass{
|
||||||
Node: ast.Node{
|
Node: ast.Node{
|
||||||
@ -1409,8 +1408,7 @@ func TestPhp7ParameterNode(t *testing.T) {
|
|||||||
EndPos: 116,
|
EndPos: 116,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Stmts: []ast.Vertex{
|
Stmts: []ast.Vertex{},
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -1606,8 +1604,7 @@ func TestPhp7ParameterNode(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Stmts: []ast.Vertex{
|
Stmts: []ast.Vertex{},
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
&ast.StmtExpression{
|
&ast.StmtExpression{
|
||||||
@ -1802,14 +1799,12 @@ func TestPhp7ParameterNode(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Stmts: []ast.Vertex{
|
Stmts: []ast.Vertex{},
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
lexer := scanner.NewLexer([]byte(src), "7.4", false, nil)
|
lexer := scanner.NewLexer([]byte(src), "7.4", false, nil)
|
||||||
php7parser := php7.NewParser(lexer, nil)
|
php7parser := php7.NewParser(lexer, nil)
|
||||||
php7parser.Parse()
|
php7parser.Parse()
|
||||||
|
BIN
internal/scanner/scanner.go
generated
BIN
internal/scanner/scanner.go
generated
Binary file not shown.
@ -35,13 +35,13 @@ func (lex *Lexer) Lex() *Token {
|
|||||||
action heredoc_lbl_start {lblStart = lex.p}
|
action heredoc_lbl_start {lblStart = lex.p}
|
||||||
action heredoc_lbl_end {lblEnd = lex.p}
|
action heredoc_lbl_end {lblEnd = lex.p}
|
||||||
|
|
||||||
action constant_string_new_line {
|
action new_line {
|
||||||
if lex.data[lex.p] == '\n' {
|
if lex.data[lex.p] == '\n' {
|
||||||
lex.newLines.Append(lex.p)
|
lex.newLines.Append(lex.p+1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if lex.data[lex.p] == '\r' && lex.data[lex.p+1] != '\n' {
|
if lex.data[lex.p] == '\r' && lex.data[lex.p+1] != '\n' {
|
||||||
lex.newLines.Append(lex.p)
|
lex.newLines.Append(lex.p+1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,7 +51,7 @@ func (lex *Lexer) Lex() *Token {
|
|||||||
action is_not_string_end_or_var { lex.isNotStringEnd('"') && lex.isNotStringVar() }
|
action is_not_string_end_or_var { lex.isNotStringEnd('"') && lex.isNotStringVar() }
|
||||||
action is_not_backqoute_end_or_var { lex.isNotStringEnd('`') && lex.isNotStringVar() }
|
action is_not_backqoute_end_or_var { lex.isNotStringEnd('`') && lex.isNotStringVar() }
|
||||||
|
|
||||||
newline = ('\r\n' >(nl, 1) | '\r' >(nl, 0) | '\n' >(nl, 0)) %{lex.newLines.Append(lex.p);};
|
newline = ('\r\n' >(nl, 1) | '\r' >(nl, 0) | '\n' >(nl, 0)) $new_line %{};
|
||||||
any_line = any | newline;
|
any_line = any | newline;
|
||||||
whitespace = [\t\v\f ];
|
whitespace = [\t\v\f ];
|
||||||
whitespace_line = [\t\v\f ] | newline;
|
whitespace_line = [\t\v\f ] | newline;
|
||||||
@ -79,45 +79,45 @@ func (lex *Lexer) Lex() *Token {
|
|||||||
# single qoute string
|
# single qoute string
|
||||||
|
|
||||||
qoute: (
|
qoute: (
|
||||||
(any - [\\'\r\n]) -> qoute
|
(any - [\\'\r\n]) -> qoute
|
||||||
| "\r" @constant_string_new_line -> qoute
|
| "\r" @new_line -> qoute
|
||||||
| "\n" @constant_string_new_line -> qoute
|
| "\n" @new_line -> qoute
|
||||||
| "\\" -> qoute_any
|
| "\\" -> qoute_any
|
||||||
| "'" -> final
|
| "'" -> final
|
||||||
),
|
),
|
||||||
qoute_any: (
|
qoute_any: (
|
||||||
(any - [\r\n]) -> qoute
|
(any - [\r\n]) -> qoute
|
||||||
| "\r" @constant_string_new_line -> qoute
|
| "\r" @new_line -> qoute
|
||||||
| "\n" @constant_string_new_line -> qoute
|
| "\n" @new_line -> qoute
|
||||||
),
|
),
|
||||||
|
|
||||||
# double qoute string
|
# double qoute string
|
||||||
|
|
||||||
double_qoute: (
|
double_qoute: (
|
||||||
(any - [\\"${\r\n]) -> double_qoute
|
(any - [\\"${\r\n]) -> double_qoute
|
||||||
| "\r" @constant_string_new_line -> double_qoute
|
| "\r" @new_line -> double_qoute
|
||||||
| "\n" @constant_string_new_line -> double_qoute
|
| "\n" @new_line -> double_qoute
|
||||||
| "\\" -> double_qoute_any
|
| "\\" -> double_qoute_any
|
||||||
| '"' -> final
|
| '"' -> final
|
||||||
| '$' -> double_qoute_nonvarname
|
| '$' -> double_qoute_nonvarname
|
||||||
| '{' -> double_qoute_nondollar
|
| '{' -> double_qoute_nondollar
|
||||||
),
|
),
|
||||||
double_qoute_any: (
|
double_qoute_any: (
|
||||||
(any - [\r\n]) -> double_qoute
|
(any - [\r\n]) -> double_qoute
|
||||||
| "\r" @constant_string_new_line -> double_qoute
|
| "\r" @new_line -> double_qoute
|
||||||
| "\n" @constant_string_new_line -> double_qoute
|
| "\n" @new_line -> double_qoute
|
||||||
),
|
),
|
||||||
double_qoute_nondollar: (
|
double_qoute_nondollar: (
|
||||||
(any - [\\$"\r\n]) -> double_qoute
|
(any - [\\$"\r\n]) -> double_qoute
|
||||||
| "\r" @constant_string_new_line -> double_qoute
|
| "\r" @new_line -> double_qoute
|
||||||
| "\n" @constant_string_new_line -> double_qoute
|
| "\n" @new_line -> double_qoute
|
||||||
| "\\" -> double_qoute_any
|
| "\\" -> double_qoute_any
|
||||||
| '"' -> final
|
| '"' -> final
|
||||||
),
|
),
|
||||||
double_qoute_nonvarname: (
|
double_qoute_nonvarname: (
|
||||||
(any - [\\{"\r\n] - varname_first) -> double_qoute
|
(any - [\\{"\r\n] - varname_first) -> double_qoute
|
||||||
| "\r" @constant_string_new_line -> double_qoute
|
| "\r" @new_line -> double_qoute
|
||||||
| "\n" @constant_string_new_line -> double_qoute
|
| "\n" @new_line -> double_qoute
|
||||||
| "\\" -> double_qoute_any
|
| "\\" -> double_qoute_any
|
||||||
| '"' -> final
|
| '"' -> final
|
||||||
);
|
);
|
||||||
|
@ -133,6 +133,12 @@ func (nsr *NamespaceResolver) ExprClosure(n *ast.ExprClosure) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (nsr *NamespaceResolver) StmtPropertyList(n *ast.StmtPropertyList) {
|
||||||
|
if n.Type != nil {
|
||||||
|
nsr.ResolveType(n.Type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (nsr *NamespaceResolver) StmtConstList(n *ast.StmtConstList) {
|
func (nsr *NamespaceResolver) StmtConstList(n *ast.StmtConstList) {
|
||||||
for _, constant := range n.Consts {
|
for _, constant := range n.Consts {
|
||||||
nsr.AddNamespacedName(constant, string(constant.(*ast.StmtConstant).ConstantName.(*ast.Identifier).Value))
|
nsr.AddNamespacedName(constant, string(constant.(*ast.StmtConstant).ConstantName.(*ast.Identifier).Value))
|
||||||
|
@ -978,3 +978,55 @@ func TestDoNotResolveReservedSpecialNames(t *testing.T) {
|
|||||||
|
|
||||||
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||||
}
|
}
|
||||||
|
func TestResolvePropertyTypeName(t *testing.T) {
|
||||||
|
nameSimple := &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("A")}, &ast.NameNamePart{Value: []byte("B")}}}
|
||||||
|
nameRelative := &ast.NameRelative{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("A")}, &ast.NameNamePart{Value: []byte("B")}}}
|
||||||
|
nameFullyQualified := &ast.NameFullyQualified{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("A")}, &ast.NameNamePart{Value: []byte("B")}}}
|
||||||
|
|
||||||
|
propertyNodeSimple := &ast.StmtPropertyList{
|
||||||
|
Type: nameSimple,
|
||||||
|
}
|
||||||
|
|
||||||
|
propertyNodeRelative := &ast.StmtPropertyList{
|
||||||
|
Type: nameRelative,
|
||||||
|
}
|
||||||
|
|
||||||
|
propertyNodeFullyQualified := &ast.StmtPropertyList{
|
||||||
|
Type: nameFullyQualified,
|
||||||
|
}
|
||||||
|
|
||||||
|
classNode := &ast.StmtClass{
|
||||||
|
ClassName: &ast.Identifier{Value: []byte("Bar")},
|
||||||
|
Stmts: []ast.Vertex{
|
||||||
|
propertyNodeSimple,
|
||||||
|
propertyNodeRelative,
|
||||||
|
propertyNodeFullyQualified,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
stmts := &ast.StmtStmtList{
|
||||||
|
Stmts: []ast.Vertex{
|
||||||
|
&ast.StmtNamespace{
|
||||||
|
NamespaceName: &ast.NameName{
|
||||||
|
Parts: []ast.Vertex{
|
||||||
|
&ast.NameNamePart{Value: []byte("Foo")},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
classNode,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := map[ast.Vertex]string{
|
||||||
|
nameSimple: "Foo\\A\\B",
|
||||||
|
nameRelative: "Foo\\A\\B",
|
||||||
|
nameFullyQualified: "A\\B",
|
||||||
|
classNode: "Foo\\Bar",
|
||||||
|
}
|
||||||
|
|
||||||
|
nsResolver := visitor.NewNamespaceResolver()
|
||||||
|
dfsTraverser := traverser.NewDFS(nsResolver)
|
||||||
|
dfsTraverser.Traverse(stmts)
|
||||||
|
|
||||||
|
assert.DeepEqual(t, expected, nsResolver.ResolvedNames)
|
||||||
|
}
|
||||||
|
@ -411,11 +411,12 @@ func (p *PrettyPrinter) printNode(n ast.Vertex) {
|
|||||||
// node
|
// node
|
||||||
|
|
||||||
func (p *PrettyPrinter) printNodeRoot(n ast.Vertex) {
|
func (p *PrettyPrinter) printNodeRoot(n ast.Vertex) {
|
||||||
|
var stmts []ast.Vertex
|
||||||
v := n.(*ast.Root)
|
v := n.(*ast.Root)
|
||||||
|
|
||||||
if len(v.Stmts) > 0 {
|
if len(v.Stmts) > 0 {
|
||||||
firstStmt := v.Stmts[0]
|
firstStmt := v.Stmts[0]
|
||||||
v.Stmts = v.Stmts[1:]
|
stmts = v.Stmts[1:]
|
||||||
|
|
||||||
switch fs := firstStmt.(type) {
|
switch fs := firstStmt.(type) {
|
||||||
case *ast.StmtInlineHtml:
|
case *ast.StmtInlineHtml:
|
||||||
@ -429,7 +430,7 @@ func (p *PrettyPrinter) printNodeRoot(n ast.Vertex) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
p.indentDepth--
|
p.indentDepth--
|
||||||
p.printNodes(v.Stmts)
|
p.printNodes(stmts)
|
||||||
io.WriteString(p.w, "\n")
|
io.WriteString(p.w, "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,10 +9,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestPrintFile(t *testing.T) {
|
func TestPrintFile(t *testing.T) {
|
||||||
o := bytes.NewBufferString("")
|
expected := `<?php
|
||||||
|
namespace Foo;
|
||||||
p := printer.NewPrettyPrinter(o, "\t")
|
abstract class Bar extends Baz
|
||||||
p.Print(&ast.Root{
|
{
|
||||||
|
public function greet()
|
||||||
|
{
|
||||||
|
echo 'Hello world';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
rootNode := &ast.Root{
|
||||||
Stmts: []ast.Vertex{
|
Stmts: []ast.Vertex{
|
||||||
&ast.StmtNamespace{
|
&ast.StmtNamespace{
|
||||||
NamespaceName: &ast.NameName{
|
NamespaceName: &ast.NameName{
|
||||||
@ -52,22 +59,20 @@ func TestPrintFile(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
|
||||||
|
|
||||||
expected := `<?php
|
|
||||||
namespace Foo;
|
|
||||||
abstract class Bar extends Baz
|
|
||||||
{
|
|
||||||
public function greet()
|
|
||||||
{
|
|
||||||
echo 'Hello world';
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
`
|
|
||||||
actual := o.String()
|
|
||||||
|
|
||||||
if expected != actual {
|
o1 := bytes.NewBufferString("")
|
||||||
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
|
p1 := printer.NewPrettyPrinter(o1, "\t")
|
||||||
|
p1.Print(rootNode)
|
||||||
|
if actual := o1.String(); expected != actual {
|
||||||
|
t.Errorf("\nPrint the 1st time\nexpected: %s\ngot: %s\n", expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
o2 := bytes.NewBufferString("")
|
||||||
|
p2 := printer.NewPrettyPrinter(o2, "\t")
|
||||||
|
p2.Print(rootNode)
|
||||||
|
if actual := o2.String(); expected != actual {
|
||||||
|
t.Errorf("\nPrint the 2nd time\nexpected: %s\ngot: %s\n", expected, actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2925,7 +2925,7 @@ func TestPrinterPrintStmtClassMethod(t *testing.T) {
|
|||||||
MethodName: &ast.Identifier{Value: []byte("foo")},
|
MethodName: &ast.Identifier{Value: []byte("foo")},
|
||||||
Params: []ast.Vertex{
|
Params: []ast.Vertex{
|
||||||
&ast.Parameter{
|
&ast.Parameter{
|
||||||
Type: &ast.Nullable{Expr: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("int")}}}},
|
Type: &ast.Nullable{Expr: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("int")}}}},
|
||||||
Var: &ast.Reference{
|
Var: &ast.Reference{
|
||||||
Var: &ast.ExprVariable{
|
Var: &ast.ExprVariable{
|
||||||
VarName: &ast.Identifier{Value: []byte("$a")},
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
||||||
@ -2974,7 +2974,7 @@ func TestPrinterPrintStmtAbstractClassMethod(t *testing.T) {
|
|||||||
MethodName: &ast.Identifier{Value: []byte("foo")},
|
MethodName: &ast.Identifier{Value: []byte("foo")},
|
||||||
Params: []ast.Vertex{
|
Params: []ast.Vertex{
|
||||||
&ast.Parameter{
|
&ast.Parameter{
|
||||||
Type: &ast.Nullable{Expr: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("int")}}}},
|
Type: &ast.Nullable{Expr: &ast.NameName{Parts: []ast.Vertex{&ast.NameNamePart{Value: []byte("int")}}}},
|
||||||
Var: &ast.Reference{
|
Var: &ast.Reference{
|
||||||
Var: &ast.ExprVariable{
|
Var: &ast.ExprVariable{
|
||||||
VarName: &ast.Identifier{Value: []byte("$a")},
|
VarName: &ast.Identifier{Value: []byte("$a")},
|
||||||
|
Loading…
Reference in New Issue
Block a user