3 +---------------------------------------------------------------------------------+
4 | Copyright (c) 2010 Haanga |
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. |
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. |
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. |
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. |
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 require dirname(__FILE__)."/parser.php";
40 function do_parsing($template, $ignore_whitespace=FALSE)
42 $lexer = new Haanga_Lexer($template, $ignore_whitespace);
46 if (!$lexer->yylex()) {
49 //var_dump(array($lexer->token, $lexer->value));
50 $parser->doParse($lexer->token, $lexer->value);
52 } catch (Exception $e) {
53 throw new CompilerException($e->getMessage(). ' on line '.$lexer->getLine());
55 $parser->doParse(0, 0);
67 private $ignore_whitespace;
69 function __construct($data, $whitespace=FALSE)
73 $this->ignore_whitespace = $whitespace;
82 public $custom_tags=array();
84 function is_custom_tag()
88 $tag = Extensions::getInstance('Haanga_tag');
90 $value = $tag->isValid($this->value);
91 $this->token = $value ? $value : Parser::T_ALPHA;
100 alpha = /([a-zA-Z_][a-zA-Z_0-9]*)/
102 numerals = /([0-9])+/
103 whitespace = /[ \r\t\n]+/
104 html = /([^{]+(.[^%{#])?)+/
105 comment = /([^\#]+\#\})+/
106 custom_tag_end = /end([a-zA-Z][a-zA-Z0-9]*)/
107 token_end = /[^a-zA-Z0-9]/
108 single_string = /[^'\\]+/
109 double_string = /[^"\\]+/
114 $this->token = Parser::T_OPEN_TAG;
115 $this->yypushstate(self::IN_CODE);
119 $this->token = Parser::T_COMMENT_OPEN;
120 $this->yypushstate(self::IN_COMMENT);
125 $this->token = Parser::T_PRINT_OPEN;
126 $this->yypushstate(self::IN_PRINT);
130 $this->token = Parser::T_HTML;
137 $this->token = Parser::T_CLOSE_TAG;
142 $this->token = Parser::T_DOT;
146 $this->token = Parser::T_FOR;
150 $this->token = Parser::T_EMPTY;
153 "firstof" token_end {
154 $this->token = Parser::T_FIRST_OF;
158 $this->token = Parser::T_BLOCK;
162 $this->token = Parser::T_AND;
166 $this->token = Parser::T_AND;
170 $this->token = Parser::T_OR;
174 $this->token = Parser::T_OR;
178 $this->token = Parser::T_EQ;
182 $this->token = Parser::T_NE;
186 $this->token = Parser::T_GE;
190 $this->token = Parser::T_BRACKETS_OPEN;
194 $this->token = Parser::T_BRACKETS_CLOSE;
198 $this->token = Parser::T_GT;
202 $this->token = Parser::T_LT;
205 $this->token = Parser::T_LE;
209 $this->token = Parser::T_PIPE;
213 $this->token = Parser::T_COLON;
217 $this->token = Parser::T_FILTER;
220 "regroup" token_end {
221 $this->token = Parser::T_REGROUP;
224 "endfilter" token_end {
225 $this->token = Parser::T_END_FILTER;
228 "autoescape" token_end {
229 $this->token = Parser::T_AUTOESCAPE;
233 "endautoescape" token_end {
234 $this->token = Parser::T_END_AUTOESCAPE;
238 "endblock" token_end {
239 $this->token = Parser::T_END_BLOCK;
242 "ifchanged" token_end {
243 $this->token = Parser::T_IFCHANGED;
247 $this->token = Parser::T_ELSE;
250 "endifchanged" token_end {
251 $this->token = Parser::T_ENDIFCHANGED;
256 $this->token = Parser::T_IN;
260 $this->token = Parser::T_CLOSEFOR;
264 $this->token = Parser::T_WITH;
267 "endwith" token_end {
268 $this->token = Parser::T_ENDWITH;
272 $this->token = Parser::T_AS;
276 $this->token = Parser::T_ON;
280 $this->token = Parser::T_OFF;
284 $this->token = Parser::T_BY;
288 $this->token = Parser::T_IF;
292 $this->token = Parser::T_ELSE;
296 $this->token = Parser::T_ENDIF;
300 $this->token = Parser::T_LPARENT;
304 $this->token = Parser::T_RPARENT;
308 $this->token = Parser::T_MOD;
312 $this->token = Parser::T_COMMA;
316 $this->token = Parser::T_PLUS;
319 $this->token = Parser::T_TIMES;
323 $this->token = Parser::T_DIV;
327 $this->token = Parser::T_STRING_SINGLE_INIT;
328 $this->yypushstate(self::IN_STRING_SINGLE);
332 $this->token = Parser::T_STRING_DOUBLE_INIT;
333 $this->yypushstate(self::IN_STRING_DOUBLE);
337 $this->token = Parser::T_CUSTOM_END;
340 "extends" token_end {
341 $this->token = Parser::T_EXTENDS;
344 "include" token_end {
345 $this->token = Parser::T_INCLUDE;
349 $this->token = Parser::T_NUMERIC;
352 numerals "." numerals {
353 $this->token = Parser::T_NUMERIC;
357 $this->is_custom_tag();
368 $this->token = Parser::T_PRINT_CLOSE;
373 $this->token = Parser::T_PIPE;
377 $this->token = Parser::T_COLON;
382 $this->token = Parser::T_DOT;
386 $this->token = Parser::T_BRACKETS_OPEN;
390 $this->token = Parser::T_BRACKETS_CLOSE;
394 $this->token = Parser::T_NUMERIC;
397 numerals "." numerals {
398 $this->token = Parser::T_NUMERIC;
402 $this->token = Parser::T_STRING_SINGLE_INIT;
403 $this->yypushstate(self::IN_STRING_SINGLE);
407 $this->token = Parser::T_STRING_DOUBLE_INIT;
408 $this->yypushstate(self::IN_STRING_DOUBLE);
412 $this->token = Parser::T_ALPHA;
421 %statename IN_STRING_DOUBLE
424 $this->token = Parser::T_STRING_CONTENT;
430 $this->token = Parser::T_STRING_CONTENT;
437 $this->token = Parser::T_STRING_DOUBLE_END;
442 $this->token = Parser::T_STRING_CONTENT;
448 %statename IN_STRING_SINGLE
450 $this->token = Parser::T_STRING_CONTENT;
456 $this->token = Parser::T_STRING_CONTENT;
463 $this->token = Parser::T_STRING_SINGLE_END;
468 $this->token = Parser::T_STRING_CONTENT;
474 %statename IN_COMMENT
476 $this->token = Parser::T_COMMENT;