more verbose compiler messages
| From: | Peter Warasin | Date: | Sun, 13 Oct 2002 21:30:20 +0000 |
| Subject: | more verbose compiler messages | ||
| Groups: | php.smarty.dev | ||
| Request: | Send a blank email to php-smarty-dev+get-294@lists.php.net to get a copy of this message | ||
hi
this patch makes the smarty compiler more verbose.
it does:
- check whether all block-tags like if, section, forech, etc. have a
correspond close tag (/if, /section, etc).
- give an error message if delimiter is missed, for example if the designer
forgets to close a template-tag
without the patch smarty results in very ugly error messages which will say
nothing to the designer / developer
please feel free to change the error messages, i am not a wizard of
non-programming languages *g*
the patch:
----------------- snip -------------------
Index: Smarty_Compiler.class.php
===================================================================
RCS file: /repository/smarty/Smarty_Compiler.class.php,v
retrieving revision 1.127
diff -u -r1.127 Smarty_Compiler.class.php
--- Smarty_Compiler.class.php 24 Sep 2002 19:31:31 -0000 1.127
+++ Smarty_Compiler.class.php 13 Oct 2002 19:10:14 -0000
@@ -51,6 +51,11 @@
var $_plugin_info = array(); // keeps track of plugins
to load
var $_init_smarty_vars = false;
+ var $_if_counter = 0; // checks whether every if
has a /if
+ var $_section_counter = 0; // checks whether every
section has a /section
+ var $_strip_counter = 0; // checks whether every
strip has a /strip
+ var $_foreach_counter = 0; // checks whether every
foreach has a /foreach
+ var $_capture_counter = 0; // checks whether every
capture has a /capture
/*======================================================================*\
Function: _compile_file()
@@ -141,11 +146,83 @@
/* Compile the template tags into PHP code. */
$compiled_tags = array();
for ($i = 0, $for_max = count($template_tags); $i < $for_max; $i++)
{
+
+ /* count lines */
+ $line_no_before_text = $this->_current_line_no;
$this->_current_line_no += substr_count($text_blocks[$i],
"\n");
+ $line_no_after_text = $this->_current_line_no;
+
+ /* check if the template programmer forgot a left delimiter */
+ $fail_rdq_pos = strpos($text_blocks[$i],
$this->right_delimiter);
+
+ if ($fail_rdq_pos !== false) {
+
+ /* go on counting linebreaks since we are in the line with
the mistake */
+ $line_no_mistake = $line_no_before_text +
substr_count(substr($text_blocks[$i], 0, $fail_rdq_pos), "\n");
+
+ if ($line_no_before_text == $line_no_mistake) {
+ $this->_syntax_error('standalone right delimiter in
line ' . $line_no_before_text . '. Propably this is a tipo
+ } else {
+ $this->_syntax_error('standalone right delimiter
between line ' . $line_no_before_text . ' and ' . $line_no_mis
+ }
+
+ }
+
+ /* check if the template programmer forgot a right delimiter */
+ $fail_ldq_pos = strpos($template_tags[$i],
$this->left_delimiter);
+
+ if ($fail_ldq_pos !== false) {
+
+ /* search the beginning of the template tag */
+ if (preg_match("!([^\"\s>&{$ldq}]*)!s", $template_tags[$i],
$begin_template_tag) > 0) {
+ $more_verbose = ' The unclosed template tag begins
with: "' . $begin_template_tag[0] . '". ';
+ }
+
+ if ($line_no_after_text == $this->_current_line_no) {
+ $this->_syntax_error('runaway template tag in line ' .
$this->_current_line_no . '.' . $more_verbose);
+ } else {
+ $this->_syntax_error('runaway template tag between line
' . $line_no_after_text . ' and ' . $this->_current_lin
+ }
+
+
+ }
+
+ /* compile */
$compiled_tags[] = $this->_compile_tag($template_tags[$i]);
$this->_current_line_no += substr_count($template_tags[$i],
"\n");
}
+ /* check whether all blocks are closed */
+ if ($this->_if_counter > 0) {
+ $this->_syntax_error($this->_if_counter . ' more "/if" tags
needed');
+ } else if ($this->_if_counter < 0) {
+ $this->_syntax_error(' ' . abs($this->_if_counter) . '
"if"
tags missed');
+ }
+
+ if ($this->_section_counter > 0) {
+ $this->_syntax_error($this->_section_counter . ' more
"/section" tags needed');
+ } else if ($this->_section_counter < 0) {
+ $this->_syntax_error(' ' . abs($this->_section_counter) . '
"section" tags missed');
+ }
+
+ if ($this->_strip_counter > 0) {
+ $this->_syntax_error($this->_strip_counter . ' more "/strip"
tags needed');
+ } else if ($this->_strip_counter < 0) {
+ $this->_syntax_error(' ' . abs($this->_strip_counter) . '
"strip" tags missed');
+ }
+
+ if ($this->_foreach_counter > 0) {
+ $this->_syntax_error($this->_foreach_counter . ' more
"/foreach" tags needed');
+ } else if ($this->_foreach_counter < 0) {
+ $this->_syntax_error(' ' . abs($this->_foreach_counter) . '
"foreach" tags missed');
+ }
+
+ if ($this->_capture_counter > 0) {
+ $this->_syntax_error($this->_capture_counter . ' more
"/capture" tags needed');
+ } else if ($this->_capture_counter < 0) {
+ $this->_syntax_error(' ' . abs($this->_capture_counter) . '
"capture" tags missed');
+ }
+
$template_compiled = '';
/* Interleave the compiled contents and text blocks to get the
final result. */
@@ -251,6 +328,7 @@
return $this->_compile_include_php_tag($tag_args);
case 'if':
+ $this->_if_counter++;
return $this->_compile_if_tag($tag_args);
case 'else':
@@ -260,12 +338,15 @@
return $this->_compile_if_tag($tag_args, true);
case '/if':
+ $this->_if_counter--;
return '<?php endif; ?>';
case 'capture':
+ $this->_capture_counter++;
return $this->_compile_capture_tag(true, $tag_args);
case '/capture':
+ $this->_capture_counter--;
return $this->_compile_capture_tag(false);
case 'ldelim':
@@ -275,6 +356,7 @@
return $this->right_delimiter;
case 'section':
+ $this->_section_counter++;
array_push($this->_sectionelse_stack, false);
return $this->_compile_section_start($tag_args);
@@ -283,12 +365,14 @@
return "<?php endfor; else: ?>";
case '/section':
+ $this->_section_counter--;
if (array_pop($this->_sectionelse_stack))
return "<?php endif; ?>";
else
return "<?php endfor; endif; ?>";
case 'foreach':
+ $this->_foreach_counter++;
array_push($this->_foreachelse_stack, false);
return $this->_compile_foreach_start($tag_args);
break;
@@ -298,6 +382,7 @@
return "<?php endforeach; else: ?>";
case '/foreach':
+ $this->_foreach_counter--;
if (array_pop($this->_foreachelse_stack))
return "<?php endif; ?>";
else
@@ -307,7 +392,10 @@
return $this->_compile_config_load_tag($tag_args);
case 'strip':
+ $this->_strip_counter++;
+ return
$this->left_delimiter.$tag_command.$this->right_delimiter;
case '/strip':
+ $this->_strip_counter--;
return
$this->left_delimiter.$tag_command.$this->right_delimiter;
case 'literal':
@@ -328,6 +416,7 @@
return $this->_compile_insert_tag($tag_args);
default:
+
if ($this->_compile_compiler_tag($tag_command, $tag_args,
$output)) {
return $output;
} else if ($this->_compile_block_tag($tag_command,
$tag_args, $output)) {
------------------ snip ------------------
peter