正则匹配

This commit is contained in:
2021-01-22 17:32:15 +08:00
parent ca612f74f2
commit 1aeef5016b
7 changed files with 51 additions and 9 deletions

View File

@ -16,6 +16,7 @@ use JerryYan\DSL\Token\TokenLogicAnd;
use JerryYan\DSL\Token\TokenLogicEqual;
use JerryYan\DSL\Token\TokenLogicFake;
use JerryYan\DSL\Token\TokenLogicOr;
use JerryYan\DSL\Token\TokenNumber;
use JerryYan\DSL\Token\TokenUseVariable;
use JerryYan\DSL\Token\TokenVariable;
@ -28,6 +29,7 @@ class DefaultFactory extends FactoryInterface
Token::LOGIC_EQUAL => TokenLogicEqual::class,
Token::LOGIC_FAKE => TokenLogicFake::class,
Token::VARIABLE => TokenVariable::class,
Token::NUMBER => TokenNumber::class,
Token::DEFINE => TokenDefine::class,
Token::USE_VARIABLE => TokenUseVariable::class,
];

View File

@ -19,16 +19,23 @@ abstract class FactoryInterface
protected $tokenMap = [];
/** @var array<string, class-string<TokenInterface>> Token别名映射 */
protected $tokenNameMap = [];
/** @var array<string, class-string<TokenInterface>> Token别名映射 */
protected $regexNameMap = [];
/** @var class-string<TokenInterface> 默认Token类 */
protected $undefinedTokenClass = TokenUndefined::class;
public function __construct()
{
/**
* @var string $key
* @var TokenInterface $token
*/
foreach ($this->tokenMap as $key=>$token) {
if (property_exists($token, "alias")){
foreach ($token::$alias as $name) {
$this->tokenNameMap[$name] = $key;
}
foreach ($token::$alias as $name) {
$this->tokenNameMap[$name] = $key;
}
foreach ($token::$regexAlias as $name) {
$this->regexNameMap[$name] = $key;
}
}
}
@ -38,6 +45,12 @@ abstract class FactoryInterface
$originalName = $name;
if (isset($this->tokenNameMap[$name])) {
$name = $this->tokenNameMap[$name];
} else {
foreach ($this->regexNameMap as $regex => $newName) {
if (preg_match($regex, $name) === 1) {
$name = $newName; break;
}
}
}
if (!isset($this->tokenMap[$name])) {
return new $this->undefinedTokenClass($originalName);

View File

@ -29,14 +29,17 @@ final class Token
const LOGIC_GREATER_EQUAL = "LOGIC_GREATER_EQUAL";
const LOGIC_LESS = "LOGIC_LESS";
const LOGIC_LESS_EQUAL = "LOGIC_LESS_EQUAL";
const LOGIC_FAKE = "LOGIC_FAKE";
// 变量相关
const DEFINE = "DEFINE";
const USE_VARIABLE = "USE_VARIABLE";
const VARIABLE = "VARIABLE";
const ASSIGN = "ASSIGN";
const CALL = "CALL";
const NUMBER = "NUMBER";
// 语义相关
const FAKE = "FAKE";
const CURRY = "CURRY";
const LOGIC_FAKE = "LOGIC_FAKE";
// 运算符相关
const OP_CONCAT = "OP_CONCAT";
const OP_CONCAT_EQUAL = "OP_CONCAT_EQUAL";

View File

@ -18,7 +18,9 @@ abstract class TokenInterface
/** @var string 原始数据 */
protected $_raw = "";
/** @var array 定义别名 */
static $alias = [];
public static $alias = [];
/** @var array 定义正则别名 */
public static $regexAlias = [];
public function __construct(string $original)
{

17
src/Token/TokenNumber.php Normal file
View File

@ -0,0 +1,17 @@
<?php
/**
* @filename TokenNumber.php
* @author Jerry Yan <792602257@qq.com>
* @date 2021/1/22 17:25
*/
namespace JerryYan\DSL\Token;
class TokenNumber extends TokenInterface
{
public static $regexAlias = [
'/(-?(\d*\.?\d+|\d+\.?\d*)([Ee][+-]?\d+)?)/'
];
}