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
166 changes: 83 additions & 83 deletions COMPATIBILITY.md

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/Grammar/BeginEndPattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

class BeginEndPattern implements HasContentNameInterface, PatternInterface
{
/**
* Whether every nested pattern includes a rule or grammar that doesn't exist.
*/
private ?bool $missing = null;

public function __construct(
public int $id,
public Regex $begin,
Expand All @@ -21,6 +26,7 @@ public function __construct(
public array $captures = [],
public array $patterns = [],
public bool $injection = false,
public bool $applyEndPatternLast = false,
) {}

public function getScopeName(array $captures): ?string
Expand Down Expand Up @@ -53,6 +59,11 @@ public function getContentName(array $captures): ?string
*/
public function compile(ParsedGrammar $grammar, GrammarRepositoryInterface $grammars, bool $allowA, bool $allowG): array
{
// Like vscode-textmate, this is decided once, the first time the rule is compiled.
if ($this->missing ??= IncludePattern::allMissing($this->patterns, $grammar, $grammars)) {
return [];
}

return [
[$this, $this->begin->get($allowA, $allowG)],
];
Expand All @@ -73,6 +84,7 @@ public function createEndPattern(MatchedPattern $matched): EndPattern
captures: $this->captures,
patterns: $this->patterns,
injection: $this->injection,
applyEndPatternLast: $this->applyEndPatternLast,
);
}

Expand Down
12 changes: 11 additions & 1 deletion src/Grammar/BeginWhilePattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

class BeginWhilePattern implements HasContentNameInterface, PatternInterface
{
/**
* Whether every nested pattern includes a rule or grammar that doesn't exist.
*/
private ?bool $missing = null;

public function __construct(
public int $id,
public Regex $begin,
Expand All @@ -34,7 +39,7 @@ public function getScopeName(array $captures): ?string

public function captures(): array
{
return count(array_filter($this->beginCaptures)) > 0 ? array_filter($this->beginCaptures) : $this->captures;
return count(array_filter($this->beginCaptures)) > 0 ? $this->beginCaptures : $this->captures;
}

public function getContentName(array $captures): ?string
Expand All @@ -53,6 +58,11 @@ public function getContentName(array $captures): ?string
*/
public function compile(ParsedGrammar $grammar, GrammarRepositoryInterface $grammars, bool $allowA, bool $allowG): array
{
// Like vscode-textmate, this is decided once, the first time the rule is compiled.
if ($this->missing ??= IncludePattern::allMissing($this->patterns, $grammar, $grammars)) {
return [];
}

return [
[$this, $this->begin->get($allowA, $allowG)],
];
Expand Down
9 changes: 5 additions & 4 deletions src/Grammar/EndPattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function __construct(
public array $captures = [],
public array $patterns = [],
public bool $injection = false,
public bool $applyEndPatternLast = false,
) {}

public function getScopeName(array $captures): ?string
Expand All @@ -42,15 +43,15 @@ public function captures(): array
*/
public function compile(ParsedGrammar $grammar, GrammarRepositoryInterface $grammars, bool $allowA, bool $allowG): array
{
$compiled = [
[$this, $this->end->get($allowA, $allowG, $this->begin->matches)],
];
$end = [$this, $this->end->get($allowA, $allowG, $this->begin->matches)];
$compiled = [];

foreach ($this->patterns as $pattern) {
$compiled = array_merge($compiled, $pattern->compile($grammar, $grammars, $allowA, $allowG));
}

return $compiled;
// The end pattern wins a tie with the nested patterns, unless `applyEndPatternLast` is set.
return $this->applyEndPatternLast ? [...$compiled, $end] : [$end, ...$compiled];
}

public function getId(): int
Expand Down
Loading
Loading