更换成链表结构
This commit is contained in:
parent
1e0a82eade
commit
afaab00f62
@ -19,10 +19,12 @@ unitTest:
|
|||||||
- phpunit-report.xml
|
- phpunit-report.xml
|
||||||
coverage: '/^\s*Lines:\s*\d+.\d+\%/'
|
coverage: '/^\s*Lines:\s*\d+.\d+\%/'
|
||||||
|
|
||||||
composerPublish:
|
#composerPublish:
|
||||||
image: curlimages/curl:latest
|
# image: curlimages/curl:latest
|
||||||
only:
|
# only:
|
||||||
- tags
|
# - tags
|
||||||
stage: deploy
|
# stage: deploy
|
||||||
script:
|
# script:
|
||||||
- curl --data "tag=${CI_COMMIT_TAG}" "http://__token__:${CI_JOB_TOKEN}@gitlab/api/v4/projects/${CI_PROJECT_ID}/packages/composer"
|
# - 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;
|
$this->currentToken = $curToken;
|
||||||
return $curChar !== "";
|
return $curToken !== "";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -19,4 +19,5 @@ final class Token
|
|||||||
{
|
{
|
||||||
const AND = "And";
|
const AND = "And";
|
||||||
const OR = "Or";
|
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 = [
|
private $tokenMap = [
|
||||||
Token::AND => TokenAnd::class,
|
Token::AND => TokenAnd::class,
|
||||||
Token::OR => TokenOr::class,
|
Token::OR => TokenOr::class,
|
||||||
|
Token::VAR => TokenVar::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
protected $tokenNameMap = [
|
protected $tokenNameMap = [
|
||||||
@ -23,13 +24,14 @@ class TokenFactory
|
|||||||
|
|
||||||
public function getTokenByName(string $name): TokenInterface
|
public function getTokenByName(string $name): TokenInterface
|
||||||
{
|
{
|
||||||
if (!isset($this->tokenNameMap[$name])) {
|
$originalName = $name;
|
||||||
return new TokenUndefined();
|
if (isset($this->tokenNameMap[$name])) {
|
||||||
|
$name = $this->tokenNameMap[$name];
|
||||||
}
|
}
|
||||||
$tokenType = $this->tokenNameMap[$name];
|
if (!isset($this->tokenMap[$name])) {
|
||||||
if (!isset($this->tokenMap[$tokenType])) {
|
return new TokenUndefined($originalName);
|
||||||
return new TokenUndefined();
|
} else {
|
||||||
|
return new $this->tokenMap[$name]($originalName);
|
||||||
}
|
}
|
||||||
return new $this->tokenMap[$tokenType];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -11,5 +11,55 @@ namespace JerryYan\DSL\Token;
|
|||||||
|
|
||||||
abstract class TokenInterface
|
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;
|
namespace JerryYan\DSL\Tokenizer;
|
||||||
|
|
||||||
|
|
||||||
use ArrayIterator;
|
|
||||||
use JerryYan\DSL\Reader\ReaderInterface;
|
use JerryYan\DSL\Reader\ReaderInterface;
|
||||||
use JerryYan\DSL\Token\TokenFactory;
|
use JerryYan\DSL\Token\TokenFactory;
|
||||||
|
use JerryYan\DSL\Token\TokenInterface;
|
||||||
|
|
||||||
class Tokenizer extends TokenizerInterface
|
class Tokenizer extends TokenizerInterface
|
||||||
{
|
{
|
||||||
@ -24,16 +24,19 @@ class Tokenizer extends TokenizerInterface
|
|||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
function tokenize(ReaderInterface $reader): ArrayIterator
|
function tokenize(ReaderInterface $reader): ?TokenInterface
|
||||||
{
|
{
|
||||||
$reader->resetCursor();
|
$reader->resetCursor();
|
||||||
$tokens = [];
|
/** @var TokenInterface $lastToken */
|
||||||
while($reader->moveToNextToken())
|
$lastToken = NULL;
|
||||||
|
do
|
||||||
{
|
{
|
||||||
$currentTokenName = $reader->getCurrentToken();
|
$currentTokenName = $reader->getCurrentToken();
|
||||||
$currentToken = $this->tokenFactory->getTokenByName($currentTokenName);
|
$currentToken = $this->tokenFactory->getTokenByName($currentTokenName);
|
||||||
$tokens[] = $currentToken;
|
$currentToken->setPrevToken($lastToken);
|
||||||
}
|
if ($lastToken !== NULL) $lastToken->setNextToken($currentToken);
|
||||||
return new ArrayIterator($tokens);
|
$lastToken = $currentToken;
|
||||||
|
}while($reader->moveToNextToken());
|
||||||
|
return $lastToken->getFirstToken();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -9,7 +9,6 @@
|
|||||||
namespace JerryYan\DSL\Tokenizer;
|
namespace JerryYan\DSL\Tokenizer;
|
||||||
|
|
||||||
|
|
||||||
use ArrayIterator;
|
|
||||||
use JerryYan\DSL\Reader\ReaderInterface;
|
use JerryYan\DSL\Reader\ReaderInterface;
|
||||||
use JerryYan\DSL\Token\TokenInterface;
|
use JerryYan\DSL\Token\TokenInterface;
|
||||||
|
|
||||||
@ -20,9 +19,9 @@ abstract class TokenizerInterface
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param ReaderInterface $reader
|
* @param ReaderInterface $reader
|
||||||
* @return ArrayIterator<TokenInterface>
|
* @return ?TokenInterface
|
||||||
* @author Jerry Yan <792602257@qq.com>
|
* @author Jerry Yan <792602257@qq.com>
|
||||||
* @date 2020/12/18 11:43
|
* @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