From 454b4c63e980df9315afcd861acb6ae56ce9bd01 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sat, 23 Jul 2022 22:37:25 -0400 Subject: [PATCH] Better error message. --- src/Mustache/Parser.php | 2 +- src/Mustache/Tokenizer.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Mustache/Parser.php b/src/Mustache/Parser.php index bf8bb40..1ad7e6d 100644 --- a/src/Mustache/Parser.php +++ b/src/Mustache/Parser.php @@ -334,7 +334,7 @@ class Mustache_Parser $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); diff --git a/src/Mustache/Tokenizer.php b/src/Mustache/Tokenizer.php index d97e36f..d96f129 100644 --- a/src/Mustache/Tokenizer.php +++ b/src/Mustache/Tokenizer.php @@ -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'; @@ -358,6 +374,7 @@ class Mustache_Tokenizer return $end + $this->ctagLen - 1; } + private function throwUnclosedTagException() { $name = trim($this->buffer); @@ -376,4 +393,16 @@ class Mustache_Tokenizer 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'; + } }