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

Add annotation support. #425

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Parser
const SOURCE_INDEX = -1;
const SOURCE_LINE = -2;
const SOURCE_COLUMN = -3;
const SOURCE_ANNOTATION = -4;

/**
* @var array
Expand All @@ -52,6 +53,7 @@ class Parser
protected static $commentPattern;
protected static $operatorPattern;
protected static $whitePattern;
protected static $annotationPattern;

private $sourceName;
private $sourceIndex;
Expand Down Expand Up @@ -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 = '/@(?<type>[^\W]+)\W*(?<value>.*[^\W])/';
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

}