44 "bytes"
55 "github.com/rectorphp/php-parser-in-go/pkg/ast"
66 "github.com/rectorphp/php-parser-in-go/pkg/token"
7+ "strconv"
78)
89
910type formatterState int
@@ -19,6 +20,9 @@ type formatter struct {
1920 freeFloating []* token.Token
2021
2122 lastSemiColon * token.Token
23+ // heredocClosed is set after a heredoc's closing label: before PHP 7.3
24+ // nothing but a semicolon may follow it on its line.
25+ heredocClosed bool
2226}
2327
2428func NewFormatter () * formatter {
@@ -79,6 +83,13 @@ func (formatter *formatter) getFreeFloating() []*token.Token {
7983}
8084
8185func (formatter * formatter ) newToken (tokenID token.ID , value []byte ) * token.Token {
86+ if formatter .heredocClosed {
87+ formatter .heredocClosed = false
88+ if tokenID != ';' {
89+ formatter .freeFloating = append ([]* token.Token {{ID : token .T_WHITESPACE , Value : []byte ("\n " )}}, formatter .freeFloating ... )
90+ }
91+ }
92+
8293 return & token.Token {
8394 ID : tokenID ,
8495 Value : value ,
@@ -2345,12 +2356,74 @@ func (formatter *formatter) ScalarEncapsedStringBrackets(node *ast.ScalarEncapse
23452356 node .CloseCurlyBracketTkn = formatter .newToken ('}' , []byte ("}" ))
23462357}
23472358
2359+ // ScalarHeredoc keeps the label a heredoc was written with, and whether it
2360+ // is a nowdoc, whose body is not interpolated: a label the body holds at
2361+ // the start of a line would end it there.
23482362func (formatter * formatter ) ScalarHeredoc (node * ast.ScalarHeredoc ) {
2349- node .OpenHeredocTkn = formatter .newToken (token .T_START_HEREDOC , []byte ("<<<EOT\n " ))
2363+ label , nowdoc := heredocLabel (node .OpenHeredocTkn )
2364+ label = freeLabel (label , node .Parts )
2365+ open := "<<<" + label + "\n "
2366+ if nowdoc {
2367+ open = "<<<'" + label + "'\n "
2368+ }
2369+ node .OpenHeredocTkn = formatter .newToken (token .T_START_HEREDOC , []byte (open ))
23502370 for _ , part := range node .Parts {
23512371 part .Accept (formatter )
23522372 }
2353- node .CloseHeredocTkn = formatter .newToken (token .T_START_HEREDOC , []byte ("EOT" ))
2373+ node .CloseHeredocTkn = formatter .newToken (token .T_END_HEREDOC , []byte (label ))
2374+ formatter .heredocClosed = true
2375+ }
2376+
2377+ // heredocLabel returns the label an opening <<<LABEL, <<<"LABEL" or
2378+ // <<<'LABEL' token names, and whether it opens a nowdoc: EOT without one.
2379+ func heredocLabel (open * token.Token ) (string , bool ) {
2380+ if open == nil {
2381+ return "EOT" , false
2382+ }
2383+ value := bytes .TrimLeft (bytes .TrimPrefix (open .Value , []byte ("<<<" )), " \t " )
2384+ value = bytes .TrimRight (value , "\r \n " )
2385+ nowdoc := bytes .HasPrefix (value , []byte ("'" ))
2386+ value = bytes .Trim (value , "'\" " )
2387+ if len (value ) == 0 {
2388+ return "EOT" , false
2389+ }
2390+ return string (value ), nowdoc
2391+ }
2392+
2393+ // freeLabel returns label, or one made from it, that starts no line of the
2394+ // heredoc's body as a whole word.
2395+ func freeLabel (label string , parts []ast.Vertex ) string {
2396+ var body []byte
2397+ for _ , part := range parts {
2398+ if text , ok := part .(* ast.ScalarEncapsedStringPart ); ok {
2399+ body = append (body , text .Value ... )
2400+ }
2401+ }
2402+ candidate := label
2403+ for i := 1 ; endsHeredoc (body , candidate ); i ++ {
2404+ candidate = label + strconv .Itoa (i )
2405+ }
2406+ return candidate
2407+ }
2408+
2409+ // endsHeredoc reports whether a line of body would end a heredoc labelled
2410+ // label: it starts, after indentation, with the label as a whole word.
2411+ func endsHeredoc (body []byte , label string ) bool {
2412+ for line := range bytes .SplitSeq (body , []byte ("\n " )) {
2413+ line = bytes .TrimLeft (line , " \t " )
2414+ rest , ok := bytes .CutPrefix (line , []byte (label ))
2415+ if ! ok {
2416+ continue
2417+ }
2418+ if len (rest ) == 0 {
2419+ return true
2420+ }
2421+ next := rest [0 ]
2422+ if next != '_' && (next < '0' || next > '9' ) && (next < 'a' || next > 'z' ) && (next < 'A' || next > 'Z' ) && next < 0x80 {
2423+ return true
2424+ }
2425+ }
2426+ return false
23542427}
23552428
23562429func (formatter * formatter ) ScalarLnumber (node * ast.ScalarLnumber ) {
0 commit comments