Skip to content

Commit

Permalink
class name resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Mar 20, 2024
1 parent bca654f commit da6aab8
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 42 deletions.
26 changes: 26 additions & 0 deletions src/Languages/Php/Patterns/ClassResolutionPattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Languages\Php\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
use Tempest\Highlight\PatternTest;
use Tempest\Highlight\Tokens\TokenType;

#[PatternTest(input: '$foo::class', output: 'class')]
final readonly class ClassResolutionPattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '\:\:(?<match>class)';
}

public function getTokenType(): TokenType
{
return TokenType::KEYWORD;
}
}
5 changes: 4 additions & 1 deletion src/Languages/Php/Patterns/StaticClassCallPattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
use Tempest\Highlight\PatternTest;
use Tempest\Highlight\Tokens\TokenType;

#[PatternTest(input: 'Foo::bar()', output: 'Foo')]
#[PatternTest(input: 'Foo::BAR', output: 'Foo')]
final readonly class StaticClassCallPattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '(?<match>[\w]+)\:\:';
return '(?<!\\$)(?<match>[\w]+)\:\:';
}

public function getTokenType(): TokenType
Expand Down
26 changes: 26 additions & 0 deletions src/Languages/Php/Patterns/VariablePattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Languages\Php\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
use Tempest\Highlight\PatternTest;
use Tempest\Highlight\Tokens\TokenType;

#[PatternTest(input: '$foo', output: '$foo')]
final readonly class VariablePattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '(?<match>\\$[\w]+)';
}

public function getTokenType(): TokenType
{
return TokenType::VARIABLE;
}
}
6 changes: 6 additions & 0 deletions src/Languages/Php/PhpLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Tempest\Highlight\Languages\Php\Patterns\AttributeTypePattern;
use Tempest\Highlight\Languages\Php\Patterns\ClassNamePattern;
use Tempest\Highlight\Languages\Php\Patterns\ClassPropertyPattern;
use Tempest\Highlight\Languages\Php\Patterns\ClassResolutionPattern;
use Tempest\Highlight\Languages\Php\Patterns\ConstantNamePattern;
use Tempest\Highlight\Languages\Php\Patterns\ConstantPropertyPattern;
use Tempest\Highlight\Languages\Php\Patterns\ConstantTypesPattern;
Expand All @@ -36,6 +37,7 @@
use Tempest\Highlight\Languages\Php\Patterns\UntypedClassPropertyPattern;
use Tempest\Highlight\Languages\Php\Patterns\UseAsPattern;
use Tempest\Highlight\Languages\Php\Patterns\UsePattern;
use Tempest\Highlight\Languages\Php\Patterns\VariablePattern;

class PhpLanguage extends BaseLanguage
{
Expand Down Expand Up @@ -125,6 +127,7 @@ public function getPatterns(): array
new KeywordPattern('xor'),
new KeywordPattern('yield'),
new KeywordPattern('yield from'),
new ClassResolutionPattern(),

// ATTRIBUTES
new AttributePattern(),
Expand Down Expand Up @@ -161,6 +164,9 @@ public function getPatterns(): array
new ConstantNamePattern(),
new UntypedClassPropertyPattern(),

// VARIABLES
new VariablePattern(),

// VALUES
new SingleQuoteValuePattern(),
new DoubleQuoteValuePattern(),
Expand Down
1 change: 1 addition & 0 deletions src/Themes/CssTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function before(string|TokenType $tokenType): string
TokenType::COMMENT => 'hl-comment',
TokenType::ATTRIBUTE => 'hl-attribute',
TokenType::INJECTION => 'hl-injection',
TokenType::VARIABLE => 'hl-variable',
default => $tokenType,
};

Expand Down
3 changes: 2 additions & 1 deletion src/Tokens/TokenType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ enum TokenType: string
case TYPE = 'type';
case GENERIC = 'generic';
case VALUE = 'value';
case VARIABLE = 'variable';
case COMMENT = 'comment';
case INJECTION = 'injection';

public function canContain(self $other): bool
{
return match ($this) {
self::VALUE, self::INJECTION, self::COMMENT => false,
self::VARIABLE, self::VALUE, self::INJECTION, self::COMMENT => false,
default => true,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function test_injection(): void
$this->assertMatches(
injection: new BladeEchoInjection(),
content: '{{ count($foo) }}',
expected: '{{ <span class="hl-property">count</span>($foo) }}',
expected: '{{ <span class="hl-property">count</span>(<span class="hl-variable">$foo</span>) }}',
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function test_injection_raw_echo(): void
$this->assertMatches(
injection: new BladeRawEchoInjection(),
content: '{!! count($foo) !!}',
expected: '{!! <span class="hl-property">count</span>($foo) !!}',
expected: '{!! <span class="hl-property">count</span>(<span class="hl-variable">$foo</span>) !!}',
);
}
}
2 changes: 1 addition & 1 deletion tests/Languages/Php/Injections/PhpInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function test_injection(): void
$expected = '
&lt;?php
<span class="hl-comment">/** @var \Tempest\View\GenericView $this */</span>
$var = <span class="hl-keyword">new</span> <span class="hl-type">Foo</span>();
<span class="hl-variable">$var</span> = <span class="hl-keyword">new</span> <span class="hl-type">Foo</span>();
?&gt;
Hello, &lt;?= $this-&gt;name ?&gt;
Expand Down
4 changes: 2 additions & 2 deletions tests/Languages/Php/Injections/PhpShortEchoInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public function test_injection(): void
';

$expected = '
Hello, &lt;?= $this-&gt;<span class="hl-property">name</span> ?&gt;
Hello, &lt;?= $this-&gt;<span class="hl-property">other</span> ?&gt;
Hello, &lt;?= <span class="hl-variable">$this</span>-&gt;<span class="hl-property">name</span> ?&gt;
Hello, &lt;?= <span class="hl-variable">$this</span>-&gt;<span class="hl-property">other</span> ?&gt;
';

$this->assertMatches(
Expand Down
29 changes: 0 additions & 29 deletions tests/Languages/Php/Patterns/StaticClassCallPatternTest.php

This file was deleted.

7 changes: 4 additions & 3 deletions tests/Languages/Php/PhpLanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ public static function data(): array
["'php()'", "'<span class=\"hl-value\">php()</span>'"],
["public const string|\Stringable MESSAGE = 'hi';", '<span class="hl-keyword">public</span> <span class="hl-keyword">const</span> <span class="hl-type">string|\Stringable</span> <span class="hl-property">MESSAGE</span> = \'<span class="hl-value">hi</span>\';'],
["public string|\Stringable \$message;", '<span class="hl-keyword">public</span> <span class="hl-type">string|\<span class="hl-type">Stringable</span></span> <span class="hl-property">$message</span>;'],
['for($x = 0; $x < 150; $x++) {', '<span class="hl-keyword">for</span>($x = 0; $x &lt; 150; $x++) {'],
['for($x = 0; $x < 150; $x++) {', '<span class="hl-keyword">for</span>(<span class="hl-variable">$x</span> = 0; <span class="hl-variable">$x</span> &lt; 150; <span class="hl-variable">$x</span>++) {'],
["'namespace ';", "'<span class=\"hl-value\">namespace </span>';"],
['$class', '$class'],
['$class', '<span class="hl-variable">$class</span>'],
['protected $resolved = [];', '<span class="hl-keyword">protected</span> <span class="hl-property">$resolved</span> = [];'],
['protected Foo $resolved = [];', '<span class="hl-keyword">protected</span> <span class="hl-type">Foo</span> <span class="hl-property">$resolved</span> = [];'],
['$concrete instanceof Closure', '$concrete <span class="hl-keyword">instanceof</span> <span class="hl-type">Closure</span>'],
['$concrete instanceof Closure', '<span class="hl-variable">$concrete</span> <span class="hl-keyword">instanceof</span> <span class="hl-type">Closure</span>'],
['extends Foo implements ArrayAccess, ContainerContract', '<span class="hl-keyword">extends</span> <span class="hl-type">Foo</span> <span class="hl-keyword">implements</span><span class="hl-type"> ArrayAccess, ContainerContract</span>'],
['use Illuminate\Contracts\Container\Container as ContainerContract', '<span class="hl-keyword">use</span> <span class="hl-type">Illuminate\Contracts\Container\Container</span> <span class="hl-keyword">as</span> <span class="hl-type">ContainerContract</span>'],
['$foo::class;', '<span class="hl-variable">$foo</span>::<span class="hl-keyword">class</span>;'],
[
"// We'll
Expand Down
4 changes: 2 additions & 2 deletions tests/stubs/02.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
&lt;<span class="hl-keyword">body</span>&gt;
&lt;<span class="hl-keyword">div</span>&gt;
&lt;?php
$sql = &lt;&lt;&lt;<span class="hl-property"><span class="hl-property">SQL</span></span><span class="hl-injection"><span class="hl-injection">
<span class="hl-variable">$sql</span> = &lt;&lt;&lt;<span class="hl-property"><span class="hl-property">SQL</span></span><span class="hl-injection"><span class="hl-injection">
<span class="hl-keyword">SELECT</span> <span class="hl-type">posts</span>.*, <span class="hl-type">authors</span>.<span class="hl-property">name</span>
<span class="hl-keyword">FROM</span> <span class="hl-type">posts</span>
<span class="hl-keyword">INNER <span class="hl-keyword">JOIN</span></span> <span class="hl-type">authors</span> <span class="hl-keyword">ON</span> <span class="hl-type">posts</span>.<span class="hl-property">author_id</span> = <span class="hl-type">authors</span>.<span class="hl-property">id</span>
</span></span><span class="hl-property"><span class="hl-property">SQL</span></span>;

$posts = <span class="hl-type">DB</span>::<span class="hl-property">get</span>($sql);
<span class="hl-variable">$posts</span> = <span class="hl-type">DB</span>::<span class="hl-property">get</span>(<span class="hl-variable">$sql</span>);

<span class="hl-comment">// Don't do this IRL :)</span>
?&gt;
Expand Down
2 changes: 1 addition & 1 deletion tests/test.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
```php
use Illuminate\Contracts\Container\Container as ContainerContract
$foo::class;
```

0 comments on commit da6aab8

Please sign in to comment.