diff --git a/internal/php5/parser_test.go b/internal/php5/parser_test.go index 7930f17..dcb70ae 100644 --- a/internal/php5/parser_test.go +++ b/internal/php5/parser_test.go @@ -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() diff --git a/internal/php7/parser_test.go b/internal/php7/parser_test.go index 0e1ff12..47a0eca 100644 --- a/internal/php7/parser_test.go +++ b/internal/php7/parser_test.go @@ -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() diff --git a/internal/scanner/scanner.go b/internal/scanner/scanner.go index 2a9bc8d..c6ebf1c 100644 Binary files a/internal/scanner/scanner.go and b/internal/scanner/scanner.go differ diff --git a/internal/scanner/scanner.rl b/internal/scanner/scanner.rl index b1c1083..cd29221 100644 --- a/internal/scanner/scanner.rl +++ b/internal/scanner/scanner.rl @@ -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; @@ -79,45 +79,45 @@ func (lex *Lexer) Lex() *Token { # single qoute string qoute: ( - (any - [\\'\r\n]) -> qoute - | "\r" @constant_string_new_line -> qoute - | "\n" @constant_string_new_line -> qoute - | "\\" -> qoute_any - | "'" -> final + (any - [\\'\r\n]) -> 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 + (any - [\r\n]) -> 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 - | "\\" -> double_qoute_any - | '"' -> final - | '$' -> double_qoute_nonvarname - | '{' -> double_qoute_nondollar + (any - [\\"${\r\n]) -> double_qoute + | "\r" @new_line -> double_qoute + | "\n" @new_line -> double_qoute + | "\\" -> double_qoute_any + | '"' -> final + | '$' -> double_qoute_nonvarname + | '{' -> double_qoute_nondollar ), double_qoute_any: ( - (any - [\r\n]) -> double_qoute - | "\r" @constant_string_new_line -> double_qoute - | "\n" @constant_string_new_line -> double_qoute + (any - [\r\n]) -> 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 - | "\\" -> double_qoute_any - | '"' -> final + (any - [\\$"\r\n]) -> 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 ); diff --git a/pkg/ast/visitor/namespace_resolver.go b/pkg/ast/visitor/namespace_resolver.go index 3029c5c..9438971 100644 --- a/pkg/ast/visitor/namespace_resolver.go +++ b/pkg/ast/visitor/namespace_resolver.go @@ -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)) diff --git a/pkg/ast/visitor/namespace_resolver_test.go b/pkg/ast/visitor/namespace_resolver_test.go index 6cab1d0..d52d635 100644 --- a/pkg/ast/visitor/namespace_resolver_test.go +++ b/pkg/ast/visitor/namespace_resolver_test.go @@ -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) +} diff --git a/pkg/printer/pretty_printer.go b/pkg/printer/pretty_printer.go index 77375ea..0948cdd 100644 --- a/pkg/printer/pretty_printer.go +++ b/pkg/printer/pretty_printer.go @@ -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") } diff --git a/pkg/printer/pretty_printer_test.go b/pkg/printer/pretty_printer_test.go index a686aff..dd20c5d 100644 --- a/pkg/printer/pretty_printer_test.go +++ b/pkg/printer/pretty_printer_test.go @@ -9,10 +9,17 @@ import ( ) func TestPrintFile(t *testing.T) { - o := bytes.NewBufferString("") - - p := printer.NewPrettyPrinter(o, "\t") - p.Print(&ast.Root{ + expected := `