TokenFactory和Tokenizer基础

This commit is contained in:
2021-01-22 14:21:11 +08:00
parent 10bf8e6481
commit 074f1805eb
7 changed files with 240 additions and 42 deletions

View 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);
}
}
}