Skip to content

Commit c58ac6c

Browse files
author
jswanhart
committed
Do not treat comments inside strings as comments
1 parent 8592236 commit c58ac6c

1 file changed

Lines changed: 27 additions & 24 deletions

File tree

src/PHPSQLParser/lexer/PHPSQLLexer.php

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ public function split($sql) {
8585
throw new InvalidParameterException($sql);
8686
}
8787
$tokens = preg_split($this->splitters->getSplittersRegexPattern(), $sql, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
88-
echo "After split:" . print_r($tokens,true);
8988
$tokens = $this->concatComments($tokens);
9089
$tokens = $this->concatEscapeSequences($tokens);
9190
$tokens = $this->balanceBackticks($tokens);
@@ -212,7 +211,7 @@ protected function concatComments($tokens) {
212211
$i = 0;
213212
$cnt = count($tokens);
214213
$comment = false;
215-
214+
$in_string = false;
216215
while ($i < $cnt) {
217216

218217
if (!isset($tokens[$i])) {
@@ -221,32 +220,36 @@ protected function concatComments($tokens) {
221220
}
222221

223222
$token = $tokens[$i];
224-
225-
if ($comment !== false) {
226-
if ($inline === true && ($token === "\n" || $token === "\r\n")) {
227-
$comment = false;
228-
} else {
229-
unset($tokens[$i]);
230-
$tokens[$comment] .= $token;
231-
}
232-
if ($inline === false && ($token === "*/")) {
233-
$comment = false;
234-
}
223+
if($token == "\"" || $token == "'") {
224+
$in_string = !$in_string;
235225
}
226+
if(!$in_string) {
227+
if ($comment !== false) {
228+
if ($inline === true && ($token === "\n" || $token === "\r\n")) {
229+
$comment = false;
230+
} else {
231+
unset($tokens[$i]);
232+
$tokens[$comment] .= $token;
233+
}
234+
if ($inline === false && ($token === "*/")) {
235+
$comment = false;
236+
}
237+
}
236238

237-
if (($comment === false) && ($token === "--")) {
238-
$comment = $i;
239-
$inline = true;
240-
}
239+
if (($comment === false) && ($token === "--")) {
240+
$comment = $i;
241+
$inline = true;
242+
}
241243

242-
/*if (($comment === false) && (substr($token, 0, 1) === "#")) {
243-
$comment = $i;
244-
$inline = true;
245-
}*/
244+
if (($comment === false) && (substr($token, 0, 1) === "#")) {
245+
$comment = $i;
246+
$inline = true;
247+
}
246248

247-
if (($comment === false) && ($token === "/*")) {
248-
$comment = $i;
249-
$inline = false;
249+
if (($comment === false) && ($token === "/*")) {
250+
$comment = $i;
251+
$inline = false;
252+
}
250253
}
251254

252255
$i++;

0 commit comments

Comments
 (0)