Skip to content
Open
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
66 changes: 64 additions & 2 deletions pkg/visitor/formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"github.com/rectorphp/php-parser-in-go/pkg/ast"
"github.com/rectorphp/php-parser-in-go/pkg/token"
"strconv"
)

type formatterState int
Expand Down Expand Up @@ -2146,12 +2147,73 @@ func (formatter *formatter) ScalarEncapsedStringBrackets(node *ast.ScalarEncapse
node.CloseCurlyBracketTkn = formatter.newToken('}', []byte("}"))
}

// ScalarHeredoc keeps the label a heredoc was written with, and whether it
// is a nowdoc, whose body is not interpolated: a label the body holds at
// the start of a line would end it there.
func (formatter *formatter) ScalarHeredoc(node *ast.ScalarHeredoc) {
node.OpenHeredocTkn = formatter.newToken(token.T_START_HEREDOC, []byte("<<<EOT\n"))
label, nowdoc := heredocLabel(node.OpenHeredocTkn)
label = freeLabel(label, node.Parts)
open := "<<<" + label + "\n"
if nowdoc {
open = "<<<'" + label + "'\n"
}
node.OpenHeredocTkn = formatter.newToken(token.T_START_HEREDOC, []byte(open))
for _, part := range node.Parts {
part.Accept(formatter)
}
node.CloseHeredocTkn = formatter.newToken(token.T_START_HEREDOC, []byte("EOT"))
node.CloseHeredocTkn = formatter.newToken(token.T_END_HEREDOC, []byte(label))
}

// heredocLabel returns the label an opening <<<LABEL, <<<"LABEL" or
// <<<'LABEL' token names, and whether it opens a nowdoc: EOT without one.
func heredocLabel(open *token.Token) (string, bool) {
if open == nil {
return "EOT", false
}
value := bytes.TrimLeft(bytes.TrimPrefix(open.Value, []byte("<<<")), " \t")
value = bytes.TrimRight(value, "\r\n")
nowdoc := bytes.HasPrefix(value, []byte("'"))
value = bytes.Trim(value, "'\"")
if len(value) == 0 {
return "EOT", false
}
return string(value), nowdoc
}

// freeLabel returns label, or one made from it, that starts no line of the
// heredoc's body as a whole word.
func freeLabel(label string, parts []ast.Vertex) string {
var body []byte
for _, part := range parts {
if text, ok := part.(*ast.ScalarEncapsedStringPart); ok {
body = append(body, text.Value...)
}
}
candidate := label
for i := 1; endsHeredoc(body, candidate); i++ {
candidate = label + strconv.Itoa(i)
}
return candidate
}

// endsHeredoc reports whether a line of body would end a heredoc labelled
// label: it starts, after indentation, with the label as a whole word.
func endsHeredoc(body []byte, label string) bool {
for _, line := range bytes.Split(body, []byte("\n")) {
line = bytes.TrimLeft(line, " \t")
rest, ok := bytes.CutPrefix(line, []byte(label))
if !ok {
continue
}
if len(rest) == 0 {
return true
}
c := rest[0]
if !(c == '_' || c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= 0x80) {
return true
}
}
return false
}

func (formatter *formatter) ScalarLnumber(node *ast.ScalarLnumber) {
Expand Down
61 changes: 61 additions & 0 deletions pkg/visitor/formatter/heredoc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package formatter_test

import (
"bytes"
"strings"
"testing"

"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/parser"
"github.com/rectorphp/php-parser-in-go/pkg/version"
"github.com/rectorphp/php-parser-in-go/pkg/visitor/formatter"
"github.com/rectorphp/php-parser-in-go/pkg/visitor/printer"
)

// formatHeredoc parses src, lays it out from scratch and prints it,
// without the open tag the formatter writes.
func formatHeredoc(test *testing.T, src string) string {
test.Helper()
ver, err := version.New("8.4")
if err != nil {
test.Fatal(err)
}
root, err := parser.Parse([]byte(src), conf.Config{Version: ver})
if err != nil {
test.Fatal(err)
}
root.Accept(formatter.NewFormatter())
var buffer bytes.Buffer
root.(*ast.Root).Accept(printer.NewPrinter(&buffer))
return strings.TrimLeft(strings.TrimPrefix(buffer.String(), "<?php"), " \n")
}

// A nowdoc stays one: as a heredoc, PHP would interpolate its body.
func TestFormatter_KeepsNowdoc(test *testing.T) {
actual := formatHeredoc(test, "<?php $a = <<<'TXT'\nval $x \\t end\nTXT;\n")
expected := "$a = <<<'TXT'\nval $x \\t end\nTXT;\n"
if actual != expected {
test.Errorf("\nexpected: %q\nactual: %q\n", expected, actual)
}
}

// A heredoc keeps its label, and takes another where its body holds a line
// that would end it: here EOT.
func TestFormatter_KeepsHeredocLabel(test *testing.T) {
actual := formatHeredoc(test, "<?php $a = <<<TXT\nEOT; x\nTXT;\n$b = <<<TXT\nTXT1\nTXT;\n")
expected := "$a = <<<TXT\nEOT; x\nTXT;\n$b = <<<TXT\nTXT1\nTXT;\n"
if actual != expected {
test.Errorf("\nexpected: %q\nactual: %q\n", expected, actual)
}
}

// Before PHP 7.3 nothing but a semicolon may follow a heredoc's closing
// label on its line.
func TestFormatter_HeredocArgumentEndsLine(test *testing.T) {
actual := formatHeredoc(test, "<?php echo strtoupper(<<<EOT\nabc\nEOT\n), 1;")
expected := "echo strtoupper(<<<EOT\nabc\nEOT\n), 1;"
if actual != expected {
test.Errorf("\nexpected: %q\nactual: %q\n", expected, actual)
}
}
15 changes: 13 additions & 2 deletions pkg/visitor/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package printer

import (
"bytes"
"io"

"github.com/rectorphp/php-parser-in-go/pkg/ast"
"github.com/rectorphp/php-parser-in-go/pkg/token"
"io"
)

type printerState int
Expand All @@ -18,6 +19,9 @@ type printer struct {
output io.Writer
state printerState
last []byte
// heredocClosed is set after a heredoc's closing label: before PHP 7.3
// nothing but a semicolon may follow it on its line.
heredocClosed bool
}

func NewPrinter(output io.Writer) *printer {
Expand Down Expand Up @@ -57,6 +61,13 @@ func (printer *printer) write(content []byte) {
printer.state = PrinterStatePHP
}

if printer.heredocClosed {
printer.heredocClosed = false
if content[0] != ';' && content[0] != '\n' && content[0] != '\r' {
printer.output.Write([]byte("\n"))
}
}

if printer.last != nil && isValidVarName(printer.last[len(printer.last)-1]) && isValidVarName(content[0]) {
printer.output.Write([]byte(" "))
}
Expand Down Expand Up @@ -313,7 +324,6 @@ func (printer *printer) StmtDo(node *ast.StmtDo) {
printer.printNode(node.Cond)
printer.printToken(node.CloseParenthesisTkn, []byte(")"))
printer.printToken(node.SemiColonTkn, []byte(";"))

}

func (printer *printer) StmtEcho(node *ast.StmtEcho) {
Expand Down Expand Up @@ -1310,6 +1320,7 @@ func (printer *printer) ScalarHeredoc(node *ast.ScalarHeredoc) {
printer.printToken(node.OpenHeredocTkn, []byte("<<<EOT\n"))
printer.printList(node.Parts)
printer.printToken(node.CloseHeredocTkn, []byte("EOT"))
printer.heredocClosed = true
}

func (printer *printer) ScalarLnumber(node *ast.ScalarLnumber) {
Expand Down