Skip to content

Instantly share code, notes, and snippets.

@thekid
Created August 7, 2023 15:35
Show Gist options
  • Save thekid/61ab01adb3cbdf8de96e22a3e99b6db4 to your computer and use it in GitHub Desktop.
Save thekid/61ab01adb3cbdf8de96e22a3e99b6db4 to your computer and use it in GitHub Desktop.
Generic methods syntax diff
diff --git a/src/main/php/lang/ast/nodes/Signature.class.php b/src/main/php/lang/ast/nodes/Signature.class.php
index 76e3e94..0d946fb 100755
--- a/src/main/php/lang/ast/nodes/Signature.class.php
+++ b/src/main/php/lang/ast/nodes/Signature.class.php
@@ -5,6 +5,7 @@ use lang\ast\Node;
class Signature extends Node {
public $kind= 'signature';
public $parameters, $returns, $byref;
+ public $generics= [];
public function __construct($parameters= [], $returns= null, $byref= false, $line= -1) {
$this->parameters= $parameters;
diff --git a/src/main/php/lang/ast/syntax/PHP.class.php b/src/main/php/lang/ast/syntax/PHP.class.php
index 6a89ad0..7b814bb 100755
--- a/src/main/php/lang/ast/syntax/PHP.class.php
+++ b/src/main/php/lang/ast/syntax/PHP.class.php
@@ -1083,8 +1083,36 @@ class PHP extends Language {
$parse->raise('Cannot redeclare method '.$lookup);
}
+ // Attach generic components to signature
$parse->forward();
+ $components= [];
+ if ('<' === $parse->token->value) {
+ $parse->forward();
+ do {
+
+ // Resolve ambiguity between generic placeholders and nullable
+ if ('?' === $parse->token->value) {
+ $parse->forward();
+ if (',' === $parse->token->value || '>' === $parse->token->value) {
+ $components[]= new IsLiteral('?');
+ } else {
+ $components[]= new IsNullable($this->type($parse, false));
+ }
+ } else {
+ $components[]= $this->type($parse, false);
+ }
+
+ if ('>' === $parse->token->symbol->id) {
+ break;
+ } else if ('>>' === $parse->token->value) {
+ array_unshift($parse->queue, $parse->token= new Token(self::symbol('>')));
+ break;
+ }
+ } while (',' === $parse->token->value && true | $parse->forward());
+ $parse->expecting('>', 'generic components');
+ }
$signature= $this->signature($parse, $byref);
+ $signature->generics= $components;
if ('{' === $parse->token->value) { // Regular body
$parse->forward();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment