rfc:flexible_heredoc_nowdoc_syntaxes

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
rfc:flexible_heredoc_nowdoc_syntaxes [2017/09/16 16:26] – created tpuntrfc:flexible_heredoc_nowdoc_syntaxes [2017/09/27 10:43] tpunt
Line 39: Line 39:
 // no indentation // no indentation
 echo <<<END echo <<<END
-   +      
-  +     
- c+    c
 END; END;
 /* /*
-   +      
-  +     
- c+    c
 */ */
  
-// 1 space of indentation+// 4 spaces of indentation
 echo <<<END echo <<<END
-   +      
-  +     
- c +    
- END;+    END;
 /* /*
   a   a
  b  b
-c 
-*/ 
- 
-// 2 spaces of indentation 
-echo <<<END 
-   a 
-  b 
- c 
-  END; 
-/* 
- a 
-b 
-c 
-*/ 
- 
-// 3 (or more) spaces of indentation 
-echo <<<END 
-   a 
-  b 
- c 
-   END; 
-/* 
-a 
-b 
 c c
 */ */
 </code> </code>
  
-Tabs are supported as well. If tabs and spaces are intermixed (for whatever reason...), then each space and each tab is considered as 1 indentation. So if the closing marker is indented by 1 tab, and the heredoc/nowdoc body is indented by spaces, then regardless of the closing marked *looking* further indented, only 1 bit of whitespace will still be stripped from each line:+If the closing marker is indented further than any lines of the body, then a ''ParseError'' will be thrown:
 <code php> <code php>
-// 1 tab indentation+
 echo <<<END echo <<<END
-   a 
-  b 
- c 
- END; 
-/* 
   a   a
  b  b
 c c
-*/+ END; 
 + 
 +// Parse error: Invalid body indentation level (expecting an indentation at least 5) in %s on line %d
 </code> </code>
  
-Moral of the story: don't mix tabs and spaces...+Tabs are supported as well, however, tabs and spaces **must not** be intermixed regarding the indentation of the closing marker and the indentation of the body (up to the closing marker). In any of these cases, a ''ParseError'' will be thrown: 
 +<code php> 
 +// different indentation for body (spaces) ending marker (tabs
 +
 + echo <<<END 
 +
 + END; 
 +
 + 
 +// mixing spaces and tabs in body 
 +
 +    echo <<<END 
 +    a 
 +     END; 
 +
 + 
 +// mixing spaces and tabs in ending marker 
 +
 + echo <<<END 
 +
 + END; 
 +
 +</code> 
 + 
 +These whitespace constraints have been included because mixing tabs and spaces for indentation is harmful to legibility.
  
 ==== Closing Marker New Line ==== ==== Closing Marker New Line ====
  
 +Currently, in order to terminate a heredoc or nowdoc, a new line **must** be used after the closing marker (with the option
 Removing the closing marker requirement will change code from: Removing the closing marker requirement will change code from:
 <code php> <code php>
Line 151: Line 149:
 The implementation I am proposing avoids this problem by checking to see if a continuation of the found marker exists, and if so, then if it forms a valid identifier. This means that the terminating marker string will only be considered as such if it is matched exactly as a standalone, valid symbol (that is also found at the start of the line). This enables for the above snippet to now work. The implementation I am proposing avoids this problem by checking to see if a continuation of the found marker exists, and if so, then if it forms a valid identifier. This means that the terminating marker string will only be considered as such if it is matched exactly as a standalone, valid symbol (that is also found at the start of the line). This enables for the above snippet to now work.
  
-Something such as the following will still not work, however:+Examples such as the following will still not work, however:
 <code php> <code php>
 $values = [<<<END $values = [<<<END
Line 160: Line 158:
 /* /*
 Parse error: syntax error, unexpected 'ING' (T_STRING), expecting ']' in %s on line %d Parse error: syntax error, unexpected 'ING' (T_STRING), expecting ']' in %s on line %d
 +*/
 +
 +echo <<<END
 +END{$var}
 +END;
 +/*
 +Parse error: syntax error, unexpected '$var' (T_VARIABLE) in %s on line %d
 */ */
 </code> </code>
  
-(Notice the space after the first ''END''.) There is not a great deal that can be done about this. So the simple rule is: **do not choose a marker that appears in the body of the text** (though it would specifically have to occur at the start of a line in the text to cause problems).+There is not a great deal that can be done about this. So the simple rule is: **do not choose a marker that appears in the body of the text** (though it would specifically have to occur at the start of a line in the text to cause problems).
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 181: Line 186:
 { {
     stringManipulator(<<<END     stringManipulator(<<<END
-       +   
-      +  
-     c+ c
 END END
 ); );
rfc/flexible_heredoc_nowdoc_syntaxes.txt · Last modified: 2018/04/13 19:59 by nikic