diff options
author | Fatih Kızmaz <barka_21@hotmail.com> | 2022-05-19 05:03:44 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-19 05:03:44 +0300 |
commit | 3e6700d1576c6582169a5d50c95da106ee3017cf (patch) | |
tree | 66acb56fd05278a6f7ef328a1cc85c2ecfa9a8d2 /src/NXP/Classes/Tokenizer.php | |
parent | f71b77a62eb27184b5653d6293250e7fda2fdfef (diff) |
Added ability to escape quotes in strings. (#110)
* Added ability to escape quotes in strings.
* Removed type checking for customfunc arguments. It was a bad idea to check types, because php automatically tries to convert a parameter to required type and throws if it failures. On the other hand, we can check types also in callables if required.
* Update phpdoc
* Fix some typos + improve min, max, avg funcs.
* Update readme + improvements.
* Fix a typo in sample.
* Fix unshown backslash in readme.
Diffstat (limited to 'src/NXP/Classes/Tokenizer.php')
-rw-r--r-- | src/NXP/Classes/Tokenizer.php | 58 |
1 files changed, 49 insertions, 9 deletions
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; |