diff --git a/src/Token/Factory/DefaultFactory.php b/src/Token/Factory/DefaultFactory.php index d5da7e9..c6fa532 100644 --- a/src/Token/Factory/DefaultFactory.php +++ b/src/Token/Factory/DefaultFactory.php @@ -10,23 +10,21 @@ 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; +use JerryYan\DSL\Token\TokenLogicAnd; +use JerryYan\DSL\Token\TokenDefine; +use JerryYan\DSL\Token\TokenLogicEqual; +use JerryYan\DSL\Token\TokenLogicOr; +use JerryYan\DSL\Token\TokenVariable; class DefaultFactory extends FactoryInterface { protected $tokenMap = [ - Token::AND => TokenAnd::class, - Token::OR => TokenOr::class, - Token::VAR => TokenVar::class, + Token::LOGIC_AND => TokenLogicAnd::class, + Token::LOGIC_OR => TokenLogicOr::class, + Token::LOGIC_EQUAL => TokenLogicEqual::class, + Token::VARIABLE => TokenVariable::class, + Token::DEFINE => TokenDefine::class, ]; - protected $tokenNameMap = [ - "和" => Token::AND, - "或者" => Token::OR, - "或" => Token::OR, - ]; - - protected $undefinedTokenClass = TokenVar::class; + protected $undefinedTokenClass = TokenVariable::class; } \ No newline at end of file diff --git a/src/Token/Factory/FactoryInterface.php b/src/Token/Factory/FactoryInterface.php index 27a575a..7672f52 100644 --- a/src/Token/Factory/FactoryInterface.php +++ b/src/Token/Factory/FactoryInterface.php @@ -17,11 +17,22 @@ abstract class FactoryInterface { /** @var array> Token类型及映射类 */ protected $tokenMap = []; - /** @var array Token别名映射 */ + /** @var array> Token别名映射 */ protected $tokenNameMap = []; /** @var class-string 默认Token类 */ protected $undefinedTokenClass = TokenUndefined::class; + public function __construct() + { + foreach ($this->tokenMap as $key=>$token) { + if (property_exists($token, "alias")){ + foreach ($token::$alias as $name) { + $this->tokenNameMap[$name] = $key; + } + } + } + } + public function getTokenByName(string $name): TokenInterface { $originalName = $name; diff --git a/src/Token/Token.php b/src/Token/Token.php index 8063c9a..42e1504 100644 --- a/src/Token/Token.php +++ b/src/Token/Token.php @@ -17,7 +17,31 @@ namespace JerryYan\DSL\Token; */ final class Token { - const AND = "And"; - const OR = "Or"; - const VAR = "Var"; + // 逻辑相关 + const LOGIC_OR = "LOGIC_OR"; + const LOGIC_AND = "LOGIC_AND"; + const LOGIC_NOT = "LOGIC_NOT"; + const LOGIC_EQUAL = "LOGIC_EQUAL"; + const LOGIC_NOT_EQUAL = "LOGIC_NOT_EQUAL"; + const LOGIC_GREATER = "LOGIC_GREATER"; + const LOGIC_GREATER_EQUAL = "LOGIC_GREATER_EQUAL"; + const LOGIC_LESS = "LOGIC_LESS"; + const LOGIC_LESS_EQUAL = "LOGIC_LESS_EQUAL"; + // 变量相关 + const DEFINE = "DEFINE"; + const VARIABLE = "VARIABLE"; + const VAR_ASSIGN = "VAR_ASSIGN"; + // 运算符相关 + const OP_CONCAT = "Concat"; + const OP_PLUS = "OP_PLUS"; + const OP_PLUS_EQUAL = "OP_PLUS_EQUAL"; + const OP_MINUS = "OP_MINUS"; + const OP_MINUS_EQUAL = "OP_MINUS_EQUAL"; + const OP_MULTIPLY = "OP_MULTIPLY"; + const OP_MULTIPLY_EQUAL = "OP_MULTIPLY_EQUAL"; + // 逻辑运算相关 + const OP_OR = "OP_OR"; + const OP_AND = "OP_AND"; + const OP_XOR = "OP_XOR"; + const OP_NOR = "OP_NOR"; } \ No newline at end of file diff --git a/src/Token/TokenAnd.php b/src/Token/TokenAnd.php deleted file mode 100644 index c592f95..0000000 --- a/src/Token/TokenAnd.php +++ /dev/null @@ -1,15 +0,0 @@ - - * @date 2020/12/17 15:20 - */ - - -namespace JerryYan\DSL\Token; - - -class TokenAnd extends TokenInterface -{ - -} \ No newline at end of file diff --git a/src/Token/TokenInterface.php b/src/Token/TokenInterface.php index 4b1dead..c0e626d 100644 --- a/src/Token/TokenInterface.php +++ b/src/Token/TokenInterface.php @@ -17,6 +17,8 @@ abstract class TokenInterface protected $nextToken=NULL; /** @var string 原始数据 */ protected $_raw = ""; + /** @var array 定义别名 */ + static $alias = []; public function __construct(string $original) { diff --git a/src/Token/TokenLogicAnd.php b/src/Token/TokenLogicAnd.php new file mode 100644 index 0000000..6e1a7a5 --- /dev/null +++ b/src/Token/TokenLogicAnd.php @@ -0,0 +1,17 @@ + + * @date 2020/12/17 15:20 + */ + + +namespace JerryYan\DSL\Token; + + +class TokenLogicAnd extends TokenInterface +{ + public static $alias = [ + '和', '且' + ]; +} \ No newline at end of file diff --git a/src/Token/TokenLogicEqual.php b/src/Token/TokenLogicEqual.php new file mode 100644 index 0000000..eb47bf3 --- /dev/null +++ b/src/Token/TokenLogicEqual.php @@ -0,0 +1,15 @@ + + * @date 2021/1/22 14:56 + */ + + +namespace JerryYan\DSL\Token; + + +class TokenLogicEqual extends TokenInterface +{ + +} \ No newline at end of file diff --git a/src/Token/TokenLogicOr.php b/src/Token/TokenLogicOr.php new file mode 100644 index 0000000..8d503b9 --- /dev/null +++ b/src/Token/TokenLogicOr.php @@ -0,0 +1,17 @@ + + * @date 2020/12/17 15:20 + */ + + +namespace JerryYan\DSL\Token; + + +class TokenLogicOr extends TokenInterface +{ + public static $alias = [ + '或者' + ]; +} \ No newline at end of file diff --git a/src/Token/TokenOr.php b/src/Token/TokenOr.php deleted file mode 100644 index 16d1516..0000000 --- a/src/Token/TokenOr.php +++ /dev/null @@ -1,15 +0,0 @@ - - * @date 2020/12/17 15:20 - */ - - -namespace JerryYan\DSL\Token; - - -class TokenOr extends TokenInterface -{ - -} \ No newline at end of file diff --git a/src/Token/TokenVar.php b/src/Token/TokenVariable.php similarity index 60% rename from src/Token/TokenVar.php rename to src/Token/TokenVariable.php index 84dd6bb..1c236ef 100644 --- a/src/Token/TokenVar.php +++ b/src/Token/TokenVariable.php @@ -1,6 +1,6 @@ * @date 2021/1/22 9:41 */ @@ -9,7 +9,7 @@ namespace JerryYan\DSL\Token; -class TokenVar extends TokenInterface +class TokenVariable extends TokenInterface { } \ No newline at end of file diff --git a/tests/Token/TokenInterfaceTest.php b/tests/Token/TokenInterfaceTest.php index 93ed3ba..42f2d3a 100644 --- a/tests/Token/TokenInterfaceTest.php +++ b/tests/Token/TokenInterfaceTest.php @@ -7,23 +7,23 @@ namespace JerryYan\DSL\Test\Token; -use JerryYan\DSL\Token\TokenAnd; +use JerryYan\DSL\Token\TokenLogicAnd; use JerryYan\DSL\Token\TokenDefine; use JerryYan\DSL\Token\TokenInterface; -use JerryYan\DSL\Token\TokenOr; +use JerryYan\DSL\Token\TokenLogicOr; use JerryYan\DSL\Token\TokenUndefined; -use JerryYan\DSL\Token\TokenVar; +use JerryYan\DSL\Token\TokenVariable; use PHPUnit\Framework\TestCase; class TokenInterfaceTest extends TestCase { /** @var class-string[] TokenClass */ private $tokenTypes = [ - TokenAnd::class, + TokenLogicAnd::class, TokenDefine::class, - TokenOr::class, + TokenLogicOr::class, TokenUndefined::class, - TokenVar::class, + TokenVariable::class, ]; protected $chainFirst; protected $chainLast; diff --git a/tests/Tokenizer/TokenizerTest.php b/tests/Tokenizer/TokenizerTest.php index f651970..68c28c5 100644 --- a/tests/Tokenizer/TokenizerTest.php +++ b/tests/Tokenizer/TokenizerTest.php @@ -9,10 +9,10 @@ namespace JerryYan\DSL\Test\Tokenizer; use JerryYan\DSL\Reader\StringReader; use JerryYan\DSL\Token\Factory\DefaultFactory; -use JerryYan\DSL\Token\TokenAnd; +use JerryYan\DSL\Token\TokenLogicAnd; use JerryYan\DSL\Token\TokenInterface; -use JerryYan\DSL\Token\TokenOr; -use JerryYan\DSL\Token\TokenVar; +use JerryYan\DSL\Token\TokenLogicOr; +use JerryYan\DSL\Token\TokenVariable; use JerryYan\DSL\Tokenizer\Tokenizer; use PHPUnit\Framework\TestCase; @@ -23,13 +23,13 @@ class TokenizerTest extends TestCase private $text = "这个 和 那个 或者 那个 和 这个"; /** @var class-string[] 预期的类型 */ private $textTokenType = [ - TokenVar::class, - TokenAnd::class, - TokenVar::class, - TokenOr::class, - TokenVar::class, - TokenAnd::class, - TokenVar::class, + TokenVariable::class, + TokenLogicAnd::class, + TokenVariable::class, + TokenLogicOr::class, + TokenVariable::class, + TokenLogicAnd::class, + TokenVariable::class, ]; protected function setUp(): void {