Skip to content

Commit 960df28

Browse files
committed
Moving reserved logic from tokenizer
1 parent 62f22df commit 960df28

File tree

2 files changed

+77
-52
lines changed

2 files changed

+77
-52
lines changed

src/Tokenizer/Parser/Reserved.php

Lines changed: 65 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,45 @@
1818
*/
1919
final class Reserved
2020
{
21+
/**
22+
* @param array $matches
23+
* @param string $previous
24+
* @param string $string
25+
* @param string $reservedTopLevel
26+
* @param string $reservedNewLine
27+
* @param string $boundaries
28+
* @param string $reserved
29+
*
30+
* @return array
31+
*/
32+
public static function isReserved(
33+
array &$matches,
34+
$previous,
35+
$string,
36+
$reservedTopLevel,
37+
$reservedNewLine,
38+
$boundaries,
39+
$reserved
40+
) {
41+
$reservedArray = [];
42+
43+
if (Reserved::isReservedPrecededByDotCharacter($previous)) {
44+
if (Reserved::isReservedString($string, $matches, $reservedTopLevel, $boundaries)) {
45+
$reservedArray = Reserved::getReservedTopLevelString($string, $matches);
46+
}
47+
48+
if (empty($reservedArray) && Reserved::isReservedString($string, $matches, $reservedNewLine, $boundaries)) {
49+
$reservedArray = Reserved::getReservedNewLineString($string, $matches);
50+
}
51+
52+
if (empty($reservedArray) && Reserved::isReservedString($string, $matches, $reserved, $boundaries)) {
53+
$reservedArray = Reserved::getReservedString($string, $matches);
54+
}
55+
}
56+
57+
return $reservedArray;
58+
}
59+
2160
/**
2261
* A reserved word cannot be preceded by a "." in order to differentiate "mytable.from" from the token "from".
2362
*
@@ -30,45 +69,47 @@ public static function isReservedPrecededByDotCharacter($previous)
3069
return !$previous || !isset($previous[Tokenizer::TOKEN_VALUE]) || $previous[Tokenizer::TOKEN_VALUE] !== '.';
3170
}
3271

33-
3472
/**
35-
* @param string $string
36-
* @param array $matches
73+
* @param string $upper
74+
* @param array $matches
75+
* @param string $regexReserved
76+
* @param string $regexBoundaries
3777
*
38-
* @return array
78+
* @return bool
3979
*/
40-
public static function getReservedTopLevelString($string, array &$matches)
80+
public static function isReservedString($upper, array &$matches, $regexReserved, $regexBoundaries)
4181
{
42-
return self::getStringTypeArray(Tokenizer::TOKEN_TYPE_RESERVED_TOP_LEVEL, $string, $matches);
82+
return 1 == preg_match(
83+
'/^(' . $regexReserved . ')($|\s|' . $regexBoundaries . ')/',
84+
strtoupper($upper),
85+
$matches
86+
);
4387
}
4488

45-
4689
/**
4790
* @param string $string
4891
* @param array $matches
4992
*
5093
* @return array
5194
*/
52-
public static function getReservedNewLineString($string, array &$matches)
95+
public static function getReservedTopLevelString($string, array &$matches)
5396
{
54-
return self::getStringTypeArray(Tokenizer::TOKEN_TYPE_RESERVED_NEWLINE, strtoupper($string), $matches);
97+
return self::getStringTypeArray(Tokenizer::TOKEN_TYPE_RESERVED_TOP_LEVEL, $string, $matches);
5598
}
5699

57100
/**
58-
* @param string $upper
101+
* @param $type
102+
* @param string $string
59103
* @param array $matches
60-
* @param string $regexReserved
61-
* @param string $regexBoundaries
62104
*
63-
* @return bool
105+
* @return array
64106
*/
65-
public static function isReservedString($upper, array &$matches, $regexReserved, $regexBoundaries)
107+
protected static function getStringTypeArray($type, $string, array &$matches)
66108
{
67-
return 1 == preg_match(
68-
'/^(' . $regexReserved . ')($|\s|' . $regexBoundaries . ')/',
69-
strtoupper($upper),
70-
$matches
71-
);
109+
return [
110+
Tokenizer::TOKEN_TYPE => $type,
111+
Tokenizer::TOKEN_VALUE => substr($string, 0, strlen($matches[1]))
112+
];
72113
}
73114

74115
/**
@@ -77,23 +118,19 @@ public static function isReservedString($upper, array &$matches, $regexReserved,
77118
*
78119
* @return array
79120
*/
80-
public static function getReservedString($string, array &$matches)
121+
public static function getReservedNewLineString($string, array &$matches)
81122
{
82-
return self::getStringTypeArray(Tokenizer::TOKEN_TYPE_RESERVED, $string, $matches);
123+
return self::getStringTypeArray(Tokenizer::TOKEN_TYPE_RESERVED_NEWLINE, strtoupper($string), $matches);
83124
}
84125

85126
/**
86-
* @param $type
87127
* @param string $string
88-
* @param array $matches
128+
* @param array $matches
89129
*
90130
* @return array
91131
*/
92-
protected static function getStringTypeArray($type, $string, array &$matches)
132+
public static function getReservedString($string, array &$matches)
93133
{
94-
return [
95-
Tokenizer::TOKEN_TYPE => $type,
96-
Tokenizer::TOKEN_VALUE => substr($string, 0, strlen($matches[1]))
97-
];
134+
return self::getStringTypeArray(Tokenizer::TOKEN_TYPE_RESERVED, $string, $matches);
98135
}
99136
}

src/Tokenizer/Tokenizer.php

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -228,30 +228,18 @@ protected function getNextToken($string, $previous = null)
228228
return Boundary::getBoundaryCharacter($matches);
229229
}
230230

231-
if (Reserved::isReservedPrecededByDotCharacter($previous)) {
232-
if (Reserved::isReservedString(
233-
$string,
234-
$matches,
235-
$this->regexReservedTopLevel,
236-
$this->regexBoundaries
237-
)
238-
) {
239-
return Reserved::getReservedTopLevelString($string, $matches);
240-
}
241-
242-
if (Reserved::isReservedString(
243-
$string,
244-
$matches,
245-
$this->regexReservedNewLine,
246-
$this->regexBoundaries
247-
)
248-
) {
249-
return Reserved::getReservedNewLineString($string, $matches);
250-
}
251-
252-
if (Reserved::isReservedString($string, $matches, $this->regexReserved, $this->regexBoundaries)) {
253-
return Reserved::getReservedString($string, $matches);
254-
}
231+
$isReserved = Reserved::isReserved(
232+
$matches,
233+
$previous,
234+
$string,
235+
$this->regexReservedTopLevel,
236+
$this->regexReservedNewLine,
237+
$this->regexBoundaries,
238+
$this->regexReserved
239+
);
240+
241+
if (0 !== count($isReserved)) {
242+
return $isReserved;
255243
}
256244

257245
if (String::isFunctionString($string, $matches, $this->regexFunction)) {

0 commit comments

Comments
 (0)