Skip to content
This repository was archived by the owner on Sep 9, 2019. It is now read-only.

Commit 34994ac

Browse files
committed
Merge pull request #37 from xp-lang/issue-36
Support "new T()", "T::const" and "T::$static" inside annotations
2 parents f4a421c + ff8c005 commit 34994ac

File tree

9 files changed

+3026
-2733
lines changed

9 files changed

+3026
-2733
lines changed

src/main/jay/grammars/php.jay

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ annotation:
339339
$$= $yyLex->create(new AnnotationNode());
340340
$$->type= $2;
341341
}
342-
| '@' qualifiedname '(' literal ')' {
342+
| '@' qualifiedname '(' annotationvalue ')' {
343343
$$= $yyLex->create(new AnnotationNode());
344344
$$->type= $2;
345345
$4 && $$->parameters= array('default' => $4);
@@ -357,10 +357,27 @@ annotationmembers:
357357
;
358358

359359
annotationmember:
360-
T_WORD '=' literal { $$= array($1 => $3); }
361-
| T_CLASS '=' literal { $$= array($1 => $3); }
360+
T_WORD '=' annotationvalue { $$= array($1 => $3); }
361+
| T_CLASS '=' annotationvalue { $$= array($1 => $3); }
362362
;
363363

364+
annotationvalue:
365+
literal
366+
| T_NEW typename '(' expressionlist_opt ')' {
367+
$$= $yyLex->create(new InstanceCreationNode());
368+
$$->type= $2;
369+
$$->parameters= $4;
370+
$$->body= NULL;
371+
}
372+
| qualifiedname T_DOUBLE_COLON T_WORD {
373+
$$= $yyLex->create(new ConstantAccessNode(new TypeName($1), $3));
374+
}
375+
| qualifiedname T_DOUBLE_COLON T_VARIABLE {
376+
$$= $yyLex->create(new StaticMemberAccessNode(new TypeName($1), $3));
377+
}
378+
;
379+
380+
364381
methodbody:
365382
';' { $$= NULL; }
366383
| '{' statements_opt '}' { $$= (array)$2; }

src/main/jay/grammars/xp.jay

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ annotation:
442442
'@' annotation_target {
443443
$$= $2;
444444
}
445-
| '@' annotation_target '(' literal ')' {
445+
| '@' annotation_target '(' annotationvalue ')' {
446446
$4 && $2->parameters= array('default' => $4);
447447
$$= $2;
448448
}
@@ -475,8 +475,24 @@ annotationmembers:
475475
;
476476

477477
annotationmember:
478-
T_WORD '=' literal { $$= array($1 => $3); }
479-
| T_CLASS '=' literal { $$= array($1 => $3); }
478+
T_WORD '=' annotationvalue { $$= array($1 => $3); }
479+
| T_CLASS '=' annotationvalue { $$= array($1 => $3); }
480+
;
481+
482+
annotationvalue:
483+
literal
484+
| T_NEW typename '(' expressionlist_opt ')' {
485+
$$= $yyLex->create(new InstanceCreationNode());
486+
$$->type= $2;
487+
$$->parameters= $4;
488+
$$->body= NULL;
489+
}
490+
| qualifiedname T_DOUBLE_COLON T_WORD {
491+
$$= $yyLex->create(new ConstantAccessNode(new TypeName($1), $3));
492+
}
493+
| qualifiedname T_DOUBLE_COLON T_VARIABLE {
494+
$$= $yyLex->create(new StaticMemberAccessNode(new TypeName($1), $3));
495+
}
480496
;
481497

482498
methodbody:

src/main/php/xp/compiler/ast/ConstantAccessNode.class.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,27 @@ public function __construct($type= null, $name= '') {
2121
$this->type= $type;
2222
$this->name= $name;
2323
}
24+
25+
/**
26+
* Returns a hashcode
27+
*
28+
* @return string
29+
*/
30+
public function hashCode() {
31+
return $this->type->compoundName().'::'.$this->name;
32+
}
33+
34+
/**
35+
* Returns whether another object equals this.
36+
*
37+
* @param lang.Generic cmp
38+
* @return bool
39+
*/
40+
public function equals($cmp) {
41+
return
42+
$cmp instanceof self &&
43+
$this->type->equals($cmp->type) &&
44+
$this->name === $cmp->name
45+
;
46+
}
2447
}

0 commit comments

Comments
 (0)