Compare commits

...

2 Commits

4 changed files with 258 additions and 198 deletions

View File

@ -81,95 +81,80 @@
# Директивы # Директивы
template = chunks { template = chunks {
$this->template->st->functions['main']['body'] = "function fn_main() {\$stack = array();\n\$t = '';\n".$1."\nreturn \$t;\n}\n"; $this->template->st->AST = $1;
$$ = ''; $$ = '';
} }
. .
chunks = { chunks = {
$$ = ''; $$ = [];
} }
| chunks chunk { | chunks chunk {
$$ = $1 . $2; $$ = $1;
if ($2) {
$$[] = $2;
}
} }
. .
chunk = literal { chunk = literal {
$$ = ($1 != "''" && $1 != '""' ? '$t .= ' . $1 . ";\n" : ''); $$ = [ 'literal', $1 ];
} }
| "<!--" code_chunk/c "-->" { | "<!--" code_chunk/c "-->" {
$$ = $c; $$ = $c;
} }
| "{{" exp/e "}}" { | "{{" exp/e "}}" {
$$ = '$t .= ' . ($e[1] || !$this->template->options->auto_escape ? $e[0] : $this->template->compile_function($this->template->options->auto_escape, [ $e ])[0]) . ";\n"; $$ = [ 'subst', $e ];
} }
| error/e { | error/e {
$$ = ''; $$ = false;
} }
. .
code_chunk = c_if/$ | c_set/$ | c_fn/$ | c_for/$ | exp/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"; $$ = [ 'subst', $e ];
} }
. .
c_if = "IF" exp/e "-->" chunks/if "<!--" "END" { c_if = "IF" exp/e "-->" chunks/if "<!--" "END" {
$$ = "if (" . $e[0] . ") {\n" . $if . "}\n"; $$ = [ 'if', [ $e, $if ] ];
} }
| "IF" exp/e "-->" chunks/if "<!--" "ELSE" "-->" chunks/else "<!--" "END" { | "IF" exp/e "-->" chunks/if "<!--" "ELSE" "-->" chunks/else "<!--" "END" {
$$ = "if (" . $e[0] . ") {\n" . $if . "} else {\n" . $else . "}\n"; $$ = [ 'if', [ $e, $if ], [ false, $else ] ];
} }
| "IF" exp/e "-->" chunks/if c_elseifs/ei chunks/ec "<!--" "END" { | "IF" exp/e "-->" chunks/if c_elseifs/ei chunks/ec "<!--" "END" {
$$ = "if (" . $e[0] . ") {\n" . $if . $ei . $ec . "}\n"; $$ = $ei;
$$[count($$)-1] = $ec;
$$ = array_merge([ 'if', [ $e, $if ] ], $ei);
} }
| "IF" exp/e "-->" chunks/if c_elseifs/ei chunks/ec "<!--" "ELSE" "-->" chunks/else "<!--" "END" { | "IF" exp/e "-->" chunks/if c_elseifs/ei chunks/ec "<!--" "ELSE" "-->" chunks/else "<!--" "END" {
$$ = "if (" . $e[0] . ") {\n" . $if . $ei . $ec . "} else {\n" . $else . "}\n"; $$ = $ei;
$$[count($$)-1] = $ec;
$$[] = [ false, $else ];
$$ = array_merge([ 'if', [ $e, $if ] ], $$);
} }
. .
c_elseifs = "<!--" elseif exp/e "-->" { c_elseifs = "<!--" elseif exp/e "-->" {
$$ = "} elseif (" . $e[0] . ") {\n"; $$ = [ [ $e ] ];
} }
| c_elseifs/p chunks/cs "<!--" elseif exp/e "-->" { | c_elseifs/p chunks/cs "<!--" elseif exp/e "-->" {
$$ = $p . $cs . "} elseif (" . $e[0] . ") {\n"; $$ = $p;
$$[count($$)-1][] = $cs;
$$[] = [ $e ];
} }
. .
c_set = "SET" varref/v "=" exp/e { c_set = "SET" varref/v "=" exp/e {
$$ = $v[0] . ' = ' . $e[0] . ";\n"; $$ = [ 'set', $v, [ 'subst', $e ] ];
} }
| "SET" varref/v "-->" chunks/cs "<!--" "END" { | "SET" varref/v "-->" chunks/cs "<!--" "END" {
$$ = "\$stack[] = \$t;\n\$t = '';\n" . $cs . $v[0] . " = \$t;\n\$t = array_pop(\$stack);\n"; $$ = [ 'set', $v, $cs ];
} }
. .
c_fn = fn name/name "(" arglist/args ")" "=" exp/exp { c_fn = fn name/name "(" arglist/args ")" "=" exp/exp {
$this->template->st->functions[$name] = array( $$ = [ 'function', $name, $args, [ 'subst', $exp ] ];
'name' => $name,
'args' => $args,
'body' => 'function fn_'.$name." () {\nreturn ".$exp.";\n}\n",
//'line' => $line, Ой, я чо - аргументы не юзаю?
//'pos' => $pos,
);
$$ = '';
} }
| fn name/name "(" arglist/args ")" "-->" chunks/cs "<!--" "END" { | fn name/name "(" arglist/args ")" "-->" chunks/cs "<!--" "END" {
$this->template->st->functions[$name] = array( $$ = [ 'function', $name, $args, $cs ];
'name' => $name,
'args' => $args,
'body' => 'function fn_'.$name." () {\$stack = array();\n\$t = '';\n".$cs."\nreturn \$t;\n}\n",
//'line' => $line,
//'pos' => $pos,
);
$$ = '';
} }
. .
c_for = for varref/varref "=" exp/exp "-->" chunks/cs "<!--" "END" { c_for = for varref/varref "=" exp/exp "-->" chunks/cs "<!--" "END" {
$varref_index = substr($varref[0], 0, -1) . ".'_index']"; $$ = [ 'for', $varref, $exp, $cs ];
$$ = "\$stack[] = ".$varref[0].";
\$stack[] = ".$varref_index.";
\$stack[] = 0;
foreach ((array)($exp[0]) as \$item) {
".$varref[0]." = \$item;
".$varref_index." = \$stack[count(\$stack)-1]++;
".$cs."}
array_pop(\$stack);
".$varref_index." = array_pop(\$stack);
".$varref[0]." = array_pop(\$stack);
";
} }
. .
fn = "FUNCTION" | "BLOCK" | "MACRO" . fn = "FUNCTION" | "BLOCK" | "MACRO" .
@ -179,95 +164,91 @@ elseif = "ELSE" "IF" | "ELSIF" | "ELSEIF" .
# Выражения # Выражения
exp: exp/a ".." exp/b { exp: exp/a ".." exp/b {
$$ = [ '(' . $a[0] . ' . ' . $b[0] . ')', $a[1] && $b[1] ]; $$ = [ 'op', '.', $a, $b ];
} }
| exp/a "||" exp/b { | exp/a "||" exp/b {
$$ = [ '(' . $a[0] . ' ?: ' . $b[0] . ')', $a[1] && $b[1] ]; $$ = [ 'op', '||', $a, $b ];
} }
| exp/a "OR" exp/b { | exp/a "OR" exp/b {
$$ = [ '(' . $a[0] . ' ?: ' . $b[0] . ')', $a[1] && $b[1] ]; $$ = [ 'op', '||', $a, $b ];
} }
| exp/a "XOR" exp/b { | exp/a "XOR" exp/b {
$$ = [ '(' . $a[0] . ' XOR ' . $b[0] . ')', true ]; $$ = [ 'op', 'XOR', $a, $b ];
} }
| exp/a "&&" exp/b { | exp/a "&&" exp/b {
$$ = [ '(' . $a[0] . ' && ' . $b[0] . ')', true ]; $$ = [ 'op', '&&', $a, $b ];
} }
| exp/a "AND" exp/b { | exp/a "AND" exp/b {
$$ = [ '(' . $a[0] . ' && ' . $b[0] . ')', true ]; $$ = [ 'op', '&&', $a, $b ];
} }
| exp/a "==" exp/b { | exp/a "==" exp/b {
$$ = [ '(' . $a[0] . ' == ' . $b[0] . ')', true ]; $$ = [ 'op', '==', $a, $b ];
} }
| exp/a "!=" exp/b { | exp/a "!=" exp/b {
$$ = [ '(' . $a[0] . ' != ' . $b[0] . ')', true ]; $$ = [ 'op', '!=', $a, $b ];
} }
| exp/a "<" exp/b { | exp/a "<" exp/b {
$$ = [ '(' . $a[0] . ' < ' . $b[0] . ')', true ]; $$ = [ 'op', '<', $a, $b ];
} }
| exp/a ">" exp/b { | exp/a ">" exp/b {
$$ = [ '(' . $a[0] . ' > ' . $b[0] . ')', true ]; $$ = [ 'op', '>', $a, $b ];
} }
| exp/a "<=" exp/b { | exp/a "<=" exp/b {
$$ = [ '(' . $a[0] . ' <= ' . $b[0] . ')', true ]; $$ = [ 'op', '<=', $a, $b ];
} }
| exp/a ">=" exp/b { | exp/a ">=" exp/b {
$$ = [ '(' . $a[0] . ' >= ' . $b[0] . ')', true ]; $$ = [ 'op', '>=', $a, $b ];
} }
| exp/a "+" exp/b { | exp/a "+" exp/b {
$$ = [ '(' . $a[0] . ' + ' . $b[0] . ')', true ]; $$ = [ 'op', '+', $a, $b ];
} }
| exp/a "-" exp/b { | exp/a "-" exp/b {
$$ = [ '(' . $a[0] . ' - ' . $b[0] . ')', true ]; $$ = [ 'op', '-', $a, $b ];
} }
| exp/a "&" exp/b { | exp/a "&" exp/b {
$$ = [ '(' . $a[0] . ' & ' . $b[0] . ')', true ]; $$ = [ 'op', '&', $a, $b ];
} }
| exp/a "*" exp/b { | exp/a "*" exp/b {
$$ = [ '(' . $a[0] . ' * ' . $b[0] . ')', true ]; $$ = [ 'op', '*', $a, $b ];
} }
| exp/a "/" exp/b { | exp/a "/" exp/b {
$$ = [ '(' . $a[0] . ' / ' . $b[0] . ')', true ]; $$ = [ 'op', '/', $a, $b ];
} }
| exp/a "%" exp/b { | exp/a "%" exp/b {
$$ = [ '(' . $a[0] . ' % ' . $b[0] . ')', true ]; $$ = [ 'op', '%', $a, $b ];
} }
| p10/$ | p10/$
. .
p10: p11/$ p10: p11/$
| '-' p11/a { | '-' p11/a {
$$ = [ '(-'.$a[0].')', true ]; $$ = [ 'op', '-', $a ];
} }
. .
p11: nonbrace p11: nonbrace/$
| '(' exp/e ')' varpath/p { | '(' exp/e ')' varpath/p {
$$ = [ ($p !== '' ? 'self::noop('.$e[0].')'.$p : '('.$e[0].')'), false ]; $$ = [ 'varpath', $e, $p ];
} }
| '!' p11/a { | '!' p11/a {
$$ = [ '(!'.$a[0].')', true ]; $$ = [ 'op', '!', $a ];
} }
| "NOT" p11/a { | "NOT" p11/a {
$$ = [ '(!'.$a[0].')', true ]; $$ = [ 'op', '!', $a ];
} }
. .
nonbrace: '{' hash/h '}' { nonbrace: '{' hash/$ '}'
$$ = [ 'array(' . $h . ')', true ]; | literal/$
}
| literal {
$$ = [ $1, true ];
}
| varref/$ | varref/$
| name/f '(' ')' { | name/f '(' ')' {
$$ = $this->template->compile_function($f, []); $$ = [ 'call', $f, [] ];
} }
| name/f '(' list/args ')' { | name/f '(' list/args ')' {
$$ = $this->template->compile_function($f, $args); $$ = [ 'call', $f, $args ];
} }
| name/f '(' gthash/args ')' { | name/f '(' gthash/args ')' {
$$ = [ "\$this->parent->call_block('".addcslashes($f, "'\\")."', array(".$args."), '".addcslashes($this->template->lexer->errorinfo(), "'\\")."')", true ]; $$ = [ 'call_block', $f, $args, $this->template->lexer->errorpos() ];
} }
| name/f nonbrace/arg { | name/f nonbrace/arg {
$$ = $this->template->compile_function($f, [ $arg ]); $$ = [ 'call', $f, [ $arg ] ];
} }
. .
list: exp/e { list: exp/e {
@ -289,57 +270,61 @@ arglist: name/n {
$$ = []; $$ = [];
} }
. .
hash: pair/$ hash: pair/p {
$$ = [ 'hash', $p ];
}
| pair/p ',' hash/h { | pair/p ',' hash/h {
$$ = $p . ', ' . $h; array_splice($h, 1, 0, [ $p ]);
$$ = $h;
} }
| { | {
$$ = ''; $$ = [ 'hash' ];
} }
. .
gthash: gtpair/$ gthash: gtpair/p {
$$ = [ 'hash', $p ];
}
| gtpair/p ',' gthash/h { | gtpair/p ',' gthash/h {
$$ = $p . ', ' . $h; array_splice($h, 1, 0, [ $p ]);
$$ = $h;
} }
. .
pair: exp/k ',' exp/v { pair: exp/k ',' exp/v {
$$ = $k[0] . ' => ' . $v[0]; $$ = [ $k, $v ];
} }
| gtpair/$ | gtpair/$
. .
gtpair: exp/k "=>" exp/v { gtpair: exp/k "=>" exp/v {
$$ = $k[0] . ' => ' . $v[0]; $$ = [ $k, $v ];
} }
. .
varref: name/n { varref: name/n {
$$ = [ "\$this->tpldata['".addcslashes($n, "\\\'")."']", false ]; $$ = [ 'varref', $n ];
} }
| varref/v varpart/p { | varref/v varpart/p {
$$ = [ $v[0] . $p, false ]; $v[] = $p;
$$ = $v;
} }
. .
varpart: '.' namekw/n { varpart: '.' namekw/n {
$$ = "['".addcslashes($n, "\\\'")."']"; $$ = [ 'index', $n ];
} }
| '[' exp/e ']' { | '[' exp/e ']' {
$$ = '['.$e[0].']'; $$ = [ 'index', $e ];
} }
| '.' namekw/n '(' ')' { | '.' namekw/n '(' ')' {
$$ = '->'.$n.'()'; $$ = [ 'method', $n, [] ];
} }
| '.' namekw/n '(' list/l ')' { | '.' namekw/n '(' list/l ')' {
$argv = []; $$ = [ 'method', $n, $l ];
foreach ($l as $a) {
$argv[] = $a[0];
}
$$ = '->'.$n.'('.implode(', ', $argv).')';
} }
. .
varpath: { varpath: {
$$ = ''; $$ = [];
} }
| varpath/a varpart/p { | varpath/a varpart/p {
$$ = $a . $p; $a[] = $p;
$$ = $a;
} }
. .
namekw: name namekw: name

View File

@ -181,6 +181,8 @@ class VMXTemplateCompiler
} }
$this->lexer->set_code($code); $this->lexer->set_code($code);
$this->lexer->feed($this->parser); $this->lexer->feed($this->parser);
print json_encode($this->st->AST, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
exit;
if ($this->st->functions['main']['body'] === '') if ($this->st->functions['main']['body'] === '')
{ {
@ -658,6 +660,68 @@ $code
array_shift($args); array_shift($args);
return "array_map(function(\$arg) { return $fn; }, self::merge_to_array(".implode(", ", $args)."))"; return "array_map(function(\$arg) { return $fn; }, self::merge_to_array(".implode(", ", $args)."))";
} }
function track_dom($str, $pos)
{
static $st = 0;
static $quot = '';
static $tagre = '[a-z_:][a-z0-9\\.\\-_:]*';
static $tree = [];
print "code position $pos\n";
while ($str !== '' && $str !== false)
{
if ($st == 0 && preg_match(
"#^[^<]*<($tagre)(?:\s+$tagre\s*=\s*".
'(?:"[^"]*"|\'[^\']*\'|[^\s>"\'][^\s>]*))*(\s*(/?)>)?#is', $str, $m, PREG_OFFSET_CAPTURE))
{
// opening or enclosed tag + closed attributes
$st = !empty($m[2][0]) ? 0 : 1;
print (!empty($m[3][0]) ? 'enclosed' : ($st ? 'enter tag attrs' : 'open'))." <".$m[1][0].">\n";
$str = substr($str, $m[0][1]+strlen($m[0][0]));
}
elseif ($st == 0 && preg_match("#</($tagre)\s*>#is", $str, $m, PREG_OFFSET_CAPTURE))
{
// closing tag
print "leave <".$m[1][0].">\n";
$str = substr($str, $m[0][1]+strlen($m[0][0]));
}
elseif ($st == 1 && preg_match("#^\s+($tagre)\s*=\s*(\"[^\"]*|\'[^\']*)$#is", $str, $m, PREG_OFFSET_CAPTURE))
{
// enter attribute
$st = 2;
$quot = $m[2][0]{0};
print "enter attr '".$m[1][0]."', prefix=".substr($m[2][0], 1)."\n";
$str = substr($str, $m[0][1]+strlen($m[0][0]));
}
elseif ($st == 2)
{
if (preg_match("#^([^$quot]*)$quot#s", $str, $m, PREG_OFFSET_CAPTURE))
{
// leave attribute
$st = 1;
print "exit attr, suffix=".$m[1][0]."\n";
$str = substr($str, $m[0][1]+strlen($m[0][0]));
}
else
{
// continue attribute
print "continue attr, infix=$str\n";
$str = '';
}
}
elseif ($st == 1 && preg_match("#^(?:$tagre\s*=\s*".
'(?:"[^"]*"|\'[^\']*\'|[^\s>"\'][^\s>]*))*(\s*(/?)>)?$#is', $str, $m, PREG_OFFSET_CAPTURE) && $m[0])
{
$st = $m[1][0] ? 0 : 1;
print ($m[1][0] ? 'leave ' : 'continue ').($m[2][0] ? 'enclosed' : 'opening')." tag attrs\n";
$str = substr($str, $m[0][1]+strlen($m[0][0]));
}
else
{
$str = '';
}
}
}
} }
/** /**
@ -752,6 +816,11 @@ class VMXTemplateLexer
return " in {$this->options->input_filename}, line ".($this->lineno+1).", byte {$this->pos}, marked by ^^^ in $line"; return " in {$this->options->input_filename}, line ".($this->lineno+1).", byte {$this->pos}, marked by ^^^ in $line";
} }
function errorpos()
{
return [ 'E', $this->pos ];
}
function warn($text) function warn($text)
{ {
$this->options->error($text.$this->errorinfo()); $this->options->error($text.$this->errorinfo());
@ -794,7 +863,7 @@ class VMXTemplateLexer
$this->force_literal = 0; $this->force_literal = 0;
if ($code_pos === false && $subst_pos === false) if ($code_pos === false && $subst_pos === false)
{ {
$r = array('literal', "'".addcslashes(substr($this->code, $this->pos), "'\\")."'"); $r = array('literal', substr($this->code, $this->pos));
$this->lineno += substr_count($r[1], "\n"); $this->lineno += substr_count($r[1], "\n");
$this->pos = $this->codelen; $this->pos = $this->codelen;
} }
@ -809,7 +878,7 @@ class VMXTemplateLexer
{ {
$str = preg_replace('/\n[ \t]*$/s', $was_code ? '' : "\n", $str); $str = preg_replace('/\n[ \t]*$/s', $was_code ? '' : "\n", $str);
} }
$r = array('literal', "'".addcslashes($str, "'\\")."'"); $r = array('literal', $str);
$this->lineno += substr_count($r[1], "\n"); $this->lineno += substr_count($r[1], "\n");
$this->pos = $code_pos; $this->pos = $code_pos;
} }
@ -841,7 +910,7 @@ class VMXTemplateLexer
// Substitution is closer // Substitution is closer
if ($subst_pos > $this->pos) if ($subst_pos > $this->pos)
{ {
$r = array('literal', "'".addcslashes(substr($this->code, $this->pos, $subst_pos-$this->pos), "'\\")."'"); $r = array('literal', substr($this->code, $this->pos, $subst_pos-$this->pos));
$this->lineno += substr_count($r[1], "\n"); $this->lineno += substr_count($r[1], "\n");
$this->pos = $subst_pos; $this->pos = $subst_pos;
} }
@ -894,7 +963,7 @@ class VMXTemplateLexer
$t = str_replace('$', '\\$', $t); $t = str_replace('$', '\\$', $t);
} }
$this->pos += strlen($m[0]); $this->pos += strlen($m[0]);
return array('literal', $t); return array('literal', eval("return $t;"));
} }
else else
{ {
@ -3446,7 +3515,7 @@ class VMXTemplateParser extends lime_parser {
// (0) template := chunks // (0) template := chunks
$result = reset($tokens); $result = reset($tokens);
$this->template->st->functions['main']['body'] = "function fn_main() {\$stack = array();\n\$t = '';\n".$tokens[0]."\nreturn \$t;\n}\n"; $this->template->st->AST = $tokens[0];
$result = ''; $result = '';
} }
@ -3454,21 +3523,24 @@ class VMXTemplateParser extends lime_parser {
// (1) chunks := ε // (1) chunks := ε
$result = reset($tokens); $result = reset($tokens);
$result = ''; $result = [];
} }
function reduce_2_chunks_2($tokens, &$result) { function reduce_2_chunks_2($tokens, &$result) {
// (2) chunks := chunks chunk // (2) chunks := chunks chunk
$result = reset($tokens); $result = reset($tokens);
$result = $tokens[0] . $tokens[1]; $result = $tokens[0];
if ($tokens[1]) {
$result[] = $tokens[1];
}
} }
function reduce_3_chunk_1($tokens, &$result) { function reduce_3_chunk_1($tokens, &$result) {
// (3) chunk := literal // (3) chunk := literal
$result = reset($tokens); $result = reset($tokens);
$result = ($tokens[0] != "''" && $tokens[0] != '""' ? '$t .= ' . $tokens[0] . ";\n" : ''); $result = [ 'literal', $tokens[0] ];
} }
function reduce_4_chunk_2($tokens, &$result) { function reduce_4_chunk_2($tokens, &$result) {
@ -3484,7 +3556,7 @@ class VMXTemplateParser extends lime_parser {
$result = reset($tokens); $result = reset($tokens);
$e = &$tokens[1]; $e = &$tokens[1];
$result = '$t .= ' . ($e[1] || !$this->template->options->auto_escape ? $e[0] : $this->template->compile_function($this->template->options->auto_escape, [ $e ])[0]) . ";\n"; $result = [ 'subst', $e ];
} }
function reduce_6_chunk_4($tokens, &$result) { function reduce_6_chunk_4($tokens, &$result) {
@ -3492,7 +3564,7 @@ class VMXTemplateParser extends lime_parser {
$result = reset($tokens); $result = reset($tokens);
$e = &$tokens[0]; $e = &$tokens[0];
$result = ''; $result = false;
} }
function reduce_7_code_chunk_1($tokens, &$result) { function reduce_7_code_chunk_1($tokens, &$result) {
@ -3520,7 +3592,7 @@ class VMXTemplateParser extends lime_parser {
$result = reset($tokens); $result = reset($tokens);
$e = &$tokens[0]; $e = &$tokens[0];
$result = '$t .= ' . ($e[1] || !$this->template->options->auto_escape ? $e[0] : $this->template->compile_function($this->template->options->auto_escape, [ $e ])[0]) . ";\n"; $result = [ 'subst', $e ];
} }
function reduce_12_c_if_1($tokens, &$result) { function reduce_12_c_if_1($tokens, &$result) {
@ -3529,7 +3601,7 @@ class VMXTemplateParser extends lime_parser {
$e = &$tokens[1]; $e = &$tokens[1];
$if = &$tokens[3]; $if = &$tokens[3];
$result = "if (" . $e[0] . ") {\n" . $if . "}\n"; $result = [ 'if', $e, $if ];
} }
function reduce_13_c_if_2($tokens, &$result) { function reduce_13_c_if_2($tokens, &$result) {
@ -3539,7 +3611,7 @@ class VMXTemplateParser extends lime_parser {
$if = &$tokens[3]; $if = &$tokens[3];
$else = &$tokens[7]; $else = &$tokens[7];
$result = "if (" . $e[0] . ") {\n" . $if . "} else {\n" . $else . "}\n"; $result = [ 'if', $e, $if, 'else', $else ];
} }
function reduce_14_c_if_3($tokens, &$result) { function reduce_14_c_if_3($tokens, &$result) {
@ -3550,7 +3622,7 @@ class VMXTemplateParser extends lime_parser {
$ei = &$tokens[4]; $ei = &$tokens[4];
$ec = &$tokens[5]; $ec = &$tokens[5];
$result = "if (" . $e[0] . ") {\n" . $if . $ei . $ec . "}\n"; $result = array_merge([ 'if', $e, $if ], $ei, [ $ec ]);
} }
function reduce_15_c_if_4($tokens, &$result) { function reduce_15_c_if_4($tokens, &$result) {
@ -3562,7 +3634,7 @@ class VMXTemplateParser extends lime_parser {
$ec = &$tokens[5]; $ec = &$tokens[5];
$else = &$tokens[9]; $else = &$tokens[9];
$result = "if (" . $e[0] . ") {\n" . $if . $ei . $ec . "} else {\n" . $else . "}\n"; $result = array_merge([ 'if', $e, $if ], $ei, [ $ec, 'else', $else ]);
} }
function reduce_16_c_elseifs_1($tokens, &$result) { function reduce_16_c_elseifs_1($tokens, &$result) {
@ -3570,7 +3642,7 @@ class VMXTemplateParser extends lime_parser {
$result = reset($tokens); $result = reset($tokens);
$e = &$tokens[2]; $e = &$tokens[2];
$result = "} elseif (" . $e[0] . ") {\n"; $result = [ 'elseif', $e ];
} }
function reduce_17_c_elseifs_2($tokens, &$result) { function reduce_17_c_elseifs_2($tokens, &$result) {
@ -3580,7 +3652,7 @@ class VMXTemplateParser extends lime_parser {
$cs = &$tokens[1]; $cs = &$tokens[1];
$e = &$tokens[4]; $e = &$tokens[4];
$result = $p . $cs . "} elseif (" . $e[0] . ") {\n"; $result = array_merge($p, [ $cs, $e ]);
} }
function reduce_18_c_set_1($tokens, &$result) { function reduce_18_c_set_1($tokens, &$result) {
@ -3589,7 +3661,7 @@ class VMXTemplateParser extends lime_parser {
$v = &$tokens[1]; $v = &$tokens[1];
$e = &$tokens[3]; $e = &$tokens[3];
$result = $v[0] . ' = ' . $e[0] . ";\n"; $result = [ 'set', $v, [ 'subst', $e ] ];
} }
function reduce_19_c_set_2($tokens, &$result) { function reduce_19_c_set_2($tokens, &$result) {
@ -3598,7 +3670,7 @@ class VMXTemplateParser extends lime_parser {
$v = &$tokens[1]; $v = &$tokens[1];
$cs = &$tokens[3]; $cs = &$tokens[3];
$result = "\$stack[] = \$t;\n\$t = '';\n" . $cs . $v[0] . " = \$t;\n\$t = array_pop(\$stack);\n"; $result = [ 'set', $v, $cs ];
} }
function reduce_20_c_fn_1($tokens, &$result) { function reduce_20_c_fn_1($tokens, &$result) {
@ -3608,14 +3680,7 @@ class VMXTemplateParser extends lime_parser {
$args = &$tokens[3]; $args = &$tokens[3];
$exp = &$tokens[6]; $exp = &$tokens[6];
$this->template->st->functions[$name] = array( $result = [ 'function', $name, $args, [ 'subst', $exp ] ];
'name' => $name,
'args' => $args,
'body' => 'function fn_'.$name." () {\nreturn ".$exp.";\n}\n",
//'line' => $line, Ой, я чо - аргументы не юзаю?
//'pos' => $pos,
);
$result = '';
} }
function reduce_21_c_fn_2($tokens, &$result) { function reduce_21_c_fn_2($tokens, &$result) {
@ -3625,14 +3690,7 @@ class VMXTemplateParser extends lime_parser {
$args = &$tokens[3]; $args = &$tokens[3];
$cs = &$tokens[6]; $cs = &$tokens[6];
$this->template->st->functions[$name] = array( $result = [ 'function', $name, $args, $cs ];
'name' => $name,
'args' => $args,
'body' => 'function fn_'.$name." () {\$stack = array();\n\$t = '';\n".$cs."\nreturn \$t;\n}\n",
//'line' => $line,
//'pos' => $pos,
);
$result = '';
} }
function reduce_22_c_for_1($tokens, &$result) { function reduce_22_c_for_1($tokens, &$result) {
@ -3642,18 +3700,7 @@ class VMXTemplateParser extends lime_parser {
$exp = &$tokens[3]; $exp = &$tokens[3];
$cs = &$tokens[5]; $cs = &$tokens[5];
$varref_index = substr($varref[0], 0, -1) . ".'_index']"; $result = [ 'for', $varref, $exp, $cs ];
$result = "\$stack[] = ".$varref[0].";
\$stack[] = ".$varref_index.";
\$stack[] = 0;
foreach ((array)($exp[0]) as \$item) {
".$varref[0]." = \$item;
".$varref_index." = \$stack[count(\$stack)-1]++;
".$cs."}
array_pop(\$stack);
".$varref_index." = array_pop(\$stack);
".$varref[0]." = array_pop(\$stack);
";
} }
function reduce_23_fn_1($tokens, &$result) { function reduce_23_fn_1($tokens, &$result) {
@ -3702,7 +3749,7 @@ class VMXTemplateParser extends lime_parser {
$a = &$tokens[0]; $a = &$tokens[0];
$b = &$tokens[2]; $b = &$tokens[2];
$result = [ '(' . $a[0] . ' . ' . $b[0] . ')', $a[1] && $b[1] ]; $result = [ 'op', '.', $a, $b ];
} }
function reduce_32_exp_2($tokens, &$result) { function reduce_32_exp_2($tokens, &$result) {
@ -3711,7 +3758,7 @@ class VMXTemplateParser extends lime_parser {
$a = &$tokens[0]; $a = &$tokens[0];
$b = &$tokens[2]; $b = &$tokens[2];
$result = [ '(' . $a[0] . ' ?: ' . $b[0] . ')', $a[1] && $b[1] ]; $result = [ 'op', '||', $a, $b ];
} }
function reduce_33_exp_3($tokens, &$result) { function reduce_33_exp_3($tokens, &$result) {
@ -3720,7 +3767,7 @@ class VMXTemplateParser extends lime_parser {
$a = &$tokens[0]; $a = &$tokens[0];
$b = &$tokens[2]; $b = &$tokens[2];
$result = [ '(' . $a[0] . ' ?: ' . $b[0] . ')', $a[1] && $b[1] ]; $result = [ 'op', '||', $a, $b ];
} }
function reduce_34_exp_4($tokens, &$result) { function reduce_34_exp_4($tokens, &$result) {
@ -3729,7 +3776,7 @@ class VMXTemplateParser extends lime_parser {
$a = &$tokens[0]; $a = &$tokens[0];
$b = &$tokens[2]; $b = &$tokens[2];
$result = [ '(' . $a[0] . ' XOR ' . $b[0] . ')', true ]; $result = [ 'op', 'XOR', $a, $b ];
} }
function reduce_35_exp_5($tokens, &$result) { function reduce_35_exp_5($tokens, &$result) {
@ -3738,7 +3785,7 @@ class VMXTemplateParser extends lime_parser {
$a = &$tokens[0]; $a = &$tokens[0];
$b = &$tokens[2]; $b = &$tokens[2];
$result = [ '(' . $a[0] . ' && ' . $b[0] . ')', true ]; $result = [ 'op', '&&', $a, $b ];
} }
function reduce_36_exp_6($tokens, &$result) { function reduce_36_exp_6($tokens, &$result) {
@ -3747,7 +3794,7 @@ class VMXTemplateParser extends lime_parser {
$a = &$tokens[0]; $a = &$tokens[0];
$b = &$tokens[2]; $b = &$tokens[2];
$result = [ '(' . $a[0] . ' && ' . $b[0] . ')', true ]; $result = [ 'op', '&&', $a, $b ];
} }
function reduce_37_exp_7($tokens, &$result) { function reduce_37_exp_7($tokens, &$result) {
@ -3756,7 +3803,7 @@ class VMXTemplateParser extends lime_parser {
$a = &$tokens[0]; $a = &$tokens[0];
$b = &$tokens[2]; $b = &$tokens[2];
$result = [ '(' . $a[0] . ' == ' . $b[0] . ')', true ]; $result = [ 'op', '==', $a, $b ];
} }
function reduce_38_exp_8($tokens, &$result) { function reduce_38_exp_8($tokens, &$result) {
@ -3765,7 +3812,7 @@ class VMXTemplateParser extends lime_parser {
$a = &$tokens[0]; $a = &$tokens[0];
$b = &$tokens[2]; $b = &$tokens[2];
$result = [ '(' . $a[0] . ' != ' . $b[0] . ')', true ]; $result = [ 'op', '!=', $a, $b ];
} }
function reduce_39_exp_9($tokens, &$result) { function reduce_39_exp_9($tokens, &$result) {
@ -3774,7 +3821,7 @@ class VMXTemplateParser extends lime_parser {
$a = &$tokens[0]; $a = &$tokens[0];
$b = &$tokens[2]; $b = &$tokens[2];
$result = [ '(' . $a[0] . ' < ' . $b[0] . ')', true ]; $result = [ 'op', '<', $a, $b ];
} }
function reduce_40_exp_10($tokens, &$result) { function reduce_40_exp_10($tokens, &$result) {
@ -3783,7 +3830,7 @@ class VMXTemplateParser extends lime_parser {
$a = &$tokens[0]; $a = &$tokens[0];
$b = &$tokens[2]; $b = &$tokens[2];
$result = [ '(' . $a[0] . ' > ' . $b[0] . ')', true ]; $result = [ 'op', '>', $a, $b ];
} }
function reduce_41_exp_11($tokens, &$result) { function reduce_41_exp_11($tokens, &$result) {
@ -3792,7 +3839,7 @@ class VMXTemplateParser extends lime_parser {
$a = &$tokens[0]; $a = &$tokens[0];
$b = &$tokens[2]; $b = &$tokens[2];
$result = [ '(' . $a[0] . ' <= ' . $b[0] . ')', true ]; $result = [ 'op', '<=', $a, $b ];
} }
function reduce_42_exp_12($tokens, &$result) { function reduce_42_exp_12($tokens, &$result) {
@ -3801,7 +3848,7 @@ class VMXTemplateParser extends lime_parser {
$a = &$tokens[0]; $a = &$tokens[0];
$b = &$tokens[2]; $b = &$tokens[2];
$result = [ '(' . $a[0] . ' >= ' . $b[0] . ')', true ]; $result = [ 'op', '>=', $a, $b ];
} }
function reduce_43_exp_13($tokens, &$result) { function reduce_43_exp_13($tokens, &$result) {
@ -3810,7 +3857,7 @@ class VMXTemplateParser extends lime_parser {
$a = &$tokens[0]; $a = &$tokens[0];
$b = &$tokens[2]; $b = &$tokens[2];
$result = [ '(' . $a[0] . ' + ' . $b[0] . ')', true ]; $result = [ 'op', '+', $a, $b ];
} }
function reduce_44_exp_14($tokens, &$result) { function reduce_44_exp_14($tokens, &$result) {
@ -3819,7 +3866,7 @@ class VMXTemplateParser extends lime_parser {
$a = &$tokens[0]; $a = &$tokens[0];
$b = &$tokens[2]; $b = &$tokens[2];
$result = [ '(' . $a[0] . ' - ' . $b[0] . ')', true ]; $result = [ 'op', '-', $a, $b ];
} }
function reduce_45_exp_15($tokens, &$result) { function reduce_45_exp_15($tokens, &$result) {
@ -3828,7 +3875,7 @@ class VMXTemplateParser extends lime_parser {
$a = &$tokens[0]; $a = &$tokens[0];
$b = &$tokens[2]; $b = &$tokens[2];
$result = [ '(' . $a[0] . ' & ' . $b[0] . ')', true ]; $result = [ 'op', '&', $a, $b ];
} }
function reduce_46_exp_16($tokens, &$result) { function reduce_46_exp_16($tokens, &$result) {
@ -3837,7 +3884,7 @@ class VMXTemplateParser extends lime_parser {
$a = &$tokens[0]; $a = &$tokens[0];
$b = &$tokens[2]; $b = &$tokens[2];
$result = [ '(' . $a[0] . ' * ' . $b[0] . ')', true ]; $result = [ 'op', '*', $a, $b ];
} }
function reduce_47_exp_17($tokens, &$result) { function reduce_47_exp_17($tokens, &$result) {
@ -3846,7 +3893,7 @@ class VMXTemplateParser extends lime_parser {
$a = &$tokens[0]; $a = &$tokens[0];
$b = &$tokens[2]; $b = &$tokens[2];
$result = [ '(' . $a[0] . ' / ' . $b[0] . ')', true ]; $result = [ 'op', '/', $a, $b ];
} }
function reduce_48_exp_18($tokens, &$result) { function reduce_48_exp_18($tokens, &$result) {
@ -3855,7 +3902,7 @@ class VMXTemplateParser extends lime_parser {
$a = &$tokens[0]; $a = &$tokens[0];
$b = &$tokens[2]; $b = &$tokens[2];
$result = [ '(' . $a[0] . ' % ' . $b[0] . ')', true ]; $result = [ 'op', '%', $a, $b ];
} }
function reduce_49_exp_19($tokens, &$result) { function reduce_49_exp_19($tokens, &$result) {
@ -3873,12 +3920,12 @@ class VMXTemplateParser extends lime_parser {
$result = reset($tokens); $result = reset($tokens);
$a = &$tokens[1]; $a = &$tokens[1];
$result = [ '(-'.$a[0].')', true ]; $result = [ 'op', '-', $a ];
} }
function reduce_52_p11_1($tokens, &$result) { function reduce_52_p11_1($tokens, &$result) {
// (52) p11 := nonbrace // (52) p11 := nonbrace
$result = reset($tokens); $result = $tokens[0];
} }
function reduce_53_p11_2($tokens, &$result) { function reduce_53_p11_2($tokens, &$result) {
@ -3887,7 +3934,7 @@ class VMXTemplateParser extends lime_parser {
$e = &$tokens[1]; $e = &$tokens[1];
$p = &$tokens[3]; $p = &$tokens[3];
$result = [ ($p !== '' ? 'self::noop('.$e[0].')'.$p : '('.$e[0].')'), false ]; $result = [ 'varpath', $e, $p ];
} }
function reduce_54_p11_3($tokens, &$result) { function reduce_54_p11_3($tokens, &$result) {
@ -3895,7 +3942,7 @@ class VMXTemplateParser extends lime_parser {
$result = reset($tokens); $result = reset($tokens);
$a = &$tokens[1]; $a = &$tokens[1];
$result = [ '(!'.$a[0].')', true ]; $result = [ 'op', '!', $a ];
} }
function reduce_55_p11_4($tokens, &$result) { function reduce_55_p11_4($tokens, &$result) {
@ -3903,22 +3950,17 @@ class VMXTemplateParser extends lime_parser {
$result = reset($tokens); $result = reset($tokens);
$a = &$tokens[1]; $a = &$tokens[1];
$result = [ '(!'.$a[0].')', true ]; $result = [ 'op', '!', $a ];
} }
function reduce_56_nonbrace_1($tokens, &$result) { function reduce_56_nonbrace_1($tokens, &$result) {
// (56) nonbrace := { hash } // (56) nonbrace := { hash }
$result = reset($tokens); $result = $tokens[1];
$h = &$tokens[1];
$result = [ 'array(' . $h . ')', true ];
} }
function reduce_57_nonbrace_2($tokens, &$result) { function reduce_57_nonbrace_2($tokens, &$result) {
// (57) nonbrace := literal // (57) nonbrace := literal
$result = reset($tokens); $result = $tokens[0];
$result = [ $tokens[0], true ];
} }
function reduce_58_nonbrace_3($tokens, &$result) { function reduce_58_nonbrace_3($tokens, &$result) {
@ -3931,7 +3973,7 @@ class VMXTemplateParser extends lime_parser {
$result = reset($tokens); $result = reset($tokens);
$f = &$tokens[0]; $f = &$tokens[0];
$result = $this->template->compile_function($f, []); $result = [ 'call', $f, [] ];
} }
function reduce_60_nonbrace_5($tokens, &$result) { function reduce_60_nonbrace_5($tokens, &$result) {
@ -3940,7 +3982,7 @@ class VMXTemplateParser extends lime_parser {
$f = &$tokens[0]; $f = &$tokens[0];
$args = &$tokens[2]; $args = &$tokens[2];
$result = $this->template->compile_function($f, $args); $result = [ 'call', $f, $args ];
} }
function reduce_61_nonbrace_6($tokens, &$result) { function reduce_61_nonbrace_6($tokens, &$result) {
@ -3949,7 +3991,7 @@ class VMXTemplateParser extends lime_parser {
$f = &$tokens[0]; $f = &$tokens[0];
$args = &$tokens[2]; $args = &$tokens[2];
$result = [ "\$this->parent->call_block('".addcslashes($f, "'\\")."', array(".$args."), '".addcslashes($this->template->lexer->errorinfo(), "'\\")."')", true ]; $result = [ 'call_block', $f, $args, $this->template->lexer->errorpos() ];
} }
function reduce_62_nonbrace_7($tokens, &$result) { function reduce_62_nonbrace_7($tokens, &$result) {
@ -3958,7 +4000,7 @@ class VMXTemplateParser extends lime_parser {
$f = &$tokens[0]; $f = &$tokens[0];
$arg = &$tokens[1]; $arg = &$tokens[1];
$result = $this->template->compile_function($f, [ $arg ]); $result = [ 'call', $f, [ $arg ] ];
} }
function reduce_63_list_1($tokens, &$result) { function reduce_63_list_1($tokens, &$result) {
@ -4006,7 +4048,10 @@ class VMXTemplateParser extends lime_parser {
function reduce_68_hash_1($tokens, &$result) { function reduce_68_hash_1($tokens, &$result) {
// (68) hash := pair // (68) hash := pair
$result = $tokens[0]; $result = reset($tokens);
$p = &$tokens[0];
$result = [ 'hash', $p ];
} }
function reduce_69_hash_2($tokens, &$result) { function reduce_69_hash_2($tokens, &$result) {
@ -4015,19 +4060,23 @@ class VMXTemplateParser extends lime_parser {
$p = &$tokens[0]; $p = &$tokens[0];
$h = &$tokens[2]; $h = &$tokens[2];
$result = $p . ', ' . $h; array_splice($h, 1, 0, [ $p ]);
$result = $h;
} }
function reduce_70_hash_3($tokens, &$result) { function reduce_70_hash_3($tokens, &$result) {
// (70) hash := ε // (70) hash := ε
$result = reset($tokens); $result = reset($tokens);
$result = ''; $result = [ 'hash' ];
} }
function reduce_71_gthash_1($tokens, &$result) { function reduce_71_gthash_1($tokens, &$result) {
// (71) gthash := gtpair // (71) gthash := gtpair
$result = $tokens[0]; $result = reset($tokens);
$p = &$tokens[0];
$result = [ 'hash', $p ];
} }
function reduce_72_gthash_2($tokens, &$result) { function reduce_72_gthash_2($tokens, &$result) {
@ -4036,7 +4085,8 @@ class VMXTemplateParser extends lime_parser {
$p = &$tokens[0]; $p = &$tokens[0];
$h = &$tokens[2]; $h = &$tokens[2];
$result = $p . ', ' . $h; array_splice($h, 1, 0, [ $p ]);
$result = $h;
} }
function reduce_73_pair_1($tokens, &$result) { function reduce_73_pair_1($tokens, &$result) {
@ -4045,7 +4095,7 @@ class VMXTemplateParser extends lime_parser {
$k = &$tokens[0]; $k = &$tokens[0];
$v = &$tokens[2]; $v = &$tokens[2];
$result = $k[0] . ' => ' . $v[0]; $result = [ $k, $v ];
} }
function reduce_74_pair_2($tokens, &$result) { function reduce_74_pair_2($tokens, &$result) {
@ -4059,7 +4109,7 @@ class VMXTemplateParser extends lime_parser {
$k = &$tokens[0]; $k = &$tokens[0];
$v = &$tokens[2]; $v = &$tokens[2];
$result = $k[0] . ' => ' . $v[0]; $result = [ $k, $v ];
} }
function reduce_76_varref_1($tokens, &$result) { function reduce_76_varref_1($tokens, &$result) {
@ -4067,7 +4117,7 @@ class VMXTemplateParser extends lime_parser {
$result = reset($tokens); $result = reset($tokens);
$n = &$tokens[0]; $n = &$tokens[0];
$result = [ "\$this->tpldata['".addcslashes($n, "\\\'")."']", false ]; $result = [ 'varref', $n ];
} }
function reduce_77_varref_2($tokens, &$result) { function reduce_77_varref_2($tokens, &$result) {
@ -4076,7 +4126,8 @@ class VMXTemplateParser extends lime_parser {
$v = &$tokens[0]; $v = &$tokens[0];
$p = &$tokens[1]; $p = &$tokens[1];
$result = [ $v[0] . $p, false ]; $v[] = $p;
$result = $v;
} }
function reduce_78_varpart_1($tokens, &$result) { function reduce_78_varpart_1($tokens, &$result) {
@ -4084,7 +4135,7 @@ class VMXTemplateParser extends lime_parser {
$result = reset($tokens); $result = reset($tokens);
$n = &$tokens[1]; $n = &$tokens[1];
$result = "['".addcslashes($n, "\\\'")."']"; $result = [ 'index', $n ];
} }
function reduce_79_varpart_2($tokens, &$result) { function reduce_79_varpart_2($tokens, &$result) {
@ -4092,7 +4143,7 @@ class VMXTemplateParser extends lime_parser {
$result = reset($tokens); $result = reset($tokens);
$e = &$tokens[1]; $e = &$tokens[1];
$result = '['.$e[0].']'; $result = [ 'index', $e ];
} }
function reduce_80_varpart_3($tokens, &$result) { function reduce_80_varpart_3($tokens, &$result) {
@ -4100,7 +4151,7 @@ class VMXTemplateParser extends lime_parser {
$result = reset($tokens); $result = reset($tokens);
$n = &$tokens[1]; $n = &$tokens[1];
$result = '->'.$n.'()'; $result = [ 'method', $n, [] ];
} }
function reduce_81_varpart_4($tokens, &$result) { function reduce_81_varpart_4($tokens, &$result) {
@ -4109,18 +4160,14 @@ class VMXTemplateParser extends lime_parser {
$n = &$tokens[1]; $n = &$tokens[1];
$l = &$tokens[3]; $l = &$tokens[3];
$argv = []; $result = [ 'method', $n, $l ];
foreach ($l as $a) {
$argv[] = $a[0];
}
$result = '->'.$n.'('.implode(', ', $argv).')';
} }
function reduce_82_varpath_1($tokens, &$result) { function reduce_82_varpath_1($tokens, &$result) {
// (82) varpath := ε // (82) varpath := ε
$result = reset($tokens); $result = reset($tokens);
$result = ''; $result = [];
} }
function reduce_83_varpath_2($tokens, &$result) { function reduce_83_varpath_2($tokens, &$result) {
@ -4129,7 +4176,8 @@ class VMXTemplateParser extends lime_parser {
$a = &$tokens[0]; $a = &$tokens[0];
$p = &$tokens[1]; $p = &$tokens[1];
$result = $a . $p; $a[] = $p;
$result = $a;
} }
function reduce_84_namekw_1($tokens, &$result) { function reduce_84_namekw_1($tokens, &$result) {
@ -4829,5 +4877,5 @@ class VMXTemplateParser extends lime_parser {
); );
} }
// Time: 4,2855579853058 seconds // Time: 2,8789830207825 seconds
// Memory: 11192016 bytes // Memory: 11154544 bytes

14
test.php Normal file
View File

@ -0,0 +1,14 @@
<?php
require 'template.php';
system('mkdir -p ./cache');
system('rm ./cache/tpl*.php');
$template = new VMXTemplate([
'cache_dir' => './cache',
'root' => '.',
'auto_escape' => 's',
]);
$template->parse('test.tpl');

13
test.tpl Normal file
View File

@ -0,0 +1,13 @@
<html>
<head>
</head>
<body>
<div>x{intro}y</div>
<select>
<!-- FOR o = options -->
<option value="{o.url}"<!-- IF o.selected --> selected="selected"<!-- END -->>{o.name}</option>
<!-- END -->
</select>
<span>{v.end('x', 'y')['z'].begin()}</span>
</body>
</html>