Documentation

Class: TagMap

Full name: Clarity\Engine\Compiler\TagMap

Every tag in a template source, in source order, with byte offsets.

This is the one place the compiler is allowed to look for structure. Before it existed, the pre-scan passes (inheritance, blocks, macros, raw PHP) ran PCRE over the raw source, so a tag inside a comment or inside a string literal was indistinguishable from a real one — a commented-out {% extends %} switched the template to child mode, and {{ "{% parent %}" }} leaked an internal source-map marker into the HTML.

A TagMap is built from the tokenizer, which is quote-, brace- and comment-aware, so those cases cannot arise. The pre-scan then asks questions ("where is the first extends?", "which endblock closes this block?") and cuts the source by byte range.

Cutting by byte range rather than rebuilding text is deliberate: the untouched bytes stay byte-identical, so every line number the compiler later reports is still the author's own. Removing a tag means blanking its non-newline bytes, which keeps the line count and the offsets stable too.

Public methods

fromSource() ·

public static function fromSource(string $source): Clarity\Engine\Compiler\TagMap

Scan $source into a TagMap.

A dedicated Tokenizer instance is used so this never disturbs the compiler's own tokenizer, whose escaping context is carried across segments as the body is compiled. Scanning here is purely lexical.

Parameters

Name Type Default Description
$source string -

Return value

Throws


source() ·

public function source(): string

Return value

  • Type: string

all() ·

public function all(): array

Return value

  • Type: array

blocks() ·

public function blocks(array $keywords): array

The block tags whose keyword is one of $keywords, in source order.

Parameters

Name Type Default Description
$keywords array -

Return value

  • Type: array

first() ·

public function first(string $keyword): Clarity\Engine\Compiler\Tag|null

The first block tag with this keyword, or null.

Parameters

Name Type Default Description
$keyword string -

Return value

  • Type: Tag|null

allOf() ·

public function allOf(string $keyword): array

Every block tag with this keyword.

@return list<Tag>

Parameters

Name Type Default Description
$keyword string -

Return value

  • Type: array

indexOf() ·

public function indexOf(Clarity\Engine\Compiler\Tag $tag): int

Index of $tag in all(), or -1.

Parameters

Name Type Default Description
$tag Tag -

Return value

  • Type: int

pairs() ·

public function pairs(string $opener, string $closer, bool $requireAll = true): array

Pair each opener with its closer, honouring nesting.

Returns [openIndex, closeIndex] pairs keyed by position in all(). {% block %} nested in {% block %} pairs with its own closer, which a flat search for the next endblock would get wrong.

By default an opener left unpaired is reported, because for block and macro that is always the author's mistake. Pass $requireAll = false when an unpaired opener is legal — {% call %} without {% endcall %} is the plain macro-call form — and the survivors are simply left out.

Parameters

Name Type Default Description
$opener string -
$closer string -
$requireAll bool true

Return value

  • Type: array
  • Description: [openIdx, closeIdx, openTag, closeTag]

without() ·

public function without(array $ranges): string

A copy of the source with the given byte ranges removed.

The bytes inside each range are replaced by spaces, except newlines, which are kept. Two consequences, both wanted:

  • every surviving byte keeps its offset, so the tag list stays valid;
  • every line keeps its number, so a later error still points at the line the author wrote.

Removing a tag leaves its surrounding text adjacent, which is what the inheritance merge and the macro extractor both need.

Parameters

Name Type Default Description
$ranges array - half-open [start, end) offsets

Return value

  • Type: string

withoutTag() ·

public function withoutTag(Clarity\Engine\Compiler\Tag $tag): string

The source with a single tag removed (see without()).

Parameters

Name Type Default Description
$tag Tag -

Return value

  • Type: string

between() ·

public function between(Clarity\Engine\Compiler\Tag $open, Clarity\Engine\Compiler\Tag $close): string

The inner source of a pair: everything between the opener and its closer.

Used for block bodies. Offsets are absolute, so a caller can slice further inside the returned text only by re-basing — prefer innerOf() when the offsets matter.

Parameters

Name Type Default Description
$open Tag -
$close Tag -

Return value

  • Type: string

innerOf() ·

public function innerOf(Clarity\Engine\Compiler\Tag $open, Clarity\Engine\Compiler\Tag $close): array

The inner source of a pair with its absolute offset, for callers that need to map a position inside it back to the template.

Parameters

Name Type Default Description
$open Tag -
$close Tag -

Return value

  • Type: array

Back to the Index