Skip to content

Commit 9563949

Browse files
authored
Merge pull request #173 from vrana/pgsql-regexp
Treat ~* as operator
2 parents ce37ff2 + 6d638a7 commit 9563949

7 files changed

Lines changed: 19 additions & 3 deletions

File tree

src/Tokenizer.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,7 @@ final class Tokenizer
743743
'>',
744744
'+',
745745
'-',
746+
'~*', // https://www.postgresql.org/docs/current/functions-matching.html#FUNCTIONS-POSIX-REGEXP
746747
'*',
747748
'/',
748749
'!',
@@ -820,10 +821,10 @@ private function makeRegexFromList(array $values, bool $sorted = false): string
820821
$prefix = (string) $prefix;
821822
}
822823

823-
$regex .= preg_quote($prefix, '/');
824+
$regex .= preg_quote($prefix);
824825

825826
$regex .= count($items) === 1
826-
? preg_quote(substr(reset($items), strlen($prefix)), '/')
827+
? preg_quote(substr(reset($items), strlen($prefix)))
827828
: $this->makeRegexFromList(array_map(static fn ($v) => substr($v, strlen($prefix)), $items), true);
828829
}
829830

@@ -887,7 +888,7 @@ private function makeTokenizeRegex(array $regexes): string
887888
$parts[] = '(?<t_' . $type . '>' . $regex . ')';
888889
}
889890

890-
return '~\G(?:' . implode('|', $parts) . ')~';
891+
return '(\G(?:' . implode('|', $parts) . '))';
891892
}
892893

893894
/**

tests/clihighlight.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,3 +1211,6 @@ MY_NON_TOP_LEVEL_KEYWORD_FX_3();
12111211
---
12121212
SELECT
12131213
vector1 <#> vector2
1214+
---
1215+
SELECT
1216+
text ~* '\w+'

tests/compress.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,5 @@ CREATE TABLE t (c VARCHAR(20)) DEFAULT CHARACTER SET utf8mb4 ENGINE = InnoDB
115115
SELECT '{}'::json #> '{}'
116116
---
117117
SELECT vector1 <#> vector2
118+
---
119+
SELECT text ~* '\w+'

tests/format-highlight.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,3 +1211,6 @@
12111211
---
12121212
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span>
12131213
<span style="color: #333;">vector1</span> <span >&lt;</span><span >#</span><span >&gt;</span> <span style="color: #333;">vector2</span></pre>
1214+
---
1215+
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span>
1216+
<span style="color: #333;">text</span> <span >~*</span> <span style="color: blue;">'\w+'</span></pre>

tests/format.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,3 +1209,6 @@ SELECT
12091209
---
12101210
SELECT
12111211
vector1 <#> vector2
1212+
---
1213+
SELECT
1214+
text ~* '\w+'

tests/highlight.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,5 @@
429429
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: blue;">'{}'</span><span >::</span><span style="color: #333;">json</span> <span >#</span><span >&gt;</span> <span style="color: blue;">'{}'</span></pre>
430430
---
431431
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: #333;">vector1</span> <span >&lt;</span><span >#</span><span >&gt;</span> <span style="color: #333;">vector2</span></pre>
432+
---
433+
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: #333;">text</span> <span >~*</span> <span style="color: blue;">'\w+'</span></pre>

tests/sql.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,5 @@ CREATE TABLE t (c VARCHAR(20)) DEFAULT CHARACTER SET utf8mb4 ENGINE = InnoDB
429429
SELECT '{}'::json #> '{}'
430430
---
431431
SELECT vector1 <#> vector2
432+
---
433+
SELECT text ~* '\w+'

0 commit comments

Comments
 (0)