Skip to content
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

PHP8 attributes support #7

Merged
merged 20 commits into from
Jul 19, 2020
Merged

PHP8 attributes support #7

merged 20 commits into from
Jul 19, 2020

Conversation

thekid
Copy link
Member

@thekid thekid commented Jul 12, 2020

See https://wiki.php.net/rfc/shorter_attribute_syntax

use unittest\Test;

class DemoTest {

  @@Test
  public function can_create() {
    new Demo();
  }
}
  • Types
  • Properties
  • Methods
  • Parameters
  • Constants
  • Anonymous class instances
  • Functions
  • Closure and lambda expressions

} else {
$value= $arguments;
}
$parse->expecting(')', 'annotations');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be re-considered - and maybe moved to either emitter or to XP Framework...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could change annotation layout from an optional value to a list arguments, giving us:

// No argument / without value
@@Test #[@test]
$arguments= [];

// Single argument / with value
@@Author('Timm') #[@author('Timm')]
$arguments= ['Timm'];

// Single array argument / array value
@@Values(['a', 'b', 'c']) #[@values(['a', 'b', 'c')]
$arguments= [['a', 'b', 'c']];

// Multiple arguments - not supported by XP annotations!
@@Values('a', 'b', 'c')
$arguments= ['a', 'b', 'c']

This would create a BC break for this library

@thekid
Copy link
Member Author

thekid commented Jul 18, 2020

Conflict between PHP attributes, which have 0..n arguments versus XP annotations, which have an optional value.

// No argument / without value
@@Test
#[@test]

// Single argument / with value
@@Author('Timm')
#[@author('Timm')]

// Single array argument / array value
@@Values(['a', 'b', 'c'])
#[@values(['a', 'b', 'c')]

// Multiple arguments - not supported by XP annotations!
@@Values('a', 'b', 'c')
...

Should @@Values('a', 'b', 'c') simply yield the same as @@Values(['a', 'b', 'c']) (thus making the first three forms equivalent) OR should PHP 8 attributes always yield arrays as value (thus requiring quite a bit of change in code processing both annotations and attributes)?

@thekid
Copy link
Member Author

thekid commented Jul 19, 2020

With this patch to the XP compiler, backwards- and forwards compatibility can be ensured:

$ git diff
diff --git a/src/main/php/lang/ast/emit/PHP.class.php b/src/main/php/lang/ast/emit/PHP.class.php
index 980d247..0b19ca8 100755
--- a/src/main/php/lang/ast/emit/PHP.class.php
+++ b/src/main/php/lang/ast/emit/PHP.class.php
@@ -285,13 +285,24 @@ abstract class PHP extends Emitter {
   }

   protected function emitAnnotations($result, $annotations) {
-    foreach ($annotations as $name => $annotation) {
+    foreach ($annotations as $name => $arguments) {
       $result->out->write("'".$name."' => ");
-      if ($annotation) {
-        $this->emitOne($result, $annotation);
+
+      if (empty($arguments)) {
+        $result->out->write('null,');
+      } else if ($arguments instanceof Node) {
+        $this->emitOne($result, $arguments);
+        $result->out->write(',');
+      } else if (1 === sizeof($arguments)) {
+        $this->emitOne($result, $arguments[0]);
         $result->out->write(',');
       } else {
-        $result->out->write('null,');
+        $result->out->write('[');
+        foreach ($arguments as $argument) {
+          $this->emitOne($result, $argument);
+          $result->out->write(',');
+        }
+        $result->out->write('],');
       }
     }
   }

However, in the long run it might be more elegant to always emit DETAIL_ANNOTATIONS as an array and change the XP Framework itself. Hrm...

thekid added 10 commits July 19, 2020 13:24
TODO: Emitting these will currently not emit their annotations as they
have nowhere to go in the XP meta ecosystem. However, for PHP 8.0, we
might emit the actual attributes to make this work
These currently go nowhere as the XP Framework currently handles class
constants as key-value pairs only and does not have access to details
Instead, attach annotations to parsed elements directly
@thekid thekid merged commit 7637f7a into master Jul 19, 2020
@thekid thekid deleted the refactor/php8-annotations branch July 19, 2020 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant