更换成链表结构

This commit is contained in:
2021-01-22 10:20:05 +08:00
parent 1e0a82eade
commit afaab00f62
9 changed files with 111 additions and 24 deletions

@ -19,4 +19,5 @@ final class Token
{
const AND = "And";
const OR = "Or";
const VAR = "Var";
}

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

@ -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
{
}