Skip to content

Commit

Permalink
Single and double quote too greedy
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Mar 20, 2024
1 parent 0a869fc commit 3989089
Show file tree
Hide file tree
Showing 17 changed files with 1,541 additions and 87 deletions.
11 changes: 1 addition & 10 deletions src/Languages/JavaScript/Patterns/JsDoubleQuoteValuePattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,13 @@
input: 'return "hello"',
output: 'hello',
)]

#[PatternTest(
input: 'return "hello
world"',
output: 'hello
world',
)]
final readonly class JsDoubleQuoteValuePattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '"(?<match>(\n|.)*?)"';
return '"(?<match>.*?)"';
}

public function getTokenType(): TokenType
Expand Down
10 changes: 1 addition & 9 deletions src/Languages/JavaScript/Patterns/JsSingleQuoteValuePattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,13 @@
input: "return 'hello';",
output: 'hello',
)]
#[PatternTest(
input: "return 'hello
world';",
output: 'hello
world',
)]
final readonly class JsSingleQuoteValuePattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return "'(?<match>(\n|.)*?)'";
return "'(?<match>.*?)'";
}

public function getTokenType(): TokenType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public function getPattern(): string
{
return '(?<match>\"(.|\n)*?\")';
return '(?<match>\".*?\")';
}

public function getTokenType(): TokenType
Expand Down
11 changes: 1 addition & 10 deletions src/Languages/Php/Patterns/DoubleQuoteValuePattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,13 @@
input: 'return "hello"',
output: 'hello',
)]

#[PatternTest(
input: 'return "hello
world"',
output: 'hello
world',
)]
final readonly class DoubleQuoteValuePattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '"(?<match>(\n|.)*?)"';
return '"(?<match>.*?)"';
}

public function getTokenType(): TokenType
Expand Down
10 changes: 1 addition & 9 deletions src/Languages/Php/Patterns/SingleQuoteValuePattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,13 @@
input: "return 'hello';",
output: 'hello',
)]
#[PatternTest(
input: "return 'hello
world';",
output: 'hello
world',
)]
final readonly class SingleQuoteValuePattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return "'(?<match>(\n|.)*?)'";
return "'(?<match>.*?)'";
}

public function getTokenType(): TokenType
Expand Down
8 changes: 4 additions & 4 deletions src/Languages/Php/PhpLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,6 @@ public function getPatterns(): array
new MultilineSingleDocCommentPattern(),
new SinglelineCommentPattern(),

// VALUES
new SingleQuoteValuePattern(),
new DoubleQuoteValuePattern(),

// TYPES
new AttributeTypePattern(),
new ImplementsPattern(),
Expand All @@ -158,6 +154,10 @@ public function getPatterns(): array
new FunctionCallPattern(),
new ConstantPropertyPattern(),
new ConstantNamePattern(),

// VALUES
new SingleQuoteValuePattern(),
new DoubleQuoteValuePattern(),
];
}
}
7 changes: 1 addition & 6 deletions src/Languages/Sql/Patterns/SqlDoubleQuoteValuePattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,13 @@
use Tempest\Highlight\Tokens\TokenType;

#[PatternTest(input: 'bar = "baz"', output: 'baz')]
#[PatternTest(input: 'bar = "ba
z"', output: 'ba
z')]
final readonly class SqlDoubleQuoteValuePattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '\"(?<match>(.|\n)*?)\"';
return '\"(?<match>.*?)\"';
}

public function getTokenType(): TokenType
Expand Down
7 changes: 1 addition & 6 deletions src/Languages/Sql/Patterns/SqlSingleQuoteValuePattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,13 @@
use Tempest\Highlight\Tokens\TokenType;

#[PatternTest(input: "bar = 'baz'", output: 'baz')]
#[PatternTest(input: "bar = 'ba
z'", output: 'ba
z')]
final readonly class SqlSingleQuoteValuePattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '\'(?<match>(.|\n)*?)\'';
return '\'(?<match>.*?)\'';
}

public function getTokenType(): TokenType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public function getPattern(): string
{
return '\"(?<match>(.|\n)*?)\"';
return '\"(?<match>.*?)\"';
}

public function getTokenType(): TokenType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public function getPattern(): string
{
return '\'(?<match>(.|\n)*?)\'';
return '\'(?<match>.*?)\'';
}

public function getTokenType(): TokenType
Expand Down
6 changes: 5 additions & 1 deletion src/Tokens/GroupTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

final readonly class GroupTokens
{
/**
* @param \Tempest\Highlight\Tokens\Token[] $tokens
* @return \Tempest\Highlight\Tokens\Token[]
*/
public function __invoke(array $tokens): array
{
// Sort tokens in the right order
Expand All @@ -25,7 +29,7 @@ public function __invoke(array $tokens): array
$token = $token->cloneWithoutParent();

foreach ($tokens as $compareKey => $compareToken) {
if ($token->contains($compareToken)) {
if ($token->containsOrOverlaps($compareToken)) {
if ($token->canContain($compareToken)) {
$token->addChild($compareToken);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Tokens/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public function equals(Token $other): bool
// && $this->type === $other->type;
}

public function contains(Token $other): bool
public function containsOrOverlaps(Token $other): bool
{
return
! $this->equals($other)
&& $this->start <= $other->start
&& $this->end >= $other->end;
&& $other->start < $this->end;
}

public function addChild(Token $child): void
Expand Down
12 changes: 12 additions & 0 deletions tests/Languages/Php/PhpLanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ public static function data(): array
["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++) {'],
["'namespace ';", "'<span class=\"hl-value\">namespace </span>';"],
[
"// We'll
foo()
// We'll",
'<span class="hl-comment">// We\'ll</span>
<span class="hl-property">foo</span>()
<span class="hl-comment">// We\'ll</span>',
],
];
}
}
4 changes: 2 additions & 2 deletions tests/TokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function test_contains(): void
type: TokenType::VALUE,
);

$this->assertTrue($a->contains($b));
$this->assertFalse($b->contains($a));
$this->assertTrue($a->containsOrOverlaps($b));
$this->assertFalse($b->containsOrOverlaps($a));
}
}
2 changes: 1 addition & 1 deletion tests/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

$markdown = new MarkdownConverter($environment);

$contents = $markdown->convert(file_get_contents(__DIR__ . '/test.md'))->getContent();
$contents = $markdown->convert(file_get_contents(__DIR__ . '/test-big.md'))->getContent();

?>

Expand Down
Loading

0 comments on commit 3989089

Please sign in to comment.