diff options
Diffstat (limited to 'src/NXP/Classes')
-rw-r--r-- | src/NXP/Classes/CustomFunction.php | 22 | ||||
-rw-r--r-- | src/NXP/Classes/Tokenizer.php | 58 |
2 files changed, 54 insertions, 26 deletions
diff --git a/src/NXP/Classes/CustomFunction.php b/src/NXP/Classes/CustomFunction.php index 1ebdd2c..6e9ffc5 100644 --- a/src/NXP/Classes/CustomFunction.php +++ b/src/NXP/Classes/CustomFunction.php @@ -2,7 +2,6 @@ namespace NXP\Classes; -use NXP\Exception\IncorrectFunctionParameterException; use NXP\Exception\IncorrectNumberOfFunctionParametersException; use ReflectionException; use ReflectionFunction; @@ -16,7 +15,7 @@ class CustomFunction */ public $function; - private ReflectionFunction $reflectionFunction; + private int $requiredParamCount; /** * CustomFunction constructor. @@ -27,36 +26,25 @@ class CustomFunction { $this->name = $name; $this->function = $function; - $this->reflectionFunction = new ReflectionFunction($function); + $this->requiredParamCount = (new ReflectionFunction($function))->getNumberOfRequiredParameters(); } /** * @param array<Token> $stack * - * @throws IncorrectNumberOfFunctionParametersException|IncorrectFunctionParameterException + * @throws IncorrectNumberOfFunctionParametersException */ public function execute(array &$stack, int $paramCountInStack) : Token { - if ($paramCountInStack < $this->reflectionFunction->getNumberOfRequiredParameters()) { + if ($paramCountInStack < $this->requiredParamCount) { throw new IncorrectNumberOfFunctionParametersException($this->name); } $args = []; if ($paramCountInStack > 0) { - $reflectionParameters = $this->reflectionFunction->getParameters(); - for ($i = 0; $i < $paramCountInStack; $i++) { - $value = \array_pop($stack)->value; - $valueType = \gettype($value); - $reflectionParameter = $reflectionParameters[\min(\count($reflectionParameters) - 1, $i)]; - //TODO to support type check for union types (php >= 8.0) and intersection types (php >= 8.1), we should increase min php level in composer.json - // For now, only support basic types. @see testFunctionParameterTypes - if ($reflectionParameter->hasType() && $reflectionParameter->getType()->getName() !== $valueType){ - throw new IncorrectFunctionParameterException(); - } - - \array_unshift($args, $value); + \array_unshift($args, \array_pop($stack)->value); } } diff --git a/src/NXP/Classes/Tokenizer.php b/src/NXP/Classes/Tokenizer.php index c8b415d..af041c1 100644 --- a/src/NXP/Classes/Tokenizer.php +++ b/src/NXP/Classes/Tokenizer.php @@ -50,27 +50,67 @@ class Tokenizer public function tokenize() : self { - foreach (\str_split($this->input, 1) as $ch) { + $isLastCharEscape = false; + + foreach (\str_split($this->input) as $ch) { switch (true) { case $this->inSingleQuotedString: - if ("'" === $ch) { - $this->tokens[] = new Token(Token::String, $this->stringBuffer); - $this->inSingleQuotedString = false; - $this->stringBuffer = ''; + if ('\\' === $ch) { + if ($isLastCharEscape) { + $this->stringBuffer .= '\\'; + $isLastCharEscape = false; + } else { + $isLastCharEscape = true; + } + + continue 2; + } elseif ("'" === $ch) { + if ($isLastCharEscape) { + $this->stringBuffer .= "'"; + $isLastCharEscape = false; + } else { + $this->tokens[] = new Token(Token::String, $this->stringBuffer); + $this->inSingleQuotedString = false; + $this->stringBuffer = ''; + } continue 2; } + + if ($isLastCharEscape) { + $this->stringBuffer .= '\\'; + $isLastCharEscape = false; + } $this->stringBuffer .= $ch; continue 2; case $this->inDoubleQuotedString: - if ('"' === $ch) { - $this->tokens[] = new Token(Token::String, $this->stringBuffer); - $this->inDoubleQuotedString = false; - $this->stringBuffer = ''; + if ('\\' === $ch) { + if ($isLastCharEscape) { + $this->stringBuffer .= '\\'; + $isLastCharEscape = false; + } else { + $isLastCharEscape = true; + } continue 2; + } elseif ('"' === $ch) { + if ($isLastCharEscape) { + $this->stringBuffer .= '"'; + $isLastCharEscape = false; + } else { + $this->tokens[] = new Token(Token::String, $this->stringBuffer); + $this->inDoubleQuotedString = false; + $this->stringBuffer = ''; + } + + continue 2; + } + + if ($isLastCharEscape) { + $this->stringBuffer .= '\\'; + $isLastCharEscape = false; } $this->stringBuffer .= $ch; |