Skip to content

Commit

Permalink
Better error message.
Browse files Browse the repository at this point in the history
  • Loading branch information
bobthecow committed Aug 23, 2022
1 parent f6c45e8 commit 454b4c6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Mustache/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ private function ensureTagAllowsDynamicNames(array $token)
$msg = sprintf(
'Invalid dynamic name: %s in %s tag',
$token[Mustache_Tokenizer::NAME],
$token[Mustache_Tokenizer::TYPE]
Mustache_Tokenizer::getTagName($token[Mustache_Tokenizer::TYPE])
);

throw new Mustache_Exception_SyntaxException($msg, $token);
Expand Down
29 changes: 29 additions & 0 deletions src/Mustache/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ class Mustache_Tokenizer
self::T_BLOCK_VAR => true,
);

private static $tagNames = array(
self::T_SECTION => 'section',
self::T_INVERTED => 'inverted section',
self::T_END_SECTION => 'section end',
self::T_COMMENT => 'comment',
self::T_PARTIAL => 'partial',
self::T_PARENT => 'parent',
self::T_DELIM_CHANGE => 'set delimiter',
self::T_ESCAPED => 'variable',
self::T_UNESCAPED => 'unescaped variable',
self::T_UNESCAPED_2 => 'unescaped variable',
self::T_PRAGMA => 'pragma',
self::T_BLOCK_VAR => 'block variable',
self::T_BLOCK_ARG => 'block variable',
);

// Token properties
const TYPE = 'type';
const NAME = 'name';
Expand Down Expand Up @@ -358,6 +374,7 @@ private function addPragma($text, $index)
return $end + $this->ctagLen - 1;
}


private function throwUnclosedTagException()
{
$name = trim($this->buffer);
Expand All @@ -376,4 +393,16 @@ private function throwUnclosedTagException()
self::INDEX => $this->seenTag - $this->otagLen,
));
}

/**
* Get the human readable name for a tag type.
*
* @param string $tagType One of the tokenizer T_* constants
*
* @return string
*/
static function getTagName($tagType)
{
return isset(self::$tagNames[$tagType]) ? self::$tagNames[$tagType] : 'unknown';
}
}

0 comments on commit 454b4c6

Please sign in to comment.