-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor function scopes: Capturing and returning #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -307,12 +307,6 @@ public function __construct() { | |
| return new CloneExpression($arguments, $token->line); | ||
| }); | ||
|
|
||
| $this->prefix('{', 0, function($parse, $token) { | ||
| $statements= $this->statements($parse); | ||
| $parse->expecting('}', 'block'); | ||
| return new Block($statements, $token->line); | ||
| }); | ||
|
|
||
| $this->prefix('[', 0, function($parse, $token) { | ||
| return new ArrayLiteral($this->list($parse, ']', 'array literal'), $token->line); | ||
| }); | ||
|
|
@@ -400,9 +394,8 @@ public function __construct() { | |
|
|
||
| $this->prefix('fn', 0, function($parse, $token) { | ||
| $signature= $this->signature($parse); | ||
| $parse->expecting('=>', 'fn'); | ||
|
|
||
| return new LambdaExpression($signature, $this->expression($parse, 0), false, $token->line); | ||
| $scope= $this->scope($parse, 'fn'); | ||
| return new LambdaExpression($signature, $scope, false, $token->line); | ||
| }); | ||
|
|
||
| $this->prefix('function', 0, function($parse, $token) { | ||
|
|
@@ -435,8 +428,8 @@ public function __construct() { | |
| } else if ('fn' === $parse->token->value) { | ||
| $parse->forward(); | ||
| $signature= $this->signature($parse); | ||
| $parse->expecting('=>', 'fn'); | ||
| return new LambdaExpression($signature, $this->expression($parse, 0), true, $token->line); | ||
| $scope= $this->scope($parse, 'fn'); | ||
| return new LambdaExpression($signature, $scope, true, $token->line); | ||
| } else { | ||
| return new Literal($token->value, $token->line); | ||
| } | ||
|
|
@@ -463,15 +456,14 @@ public function __construct() { | |
| while ('}' !== $parse->token->value) { | ||
| if ('default' === $parse->token->value) { | ||
| $parse->forward(); | ||
| $parse->expecting('=>', 'match'); | ||
| $default= $this->expression($parse, 0); | ||
| $default= $this->scope($parse, 'match'); | ||
| } else { | ||
| $match= []; | ||
| do { | ||
| $match[]= $this->expression($parse, 0); | ||
| } while (',' === $parse->token->value && $parse->forward() | true); | ||
| $parse->expecting('=>', 'match'); | ||
| $cases[]= new MatchCondition($match, $this->expression($parse, 0), $parse->token->line); | ||
|
|
||
| $cases[]= new MatchCondition($match, $this->scope($parse, 'match'), $parse->token->line); | ||
| } | ||
|
|
||
| if (',' === $parse->token->value) { | ||
|
|
@@ -533,6 +525,12 @@ public function __construct() { | |
| return new Literal($token->value, $token->line); | ||
| }); | ||
|
|
||
| // Blocks and standalone semicolons | ||
| $this->stmt('{', function($parse, $token) { | ||
| $statements= $this->statements($parse); | ||
| $parse->expecting('}', 'block'); | ||
| return new Block($statements, $token->line); | ||
| }); | ||
| $this->stmt(';', function($parse, $token) { return null; }); | ||
|
|
||
| // Unexpected standalone symbols, warn but continue | ||
|
|
@@ -886,14 +884,10 @@ public function __construct() { | |
|
|
||
| // Function expression used as statement (e.g. for pure side-effects!) | ||
| if ('(' === $parse->token->value) { | ||
| $parse->queue= [$parse->token]; | ||
| array_unshift($parse->queue, $parse->token); | ||
| $parse->token= new Token($this->symbol('function')); | ||
| $parse->token->line= $token->line; | ||
| $expr= $this->expression($parse, 0); | ||
|
|
||
| $parse->queue= [$parse->token]; | ||
| $parse->token= new Token($this->symbol(';')); | ||
| return $expr; | ||
| return $this->expression($parse, 0); | ||
| } | ||
|
|
||
| if ('&' === $parse->token->value) { | ||
|
|
@@ -907,11 +901,9 @@ public function __construct() { | |
| $parse->forward(); | ||
|
|
||
| $signature= $this->signature($parse, $byref); | ||
| $parse->expecting('{', 'function'); | ||
| $statements= $this->statements($parse); | ||
| $parse->expecting('}', 'function'); | ||
| $scope= $this->scope($parse, 'function'); | ||
|
|
||
| return new FunctionDeclaration($name, $signature, $statements, $token->line); | ||
| return new FunctionDeclaration($name, $signature, $scope, $token->line); | ||
| }); | ||
|
|
||
| $this->stmt('class', function($parse, $token) { | ||
|
|
@@ -1138,23 +1130,28 @@ public function __construct() { | |
| $parse->forward(); | ||
| $signature= $this->signature($parse, $byref); | ||
|
|
||
| if ('{' === $parse->token->value) { // Regular body | ||
| // Cannot use scope() here as we require a semicolon after single-expressions | ||
| if ('{' === $parse->token->value) { | ||
| $parse->forward(); | ||
| $statements= $this->statements($parse); | ||
| $scope= new Block($this->statements($parse), $parse->token->line); | ||
| $parse->expecting('}', 'method declaration'); | ||
| } else if (';' === $parse->token->value) { // Abstract or interface method | ||
| $statements= null; | ||
| } else if ('=>' === $parse->token->value) { | ||
| $parse->forward(); | ||
| $scope= $this->expression($parse, 0); | ||
| $parse->expecting(';', 'method declaration'); | ||
| } else if (';' === $parse->token->value) { | ||
| $scope= null; | ||
| $parse->expecting(';', 'method declaration'); | ||
| } else { | ||
| $parse->expecting('{ or ;', 'method declaration'); | ||
| $parse->expecting('{, => or ;', 'method declaration'); | ||
| return; | ||
| } | ||
|
|
||
| $body[$lookup]= new Method( | ||
| $modifiers, | ||
| $name, | ||
| $signature, | ||
| $statements, | ||
| $scope, | ||
| $meta[DETAIL_ANNOTATIONS] ?? null, | ||
| $comment, | ||
| $line | ||
|
|
@@ -1591,6 +1588,29 @@ private function parameters($parse) { | |
| return $parameters; | ||
| } | ||
|
|
||
| public function scope($parse, $context) { | ||
| if ('=>' === $parse->token->value) { | ||
| $parse->forward(); | ||
|
|
||
| // BC: Support deprecated arrow block syntax | ||
| if ('{' === $parse->token->value) { | ||
| trigger_error('Arrow block syntax `=> { ... }`', E_USER_DEPRECATED); | ||
| goto block; | ||
| } | ||
|
Comment on lines
+1595
to
+1599
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be removed in the next major release |
||
|
|
||
| return $this->expression($parse, 0); | ||
| } else if ('{' === $parse->token->value) { | ||
| block: $line= $parse->token->line; | ||
| $parse->forward(); | ||
| $statements= $this->statements($parse); | ||
| $parse->expecting('}', $context); | ||
| return new Block($statements, $line); | ||
| } else { | ||
| $parse->expecting('=> or { ... }', $context); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| public function body($id, $func) { | ||
| $this->body[$id]= $func->bindTo($this, static::class); | ||
| } | ||
|
|
@@ -1689,11 +1709,8 @@ public function closure($parse, $static) { | |
| $return= null; | ||
| } | ||
|
|
||
| $parse->expecting('{', 'function'); | ||
| $statements= $this->statements($parse); | ||
| $parse->expecting('}', 'function'); | ||
|
|
||
| return new ClosureExpression(new Signature($parameters, $return, false, $line), $use, $statements, $static, $line); | ||
| $scope= $this->scope($parse, 'function'); | ||
| return new ClosureExpression(new Signature($parameters, $return, false, $line), $use, $scope, $static, $line); | ||
| } | ||
|
|
||
| public function block($parse) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this the library is backwards-compatible for all instances using
foreach/emitAll()on functions', closures' and methods' body member.