Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5,242 changes: 2,662 additions & 2,580 deletions internal/php8/php8.go

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions internal/php8/php8.y
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ import (
%token <token> T_PUBLIC_SET
%token <token> T_PROTECTED_SET
%token <token> T_PRIVATE_SET
%token <token> T_PIPE
%token <token> T_VOID_CAST

%token <token> '"'
%token <token> '`'
Expand Down Expand Up @@ -216,6 +218,7 @@ import (
%left '&' T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG
%nonassoc T_IS_EQUAL T_IS_NOT_EQUAL T_IS_IDENTICAL T_IS_NOT_IDENTICAL T_SPACESHIP
%nonassoc '<' T_IS_SMALLER_OR_EQUAL '>' T_IS_GREATER_OR_EQUAL
%left T_PIPE
%left T_SL T_SR
%left '+' '-' '.'
%left '*' '/' '%'
Expand Down Expand Up @@ -1106,6 +1109,19 @@ statement:
SemiColonTkn: $2,
}
}
// PHP 8.5 `(void) expr;` discards a result on purpose; it is a statement, not an expression.
| T_VOID_CAST expr ';'
{
$$ = &ast.StmtExpression{
Position: yylex.(*Parser).builder.NewTokensPosition($1, $3),
Expr: &ast.ExprCastVoid{
Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2),
CastTkn: $1,
Expr: $2,
},
SemiColonTkn: $3,
}
}
| T_UNSET '(' unset_variables possible_comma ')' ';'
{
$3.(*ast.StmtUnset).UnsetTkn = $1
Expand Down Expand Up @@ -3227,6 +3243,27 @@ non_empty_for_exprs:

$$ = $1
}
| non_empty_for_exprs ',' T_VOID_CAST expr
{
$1.(*ParserSeparatedList).SeparatorTkns = append($1.(*ParserSeparatedList).SeparatorTkns, $2)
$1.(*ParserSeparatedList).Items = append($1.(*ParserSeparatedList).Items, &ast.ExprCastVoid{
Position: yylex.(*Parser).builder.NewTokenNodePosition($3, $4),
CastTkn: $3,
Expr: $4,
})

$$ = $1
}
| T_VOID_CAST expr
{
$$ = &ParserSeparatedList{
Items: []ast.Vertex{&ast.ExprCastVoid{
Position: yylex.(*Parser).builder.NewTokenNodePosition($1, $2),
CastTkn: $1,
Expr: $2,
}},
}
}
| expr
{
$$ = &ParserSeparatedList{
Expand Down Expand Up @@ -3906,6 +3943,16 @@ expr_without_variable:
IfFalse: $4,
}
}
// PHP 8.5 pipe: `$value |> trim(...)` passes the left side to the callable on the right.
| expr T_PIPE expr
{
$$ = &ast.ExprBinaryPipe{
Position: yylex.(*Parser).builder.NewNodesPosition($1, $3),
Left: $1,
OpTkn: $2,
Right: $3,
}
}
| expr T_COALESCE expr
{
$$ = &ast.ExprBinaryCoalesce{
Expand Down
87 changes: 87 additions & 0 deletions internal/php8/pipe_operator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package php8_test

import (
"bytes"
"testing"

"github.com/rectorphp/php-parser-in-go/internal/php8"
"github.com/rectorphp/php-parser-in-go/pkg/ast"
"github.com/rectorphp/php-parser-in-go/pkg/conf"
"github.com/rectorphp/php-parser-in-go/pkg/errors"
"github.com/rectorphp/php-parser-in-go/pkg/version"
"github.com/rectorphp/php-parser-in-go/pkg/visitor/printer"
)

// parsePHP85 parses PHP 8.5 source and returns the root plus every parser error
// it collected.
func parsePHP85(source string) (ast.Vertex, []*errors.Error) {
var parserErrors []*errors.Error
config := conf.Config{
Version: &version.Version{Major: 8, Minor: 5},
ErrorHandlerFunc: func(parserError *errors.Error) { parserErrors = append(parserErrors, parserError) },
}
lexer := php8.NewLexer([]byte(source), config)
parser := php8.NewParser(lexer, config)
parser.Parse()
return parser.GetRootNode(), parserErrors
}

func TestPipeOperator(test *testing.T) {
root, parserErrors := parsePHP85(`<?php $slug = $title |> trim(...) |> strtolower(...);`)
if len(parserErrors) != 0 {
test.Fatalf("unexpected parser errors: %v", parserErrors)
}

assign := root.(*ast.Root).Stmts[0].(*ast.StmtExpression).Expr.(*ast.ExprAssign)
outer, ok := assign.Expr.(*ast.ExprBinaryPipe)
if !ok {
test.Fatalf("expected a pipe, got %T", assign.Expr)
}
// the pipe is left-associative: ($title |> trim(...)) |> strtolower(...)
if _, ok := outer.Left.(*ast.ExprBinaryPipe); !ok {
test.Errorf("expected the left side to be the inner pipe, got %T", outer.Left)
}
if _, ok := outer.Right.(*ast.ExprFunctionCall); !ok {
test.Errorf("expected the right side to be a first-class callable, got %T", outer.Right)
}
}

// The pipe binds tighter than comparison and looser than concatenation.
func TestPipeOperatorPrecedence(test *testing.T) {
root, parserErrors := parsePHP85(`<?php $matches = $a . $b |> strtoupper(...) === 'AB';`)
if len(parserErrors) != 0 {
test.Fatalf("unexpected parser errors: %v", parserErrors)
}

assign := root.(*ast.Root).Stmts[0].(*ast.StmtExpression).Expr.(*ast.ExprAssign)
identical, ok := assign.Expr.(*ast.ExprBinaryIdentical)
if !ok {
test.Fatalf("expected the comparison at the top, got %T", assign.Expr)
}
pipe, ok := identical.Left.(*ast.ExprBinaryPipe)
if !ok {
test.Fatalf("expected the pipe under the comparison, got %T", identical.Left)
}
if _, ok := pipe.Left.(*ast.ExprBinaryConcat); !ok {
test.Errorf("expected the concatenation under the pipe, got %T", pipe.Left)
}
}

func TestPipeOperatorPrintsBackUnchanged(test *testing.T) {
source := `<?php
$slug = $title
|> trim(...)
|> strtolower(...);
`

root, parserErrors := parsePHP85(source)
if len(parserErrors) != 0 {
test.Fatalf("unexpected parser errors: %v", parserErrors)
}

output := bytes.NewBufferString("")
root.Accept(printer.NewPrinter(output))
if output.String() != source {
test.Errorf("printed output differs from source:\n%s", output.String())
}
}
Loading
Loading