159 Commits

Author SHA1 Message Date
Nikita Popov
d43edfbb31 Support CRLF newlines in pretty printer
Can be enabled using the "newlines" option.
2023-05-21 20:56:56 +02:00
Nikita Popov
5c267f55c9 Add support for typed constants
RFC: https://wiki.php.net/rfc/typed_class_constants
2023-05-20 21:02:03 +02:00
Nikita Popov
779b6950c3 Fix coding style 2023-05-20 18:55:12 +02:00
Nikita Popov
aa721520f9 Don't generate braces for "else if"
Mentioned in #915.
2023-03-05 11:30:39 +01:00
Nikita Popov
a0ed229b31 Revert "Rename PrettyPrinterAbstract to PrettyPrinter"
This reverts commit 2217f14d6e039f1c0572329e6fcc99f6c17178a3.
2023-03-04 21:54:56 +01:00
Nikita Popov
698ff1ca46 Don't always omit parentheses for argument-less yield
We may need parentheses around an argument-less yield to distinguish
(yield) - $a from yield -$a and similar.

Treat argument-less yield like a prefix operator. This will print
parentheses a bit more often than is really required, because the
arity ambiguity only exists for certain operators. This seems like
a reasonably good approximation through, as it only affects
argument-less yield on the LHS of infix operators.
2023-03-04 12:06:35 +01:00
Nikita Popov
b3158e0b53 Respect namespace style for enum scalar type printing
Doesn't matter in any practical sense because such types are always
invalid, but makes sure pretty printing round-trips.
2023-03-04 11:56:59 +01:00
Nikita Popov
2217f14d6e Rename PrettyPrinterAbstract to PrettyPrinter 2023-03-01 21:25:02 +01:00
Nikita Popov
57d4a02659 Handle isolated \r in doc string
Doc strings have a trailing \n and these will get interpreted as
\r\n and removed from the string contents.

For nowdoc, fall back to single quote if there's a trailing \r.
For heredoc, escape all isolated \r -- unlike \n and \r\n this is
really a special character, because this is no longer relevant as
an actual newline character.
2023-02-27 22:00:49 +01:00
Nikita Popov
8e100f1e69 Handle another yield edge case
match also uses =>, so we need to make sure that a trailing yield
is wrapped in parentheses.
2023-02-27 21:36:36 +01:00
Nikita Popov
fcd5934dae Prevent merging of consecutive -/+ more thoroughly
The unary -/+ or --/++ might not be the direct child. Instead
determine this by actually printing the operand and checking
whether it starts with -/+.
2023-02-27 19:07:44 +01:00
Nikita Popov
cb60eda774 Support yield precedence
Since PHP 7.0 yield is a proper expression, so print it with
proper precedence. If the pretty printer is configured for
older version (non-default), then always print parentheses.

There is an interesting interaction here, in that => is resolved
in favor of yield if the yield occurs as part of an array (or
outer yield). This kind of bypasses the entire precedence hierarchy
and *only* affects yield expressions. For the sake of simplicity
this is modeled via normal LHS precedence, because this will only
add unnecessary parentheses to a handful of low precedence unary
operators. If desired, a special marker for this purpose could be
added though.
2023-02-26 22:58:53 +01:00
Nikita Popov
1cb460ae38 Handle throw precedence
Now that this is an expression, we also need to handle precedence.
2023-02-26 18:33:24 +01:00
Nikita Popov
3bd38c5b2c Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.

For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.

To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.

This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-26 18:26:20 +01:00
Nikita Popov
7b4a8c1ebd Properly handle static deref LHS
The rules for static and array/object deref are slightly different:
The former does not allow constants.
2023-02-26 15:57:37 +01:00
Nikita Popov
9476cff37d Doc string end label may have leading whitespace
When detecting whether the string contains the end label, allow
leading whitespace in front of it. This is legal since the
introduction of flexible doc strings.
2023-02-26 15:21:33 +01:00
Nikita Popov
1eb6b5653e Properly handle new/instanceof operand restrictions
Fixes #912.
2023-02-26 15:11:36 +01:00
Nikita Popov
d24745ddbc Respect precedence during clone pretty printing
Clone is the prefix operator with the highest operator precedence,
but was not treated as an operator at all.
2023-02-26 14:58:17 +01:00
Nikita Popov
f6ddde6428 Perform end label check on escaped string
Escaping might convert a label character into an escape sequence
if it is not valid UTF-8.
2023-02-26 12:27:05 +01:00
Nikita Popov
47626c74ec Handle interpolated variable after end label
Interpolated variables start with non-label characters, and as
such also count as end label terminators since PHP 7.3.
2023-02-26 12:20:32 +01:00
Nikita Popov
ce3337b0c2 Update allowed characters after doc string label
With the introduction of flexible doc strings, the ending label
is no longer required to be followed by a semicolon or newline.
We need to prevent doc string printing if the label is followed
by any non-label character.
2023-02-26 12:14:04 +01:00
Nikita Popov
d83562e6fe Print INF as 1.0E+1000
This makes pretty printing round trip to another Float literal,
rather than a constant lookup. The 1e1000 form in particular is
chosen because that seems to be the typical form used in various
tests.
2023-02-26 09:47:19 +01:00
Nikita Popov
2df8878f5d [PHP 8.3] Support dynamic class const fetch
RFC: https://wiki.php.net/rfc/dynamic_class_constant_fetch
2023-01-29 20:49:00 +01:00
Nikita Popov
4bcdf74b8b Add support for perserving formatting for static modifier change
Closes GH-891.
2022-09-21 18:56:58 +02:00
Nikita Popov
f98341f688 Specify more types 2022-09-17 18:48:56 +02:00
Nikita Popov
43d6332dce Add return types to PrettyPrinter\Standard 2022-09-11 17:49:41 +02:00
Nikita Popov
b9fe3449e8 Add missing parameter types 2022-09-11 15:22:23 +02:00
Nikita Popov
f59f226f65 Fix some phpstan warnings 2022-09-11 12:16:12 +02:00
Nikita Popov
4917c71a91 Rename Stmt\UseUse to UseItem 2022-09-03 18:59:48 +02:00
Nikita Popov
e1345f0c09 Rename Stmt\PropertyProperty to PropertyItem 2022-09-03 18:55:22 +02:00
Nikita Popov
03ccfa3dd4 Rename Stmt\DeclareDeclare to DeclareItem 2022-09-03 18:45:28 +02:00
Nikita Popov
a44faa6328 Rename Scalar\Encapsed to Scalar\InterpolatedString 2022-09-03 15:14:04 +02:00
Nikita Popov
f4ec6a1e53 Rename Scalar\EncapsedStringPart to InterpolatedStringPart
It is no longer an expression node, which unfortunately does
require a more awkward type for the Encaps node.
2022-09-03 13:25:23 +02:00
Nikita Popov
23835d20ef Rename Scalar\LNumber to Scalar\Int_ 2022-09-03 12:07:38 +02:00
Nikita Popov
66b20bd6bc Rename Scalar\DNumber to Scalar\Float_ 2022-09-03 11:56:06 +02:00
Nikita Popov
035c1c7cd2 Rename Stmt\StaticVar to StaticVar
This is part of a statement, not a statement by itself.
2022-09-02 22:51:13 +02:00
Nikita Popov
8be56afd2d Rename Expr\ArrayItem to ArrayItem
Array items are not expressions by themselves.
2022-09-02 22:41:10 +02:00
Nikita Popov
dd63ddbc24 Add php-cs-fixer config and reformat
The formatting in this project has become something of a mess,
because it changed over time. Add a CS fixer config and reformat
to the desired style, which is PSR-12, but with sane brace placement.
2022-08-28 22:57:06 +02:00
Nikita Popov
68fc1ba4cb Always use List_ node for array destructuring
Fixes #471.
2022-08-28 18:48:26 +02:00
George Peter Banyard
9b2a01aa0c
Add support for DNF types (#862) 2022-08-07 17:10:11 +02:00
Nikita Popov
652fb0c6c1 Print trailing comma in param list if supported 2022-08-07 16:43:53 +02:00
Anton
ea9d6b2238 Always use pMaybeMultiline() for function parameters
Closes GH-861.
2022-08-07 16:34:26 +02:00
Nikita Popov
cf0cd6003e Improve heuristic for escaping in single quoted strings
It is idiomatic to not escape backslashes if they are followed by
a non-special character.
2022-07-24 22:56:44 +02:00
Nikita Popov
59145a4443 Treat assignments as unary operators
Assignment expressions can be viewed as unary operators where
the whole "$x =" part is the operator.
2022-07-23 21:53:48 +02:00
Nikita Popov
7bf6348240 Remove inc/dec from precedence map
Inc/dec are primitive expressions that only accept a variable
operand, rather than a general expression operand.
2022-07-23 21:48:10 +02:00
Nikita Popov
a73c8ee03b Add a phpVersion option to the pretty printer
This is currently just used to initialize the default for short
array syntax.

The default target version in 7.0, which also means that the
default for short arrays is flipped to on.
2022-07-23 18:23:39 +02:00
MathiasReker
4021a63cef No superfluous elseif
Replaces superfluous elseif with if.
2022-07-04 20:36:22 +02:00
MathiasReker
0086a261d0 Short scalar cast
Cast (boolean) and (integer) should be written as (bool) and (int), (double) and (real) as (float), (binary) as (string).
2022-07-04 17:50:40 +02:00
Nikita Popov
b0469d127e Rename Expr\ClosureUse -> ClosureUse
This is not a real expression, treat it similarly to Node\Arg
or Node\Param.

The old name is retained as an alias for compatibility.
2022-06-12 21:55:56 +02:00
Nikita Popov
27fe7a68c0 Include space after closure use 2022-06-04 13:22:58 +02:00