- Added property indirect references of properties
[haanga.git] / lib / Haanga / Compiler.php
blob6367d91a9d0dc28a4f969db34999ae6da341714d
1 <?php
2 /*
3 +---------------------------------------------------------------------------------+
4 | Copyright (c) 2010 César Rodas and Menéame Comunicacions S.L. |
5 +---------------------------------------------------------------------------------+
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met: |
8 | 1. Redistributions of source code must retain the above copyright |
9 | notice, this list of conditions and the following disclaimer. |
10 | |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | 3. All advertising materials mentioning features or use of this software |
16 | must display the following acknowledgement: |
17 | This product includes software developed by César D. Rodas. |
18 | |
19 | 4. Neither the name of the César D. Rodas nor the |
20 | names of its contributors may be used to endorse or promote products |
21 | derived from this software without specific prior written permission. |
22 | |
23 | THIS SOFTWARE IS PROVIDED BY CÉSAR D. RODAS ''AS IS'' AND ANY |
24 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
25 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
26 | DISCLAIMED. IN NO EVENT SHALL CÉSAR D. RODAS BE LIABLE FOR ANY |
27 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
28 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
29 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
30 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
31 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
32 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE |
33 +---------------------------------------------------------------------------------+
34 | Authors: César Rodas <crodas@php.net> |
35 +---------------------------------------------------------------------------------+
38 class Haanga_Compiler
41 // properties {{{
42 protected static $block_var=NULL;
43 protected $generator;
44 protected $forloop = array();
45 protected $forid = 0;
46 protected $sub_template = FALSE;
47 protected $name;
48 protected $check_function = FALSE;
49 protected $blocks=array();
50 protected $line=0;
51 protected $file;
52 /**
53 * number of blocks :-)
55 protected $in_block=0;
56 /**
57 * output buffers :-)
59 protected $ob_start=0;
60 protected $append;
61 protected $prepend_op;
62 /**
63 * Context at compile time
65 protected $context;
66 /**
67 * Table which contains all variables
68 * aliases defined in the template
70 protected $var_alias;
71 /**
72 * Flag the current variable as safe. This means
73 * that escape won't be called if autoescape is
74 * activated (which is activated by default)
76 public $var_is_safe=FALSE;
77 public $safes;
79 /* compiler options */
80 static protected $autoescape = TRUE;
81 static protected $if_empty = TRUE;
82 static protected $dot_as_object = TRUE;
83 static protected $strip_whitespace = FALSE;
84 static protected $is_exec_enabled = FALSE;
85 static protected $global_context = array();
86 static protected $echo_concat = '.';
87 static protected $enable_load = TRUE;
89 /**
90 * Debug file
92 protected $debug;
93 // }}}
95 function __construct()
97 $this->generator = new Haanga_Generator_PHP;
98 if (self::$block_var===NULL) {
99 self::$block_var = '{{block.'.md5('super').'}}';
103 // getOption($option) {{{
104 public static function getOption($option)
106 $value = NULL;
107 switch (strtolower($option)) {
108 case 'enable_load':
109 $value = self::$enable_load;
110 break;
111 case 'if_empty':
112 $value = self::$if_empty;
113 break;
114 case 'autoescape':
115 $value = self::$autoescape;
116 break;
117 case 'dot_as_object':
118 $value = self::$dot_as_object;
119 break;
120 case 'echo_concat':
121 $value = self::$echo_concat;
122 break;
123 case 'strip_whitespace':
124 $value = self::$strip_whitespace;
125 break;
126 case 'is_exec_enabled':
127 case 'allow_exec':
128 $value = self::$is_exec_enabled;
129 break;
130 case 'global':
131 $value = self::$global_context;
132 break;
134 return $value;
136 // }}}
138 // setOption($option, $value) {{{
140 * Set Compiler option.
142 * @return void
144 public static function setOption($option, $value)
146 switch (strtolower($option)) {
147 case 'if_empty':
148 self::$if_empty = (bool)$value;
149 break;
150 case 'enable_load':
151 self::$enable_load = (bool)$value;
152 case 'echo_concat':
153 if ($value == '.' || $value == ',') {
154 self::$echo_concat = $value;
156 break;
158 case 'autoescape':
159 self::$autoescape = (bool)$value;
160 break;
161 case 'dot_as_object':
162 self::$dot_as_object = (bool)$value;
163 break;
164 case 'strip_whitespace':
165 self::$strip_whitespace = (bool)$value;
166 break;
167 case 'is_exec_enabled':
168 case 'allow_exec':
169 self::$is_exec_enabled = (bool)$value;
170 break;
171 case 'global':
172 if (!is_array($value)) {
173 $value = array($value);
175 self::$global_context = $value;
176 break;
179 // }}}
181 // setDebug($file) {{{
182 function setDebug($file)
184 $this->debug = $file;
186 // }}}
188 // reset() {{{
189 function reset()
191 foreach (array_keys(get_object_vars($this)) as $key) {
192 if (isset($avoid_cleaning[$key])) {
193 continue;
195 $this->$key = NULL;
197 $this->generator = new Haanga_Generator_PHP;
198 $this->blocks = array();
199 $this->cycle = array();
201 // }}}
203 // get_template_name() {{{
204 final function get_template_name()
206 return $this->name;
208 // }}}
210 // Set template name {{{
211 function set_template_name($path)
213 $file = basename($path);
214 $pos = strpos($file,'.');
215 return ($this->name = substr($file, 0, $pos));
217 // }}}
219 // get_function_name(string $name) {{{
220 function get_function_name($name)
222 return "{$name}_template";
224 // }}}
226 // Compile ($code, $name=NULL) {{{
227 final function compile($code, $name=NULL, $file=NULL)
229 $this->name = $name;
231 if (count(self::$global_context) > 0) {
232 /* add global variables (if any) to the current context */
233 foreach (self::$global_context as $var) {
234 $this->set_context($var, $GLOBALS[$var]);
238 $parsed = Haanga_Compiler_Tokenizer::init($code, $this, $file);
239 $code = "";
240 $this->subtemplate = FALSE;
242 $body = new Haanga_AST;
243 $this->prepend_op = hcode();
245 if (isset($parsed[0]) && $parsed[0]['operation'] == 'base') {
246 /* {% base ... %} found */
247 $base = $parsed[0][0];
248 $code .= $this->get_base_template($base);
249 unset($parsed[0]);
252 if (defined('HAANGA_VERSION')) {
253 $body->decl('HAANGA_VERSION', HAANGA_VERSION);
256 if ($name) {
257 $func_name = $this->get_function_name($name);
258 if ($this->check_function) {
259 $body->do_if(hexpr(hexec('function_exists', $func_name), '===', FALSE));
261 if (!empty($this->file)) {
262 $body->comment("Generated from ".$this->file);
265 $body->declare_function($func_name);
267 if (count(self::$global_context) > 0) {
268 $body->do_global(self::$global_context);
272 $body->do_exec('extract', hvar('vars'));
273 $body->do_if(hexpr(hvar('return'), '==', TRUE));
274 $body->do_exec('ob_start');
275 $body->do_endif();
278 $this->generate_op_code($parsed, $body);
279 if ($this->subtemplate) {
280 $expr = $this->expr_call_base_template();
281 $this->do_print($body, $expr);
284 $body->do_if(hexpr(hvar('return'), '==', TRUE));
285 $body->do_return(hexec('ob_get_clean'));
286 $body->do_endif();
288 if ($name) {
289 $body->do_endfunction();
290 if ($this->check_function) {
291 $body->do_endif();
295 if ($this->prepend_op->stack_size() > 0) {
296 $this->prepend_op->append_ast($body);
297 $body = $this->prepend_op;
300 $op_code = $body->getArray(TRUE);
303 $code .= $this->generator->getCode($op_code);
304 if (!empty($this->append)) {
305 $code .= $this->append;
308 if (!empty($this->debug)) {
309 $op_code['php'] = $code;
310 file_put_contents($this->debug, print_r($op_code, TRUE), LOCK_EX);
312 return $code;
314 // }}}
316 // compile_file($file) {{{
318 * Compile a file
320 * @param string $file File path
321 * @param bool $safe Whether or not add check if the function is already defined
323 * @return Generated PHP code
325 final function compile_file($file, $safe=FALSE, $context=array())
327 if (!is_readable($file)) {
328 throw new Haanga_Compiler_Exception("$file is not a file");
331 $this->_base_dir = dirname($file);
332 $this->file = realpath($file);
333 $this->line = 0;
334 $this->check_function = $safe;
335 $this->context = $context;
336 $name = $this->set_template_name($file);
337 return $this->compile(file_get_contents($file), $name, $file);
339 // }}}
341 // getOpCodes($code, $file='') {{{
343 * Compile the $code and return the "opcodes"
344 * (the Abstract syntax tree).
346 * @param string $code Template content
347 * @param string $file File path (used for erro reporting)
349 * @return Haanga_AST
352 public function getOpCodes($code, $file)
354 $oldfile = $this->file;
355 $this->file = $file;
356 $parsed = Haanga_Compiler_Tokenizer::init($code, $this, $file);
357 $body = new Haanga_AST;
358 if (isset($parsed[0]) && $parsed[0]['operation'] == 'base') {
359 $this->Error("{% base is not supported on inlines %}");
361 $body = new Haanga_AST;
362 $this->generate_op_code($parsed, $body);
363 $this->file = $oldfile;
364 return $body;
366 // }}}
368 // Error($errtxt) {{{
370 * Throw an exception and appends information about the template (the path and
371 * the last processed line).
375 public function Error($err)
377 throw new Haanga_Compiler_Exception("{$err} in {$this->file}:$this->line");
379 // }}}
381 // is_expr methods {{{
382 function is_var_filter($cmd)
384 return isset($cmd['var_filter']);
387 // }}}
389 // expr_call_base_template() {{{
391 * Generate code to call base template
394 function expr_call_base_template()
396 return hexec(
397 $this->get_function_name($this->subtemplate),
398 hvar('vars'), TRUE,
399 hvar('blocks')
402 // }}}
404 // get_base_template($base) {{{
406 * Handle {% base "" %} definition. By default only
407 * static (string) are supported, but this can be overrided
408 * on subclasses.
410 * This method load the base class, compile it and return
411 * the generated code.
413 * @param array $base Base structure
415 * @return string Generated source code
417 function get_base_template($base)
419 if (!Haanga_AST::is_str($base)) {
420 $this->Error("Dynamic inheritance is not supported for compilated templates");
422 $file = $base['string'];
423 list($this->subtemplate, $new_code) = $this->compile_required_template($file);
424 return $new_code."\n\n";
426 // }}}
428 // {% base "foo.html" %} {{{
429 protected function generate_op_base()
431 $this->Error("{% base %} can be only as first statement");
433 // }}}
435 // Main Loop {{{
436 protected function generate_op_code($parsed, &$body)
438 if (!is_array($parsed)) {
439 $this->Error("Invalid \$parsed array");
441 foreach ($parsed as $op) {
442 if (!is_array($op)) {
443 continue;
445 if (!isset($op['operation'])) {
446 $this->Error("Malformed array:".print_r($op, TRUE));
448 if (isset($op['line'])) {
449 $this->line = $op['line'];
452 if ($this->subtemplate && $this->in_block == 0 && $op['operation'] != 'block') {
453 /* ignore most of tokens in subtemplates */
454 continue;
457 $method = "generate_op_".$op['operation'];
458 if (!is_callable(array($this, $method))) {
459 $this->Error("Compiler: Missing method $method");
461 $this->$method($op, $body);
464 // }}}
466 // Check the current expr {{{
467 protected function check_expr(&$expr)
469 if (Haanga_AST::is_expr($expr)) {
470 if ($expr['op_expr'] == 'in') {
471 for ($id=0; $id < 2; $id++) {
472 if ($this->is_var_filter($expr[$id])) {
473 $expr[$id] = $this->get_filtered_var($expr[$id]['var_filter'], $var);
476 if (Haanga_AST::is_str($expr[1])) {
477 $expr = hexpr(hexec('strpos', $expr[1], $expr[0]), '!==', FALSE);
478 } else {
479 $expr = hexpr(
480 hexpr_cond(
481 hexec('is_array', $expr[1]),
482 hexec('array_search', $expr[0], $expr[1]),
483 hexec('strpos', $expr[1], $expr[0])
485 ,'!==', FALSE
489 if (is_object($expr)) {
490 $expr = $expr->getArray();
492 $this->check_expr($expr[0]);
493 $this->check_expr($expr[1]);
494 } else if (is_array($expr)) {
495 if ($this->is_var_filter($expr)) {
496 $expr = $this->get_filtered_var($expr['var_filter'], $var);
497 } else if (isset($expr['args'])) {
498 /* check every arguments */
499 foreach ($expr['args'] as &$v) {
500 $this->check_expr($v);
502 unset($v);
503 } else if (isset($expr['expr_cond'])) {
504 /* Check expr conditions */
505 $this->check_expr($expr['expr_cond']);
506 $this->check_expr($expr['true']);
507 $this->check_expr($expr['false']);
511 // }}}
513 // ifequal|ifnot equal <var_filtered|string|number> <var_fitlered|string|number> ... else ... {{{
514 protected function generate_op_ifequal($details, &$body)
516 $if['expr'] = hexpr($details[1], $details['cmp'], $details[2])->getArray();
517 $if['body'] = $details['body'];
518 if (isset($details['else'])) {
519 $if['else'] = $details['else'];
521 $this->generate_op_if($if, $body);
523 // }}}
525 // {% if <expr> %} HTML {% else %} TWO {% endif $} {{{
526 protected function generate_op_if($details, &$body)
528 if (self::$if_empty && $this->is_var_filter($details['expr']) && count($details['expr']['var_filter']) == 1) {
529 /* if we are doing if <Variable> it should check
530 if it exists without throw any warning */
531 $expr = $details['expr'];
532 $expr['var_filter'][] = 'empty';
534 $variable = $this->get_filtered_var($expr['var_filter'], $var);
536 $details['expr'] = hexpr($variable, '===', FALSE)->getArray();
538 $this->check_expr($details['expr']);
539 $expr = Haanga_AST::fromArrayGetAST($details['expr']);
540 $body->do_if($expr);
541 $this->generate_op_code($details['body'], $body);
542 if (isset($details['else'])) {
543 $body->do_else();
544 $this->generate_op_code($details['else'], $body);
546 $body->do_endif();
548 // }}}
550 // Override template {{{
551 protected function compile_required_template($file)
553 if (!is_file($file)) {
554 if (isset($this->_base_dir)) {
555 $file = $this->_base_dir.'/'.$file;
558 if (!is_file($file)) {
559 $this->Error("can't find {$file} file template");
561 $class = get_class($this);
562 $comp = new $class;
563 $comp->reset();
564 $code = $comp->compile_file($file, $this->check_function);
565 return array($comp->get_template_name(), $code);
567 // }}}
569 // include "file.html" | include <var1> {{{
570 protected function generate_op_include($details, &$body)
572 if (!$details[0]['string']) {
573 $this->Error("Dynamic inheritance is not supported for compilated templates");
575 list($name,$code) = $this->compile_required_template($details[0]['string']);
576 $this->append .= "\n\n{$code}";
577 $this->do_print($body,
578 hexec($this->get_function_name($name),
579 hvar('vars'), TRUE, hvar('blocks'))
582 // }}}
584 // Handle HTML code {{{
585 protected function generate_op_html($details, &$body)
587 $string = Haanga_AST::str($details['html']);
588 $this->do_print($body, $string);
590 // }}}
592 // get_var_filtered {{{
594 * This method handles all the filtered variable (piped_list(X)'s
595 * output in the parser.
598 * @param array $variable (Output of piped_list(B) (parser))
599 * @param array &$varname Variable name
600 * @param bool $accept_string TRUE is string output are OK (ie: block.parent)
602 * @return expr
605 function get_filtered_var($variable, &$varname, $accept_string=FALSE)
607 $this->var_is_safe = FALSE;
609 if (count($variable) > 1) {
610 $count = count($variable);
611 $target = $this->generate_variable_name($variable[0]);
613 if (!Haanga_AST::is_var($target)) {
614 /* block.super can't have any filter */
615 $this->Error("This variable can't have any filter");
618 for ($i=1; $i < $count; $i++) {
619 $func_name = $variable[$i];
620 if ($func_name == 'escape') {
621 /* to avoid double cleaning */
622 $this->var_is_safe = TRUE;
624 $args = array(isset($exec) ? $exec : $target);
625 $exec = $this->do_filtering($func_name, $args);
627 unset($variable);
628 $varname = $args[0];
629 $details = $exec;
630 } else {
631 $details = $this->generate_variable_name($variable[0]);
632 $varname = $variable[0];
634 if (!Haanga_AST::is_var($details) && !$accept_string) {
635 /* generate_variable_name didn't replied a variable, weird case
636 currently just used for {{block.super}}.
638 $this->Error("Invalid variable name {$variable[0]}");
642 return $details;
644 // }}}
646 // generate_op_print_var {{{
648 * Generate code to print a variable with its filters, if there is any.
650 * All variable (except those flagged as |safe) are automatically
651 * escaped if autoescape is "on".
654 protected function generate_op_print_var($details, &$body)
657 $details = $this->get_filtered_var($details['variable'], $variable, TRUE);
659 if (!Haanga_AST::is_var($details) && !Haanga_AST::is_exec($details)) {
660 /* generate_variable_name didn't replied a variable, weird case
661 currently just used for {{block.super}}.
663 $this->do_print($body, $details);
664 return;
667 if (!$this->is_safe($details) && self::$autoescape) {
668 $args = array($details);
669 $details = $this->do_filtering('escape', $args);
673 if (is_array($details)) {
674 $details = Haanga_AST::fromArrayGetAST($details);
676 $this->do_print($body, $details);
678 // }}}
680 // {# something #} {{{
681 protected function generate_op_comment($details, &$body)
683 /* comments are annoying */
684 //$body->comment($details['comment']);
686 // }}}
688 // {% block 'name' %} ... {% endblock %} {{{
689 protected function generate_op_block($details, &$body)
691 if (is_array($details['name'])) {
692 $name = "";
693 foreach ($details['name'] as $part) {
694 if (is_string($part)) {
695 $name .= "{$part}";
696 } else if (is_array($part)) {
697 if (Haanga_AST::is_str($part)) {
698 $name .= "{$part['string']}";
699 } elseif (isset($part['object'])) {
700 $name .= "{$part['object']}";
701 } else {
702 $this->Error("Invalid blockname");
705 $name .= ".";
707 $details['name'] = substr($name, 0, -1);
709 $this->in_block++;
710 $this->blocks[] = $details['name'];
711 $block_name = hvar('blocks', $details['name']);
713 $this->ob_start($body);
714 $buffer_var = 'buffer'.$this->ob_start;
716 $content = hcode();
717 $this->generate_op_code($details['body'], $content);
719 $body->append_ast($content);
720 $this->ob_start--;
722 $buffer = hvar($buffer_var);
724 /* {{{ */
726 * isset previous block (parent block)?
727 * TRUE
728 * has reference to self::$block_var ?
729 * TRUE
730 * replace self::$block_var for current block value (buffer)
731 * FALSE
732 * print parent block
733 * FALSE
734 * print current block
737 $declare = hexpr_cond(
738 hexec('isset', $block_name),
739 hexpr_cond(
740 hexpr(hexec('strpos', $block_name, self::$block_var), '===', FALSE),
741 $block_name,
742 hexec('str_replace', self::$block_var, $buffer, $block_name)
743 ), $buffer);
744 /* }}} */
746 if (!$this->subtemplate) {
747 $this->do_print($body, $declare);
748 } else {
749 $body->decl($block_name, $declare);
750 if ($this->in_block > 1) {
751 $this->do_print($body, $block_name);
754 array_pop($this->blocks);
755 $this->in_block--;
758 // }}}
760 // regroup <var1> by <field> as <foo> {{{
761 protected function generate_op_regroup($details, &$body)
763 $body->comment("Temporary sorting");
765 $array = $this->get_filtered_var($details['array'], $varname);
767 if (Haanga_AST::is_exec($array)) {
768 $varname = hvar($details['as']);
769 $body->decl($varname, $array);
771 $var = hvar('item', $details['row']);
773 $body->decl('temp_group', array());
775 $body->do_foreach($varname, 'item', NULL,
776 hcode()->decl(hvar('temp_group', $var, NULL), hvar('item'))
779 $body->comment("Proper format");
780 $body->decl($details['as'], array());
781 $body->do_foreach('temp_group', 'item', 'group',
782 hcode()->decl(
783 hvar($details['as'], NULL),
784 array("grouper" => hvar('group'), "list" => hvar('item'))
787 $body->comment("Sorting done");
789 // }}}
791 // variable context {{{
793 * Variables context
795 * These two functions are useful to detect if a variable
796 * separated by dot (foo.bar) is an array or object. To avoid
797 * overhead we decide it at compile time, rather than
798 * ask over and over at rendering time.
800 * foo.bar:
801 * + If foo exists at compile time,
802 * and it is an array, it would be foo['bar']
803 * otherwise it'd be foo->bar.
804 * + If foo don't exists at compile time,
805 * it would be foo->bar if the compiler option
806 * dot_as_object is TRUE (by default) otherwise
807 * it'd be foo['bar']
809 * @author crodas
810 * @author gallir (ideas)
813 function set_context($varname, $value)
815 $this->context[$varname] = $value;
818 function get_context($variable)
820 if (!is_array($variable)) {
821 $variable = array($variable);
823 $varname = $variable[0];
824 if (isset($this->context[$varname])) {
825 if (count($variable) == 1) {
826 return $this->context[$varname];
828 $var = & $this->context[$varname];
829 foreach ($variable as $id => $part) {
830 if ($id != 0) {
831 if (is_array($part) && isset($part['object'])) {
832 if (is_array($part['object']) && isset($part['object']['var'])) {
833 /* object $foo->$bar */
834 $name = $part['object']['var'];
835 $name = $this->get_context($name);
836 if (!isset($var->$name)) {
837 return NULL;
839 $var = &$var->$name;
840 } else {
841 if (!isset($var->$part['object'])) {
842 return NULL;
844 $var = &$var->$part['object'];
846 } else if (is_object($var)) {
847 if (!isset($var->$part)) {
848 return NULL;
850 $var = &$var->$part;
851 } else {
852 if (!isset($var[$part])) {
853 return NULL;
855 $var = &$var[$part];
859 $variable = $var;
860 unset($var);
861 return $variable;
864 return NULL;
867 function var_is_object(Array $variable, $default=NULL)
869 $varname = $variable[0];
870 switch ($varname) {
871 case 'GLOBALS':
872 case '_SERVER':
873 case '_GET':
874 case '_POST':
875 case '_FILES':
876 case '_COOKIE':
877 case '_SESSION':
878 case '_REQUEST':
879 case '_ENV':
880 case 'forloop':
881 case 'block':
882 return FALSE; /* these are arrays */
885 $variable = $this->get_context($variable);
886 if (isset($variable)) {
887 return is_object($variable);
890 return $default===NULL ? self::$dot_as_object : $default;
892 // }}}
894 // Get variable name {{{
895 protected function generate_variable_name($variable)
897 if (is_array($variable)) {
898 switch ($variable[0]) {
899 case 'forloop':
900 if (!$this->forid) {
901 $this->Error("Invalid forloop reference outside of a loop");
904 switch ($variable[1]['object']) {
905 case 'counter':
906 $this->forloop[$this->forid]['counter'] = TRUE;
907 $variable = 'forcounter1_'.$this->forid;
908 break;
909 case 'counter0':
910 $this->forloop[$this->forid]['counter0'] = TRUE;
911 $variable = 'forcounter0_'.$this->forid;
912 break;
913 case 'last':
914 $this->forloop[$this->forid]['counter'] = TRUE;
915 $this->forloop[$this->forid]['last'] = TRUE;
916 $variable = 'islast_'.$this->forid;
917 break;
918 case 'first':
919 $this->forloop[$this->forid]['first'] = TRUE;
920 $variable = 'isfirst_'.$this->forid;
921 break;
922 case 'revcounter':
923 $this->forloop[$this->forid]['revcounter'] = TRUE;
924 $variable = 'revcount_'.$this->forid;
925 break;
926 case 'revcounter0':
927 $this->forloop[$this->forid]['revcounter0'] = TRUE;
928 $variable = 'revcount0_'.$this->forid;
929 break;
930 case 'parentloop':
931 unset($variable[1]);
932 $this->forid--;
933 $variable = $this->generate_variable_name(array_values($variable));
934 $variable = $variable['var'];
935 $this->forid++;
936 break;
937 default:
938 $this->Error("Unexpected forloop.{$variable[1]}");
940 /* no need to escape it */
941 $this->var_is_safe = TRUE;
942 break;
943 case 'block':
944 if ($this->in_block == 0) {
945 $this->Error("Can't use block.super outside a block");
947 if (!$this->subtemplate) {
948 $this->Error("Only subtemplates can call block.super");
950 /* no need to escape it */
951 $this->var_is_safe = TRUE;
952 return Haanga_AST::str(self::$block_var);
953 break;
954 default:
955 /* choose array or objects */
957 for ($i=1; $i < count($variable); $i++) {
958 $var_part = array_slice($variable, 0, $i);
959 $def_arr = TRUE;
961 if (is_array($variable[$i])) {
962 if (isset($variable[$i]['object'])) {
963 $def_arr = FALSE;
965 if (!Haanga_AST::is_var($variable[$i])) {
966 $variable[$i] = current($variable[$i]);
970 $is_obj = $this->var_is_object($var_part, 'unknown');
972 if ( $is_obj === TRUE || ($is_obj == 'unknown' && !$def_arr)) {
973 $variable[$i] = array('object' => $variable[$i]);
977 break;
980 } else if (isset($this->var_alias[$variable])) {
981 $variable = $this->var_alias[$variable]['var'];
984 return hvar($variable)->getArray();
986 // }}}
988 // Print {{{
989 public function do_print(Haanga_AST $code, $stmt)
991 /* Flag this object as a printing one */
992 $code->doesPrint = TRUE;
994 if (self::$strip_whitespace && Haanga_AST::is_str($stmt)) {
995 $stmt['string'] = preg_replace('/\s+/', ' ', $stmt['string']);
998 if ($this->ob_start == 0) {
999 $code->do_echo($stmt);
1000 return;
1003 $buffer = hvar('buffer'.$this->ob_start);
1004 $code->append($buffer, $stmt);
1008 // }}}
1010 // for [<key>,]<val> in <array> {{{
1011 protected function generate_op_loop($details, &$body)
1013 if (isset($details['empty'])) {
1014 $body->do_if(hexpr(hexec('count', hvar($details['array'])), '==', 0));
1015 $this->generate_op_code($details['empty'], $body);
1016 $body->do_else();
1019 /* ForID */
1020 $oldid = $this->forid;
1021 $this->forid = $oldid+1;
1022 $this->forloop[$this->forid] = array();
1024 /* Check if the array to iterate is an object */
1025 $var = &$details['array'][0];
1026 if (is_string($var) && $this->var_is_object(array($var), FALSE)) {
1027 /* It is an object, call to get_object_vars */
1028 $body->decl($var.'_arr', hexec('get_object_vars', hvar($var)));
1029 $var .= '_arr';
1031 unset($var);
1033 /* variables */
1034 $array = $this->get_filtered_var($details['array'], $varname);
1036 /* Loop body */
1037 if ($this->is_safe(hvar($varname))) {
1038 $this->set_safe(hvar($details['variable']));
1041 /* check if the elements in the array is an array or object */
1043 $for_body = hcode();
1044 $this->generate_op_code($details['body'], $for_body);
1046 if ($this->is_safe(hvar($varname))) {
1047 $this->set_unsafe($details['variable']);
1050 $oid = $this->forid;
1051 $size = hvar('psize_'.$oid);
1053 // counter {{{
1054 if (isset($this->forloop[$oid]['counter'])) {
1055 $var = hvar('forcounter1_'.$oid);
1056 $body->decl($var, 1);
1057 $for_body->decl($var, hexpr($var, '+', 1));
1059 // }}}
1061 // counter0 {{{
1062 if (isset($this->forloop[$oid]['counter0'])) {
1063 $var = hvar('forcounter0_'.$oid);
1064 $body->decl($var, 0);
1065 $for_body->decl($var, hexpr($var, '+', 1));
1067 // }}}
1069 // last {{{
1070 if (isset($this->forloop[$oid]['last'])) {
1071 if (!isset($cnt)) {
1072 $body->decl('psize_'.$oid, hexec('count', hvar_ex($details['array'])));
1073 $cnt = TRUE;
1075 $var = 'islast_'.$oid;
1076 $body->decl($var, hexpr(hvar('forcounter1_'.$oid), '==', $size));
1077 $for_body->decl($var, hexpr(hvar('forcounter1_'.$oid), '==', $size));
1079 // }}}
1081 // first {{{
1082 if (isset($this->forloop[$oid]['first'])) {
1083 $var = hvar('isfirst_'.$oid);
1084 $body->decl($var, TRUE);
1085 $for_body->decl($var, FALSE);
1087 // }}}
1089 // revcounter {{{
1090 if (isset($this->forloop[$oid]['revcounter'])) {
1091 if (!isset($cnt)) {
1092 $body->decl('psize_'.$oid, hexec('count', hvar_ex($details['array'])));
1093 $cnt = TRUE;
1095 $var = hvar('revcount_'.$oid);
1096 $body->decl($var, $size);
1097 $for_body->decl($var, hexpr($var, '-', 1));
1099 // }}}
1101 // revcounter0 {{{
1102 if (isset($this->forloop[$oid]['revcounter0'])) {
1103 if (!isset($cnt)) {
1104 $body->decl('psize_'.$oid, hexec('count', hvar_ex($details['array'])));
1105 $cnt = TRUE;
1107 $var = hvar('revcount0_'.$oid);
1108 $body->decl($var, hexpr($size, "-", 1));
1109 $for_body->decl($var, hexpr($var, '-', 1));
1111 // }}}
1113 /* Restore old ForID */
1114 $this->forid = $oldid;
1116 /* Merge loop body */
1117 $body->do_foreach($array, $details['variable'], $details['index'], $for_body);
1119 if (isset($details['empty'])) {
1120 $body->do_endif();
1123 // }}}
1125 // ifchanged [<var1> <var2] {{{
1126 protected function generate_op_ifchanged($details, &$body)
1128 static $ifchanged = 0;
1130 $ifchanged++;
1131 $var1 = 'ifchanged'.$ifchanged;
1132 if (!isset($details['check'])) {
1133 /* ugly */
1134 $this->ob_start($body);
1135 $var2 = hvar('buffer'.$this->ob_start);
1138 $this->generate_op_code($details['body'], $body);
1139 $this->ob_start--;
1140 $body->do_if(hexpr(hexec('isset', hvar($var1)), '==', FALSE, '||', hvar($var1), '!=', $var2));
1141 $this->do_print($body, $var2);
1142 $body->decl($var1, $var2);
1143 } else {
1144 /* beauty :-) */
1145 foreach ($details['check'] as $id=>$type) {
1146 if (!Haanga_AST::is_var($type)) {
1147 $this->Error("Unexpected string {$type['string']}, expected a varabile");
1150 $this_expr = hexpr(hexpr(
1151 hexec('isset', hvar($var1, $id)), '==', FALSE,
1152 '||', hvar($var1, $id), '!=', $type
1155 if (isset($expr)) {
1156 $this_expr = hexpr($expr, '&&', $this_expr);
1159 $expr = $this_expr;
1162 $body->do_if($expr);
1163 $this->generate_op_code($details['body'], $body);
1164 $body->decl($var1, $details['check']);
1167 if (isset($details['else'])) {
1168 $body->do_else();
1169 $this->generate_op_code($details['else'], $body);
1171 $body->do_endif();
1173 // }}}
1175 // autoescape ON|OFF {{{
1176 function generate_op_autoescape($details, &$body)
1178 $old_autoescape = self::$autoescape;
1179 self::$autoescape = strtolower($details['value']) == 'on';
1180 $this->generate_op_code($details['body'], $body);
1181 self::$autoescape = $old_autoescape;
1183 // }}}
1185 // {% spacefull %} Set to OFF strip_whitespace for a block (the compiler option) {{{
1186 function generate_op_spacefull($details, &$body)
1188 $old_strip_whitespace = self::$strip_whitespace;
1189 self::$strip_whitespace = FALSE;
1190 $this->generate_op_code($details['body'], $body);
1191 self::$strip_whitespace = $old_strip_whitespace;
1193 // }}}
1195 // ob_Start(array &$body) {{{
1197 * Start a new buffering
1200 function ob_start(&$body)
1202 $this->ob_start++;
1203 $body->decl('buffer'.$this->ob_start, "");
1205 // }}}
1207 // Custom Tags {{{
1208 function get_custom_tag($name)
1210 $function = $this->get_function_name($this->name).'_tag_'.$name;
1211 $this->append .= "\n\n".Haanga_Extension::getInstance('Tag')->getFunctionBody($name, $function);
1212 return $function;
1216 * Generate needed code for custom tags (tags that aren't
1217 * handled by the compiler).
1220 function generate_op_custom_tag($details, &$body)
1222 static $tags;
1223 if (!$tags) {
1224 $tags = Haanga_Extension::getInstance('Tag');
1227 foreach ($details['list'] as $id => $arg) {
1228 if (Haanga_AST::is_var($arg)) {
1229 $details['list'][$id] = $this->generate_variable_name($arg['var']);
1234 $tag_name = $details['name'];
1235 $tagFunction = $tags->getFunctionAlias($tag_name);
1237 if (!$tagFunction && !$tags->hasGenerator($tag_name)) {
1238 $function = $this->get_custom_tag($tag_name, isset($details['as']));
1239 } else {
1240 $function = $tagFunction;
1242 if (isset($details['body'])) {
1244 if the custom tag has 'body'
1245 then it behave the same way as a filter
1247 $this->ob_start($body);
1248 $this->generate_op_code($details['body'], $body);
1249 $target = hvar('buffer'.$this->ob_start);
1250 if ($tags->hasGenerator($tag_name)) {
1251 $args = array_merge(array($target), $details['list']);
1252 $exec = $tags->generator($tag_name, $this, $args);
1253 if (!$exec InstanceOf Haanga_AST) {
1254 $this->Error("Invalid output of custom filter {$tag_name}");
1256 if ($exec->stack_size() >= 2 || $exec->doesPrint) {
1258 The generator returned more than one statement,
1259 so we assume the output is already handled
1260 by one of those stmts.
1262 $body->append_ast($exec);
1263 $this->ob_start--;
1264 return;
1266 } else {
1267 $exec = hexec($function, $target);
1269 $this->ob_start--;
1270 $this->do_print($body, $exec);
1271 return;
1274 $var = isset($details['as']) ? $details['as'] : NULL;
1275 $args = array_merge(array($function), $details['list']);
1277 if ($tags->hasGenerator($tag_name)) {
1278 $exec = $tags->generator($tag_name, $this, $details['list'], $var);
1279 if ($exec InstanceOf Haanga_AST) {
1280 if ($exec->stack_size() >= 2 || $exec->doesPrint || $var !== NULL) {
1282 The generator returned more than one statement,
1283 so we assume the output is already handled
1284 by one of those stmts.
1286 $body->append_ast($exec);
1287 return;
1289 } else {
1290 $this->Error("Invalid output of the custom tag {$tag_name}");
1292 } else {
1293 $fnc = array_shift($args);
1294 $exec = hexec($fnc);
1295 foreach ($args as $arg) {
1296 $exec->param($arg);
1300 if ($var) {
1301 $body->decl($var, $exec);
1302 } else {
1303 $this->do_print($body, $exec);
1306 // }}}
1308 // with <variable> as <var> {{{
1313 function generate_op_alias($details, &$body)
1315 $this->var_alias[ $details['as'] ] = $this->generate_variable_name($details['var']);
1316 $this->generate_op_code($details['body'], $body);
1317 unset($this->var_alias[ $details['as'] ] );
1319 // }}}
1321 // Custom Filters {{{
1322 function get_custom_filter($name)
1324 $function = $this->get_function_name($this->name).'_filter_'.$name;
1325 $this->append .= "\n\n".Haanga_Extension::getInstance('Filter')->getFunctionBody($name, $function);
1326 return $function;
1330 function do_filtering($name, $args)
1332 static $filter;
1333 if (!$filter) {
1334 $filter = Haanga_Extension::getInstance('Filter');
1337 if (is_array($name)) {
1339 prepare array for ($func_name, $arg1, $arg2 ... )
1340 where $arg1 = last expression and $arg2.. $argX is
1341 defined in the template
1343 $args = array_merge($args, $name['args']);
1344 $name = $name[0];
1347 if (!$filter->isValid($name)) {
1348 $this->Error("{$name} is an invalid filter");
1351 if ($filter->isSafe($name)) {
1352 /* check if the filter is return HTML-safe data (to avoid double scape) */
1353 $this->var_is_safe = TRUE;
1357 if ($filter->hasGenerator($name)) {
1358 return $filter->generator($name, $this, $args);
1360 $fnc = $filter->getFunctionAlias($name);
1361 if (!$fnc) {
1362 $fnc = $this->get_custom_filter($name);
1365 $args = array_merge(array($fnc), $args);
1366 $exec = call_user_func_array('hexec', $args);
1368 return $exec;
1371 function generate_op_filter($details, &$body)
1373 $this->ob_start($body);
1374 $this->generate_op_code($details['body'], $body);
1375 $target = hvar('buffer'.$this->ob_start);
1376 foreach ($details['functions'] as $f) {
1377 $param = (isset($exec) ? $exec : $target);
1378 $exec = $this->do_filtering($f, array($param));
1380 $this->ob_start--;
1381 $this->do_print($body, $exec);
1383 // }}}
1385 /* variable safety {{{ */
1386 function set_safe($name)
1388 if (!Haanga_AST::is_Var($name)) {
1389 $name = hvar($name)->getArray();
1391 $this->safes[serialize($name)] = TRUE;
1394 function set_unsafe($name)
1396 if (!Haanga_AST::is_Var($name)) {
1397 $name = hvar($name)->getArray();
1399 unset($this->safes[serialize($name)]);
1402 function is_safe($name)
1404 if ($this->var_is_safe) {
1405 return TRUE;
1407 if (isset($this->safes[serialize($name)])) {
1408 return TRUE;
1410 return FALSE;
1412 /* }}} */
1414 final static function main_cli()
1416 $argv = $GLOBALS['argv'];
1417 $haanga = new Haanga_Compiler;
1418 $code = $haanga->compile_file($argv[1], TRUE);
1419 if (!isset($argv[2]) || $argv[2] != '--notags') {
1420 $code = "<?php\n\n$code";
1422 echo $code;
1428 * Local variables:
1429 * tab-width: 4
1430 * c-basic-offset: 4
1431 * End:
1432 * vim600: sw=4 ts=4 fdm=marker
1433 * vim<600: sw=4 ts=4