-
Notifications
You must be signed in to change notification settings - Fork 0
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
Conversation
} else { | ||
$value= $arguments; | ||
} | ||
$parse->expecting(')', 'annotations'); |
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.
This should be re-considered - and maybe moved to either emitter or to XP Framework...
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.
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
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 |
…butes See discussion regarding this matter: #7 (comment)
See discussion regarding this matter: #7 (comment)
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 |
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
See https://wiki.php.net/rfc/shorter_attribute_syntax