1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-27 12:24:39 +01:00
Commit Graph

51 Commits

Author SHA1 Message Date
Nikita Popov
8f623fb241 Use closures instead of methods for semantic actions
The dispatch using $this->{'reduceRule' . $rule}() is very expensive
because it involves
 * One allocation when converting $rule to a string
 * Another allocation when concatenating the two strings
 * Lowercasing during the method call
 * Various uncached method checks, e.g. visibility.

Using an array of closures for semantic action dispatch is
significantly more efficient.
2017-02-05 11:58:45 +01:00
Nikita Popov
3bbf8d8f7a Use local var for $stackPos
We're accessing $this->stackPos a lot, and property accesses are
much more expensive than local variable acesses. Its cheaper to
use a local var and pass it as an argument to semantic actions.
2017-02-05 11:57:21 +01:00
Nikita Popov
bfea338d36 Update doc comments after previous comment
Make some of the type annotations more accurate, and complete the
generated doc-comments to be complete (with description and
parameter annotations.)
2017-01-26 00:16:54 +01:00
Matthew Brown
e3b87f40aa Add non-void return types 2017-01-25 23:32:50 +01:00
Nikita Popov
13304d5991 Drop attribute poisoning in parser 2017-01-23 01:13:34 +01:00
Matthew Brown
7b441d2142 Fix issues reported by psalm 2017-01-23 01:06:46 +01:00
Nikita Popov
414adfb146 Drop useNopStatements option 2017-01-20 22:29:41 +01:00
Nikita Popov
48d3243abe Drop last vestiges of consistent var mode flag 2017-01-19 23:46:25 +01:00
Nikita Popov
67274b9594 Enforce useConsistentVariableNames 2017-01-19 23:24:43 +01:00
Nikita Popov
ced914a3b7 Update doc comments to be more specific
Now $stmts arrays really only contains Stmt nodes.
2017-01-19 22:49:18 +01:00
Nikita Popov
1bfbd7bcc8 Make useExpressionStatement only supported mode 2017-01-19 21:15:26 +01:00
Nikita Popov
4d2a4d02b0 Add first shot at format preserving pretty printer 2016-12-26 18:28:49 +01:00
Nikita Popov
9b2d35d1ac Add expression statement mode 2016-12-24 23:54:24 +01:00
Nikita Popov
a947e731c3 Add useConsistentVariableNodes mode
The parameter case is a bit weird, because the subnode is called
"name" here, rather than "var". Nothing we can do about that in
this version though.

The two parser options might be merged. I've kept it separate,
because I think this variable representation should become the
default (or even only representation) in the future, while I'm
less sure about the Identifier thing.
2016-12-23 00:10:59 +01:00
Nikita Popov
122f449960 Represent builtin types using Identifier as well 2016-12-22 22:23:30 +01:00
Nikita Popov
6bcc6c31dd Add useIdentifierNodes mode to parser
In this mode non-namespaced names that are currently represented
using strings will be represented using Identifier nodes instead.
Identifier nodes have a string $name subnode and coerce to string.

This allows preserving attributes and in particular location
information on identifiers.
2016-12-22 21:15:44 +01:00
Nikita Popov
c1e0bab4f8 Add support for negative interpolated offsets
A late addition to PHP 7.1 which I missed.
2016-12-11 13:44:17 +01:00
Nikita Popov
70319e27ee Parse 0 in "$a[0]" as LNumber (#325) 2016-12-07 20:24:00 +01:00
Nikita Popov
27281e9130 Fix attribute assignment for Error nodes 2016-11-23 22:51:32 +01:00
Nikita Popov
c5cdd5ad73 Support recovery from free-standing $ 2016-11-21 17:01:39 +01:00
Nikita Popov
f99a96e0a2 Introduce ErrorHandler
Add ErrorHandler interface, as well as ErrorHandler\Throwing
and ErrorHandler\Collecting. The error handler is passed to
Parser::parse(). This supersedes the throwOnError option.

NameResolver now accepts an ErrorHandler in the ctor.
2016-10-16 22:12:46 +02:00
Nikita Popov
caa5c0cc76 Graceful handling for "special" errors
Nearly all special errors are now handled gracefully, i.e. the
parser will be able to continue after encountering them. In some
cases the associated error range has been improved using the new
end attribute stack.

To achieve this the error handling code has been moved out of the
node constructors and into special methods in the parser.
2016-10-09 12:38:18 +02:00
Nikita Popov
f6eb341b15 Fix GroupUse prefix attribute assignment 2016-10-08 23:55:46 +02:00
Nikita Popov
c79ea6d1d3 Support recovery from lexer errors
Lexer::startLexing() no longer throws, instead errors can be fetched
using Lexer::getErrors().

Lexer errors now also contain full line and position information.
2016-09-30 20:23:36 +02:00
Nikita Popov
c5e0c3d7e2 Catch lexer errors in throwOnError=0 mode 2016-09-30 13:49:34 +02:00
Nikita Popov
977cbab8e7 Decrement errorState when recovering from -> error
It's likely that an error after -> will trigger another one due to
missing semicolon without shifting a single token. We prevent an
immediate failure in this case by manually setting errorState to 2,
which will suppress the duplicate error message, but allow error
recovery.
2016-07-25 17:37:54 +02:00
Nikita Popov
7a54aca468 Merge branch '2.x' 2016-07-06 02:21:57 +02:00
Nikita Popov
81f7da3b23 Fix computation of expected tokens in parse errors 2016-07-06 02:21:48 +02:00
Nikita Popov
5044fce1ff PHP 7.1: Add void+iterable support
In PHP 7 mode, these will now be represented as strings 'void'
and 'iterable'.
2016-07-05 22:35:27 +02:00
Nikita Popov
4c7ad7e194 Allow nop statements before namespace declaration 2016-04-16 21:39:49 +02:00
Nikita Popov
14de71898e Fix nop statement comment assignment
Keep around the start attributes on the lookahead token around in
a separate parser property.
2016-04-05 19:19:15 +09:00
Nikita Popov
6dffb72ce0 Merge branch '1.x' 2016-01-15 22:02:35 +01:00
Nikita Popov
eb73441032 Support hashbang before namespace declaration
Fixes issue #243.
2016-01-15 22:01:51 +01:00
Nikita Popov
f2b7a31509 Rename ParserInterface to Parser
And drop the alias of Parser to Parser\Php5.
2015-06-20 11:47:25 +02:00
Nikita Popov
0da72fad00 Support scalar type declarations 2015-06-13 20:16:09 +02:00
Nikita Popov
ca3b44bf60 Fork separate PHP 7 parser
Also add ParserInterface
2015-06-13 13:09:34 +02:00
Nikita Popov
9caa51b3a5 Add some more tests 2015-05-01 20:18:04 +02:00
Nikita Popov
3b7d8e8b5d Enable basic error recovery
Adding only a single recovery rule for now.

The API is now:
 * throwOnError parser option must be disabled.
 * List of Errors is available through $parser->getErrors(). This
   method is available either way.
 * If no recovery is possible $parser->parse() will return null.
   (Obviously only if throwOnError is disabled).
2015-04-30 17:41:57 +02:00
Nikita Popov
66896dbde6 Poison attribute initializations in parser
Those should error if they end up being used.
2015-04-26 22:32:18 +02:00
Nikita Popov
a6d2cd69f8 Clarify attribute assignment code
* Don't assign to attribute stack on reduce - why was that there
in the first place?
* Assign attributes to the position in the stack where the first
token of the production is, instead of one position earlier.
* Add a comment to clarify why we also assign attributes on read,
instead of just on shift.
2015-04-26 11:56:01 +02:00
Nikita Popov
6996db1e3a Build node attributes inside semantic action methods
Minor performance improvement for parsing, also allows to access
attributes with higher granulity in the parser, though this is not
currently done.

* #n can now be used to access the stack position of a token. $n
  is the same as $this->semStack[#n]. (Post-translate $n will
  actually be the stack position.)
* $attributeStack is now $this->startAttributeStack and
  $endAttributes is now $this->endAttributes.
* Attributes for a node are now computed inside the individual
  reduction methods, instead of being passed as a parameter.
  Accessible through the attributes() macro.
2015-04-26 11:55:59 +02:00
Nikita Popov
65f1adbe65 Merge primary error recovery code
The grammar does not yet actually implement error recovery.
2015-04-19 18:11:05 +02:00
Nikita Popov
611fa5c7f1 Test parser position information (syntax errors) 2015-04-18 13:27:57 +02:00
Nikita Popov
4defbc2174 Make Error column info attribute based 2015-04-18 12:51:26 +02:00
Gerrit Addiks
33a39fae06 added column-numbers to syntax errors 2015-04-18 10:14:49 +02:00
Máximo Cuadros Ortiz
d774dbc1b7 Add optional startFilePos and endFilePos attributes
The lexer can now optionally add startFilePos and endFilePos
attributes, which are offsets in to the lexed code string.

The end offset currently points one past the last character of
the token - this is pending further discussion.

The attributes are not added by default and have to be enabled
using the new 'usedAttributes' lexer option:

	$lexer = new Lexer([
		'usedAttributes' => [
			'comments', 'startLine', 'endLine',
			'startFilePos', 'endFilePos'
		]
	]);
2014-11-27 20:38:29 +01:00
nikic
1f143393e5 Rewrite namespace handling code
Add a check for disallowed statements between braced namespaces
while at it.
2014-04-21 15:16:00 +02:00
nikic
6d1f77132c Move Stmt\Namespace_::postprocess() to parser 2014-04-21 12:30:55 +02:00
nikic
947a897238 Make names in the parser more descriptive
And improve the code a tad bit in general.

I left YY2TBLSTATES and YYNLSTATES around, because I don't fully
understand their role in the action double indexing.
2014-04-20 23:05:51 +02:00
nikic
1edbc89749 Use normal properties instead of static ones 2014-04-20 00:34:31 +02:00