更换成链表结构
This commit is contained in:
parent
1e0a82eade
commit
afaab00f62
@ -19,10 +19,12 @@ unitTest:
|
||||
- phpunit-report.xml
|
||||
coverage: '/^\s*Lines:\s*\d+.\d+\%/'
|
||||
|
||||
composerPublish:
|
||||
image: curlimages/curl:latest
|
||||
only:
|
||||
- tags
|
||||
stage: deploy
|
||||
script:
|
||||
- curl --data "tag=${CI_COMMIT_TAG}" "http://__token__:${CI_JOB_TOKEN}@gitlab/api/v4/projects/${CI_PROJECT_ID}/packages/composer"
|
||||
#composerPublish:
|
||||
# image: curlimages/curl:latest
|
||||
# only:
|
||||
# - tags
|
||||
# stage: deploy
|
||||
# script:
|
||||
# - curl --data "tag=${CI_COMMIT_TAG}" "http://__token__:${CI_JOB_TOKEN}@gitlab/api/v4/projects/${CI_PROJECT_ID}/packages/composer"
|
||||
# dependencies:
|
||||
# - unitTest
|
@ -106,7 +106,7 @@ class StringReader extends ReaderInterface
|
||||
}
|
||||
}
|
||||
$this->currentToken = $curToken;
|
||||
return $curChar !== "";
|
||||
return $curToken !== "";
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -19,4 +19,5 @@ final class Token
|
||||
{
|
||||
const AND = "And";
|
||||
const OR = "Or";
|
||||
const VAR = "Var";
|
||||
}
|
15
src/Token/TokenDefine.php
Normal file
15
src/Token/TokenDefine.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
/**
|
||||
* @filename TokenDefine.php
|
||||
* @author Jerry Yan <792602257@qq.com>
|
||||
* @date 2021/1/22 9:41
|
||||
*/
|
||||
|
||||
|
||||
namespace JerryYan\DSL\Token;
|
||||
|
||||
|
||||
class TokenDefine extends TokenInterface
|
||||
{
|
||||
|
||||
}
|
@ -15,6 +15,7 @@ class TokenFactory
|
||||
private $tokenMap = [
|
||||
Token::AND => TokenAnd::class,
|
||||
Token::OR => TokenOr::class,
|
||||
Token::VAR => TokenVar::class,
|
||||
];
|
||||
|
||||
protected $tokenNameMap = [
|
||||
@ -23,13 +24,14 @@ class TokenFactory
|
||||
|
||||
public function getTokenByName(string $name): TokenInterface
|
||||
{
|
||||
if (!isset($this->tokenNameMap[$name])) {
|
||||
return new TokenUndefined();
|
||||
$originalName = $name;
|
||||
if (isset($this->tokenNameMap[$name])) {
|
||||
$name = $this->tokenNameMap[$name];
|
||||
}
|
||||
$tokenType = $this->tokenNameMap[$name];
|
||||
if (!isset($this->tokenMap[$tokenType])) {
|
||||
return new TokenUndefined();
|
||||
if (!isset($this->tokenMap[$name])) {
|
||||
return new TokenUndefined($originalName);
|
||||
} else {
|
||||
return new $this->tokenMap[$name]($originalName);
|
||||
}
|
||||
return new $this->tokenMap[$tokenType];
|
||||
}
|
||||
}
|
@ -11,5 +11,55 @@ namespace JerryYan\DSL\Token;
|
||||
|
||||
abstract class TokenInterface
|
||||
{
|
||||
/** @var ?TokenInterface 上一个Token */
|
||||
protected $prevToken;
|
||||
/** @var ?TokenInterface 下一个Token */
|
||||
protected $nextToken=NULL;
|
||||
/** @var string 原始数据 */
|
||||
protected $_raw = "";
|
||||
|
||||
public function __construct(string $original)
|
||||
{
|
||||
$this->_raw = $original;
|
||||
}
|
||||
|
||||
public function setPrevToken(?TokenInterface $token): void
|
||||
{
|
||||
$this->prevToken = $token;
|
||||
}
|
||||
public function setNextToken(TokenInterface $token): void
|
||||
{
|
||||
$this->nextToken = $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取链表第一个元素
|
||||
* @return ?$this
|
||||
* @author Jerry Yan <792602257@qq.com>
|
||||
* @date 2021/1/22 10:01
|
||||
*/
|
||||
public function getFirstToken(): ?TokenInterface
|
||||
{
|
||||
if ($this->hasPrevToken()) return $this->prevToken->getFirstToken();
|
||||
else return $this;
|
||||
}
|
||||
public function getLastToken(): ?TokenInterface
|
||||
{
|
||||
if ($this->hasNextToken()) return $this->nextToken->getLastToken();
|
||||
else return $this;
|
||||
}
|
||||
public function hasPrevToken(): bool
|
||||
{
|
||||
return $this->prevToken !== NULL;
|
||||
}
|
||||
public function hasNextToken(): bool
|
||||
{
|
||||
return $this->nextToken !== NULL;
|
||||
}
|
||||
public function getPrevToken(): ?TokenInterface{
|
||||
return $this->prevToken;
|
||||
}
|
||||
public function getNextToken(): ?TokenInterface{
|
||||
return $this->nextToken;
|
||||
}
|
||||
}
|
15
src/Token/TokenVar.php
Normal file
15
src/Token/TokenVar.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
/**
|
||||
* @filename TokenVar.php
|
||||
* @author Jerry Yan <792602257@qq.com>
|
||||
* @date 2021/1/22 9:41
|
||||
*/
|
||||
|
||||
|
||||
namespace JerryYan\DSL\Token;
|
||||
|
||||
|
||||
class TokenVar extends TokenInterface
|
||||
{
|
||||
|
||||
}
|
@ -9,9 +9,9 @@
|
||||
namespace JerryYan\DSL\Tokenizer;
|
||||
|
||||
|
||||
use ArrayIterator;
|
||||
use JerryYan\DSL\Reader\ReaderInterface;
|
||||
use JerryYan\DSL\Token\TokenFactory;
|
||||
use JerryYan\DSL\Token\TokenInterface;
|
||||
|
||||
class Tokenizer extends TokenizerInterface
|
||||
{
|
||||
@ -24,16 +24,19 @@ class Tokenizer extends TokenizerInterface
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
function tokenize(ReaderInterface $reader): ArrayIterator
|
||||
function tokenize(ReaderInterface $reader): ?TokenInterface
|
||||
{
|
||||
$reader->resetCursor();
|
||||
$tokens = [];
|
||||
while($reader->moveToNextToken())
|
||||
/** @var TokenInterface $lastToken */
|
||||
$lastToken = NULL;
|
||||
do
|
||||
{
|
||||
$currentTokenName = $reader->getCurrentToken();
|
||||
$currentToken = $this->tokenFactory->getTokenByName($currentTokenName);
|
||||
$tokens[] = $currentToken;
|
||||
}
|
||||
return new ArrayIterator($tokens);
|
||||
$currentToken->setPrevToken($lastToken);
|
||||
if ($lastToken !== NULL) $lastToken->setNextToken($currentToken);
|
||||
$lastToken = $currentToken;
|
||||
}while($reader->moveToNextToken());
|
||||
return $lastToken->getFirstToken();
|
||||
}
|
||||
}
|
@ -9,7 +9,6 @@
|
||||
namespace JerryYan\DSL\Tokenizer;
|
||||
|
||||
|
||||
use ArrayIterator;
|
||||
use JerryYan\DSL\Reader\ReaderInterface;
|
||||
use JerryYan\DSL\Token\TokenInterface;
|
||||
|
||||
@ -20,9 +19,9 @@ abstract class TokenizerInterface
|
||||
/**
|
||||
*
|
||||
* @param ReaderInterface $reader
|
||||
* @return ArrayIterator<TokenInterface>
|
||||
* @return ?TokenInterface
|
||||
* @author Jerry Yan <792602257@qq.com>
|
||||
* @date 2020/12/18 11:43
|
||||
*/
|
||||
abstract function tokenize(ReaderInterface $reader): ArrayIterator;
|
||||
abstract function tokenize(ReaderInterface $reader): ?TokenInterface;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user