From 370099b50befed22208ec88a621f633d032c780b Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Thu, 10 Nov 2011 21:53:52 +0100 Subject: Better error messages and a bugfix. * If there are unbalanced open/close Tags in the template, STE will now tell you exactly which Tag was not closed properly (before: the first opening tag during the current balaning check). * It was not possible to access array fields dynamically, e.g. this: $foo[$bar] was not possible before. Now fixed. --- stupid_template_engine.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/stupid_template_engine.php b/stupid_template_engine.php index e55023d..ba840f0 100644 --- a/stupid_template_engine.php +++ b/stupid_template_engine.php @@ -57,7 +57,7 @@ class VariableNode extends ASTNode if($node instanceof TextNode) return "\"" . escape_text($node->text) . "\""; else if($node instanceof VariableNode) - return $node->transcompile; + return $node->transcompile(); }, $af ) ). ']'; @@ -244,26 +244,30 @@ function mk_ast($code, $tpl, $err_off) if($matches[1][0] != "/") { - $tags_open = 1; $off = 0; $last_tag_start = 0; + $tagstack = array(array($tag->name, $tag->offset)); while(preg_match("/\\<((?:\\s*)|(?:\\s*\\/\\s*))ste:([a-zA-Z0-9_]*)(?:\\s+(?:[a-zA-Z0-9_]+)=(?:(?:\"(?:.*?)(?/s", $code, $matches, PREG_OFFSET_CAPTURE, $off) > 0) /* RegEx from hell! Matches all Tags. Opening, closing and self-closing ones. */ { if(trim($matches[3][0]) != "/") { $closingtag = trim($matches[1][0]); if($closingtag[0] == "/") - $tags_open--; + { + list($matching_opentag, $mo_off) = array_pop($tagstack); + if($matching_opentag != $matches[2][0]) + throw new ParseCompileError("Parse Error: Missing closing \"ste:$matching_opentag\"-Tag.", $tpl, $mo_off + $err_off); + } else - $tags_open++; + $tagstack[] = array($matches[2][0], $matches[0][1]); } $last_tag_start = $matches[0][1]; $off = $last_tag_start + strlen($matches[0][0]); - if($tags_open == 0) + if(empty($tagstack)) break; } - if(($tags_open != 0) or ($tag->name != $matches[2][0])) + if((!empty($tagstack)) or ($tag->name != $matches[2][0])) throw new ParseCompileError("Parse Error: Missing closing \"ste:" . $tag->name . "\"-Tag.", $tpl, $tag->offset); if($tag->name == "rawtext") -- cgit v1.2.3-54-g00ecf