# Контекстно-свободная LIME-грамматика шаблонизатора # # Для корректной работы нужен патченый LIME со следующими изменениями: # (*) Подменой лексемы 'lit' на 'str' в метаграмматике. # Это нужно, чтобы можно было юзать строковые лексемы типа '" "directive end" %token "{{" "substitution begin" %token "}}" "substitution end" %left ".." %nonassoc "?" ":" %left "||" "OR" "XOR" %left "&&" "AND" %nonassoc "==" "!=" "<" ">" "<=" ">=" %left "+" "-" %left "&" %left "*" "/" "%" # Директивы template = chunks { $this->template->st->functions['main']['body'] = "function fn_main() {\$stack = array();\n\$t = '';\n".$1."\nreturn \$t;\n}\n"; $$ = ''; } . chunks = { $$ = ''; } | chunks chunk { $$ = $1 . "# line ".$this->template->lexer->lineno."\n" . $2; } . chunk = literal { $$ = ($1 != "''" && $1 != '""' ? '$t .= ' . $1 . ";\n" : ''); } | "" { $$ = $c; } | "{{" exp/e "}}" { $$ = '$t .= ' . ($e[1] || !$this->template->options->auto_escape ? $e[0] : $this->template->compile_function($this->template->options->auto_escape, [ $e ])[0]) . ";\n"; } | error/e { $$ = ''; } . code_chunk = c_if/$ | c_set/$ | c_fn/$ | c_for/$ | exp/e { $$ = '$t .= ' . ($e[1] || !$this->template->options->auto_escape ? $e[0] : $this->template->compile_function($this->template->options->auto_escape, [ $e ])[0]) . ";\n"; } . c_if = "IF" exp/e "-->" chunks/if "" chunks/if "" chunks/else "" chunks/if c_elseifs/ei chunks/ec "" chunks/if c_elseifs/ei chunks/ec "" chunks/else "" { $$ = "} elseif (" . $e[0] . ") {\n"; } | c_elseifs/p chunks/cs "" { $$ = $p . $cs . "} elseif (" . $e[0] . ") {\n"; } . c_set = "SET" varref/v "=" exp/e { $$ = $v[0] . ' = ' . $e[0] . ";\n"; } | "SET" varref/v "-->" chunks/cs "" chunks/cs "" chunks/cs "