Skip to content

Commit ad0f880

Browse files
committed
Generalization of comment implementation
1 parent 20913af commit ad0f880

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
.idea/
12
vendor/

lib/SqlFormatter.php

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class SqlFormatter
3939
'CHARSET', 'CHECK', 'CHECKSUM', 'COLLATE', 'COLLATION', 'COLUMN', 'COLUMNS', 'COMMENT', 'COMMIT', 'COMMITTED', 'COMPRESSED', 'CONCURRENT',
4040
'CONSTRAINT', 'CONTAINS', 'CONVERT', 'CREATE', 'CROSS', 'CURRENT_TIMESTAMP', 'DATABASE', 'DATABASES', 'DAY', 'DAY_HOUR', 'DAY_MINUTE',
4141
'DAY_SECOND', 'DEFAULT', 'DEFINER', 'DELAYED', 'DELETE', 'DESC', 'DESCRIBE', 'DETERMINISTIC', 'DISTINCT', 'DISTINCTROW', 'DIV',
42-
'DO', 'DUMPFILE', 'DUPLICATE', 'DYNAMIC', 'ELSE', 'ENCLOSED', 'END', 'ENGINE', 'ENGINE_TYPE', 'ENGINES', 'ESCAPE', 'ESCAPED', 'EVENTS', 'EXEC',
42+
'DO', 'DUMPFILE', 'DUPLICATE', 'DYNAMIC', 'ELSE', 'ENCLOSED', 'END', 'ENGINE', 'ENGINE_TYPE', 'ENGINES', 'ESCAPE', 'ESCAPED', 'EVENTS', 'EXEC',
4343
'EXECUTE', 'EXISTS', 'EXPLAIN', 'EXTENDED', 'FAST', 'FIELDS', 'FILE', 'FIRST', 'FIXED', 'FLUSH', 'FOR', 'FORCE', 'FOREIGN', 'FULL', 'FULLTEXT',
4444
'FUNCTION', 'GLOBAL', 'GRANT', 'GRANTS', 'GROUP_CONCAT', 'HEAP', 'HIGH_PRIORITY', 'HOSTS', 'HOUR', 'HOUR_MINUTE',
4545
'HOUR_SECOND', 'IDENTIFIED', 'IF', 'IFNULL', 'IGNORE', 'IN', 'INDEX', 'INDEXES', 'INFILE', 'INSERT', 'INSERT_ID', 'INSERT_METHOD', 'INTERVAL',
@@ -122,6 +122,13 @@ class SqlFormatter
122122
// If not defined, it will be determined automatically
123123
public static $cli;
124124

125+
// Comments
126+
public static $commentTokens = [
127+
['#'],
128+
['--'],
129+
['/*', '*/']
130+
];
131+
125132
// For CLI syntax highlighting
126133
public static $cli_quote = "\x1b[34;1m";
127134
public static $cli_backtick_quote = "\x1b[35;1m";
@@ -214,38 +221,33 @@ protected static function getNextToken($string, $previous = null)
214221
}
215222

216223
// Comment
217-
if ($string[0] === '#' || (isset($string[1])&&($string[0]==='-'&&$string[1]==='-') || ($string[0]==='/'&&isset($string[1])&&$string[1]==='*'))) {
218-
// Comment until end of line
219-
if ($string[0] === '-' || $string[0] === '#') {
220-
$last = strpos($string, "\n");
221-
$type = self::TOKEN_TYPE_COMMENT;
222-
} else { // Comment until closing comment tag or end of query
223-
if (strpos($string, "*/", 2) !== false) {
224-
$last = strpos($string, "*/", 2) + 2;
225-
} else {
226-
$last = strlen($string);
227-
}
228-
$type = self::TOKEN_TYPE_BLOCK_COMMENT;
229-
}
230-
231-
if ($last === false) {
232-
$last = strlen($string);
224+
$found = false;
225+
foreach (self::$commentTokens as $comment) {
226+
$start = $comment[0];
227+
$end = isset($comment[1]) ? $comment[1] : "\n";
228+
$found = substr($string, 0, strlen($start)) === $start;
229+
if ($found) {
230+
break;
233231
}
234-
232+
}
233+
if ($found) {
234+
/** @var string $start */
235+
/** @var string $end */
236+
$endPos = strpos($string, $end, strlen($start));
237+
$last = $endPos ?
238+
($endPos + ($end === "\n" ? 0 : strlen($end))) : strlen($string);
235239
return array(
236240
self::TOKEN_VALUE => substr($string, 0, $last),
237-
self::TOKEN_TYPE => $type
241+
self::TOKEN_TYPE => $end === "\n" ? self::TOKEN_TYPE_COMMENT : self::TOKEN_TYPE_BLOCK_COMMENT
238242
);
239243
}
240244

241245
// Quoted String
242246
if ($string[0]==='"' || $string[0]==='\'' || $string[0]==='`' || $string[0]==='[') {
243-
$return = array(
247+
return array(
244248
self::TOKEN_TYPE => (($string[0]==='`' || $string[0]==='[')? self::TOKEN_TYPE_BACKTICK_QUOTE : self::TOKEN_TYPE_QUOTE),
245249
self::TOKEN_VALUE => self::getQuotedString($string)
246250
);
247-
248-
return $return;
249251
}
250252

251253
// User-defined Variable
@@ -254,7 +256,7 @@ protected static function getNextToken($string, $previous = null)
254256
self::TOKEN_VALUE => null,
255257
self::TOKEN_TYPE => self::TOKEN_TYPE_VARIABLE
256258
);
257-
259+
258260
// If the variable name is quoted
259261
if ($string[1]==='"' || $string[1]==='\'' || $string[1]==='`') {
260262
$ret[self::TOKEN_VALUE] = $string[0].self::getQuotedString(substr($string,1));
@@ -266,7 +268,7 @@ protected static function getNextToken($string, $previous = null)
266268
$ret[self::TOKEN_VALUE] = $matches[1];
267269
}
268270
}
269-
271+
270272
if($ret[self::TOKEN_VALUE] !== null) return $ret;
271273
}
272274

@@ -336,7 +338,7 @@ protected static function getNextToken($string, $previous = null)
336338
protected static function getQuotedString($string)
337339
{
338340
$ret = null;
339-
341+
340342
// This checks for the following patterns:
341343
// 1. backtick quoted string using `` to escape
342344
// 2. square bracket quoted string (SQL Server) using ]] to escape
@@ -345,7 +347,7 @@ protected static function getQuotedString($string)
345347
if ( preg_match('/^(((`[^`]*($|`))+)|((\[[^\]]*($|\]))(\][^\]]*($|\]))*)|(("[^"\\\\]*(?:\\\\.[^"\\\\]*)*("|$))+)|((\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*(\'|$))+))/s', $string, $matches)) {
346348
$ret = $matches[1];
347349
}
348-
350+
349351
return $ret;
350352
}
351353

@@ -699,14 +701,14 @@ public static function format($string, $highlight=true)
699701
if ($token[self::TOKEN_VALUE] === '(' || $token[self::TOKEN_VALUE] === '.') {
700702
$return = rtrim($return,' ');
701703
}
702-
704+
703705
// If this is the "-" of a negative number, it shouldn't have a space after it
704706
if($token[self::TOKEN_VALUE] === '-' && isset($tokens[$i+1]) && $tokens[$i+1][self::TOKEN_TYPE] === self::TOKEN_TYPE_NUMBER && isset($tokens[$i-1])) {
705707
$prev = $tokens[$i-1][self::TOKEN_TYPE];
706708
if($prev !== self::TOKEN_TYPE_QUOTE && $prev !== self::TOKEN_TYPE_BACKTICK_QUOTE && $prev !== self::TOKEN_TYPE_WORD && $prev !== self::TOKEN_TYPE_NUMBER) {
707709
$return = rtrim($return,' ');
708710
}
709-
}
711+
}
710712
}
711713

712714
// If there are unmatched parentheses

0 commit comments

Comments
 (0)