diff --git a/src/Parser.php b/src/Parser.php index 3cdd2c7e..8a2f7edb 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])/'; } } @@ -957,6 +961,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 +2484,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; + } + } +