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. |
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 class HG_Parser Extends Haanga_Compiler_Parser
40 /* subclass to made easier references to constants */
43 class Haanga_Compiler_Lexer
52 function __construct($data, $compiler)
55 $this->compiler = $compiler;
60 static function init($template, $compiler)
62 $lexer = new Haanga_Compiler_Lexer($template, $compiler);
63 $parser = new Haanga_Compiler_Parser($lexer);
65 $parser->compiler = $compiler;
69 if (!$lexer->yylex()) {
72 $parser->doParse($lexer->token, $lexer->value);
74 } catch (Exception $e) {
75 throw new Haanga_Compiler_Exception($e->getMessage(). ' on line '.$lexer->getLine());
77 $parser->doParse(0, 0);
78 return (array)$parser->body;
86 public $custom_tags=array();
88 function is_custom_tag()
92 $tag = Haanga_Extension::getInstance('Tag');
94 $value = $tag->isValid($this->value);
95 $this->token = $value ? $value : HG_Parser::T_ALPHA;
104 alpha = /([a-zA-Z_][a-zA-Z_0-9]*)/
106 numerals = /([0-9])+/
107 whitespace = /[ \r\t\n]+/
108 html = /([^{]+(.[^%{#])?)+/
109 comment = /([^\#]+\#\})+/
110 custom_tag_end = /end([a-zA-Z][a-zA-Z0-9]*)/
111 token_end = /[^a-zA-Z0-9_\.]/
112 single_string = /[^'\\]+/
113 double_string = /[^"\\]+/
118 $this->token = HG_Parser::T_OPEN_TAG;
119 $this->yypushstate(self::IN_CODE);
123 $this->token = HG_Parser::T_COMMENT_OPEN;
124 $this->yypushstate(self::IN_COMMENT);
129 $this->token = HG_Parser::T_PRINT_OPEN;
130 $this->yypushstate(self::IN_PRINT);
134 $this->token = HG_Parser::T_HTML;
141 $this->token = HG_Parser::T_CLOSE_TAG;
146 $this->token = HG_Parser::T_OBJ;
151 $this->token = HG_Parser::T_DOT;
155 $this->token = HG_Parser::T_BUFFER;
160 $this->token = HG_Parser::T_FOR;
164 $this->token = HG_Parser::T_EMPTY;
168 $this->token = HG_Parser::T_LOAD;
172 $this->token = HG_Parser::T_BLOCK;
176 $this->token = HG_Parser::T_AND;
180 $this->token = HG_Parser::T_AND;
184 $this->token = HG_Parser::T_OR;
188 $this->token = HG_Parser::T_OR;
192 $this->token = HG_Parser::T_EQ;
196 $this->token = HG_Parser::T_NE;
200 $this->token = HG_Parser::T_GE;
204 $this->token = HG_Parser::T_NOT;
208 $this->token = HG_Parser::T_NOT;
213 $this->token = HG_Parser::T_BRACKETS_OPEN;
217 $this->token = HG_Parser::T_BRACKETS_CLOSE;
221 $this->token = HG_Parser::T_GT;
225 $this->token = HG_Parser::T_LT;
228 $this->token = HG_Parser::T_LE;
232 $this->token = HG_Parser::T_PIPE;
236 $this->token = HG_Parser::T_COLON;
240 $this->token = HG_Parser::T_FILTER;
243 "regroup" token_end {
244 $this->token = HG_Parser::T_REGROUP;
247 "endfilter" token_end {
248 $this->token = HG_Parser::T_END_FILTER;
251 "autoescape" token_end {
252 $this->token = HG_Parser::T_AUTOESCAPE;
255 "spacefull" token_end {
256 $this->token = HG_Parser::T_SPACEFULL;
260 "endautoescape" token_end {
261 $this->token = HG_Parser::T_END_AUTOESCAPE;
265 "endblock" token_end {
266 $this->token = HG_Parser::T_END_BLOCK;
269 "ifchanged" token_end {
270 $this->token = HG_Parser::T_IFCHANGED;
273 "ifequal" token_end {
274 $this->token = HG_Parser::T_IFEQUAL;
277 "endifequal" token_end {
278 $this->token = HG_Parser::T_END_IFEQUAL;
281 "ifnotequal" token_end {
282 $this->token = HG_Parser::T_IFNOTEQUAL;
285 "endifnotequal" token_end {
286 $this->token = HG_Parser::T_END_IFNOTEQUAL;
291 $this->token = HG_Parser::T_ELSE;
294 "endifchanged" token_end {
295 $this->token = HG_Parser::T_ENDIFCHANGED;
300 $this->token = HG_Parser::T_IN;
304 $this->token = HG_Parser::T_CLOSEFOR;
308 $this->token = HG_Parser::T_WITH;
311 "endwith" token_end {
312 $this->token = HG_Parser::T_ENDWITH;
316 $this->token = HG_Parser::T_AS;
320 $this->token = HG_Parser::T_ON;
324 $this->token = HG_Parser::T_OFF;
328 $this->token = HG_Parser::T_BY;
332 $this->token = HG_Parser::T_IF;
336 $this->token = HG_Parser::T_ELSE;
340 $this->token = HG_Parser::T_ENDIF;
344 $this->token = HG_Parser::T_INTL;
349 $this->token = HG_Parser::T_LPARENT;
353 $this->token = HG_Parser::T_RPARENT;
357 $this->token = HG_Parser::T_MOD;
361 $this->token = HG_Parser::T_COMMA;
365 $this->token = HG_Parser::T_PLUS;
368 $this->token = HG_Parser::T_MINUS;
371 $this->token = HG_Parser::T_TIMES;
375 $this->token = HG_Parser::T_DIV;
379 $this->token = HG_Parser::T_STRING_SINGLE_INIT;
380 $this->yypushstate(self::IN_STRING_SINGLE);
384 $this->token = HG_Parser::T_STRING_DOUBLE_INIT;
385 $this->yypushstate(self::IN_STRING_DOUBLE);
389 $this->token = HG_Parser::T_CUSTOM_END;
392 "extends" token_end {
393 $this->token = HG_Parser::T_EXTENDS;
396 "include" token_end {
397 $this->token = HG_Parser::T_INCLUDE;
401 $this->token = HG_Parser::T_NUMERIC;
404 numerals "." numerals {
405 $this->token = HG_Parser::T_NUMERIC;
409 $this->is_custom_tag();
420 $this->token = HG_Parser::T_PRINT_CLOSE;
425 $this->token = HG_Parser::T_PIPE;
429 $this->token = HG_Parser::T_COLON;
433 $this->token = HG_Parser::T_OBJ;
438 $this->token = HG_Parser::T_DOT;
442 $this->token = HG_Parser::T_BRACKETS_OPEN;
446 $this->token = HG_Parser::T_BRACKETS_CLOSE;
450 $this->token = HG_Parser::T_NUMERIC;
453 numerals "." numerals {
454 $this->token = HG_Parser::T_NUMERIC;
458 $this->token = HG_Parser::T_STRING_SINGLE_INIT;
459 $this->yypushstate(self::IN_STRING_SINGLE);
463 $this->token = HG_Parser::T_STRING_DOUBLE_INIT;
464 $this->yypushstate(self::IN_STRING_DOUBLE);
468 $this->token = HG_Parser::T_ALPHA;
477 %statename IN_STRING_DOUBLE
480 $this->token = HG_Parser::T_STRING_CONTENT;
486 $this->token = HG_Parser::T_STRING_CONTENT;
493 $this->token = HG_Parser::T_STRING_DOUBLE_END;
498 $this->token = HG_Parser::T_STRING_CONTENT;
504 %statename IN_STRING_SINGLE
506 $this->token = HG_Parser::T_STRING_CONTENT;
512 $this->token = HG_Parser::T_STRING_CONTENT;
519 $this->token = HG_Parser::T_STRING_SINGLE_END;
524 $this->token = HG_Parser::T_STRING_CONTENT;
530 %statename IN_COMMENT
532 $this->token = HG_Parser::T_COMMENT;