From 97492e3c864c9246ef9140c0e8fea8bc8138a02c Mon Sep 17 00:00:00 2001 From: Elliot Mitchum Date: Sun, 10 Apr 2016 13:22:55 +1000 Subject: [PATCH 1/3] Add annotation support --- src/Block.php | 5 +++++ src/Parser.php | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/Block.php b/src/Block.php index 6b972a1b..e0fec28d 100644 --- a/src/Block.php +++ b/src/Block.php @@ -57,4 +57,9 @@ class Block * @var array */ public $children; + + /** + * @var array + */ + public $annotations; } diff --git a/src/Parser.php b/src/Parser.php index 3cdd2c7e..3606fa55 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -27,6 +27,7 @@ class Parser const SOURCE_INDEX = -1; const SOURCE_LINE = -2; const SOURCE_COLUMN = -3; + const SOURCE_ANNOTATION = -4; /** * @var array @@ -52,6 +53,7 @@ class Parser protected static $commentPattern; protected static $operatorPattern; protected static $whitePattern; + protected static $annotationPattern; private $sourceName; private $sourceIndex; @@ -94,6 +96,8 @@ public function __construct($sourceName, $sourceIndex = 0, $encoding = 'utf-8') self::$whitePattern = $this->utf8 ? '/' . $commentSingle . '[^\n]*\s*|(' . self::$commentPattern . ')\s*|\s+/AisuS' : '/' . $commentSingle . '[^\n]*\s*|(' . self::$commentPattern . ')\s*|\s+/AisS'; + + self:: $annotationPattern = '/@(?[^\W]+)\W*(?.*[^\W])/'; } } @@ -618,7 +622,7 @@ protected function parseChunk() ) { // check for '!flag' $assignmentFlags = $this->stripAssignmentFlags($value); - $this->append([Type::T_ASSIGN, $name, $value, $assignmentFlags], $s); + $this->append([Type::T_ASSIGN, $name, $value, $assignmentFlags, $annotations], $s); return true; } @@ -721,6 +725,8 @@ protected function pushBlock($selectors, $pos = 0) $this->env->comments = []; } + $b->annotations = $this->parseAnnotation(); + $this->env = $b; return $b; @@ -957,6 +963,7 @@ protected function append($statement, $pos = null) $statement[self::SOURCE_LINE] = $line; $statement[self::SOURCE_COLUMN] = $column; $statement[self::SOURCE_INDEX] = $this->sourceIndex; + $statement[self::SOURCE_ANNOTATION] = $this->parseAnnotation(); } $this->env->children[] = $statement; @@ -2479,4 +2486,34 @@ private function restoreEncoding() mb_internal_encoding($this->encoding); } } + + /** + * Parse current block annotation. + * + * Retrieves the last block from the tree and checks for the comment type. + * Annotations are matched against the comment lines and then returned. + * + * @return array + */ + private function parseAnnotation() { + $annotations = []; + if (isset($this->env->children)) { + $last = $this->last(); + if ($last[0] == Type::T_COMMENT) { + $lines = explode("\n", $last[1]); + foreach($lines as $line) { + preg_match(self::$annotationPattern, $line, $matches); + if (isset($matches['type']) && isset($matches['value'])) { + $annotations[] = array( + 'type' => $matches['type'], + 'value' => $matches['value'] + ); + } + } + } + } + return $annotations; + } + } + From 03da55a22b856757b333dfb3b2b26f5ff2d0b519 Mon Sep 17 00:00:00 2001 From: Elliot Mitchum Date: Sun, 10 Apr 2016 13:25:45 +1000 Subject: [PATCH 2/3] Remove annotation param --- src/Parser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Parser.php b/src/Parser.php index 3606fa55..56c5b883 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -622,7 +622,7 @@ protected function parseChunk() ) { // check for '!flag' $assignmentFlags = $this->stripAssignmentFlags($value); - $this->append([Type::T_ASSIGN, $name, $value, $assignmentFlags, $annotations], $s); + $this->append([Type::T_ASSIGN, $name, $value, $assignmentFlags], $s); return true; } From f73774c591abdbf1a439c68e6273979eebc6bfd9 Mon Sep 17 00:00:00 2001 From: Elliot Mitchum Date: Sun, 10 Apr 2016 13:27:21 +1000 Subject: [PATCH 3/3] No need to add to block --- src/Block.php | 5 ----- src/Parser.php | 2 -- 2 files changed, 7 deletions(-) diff --git a/src/Block.php b/src/Block.php index e0fec28d..6b972a1b 100644 --- a/src/Block.php +++ b/src/Block.php @@ -57,9 +57,4 @@ class Block * @var array */ public $children; - - /** - * @var array - */ - public $annotations; } diff --git a/src/Parser.php b/src/Parser.php index 56c5b883..8a2f7edb 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -725,8 +725,6 @@ protected function pushBlock($selectors, $pos = 0) $this->env->comments = []; } - $b->annotations = $this->parseAnnotation(); - $this->env = $b; return $b;