You've already forked MyDSL
TokenFactory和Tokenizer基础
This commit is contained in:
32
src/Token/Factory/DefaultFactory.php
Normal file
32
src/Token/Factory/DefaultFactory.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/**
|
||||
* @filename DefaultFactory.php
|
||||
* @author Jerry Yan <792602257@qq.com>
|
||||
* @date 2020/12/17 14:48
|
||||
*/
|
||||
|
||||
|
||||
namespace JerryYan\DSL\Token\Factory;
|
||||
|
||||
|
||||
use JerryYan\DSL\Token\Token;
|
||||
use JerryYan\DSL\Token\TokenAnd;
|
||||
use JerryYan\DSL\Token\TokenOr;
|
||||
use JerryYan\DSL\Token\TokenVar;
|
||||
|
||||
class DefaultFactory extends FactoryInterface
|
||||
{
|
||||
protected $tokenMap = [
|
||||
Token::AND => TokenAnd::class,
|
||||
Token::OR => TokenOr::class,
|
||||
Token::VAR => TokenVar::class,
|
||||
];
|
||||
|
||||
protected $tokenNameMap = [
|
||||
"和" => Token::AND,
|
||||
"或者" => Token::OR,
|
||||
"或" => Token::OR,
|
||||
];
|
||||
|
||||
protected $undefinedTokenClass = TokenVar::class;
|
||||
}
|
38
src/Token/Factory/FactoryInterface.php
Normal file
38
src/Token/Factory/FactoryInterface.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
/**
|
||||
* @filename FactoryInterface.php
|
||||
* @author Jerry Yan <792602257@qq.com>
|
||||
* @date 2021/1/22 13:42
|
||||
*/
|
||||
|
||||
|
||||
namespace JerryYan\DSL\Token\Factory;
|
||||
|
||||
|
||||
|
||||
use JerryYan\DSL\Token\TokenInterface;
|
||||
use JerryYan\DSL\Token\TokenUndefined;
|
||||
|
||||
abstract class FactoryInterface
|
||||
{
|
||||
/** @var array<string, \JerryYan\DSL\Token\TokenInterface> Token类型及映射类 */
|
||||
protected $tokenMap = [];
|
||||
/** @var array<string, string> Token别名映射 */
|
||||
protected $tokenNameMap = [];
|
||||
/** @var \JerryYan\DSL\Token\TokenInterface 默认Token类 */
|
||||
protected $undefinedTokenClass = TokenUndefined::class;
|
||||
|
||||
public function getTokenByName(string $name): TokenInterface
|
||||
{
|
||||
$originalName = $name;
|
||||
if (isset($this->tokenNameMap[$name])) {
|
||||
$name = $this->tokenNameMap[$name];
|
||||
}
|
||||
if (!isset($this->tokenMap[$name])) {
|
||||
return new $this->undefinedTokenClass($originalName);
|
||||
} else {
|
||||
return new $this->tokenMap[$name]($originalName);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @filename TokenFactory.php
|
||||
* @author Jerry Yan <792602257@qq.com>
|
||||
* @date 2020/12/17 14:48
|
||||
*/
|
||||
|
||||
|
||||
namespace JerryYan\DSL\Token;
|
||||
|
||||
|
||||
class TokenFactory
|
||||
{
|
||||
/** @var array<string, string> Token类型及映射类 */
|
||||
private $tokenMap = [
|
||||
Token::AND => TokenAnd::class,
|
||||
Token::OR => TokenOr::class,
|
||||
Token::VAR => TokenVar::class,
|
||||
];
|
||||
|
||||
protected $tokenNameMap = [
|
||||
|
||||
];
|
||||
|
||||
public function getTokenByName(string $name): TokenInterface
|
||||
{
|
||||
$originalName = $name;
|
||||
if (isset($this->tokenNameMap[$name])) {
|
||||
$name = $this->tokenNameMap[$name];
|
||||
}
|
||||
if (!isset($this->tokenMap[$name])) {
|
||||
return new TokenUndefined($originalName);
|
||||
} else {
|
||||
return new $this->tokenMap[$name]($originalName);
|
||||
}
|
||||
}
|
||||
}
|
@ -40,12 +40,12 @@ abstract class TokenInterface
|
||||
*/
|
||||
public function getFirstToken(): ?TokenInterface
|
||||
{
|
||||
if ($this->hasPrevToken()) return $this->prevToken->getFirstToken();
|
||||
if ($this->hasPrevToken()) return $this->getPrevToken()->getFirstToken();
|
||||
else return $this;
|
||||
}
|
||||
public function getLastToken(): ?TokenInterface
|
||||
{
|
||||
if ($this->hasNextToken()) return $this->nextToken->getLastToken();
|
||||
if ($this->hasNextToken()) return $this->getNextToken()->getLastToken();
|
||||
else return $this;
|
||||
}
|
||||
public function hasPrevToken(): bool
|
||||
@ -62,4 +62,22 @@ abstract class TokenInterface
|
||||
public function getNextToken(): ?TokenInterface{
|
||||
return $this->nextToken;
|
||||
}
|
||||
public function getPrevTokenLength(): int
|
||||
{
|
||||
if ($this->hasPrevToken()) return $this->getPrevToken()->getPrevTokenLength() + 1;
|
||||
else return 0;
|
||||
}
|
||||
public function getNextTokenLength(): int
|
||||
{
|
||||
if ($this->hasNextToken()) return $this->getNextToken()->getNextTokenLength() + 1;
|
||||
else return 0;
|
||||
}
|
||||
public function getLength(): int
|
||||
{
|
||||
return $this->getPrevTokenLength() + $this->getNextTokenLength() + 1;
|
||||
}
|
||||
public function count(): int
|
||||
{
|
||||
return $this->getLength();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user