Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ jobs:
with:
composer-options: "--prefer-dist"

# ScannerTest checks keyword matching under a Turkish locale
- name: Generate the tr_TR.UTF-8 locale
run: sudo locale-gen tr_TR.UTF-8

- name: Run tests
run: composer test-unit

Expand Down
2 changes: 2 additions & 0 deletions resources/docker/develop/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ install-php-extensions \
rm -f /usr/local/etc/php/conf.d/docker-php-ext-phalcon.ini
# Locales
sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen
# tr_TR: the scanner's keyword matching must not depend on the locale
sed -i -e 's/# tr_TR.UTF-8 UTF-8/tr_TR.UTF-8 UTF-8/' /etc/locale.gen
dpkg-reconfigure --frontend=noninteractive locales
update-locale LANG=en_US.UTF-8
# Unlimited memory for tests
Expand Down
17 changes: 13 additions & 4 deletions src/Scanner/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use function strlen;
use function strpos;
use function strspn;
use function strtolower;
use function substr;
use function substr_count;

Expand Down Expand Up @@ -89,11 +88,11 @@ final class Scanner

private const DOUBLE_OR_INTEGER = '/[0-9]+(?:\.[0-9]+)?/A';

private const ENDVERBATIM = '/\{%-?[ \t\r\n]*endverbatim(?![a-zA-Z0-9_])/Ai';
private const ENDVERBATIM = '/\{%-?[ \t\r\n]*[eE][nN][dD][vV][eE][rR][bB][aA][tT][iI][mM](?![a-zA-Z0-9_])/A';

private const IDENTIFIER = '/\\\\?[a-zA-Z_][a-zA-Z0-9_\\\\]*/A';

private const IS_NOT_ONE_SPACE = '/is not/Ai';
private const IS_NOT_ONE_SPACE = '/[iI][sS] [nN][oO][tT]/A';

private const IS_NOT_SPACES = '/is +not/A';

Expand Down Expand Up @@ -151,6 +150,14 @@ final class Scanner
'with' => Opcode::WITH,
];

/**
* Keywords and `is not` / `endverbatim` are matched case-insensitively
* byte by byte, as cphalcon's re2c scanner does: strtolower() follows the
* locale before PHP 8.2, and so does PCRE's `/i` once a locale is set -
* under a Turkish one `I` and `i` are not each other's case.
*/
private const LOWER_CASE = 'abcdefghijklmnopqrstuvwxyz';

/**
* Punctuation and operators, longest first within each leading character
* so the first hit is the longest match.
Expand Down Expand Up @@ -202,6 +209,8 @@ final class Scanner
*/
private const TRIM = " \t\n\r\v";

private const UPPER_CASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

private readonly string $code;

/**
Expand Down Expand Up @@ -529,7 +538,7 @@ private function scanVerbatim(): ScannerStatus
*/
private function scanWord(int $cursor, string $word): ScannerStatus
{
$keyword = strtolower($word);
$keyword = strtr($word, self::UPPER_CASE, self::LOWER_CASE);

if ($keyword === 'is') {
$isNot = $this->matchIsNot($cursor);
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/Scanner/ScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,34 @@ public function testKeywordsAreCaseInsensitive(): void
);
}

/**
* cphalcon matches keywords byte by byte. Before PHP 8.2 strtolower()
* follows the locale, and PCRE's case-insensitive matching does on every
* version once a locale is set: under a Turkish one `I` and `i` are not
* each other's case. CI and the development image generate tr_TR.UTF-8;
* where it is missing the test runs under the current locale.
*/
public function testKeywordsIgnoreTheLocale(): void
{
$previous = (string) setlocale(LC_CTYPE, '0');
setlocale(LC_CTYPE, 'tr_TR.UTF-8', 'tr_TR.ISO-8859-9', 'tr_TR');

try {
$tokens = $this->codeTokens('{% IF a IS NOT b %}{{ a IN b }}{% verbatim %}x{% ENDVERBATIM %}');
} finally {
setlocale(LC_CTYPE, $previous);
}

$this->assertSame(
[
['IF', null], ['IDENTIFIER', 'a'], ['NOTEQUALS', null], ['IDENTIFIER', 'b'],
['IDENTIFIER', 'a'], ['IN', null], ['IDENTIFIER', 'b'],
['VERBATIM', null], ['RAW_FRAGMENT', 'x'], ['ENDVERBATIM', null],
],
$tokens
);
}

public function testLeftTrimCrossesAComment(): void
{
$this->assertSame(
Expand Down
Loading