Skip to content

Commit

Permalink
Catch Type Pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Mar 20, 2024
1 parent b3cd587 commit 6b9ffe0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
26 changes: 26 additions & 0 deletions src/Languages/Php/Patterns/CatchTypePattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

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: 'catch (Foo) {}', output: 'Foo')]
#[PatternTest(input: 'catch (Foo|Bar) {}', output: 'Foo|Bar')]
#[PatternTest(input: 'catch (Foo|Bar $bar) {}', output: null)]
final readonly class CatchTypePattern implements Pattern
{
use IsPattern;

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

public function getTokenType(): TokenType
{
return TokenType::TYPE;
}
}
2 changes: 2 additions & 0 deletions src/Languages/Php/PhpLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Tempest\Highlight\Languages\Php\Injections\HeredocInjection;
use Tempest\Highlight\Languages\Php\Patterns\AttributePattern;
use Tempest\Highlight\Languages\Php\Patterns\AttributeTypePattern;
use Tempest\Highlight\Languages\Php\Patterns\CatchTypePattern;
use Tempest\Highlight\Languages\Php\Patterns\ClassNamePattern;
use Tempest\Highlight\Languages\Php\Patterns\ClassPropertyPattern;
use Tempest\Highlight\Languages\Php\Patterns\ClassResolutionPattern;
Expand Down Expand Up @@ -161,6 +162,7 @@ public function getPatterns(): array
new NewObjectPattern(),
new InstanceOfPattern(),
new UseAsPattern(),
new CatchTypePattern(),
// new GroupedTypePattern(),

// PROPERTIES
Expand Down
1 change: 1 addition & 0 deletions tests/Languages/Php/PhpLanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static function data(): array
['$foo::class;', '<span class="hl-variable">$foo</span>::<span class="hl-keyword">class</span>;'],
// ['function ((Foo&Bar)|null $bar)', '<span class="hl-keyword">function</span> (<span class="hl-type">(Foo&amp;Bar)</span><span class="hl-type">|null</span> <span class="hl-variable">$bar</span>)'],
['while (true) {', '<span class="hl-keyword">while</span> (true) {'],
['catch (Foo|Bar) {}', '<span class="hl-keyword">catch</span> (<span class="hl-type">Foo|Bar</span>) {}'],
['fn&(', '<span class="hl-keyword">fn</span>&amp;('],
[
"// We'll
Expand Down
5 changes: 1 addition & 4 deletions tests/test.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
```php
trait Test {
abstract public function test(int $input): int;
}

catch (MySpecialException|Foo) {
```

0 comments on commit 6b9ffe0

Please sign in to comment.