protected static function getExpression($v, &$context, $pos)
{
$asis = $pos === 0;
// handle number
if (is_numeric($v)) {
return static::getLiteral(strval(1 * $v), $asis);
}
// handle double quoted string
if (preg_match('/^"(.*)"$/', $v, $matched)) {
return static::getLiteral(preg_replace('/([^\\\\])\\\\\\\\"/', '$1"', preg_replace('/^\\\\\\\\"/', '"', $matched[1])), $asis, true);
}
// handle single quoted string
if (preg_match('/^\\\\\'(.*)\\\\\'$/', $v, $matched)) {
return static::getLiteral($matched[1], $asis, true);
}
// handle boolean, null and undefined
if (preg_match('/^(true|false|null|undefined)$/', $v)) {
return static::getLiteral($v, $asis);
}
$ret = array();
$levels = 0;
// handle ..
if ($v === '..') {
$v = '../';
}
// Trace to parent for ../ N times
$v = preg_replace_callback('/\\.\\.\\//', function () use(&$levels) {
$levels++;
return '';
}, trim($v));
// remove ./ in path
$v = preg_replace('/\\.\\//', '', $v, -1, $scoped);
$strp = $pos !== 0 && $context['flags']['strpar'];
if ($levels && !$strp) {
$ret[] = $levels;
if (!$context['flags']['parent']) {
$context['error'][] = 'Do not support {{../var}}, you should do compile with LightnCandy::FLAG_PARENT flag';
}
$context['usedFeature']['parent']++;
}
if ($context['flags']['advar'] && preg_match('/\\]/', $v)) {
preg_match_all(static::VARNAME_SEARCH, $v, $matchedall);
} else {
preg_match_all('/([^\\.\\/]+)/', $v, $matchedall);
}
if ($v !== '.') {
$vv = implode('.', $matchedall[1]);
if (strlen($v) !== strlen($vv)) {
$context['error'][] = "Unexpected charactor in '{$v}' ! (should it be '{$vv}' ?)";
}
}
foreach ($matchedall[1] as $m) {
if ($context['flags']['advar'] && substr($m, 0, 1) === '[') {
$ret[] = substr($m, 1, -1);
} else {
if ((!$context['flags']['this'] || $m !== 'this') && $m !== '.') {
$ret[] = $m;
} else {
$scoped++;
}
}
}
if ($strp) {
return array(static::LITERAL, "'" . implode('.', $ret) . "'");
}
if ($scoped > 0 && $levels === 0 && count($ret) > 0) {
array_unshift($ret, 0);
}
return $ret;
}