Merge branch 'master' into refactoring

This commit is contained in:
Vadym Slizov 2020-07-29 22:23:44 +02:00
commit 88dfd32d9e
9 changed files with 123 additions and 69 deletions

View File

@ -1049,8 +1049,7 @@ func TestPhp5ParameterNode(t *testing.T) {
},
},
},
Stmts: []ast.Vertex{
},
Stmts: []ast.Vertex{},
},
&ast.StmtClass{
Node: ast.Node{
@ -1278,8 +1277,7 @@ func TestPhp5ParameterNode(t *testing.T) {
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{
@ -1651,14 +1648,12 @@ func TestPhp5ParameterNode(t *testing.T) {
},
},
},
Stmts: []ast.Vertex{
},
Stmts: []ast.Vertex{},
},
},
},
}
lexer := scanner.NewLexer([]byte(src), "5.6", false, nil)
php5parser := php5.NewParser(lexer, nil)
php5parser.Parse()

View File

@ -1170,8 +1170,7 @@ func TestPhp7ParameterNode(t *testing.T) {
},
},
},
Stmts: []ast.Vertex{
},
Stmts: []ast.Vertex{},
},
&ast.StmtClass{
Node: ast.Node{
@ -1409,8 +1408,7 @@ func TestPhp7ParameterNode(t *testing.T) {
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{
@ -1802,14 +1799,12 @@ func TestPhp7ParameterNode(t *testing.T) {
},
},
},
Stmts: []ast.Vertex{
},
Stmts: []ast.Vertex{},
},
},
},
}
lexer := scanner.NewLexer([]byte(src), "7.4", false, nil)
php7parser := php7.NewParser(lexer, nil)
php7parser.Parse()

Binary file not shown.

View File

@ -35,13 +35,13 @@ func (lex *Lexer) Lex() *Token {
action heredoc_lbl_start {lblStart = lex.p}
action heredoc_lbl_end {lblEnd = lex.p}
action constant_string_new_line {
action new_line {
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' {
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_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;
whitespace = [\t\v\f ];
whitespace_line = [\t\v\f ] | newline;
@ -80,23 +80,23 @@ func (lex *Lexer) Lex() *Token {
qoute: (
(any - [\\'\r\n]) -> qoute
| "\r" @constant_string_new_line -> qoute
| "\n" @constant_string_new_line -> qoute
| "\r" @new_line -> qoute
| "\n" @new_line -> qoute
| "\\" -> qoute_any
| "'" -> final
),
qoute_any: (
(any - [\r\n]) -> qoute
| "\r" @constant_string_new_line -> qoute
| "\n" @constant_string_new_line -> qoute
| "\r" @new_line -> qoute
| "\n" @new_line -> qoute
),
# double qoute string
double_qoute: (
(any - [\\"${\r\n]) -> double_qoute
| "\r" @constant_string_new_line -> double_qoute
| "\n" @constant_string_new_line -> double_qoute
| "\r" @new_line -> double_qoute
| "\n" @new_line -> double_qoute
| "\\" -> double_qoute_any
| '"' -> final
| '$' -> double_qoute_nonvarname
@ -104,20 +104,20 @@ func (lex *Lexer) Lex() *Token {
),
double_qoute_any: (
(any - [\r\n]) -> double_qoute
| "\r" @constant_string_new_line -> double_qoute
| "\n" @constant_string_new_line -> double_qoute
| "\r" @new_line -> double_qoute
| "\n" @new_line -> double_qoute
),
double_qoute_nondollar: (
(any - [\\$"\r\n]) -> double_qoute
| "\r" @constant_string_new_line -> double_qoute
| "\n" @constant_string_new_line -> double_qoute
| "\r" @new_line -> double_qoute
| "\n" @new_line -> double_qoute
| "\\" -> double_qoute_any
| '"' -> final
),
double_qoute_nonvarname: (
(any - [\\{"\r\n] - varname_first) -> double_qoute
| "\r" @constant_string_new_line -> double_qoute
| "\n" @constant_string_new_line -> double_qoute
| "\r" @new_line -> double_qoute
| "\n" @new_line -> double_qoute
| "\\" -> double_qoute_any
| '"' -> final
);

View File

@ -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) {
for _, constant := range n.Consts {
nsr.AddNamespacedName(constant, string(constant.(*ast.StmtConstant).ConstantName.(*ast.Identifier).Value))

View File

@ -978,3 +978,55 @@ func TestDoNotResolveReservedSpecialNames(t *testing.T) {
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)
}

View File

@ -411,11 +411,12 @@ func (p *PrettyPrinter) printNode(n ast.Vertex) {
// node
func (p *PrettyPrinter) printNodeRoot(n ast.Vertex) {
var stmts []ast.Vertex
v := n.(*ast.Root)
if len(v.Stmts) > 0 {
firstStmt := v.Stmts[0]
v.Stmts = v.Stmts[1:]
stmts = v.Stmts[1:]
switch fs := firstStmt.(type) {
case *ast.StmtInlineHtml:
@ -429,7 +430,7 @@ func (p *PrettyPrinter) printNodeRoot(n ast.Vertex) {
}
}
p.indentDepth--
p.printNodes(v.Stmts)
p.printNodes(stmts)
io.WriteString(p.w, "\n")
}

View File

@ -9,10 +9,17 @@ import (
)
func TestPrintFile(t *testing.T) {
o := bytes.NewBufferString("")
p := printer.NewPrettyPrinter(o, "\t")
p.Print(&ast.Root{
expected := `<?php
namespace Foo;
abstract class Bar extends Baz
{
public function greet()
{
echo 'Hello world';
}
}
`
rootNode := &ast.Root{
Stmts: []ast.Vertex{
&ast.StmtNamespace{
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 {
t.Errorf("\nexpected: %s\ngot: %s\n", expected, actual)
o1 := bytes.NewBufferString("")
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)
}
}