127 Commits

Author SHA1 Message Date
Nikita Popov
f5adbb5e3f Add rawValue to InterpolatedStringPart and doc strings
For doc strings, the rawValue (on either the String_ or
InterpolatedStringPrts) does not include the leading indentation
(which is available as docIndentation) or the trailing newline on
the last part.
2023-09-24 12:55:38 +02:00
Nikita Popov
ab51e9d35a Remove superfluous phpdoc tags
These just specify a type that is already specified as a real PHP
type.
2023-09-17 18:24:05 +02:00
Nikita Popov
2d3dd4e23e Don't align phpdoc tags
I did this to start with, but then alignment kept being broken
during refactorings, and at some point I switched to not aligning,
and now we have a big mess.

Add a php-cs-fixer rule to consistently not align phpdoc tags.
2023-09-17 16:00:10 +02:00
Nikita Popov
ea77807592 Add more property types
Some of these are not maximally accurate due to lack of union
types.
2023-08-17 21:36:07 +02:00
Nikita Popov
502b090900 Add property types
Types omitted in two places where we violate them currently:
Namespace_::$stmts can be null during parsing, and Enum_::$scalarType
can be a complex type for invalid programs.
2023-08-16 21:37:02 +02:00
Nikita Popov
ba851243f4 Replace startLexing() with tokenize()
For now Lexer::getTokens() still exists, but should probably be
removed.
2023-08-13 16:06:10 +02:00
Nikita Popov
62853b179c Fix PhpStan errors 2023-08-13 12:45:21 +02:00
Nikita Popov
4b497045e0 Move attribute handling into parser
The Lexer now only provides the tokens to the parser, while the
parser is responsible for determining which attributes are placed
on notes. This only needs to be done when the attributes are
actually needed, rather than for all tokens.

This removes the usedAttributes lexer option (and lexer options
entirely). The attributes are now enabled unconditionally. They
have less overhead now, and the need to explicitly enable them for
some use cases (e.g. formatting-preserving printing) doesn't seem
like a good tradeoff anymore.

There are some additional changes to the Lexer interface that
should be done after this, and the docs / upgrading guide haven't
been adjusted yet.
2023-08-13 10:40:21 +02:00
Nikita Popov
b20267c5ad Make use of default actions
For the default action $$ = $1, save the closure invocation.
2023-07-09 21:18:21 +02:00
Nikita Popov
c62dda9507 Strip trailing doc string newline before parsing escape sequences
If the doc string ends on an escaped \r, it should not get eaten
as the "last newline".
2023-03-01 21:05:55 +01:00
Nikita Popov
f98341f688 Specify more types 2022-09-17 18:48:56 +02:00
Nikita Popov
032b102146 Remove deprecated Error constructor
And use this chance to provide accurate position information for
namespace errors.
2022-09-11 22:05:47 +02:00
Nikita Popov
a099803d01 Use array<string, mixed> type for $attributes
Slightly more accurate, and stops PHPStan from complaining about
the missing array type information.
2022-09-11 20:51:31 +02:00
Nikita Popov
48f470eac7 Add missing return types 2022-09-11 18:17:05 +02:00
Nikita Popov
9b46dffb12 Fix formatting preservation for alternative elseif/else syntax
Test taken from PR #797.
2022-09-11 16:58:35 +02:00
Nikita Popov
031c5e6ed0 Move verifyModifier/verifyClassModifier to Modifiers class
Now that the Modifiers are in a separate class, these *internal*
verification methods should also be moved there.
2022-09-11 16:05:21 +02:00
Nikita Popov
b9fe3449e8 Add missing parameter types 2022-09-11 15:22:23 +02:00
Nikita Popov
8dfce13d77 Add phpstan baseline 2022-09-11 12:31:50 +02:00
Nikita Popov
4917c71a91 Rename Stmt\UseUse to UseItem 2022-09-03 18:59:48 +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
8be56afd2d Rename Expr\ArrayItem to ArrayItem
Array items are not expressions by themselves.
2022-09-02 22:41:10 +02:00
Nikita Popov
0933986293 Make sure Array nodes can not contain null
Now that destructuring is always represented using List nodes,
make sure that Array nodes can no longer contain null elements,
so well-typed code doesn't have to deal with them unnecessarily.

If an array does contain empty elements, these are now result in
an error and are represented as a ArrayItem with Error value if
error recovery is used.

The implementation is a bit tricky because at the time the Array
node is created, we cannot tell whether it will be used in a
creation or destructuring context. For this reason the error
reporting is delayed parsing has finished.

Closes #876.
2022-09-01 22:12:58 +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
f62b2bfdec Introduce separate Modifiers class
Use PhpParser\Modifiers::PUBLIC instead of
PhpParser\Node\Stmt\Class_::MODIFIER_PUBLIC, etc. Old constants
of course remain available.

Fixes #476.
2022-08-28 21:20:26 +02:00
Nikita Popov
68fc1ba4cb Always use List_ node for array destructuring
Fixes #471.
2022-08-28 18:48:26 +02:00
Anton
9ef528f3f5 ParserAbstract: remove undefined class in use
Class `PhpParser\Parser\Tokens` not exists in current version
2022-08-07 15:59:06 +02:00
Nikita Popov
1e89658cae Add PhpVersion class 2022-07-23 17:52:59 +02:00
MathiasReker
050342b5df Add visibility modifiers to constants
Closes GH-848.
2022-07-23 16:04:21 +02:00
MathiasReker
4021a63cef No superfluous elseif
Replaces superfluous elseif with if.
2022-07-04 20:36:22 +02:00
MathiasReker
653757bec6 Nullable type declaration for default null value
Adds ? before type declarations for parameters with a default null value
2022-07-04 20:34:31 +02:00
MathiasReker
572af7fff2 No unused imports
Unused use statements must be removed.
2022-07-04 17:53:10 +02:00
Nikita Popov
5aae65e627 Add Parser::getLexer() method
Not sure if this is going to stick, but for now this makes it
easier to obtain the Lexer instance when creating the parser via
ParserFactory.
2022-06-19 21:05:31 +02:00
Nikita Popov
a38a60b7dd Move Tokens::T_* to Php7::T_*
Drop the separate tokens class, move them into the parser.
2022-06-19 18:12:20 +02:00
Nikita Popov
c878d7195d Move token mapping from lexer to parser
This allows a different token mapping per parser.
2022-06-19 18:05:52 +02:00
Nikita Popov
e3ff8cf035 Add support for true type
The null/false types were alread accepted previously, even though
they are only legal as standalone types since PHP 8.2.
2022-06-19 11:06:39 +02:00
Nikita Popov
d3d1297c0d Remove PHP 5 parser 2022-06-12 18:18:50 +02:00
Nikita Popov
9c5eb3ccba Add some missing type annotations 2022-06-04 12:48:12 +02:00
Tomas Votruba
678ccbe072
[PHP 8.2] Add readonly class support (#834)
RFC: https://wiki.php.net/rfc/readonly_classes

PHP implementation: php/php-src#7305
2022-05-15 23:19:31 +02:00
Máté Kocsis
55c4269232
Add support for new PHP 8.1 modifiers (#796)
Implement support for readonly properties (https://wiki.php.net/rfc/readonly_properties_v2) and
final class contstants (https://wiki.php.net/rfc/final_class_const).
2021-07-21 12:43:29 +02:00
Nikita Popov
8eb194ea1f Add never type
This should be recognized as Identifier instead of Name now.
2021-04-25 22:35:28 +02:00
Tomas Votruba
f68e1a43ff
[PHP 8.1] Add support for enums (#758)
RFC: https://wiki.php.net/rfc/enumerations

Co-authored-by: Nikita Popov <nikita.ppv@gmail.com>
2021-04-25 21:11:36 +02:00
Iskander (Alex) Sharipov
f767b9fd9f ParserAbstract: add missing '*' to the phpdoc
Otherwise, it's somewhat incompatible with the phpdoc definition.
2021-04-25 12:38:52 +02:00
Nikita Popov
d3d1ee470a Fix #738 incorrect start line for traits
Empty productions are supposed to be assigned the start attributes
of the lookahead token. Currently, this happens by assigning above
the current stack position when the token it read.

This fails in a situation where we first reduce an empty production
higher up in the stack, and then again reduce an empty production
lower in the stack, without consuming the lookahead token in the
meantime.

Fix this by moving the assignment into the reduction phase. We
also need to do this for error productions, which are effectively
empty.
2020-12-20 10:52:47 +01:00
Nikita Popov
0cee2088ea Remove self/parent/static restriction for namespace names
This no longer applies as of PHP 8.0.
2020-08-09 21:21:22 +02:00
Máté Kocsis
32f89662f3 Add support for the mixed type 2020-05-28 23:39:04 +02:00
Nikita Popov
46cbd9393a Place Nop nodes more precisely
If the Nop is created to collect comments, place it directly after
the comment, instead of at the next non-whitespace character.
2020-02-09 17:10:33 +01:00
Nikita Popov
664c10121e Add support for union types
We definitely need to introduce a general "Type" abstraction in
the next major version.
2019-11-08 14:45:32 +01:00
Nikita Popov
60d025a914 Fix attributes for zero-length nop nodes
Previously zero-length nop nodes used the lookahead start attributes
and current end attributes. This choice ends up being somewhat weird,
because the end attributes will be the at the last non-whitespace,
non-comment token, which might be quite far back. More problematically,
we may not have encountered any non-discarded token if we're at the
start of the file, in which case we will have no end attributes to
assign.

Change things to use a canonical "zero-length" node representation,
where the end position (token & file) will be exactly one before the
start position.

Fixes #589.
2019-05-11 20:01:25 +02:00