1. Go to this page and download the library: Download nette/tokenizer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
[
new Token('say', T_STRING, 0),
new Token(" \n", T_WHITESPACE, 3),
new Token('123', T_DNUMBER, 5),
]
$firstToken = $stream->tokens[0];
$firstToken->value; // say
$firstToken->type; // value of T_STRING
$firstToken->offset; // position in string: 0
$input = '
@author David Grudl
@package Nette
';
use Nette\Tokenizer\Tokenizer;
use Nette\Tokenizer\Stream;
class Parser
{
const T_AT = 1;
const T_WHITESPACE = 2;
const T_STRING = 3;
/** @var Tokenizer */
private $tokenizer;
/** @var Stream */
private $stream;
public function __construct()
{
$this->tokenizer = new Tokenizer([
self::T_AT => '@',
self::T_WHITESPACE => '\s+',
self::T_STRING => '\w+',
]);
}
public function parse(string $input): array
{
$this->stream = $this->tokenizer->tokenize($input);
$result = [];
while ($this->stream->nextToken()) {
if ($this->stream->isCurrent(self::T_AT)) {
$result[] = $this->parseAnnotation();
}
}
return $result;
}
private function parseAnnotation(): array
{
$name = $this->stream->joinUntil(self::T_WHITESPACE);
$this->stream->nextUntil(self::T_STRING);
$content = $this->stream->joinUntil(self::T_AT);
return [$name, trim($content)];
}
}
$parser = new Parser;
$annotations = $parser->parse($input);
// iterate until a string or a whitespace is found, then return the following token
$token = $stream->nextToken(T_STRING, T_WHITESPACE);
// give me next token
$token = $stream->nextToken();
// move the cursor until you find token containing only '@', then stop and return it
$token = $stream->nextToken('@');
// is the current token '@' or type of T_AT?
$stream->isCurrent(T_AT, '@');