function tokenize($input) { $this->input = $input; if ($this->names) { $this->tokens = NString::matchAll($input, $this->re); $len = 0; foreach ($this->tokens as &$match) { $name = NULL; for ($i = 1; $i < count($this->names); $i++) { if (!isset($match[$i])) { break; } elseif ($match[$i] != NULL) { $name = $this->names[$i - 1]; break; } } $match = array($match[0], $name); $len += strlen($match[0]); } if ($len !== strlen($input)) { $errorOffset = $len; } } else { $this->tokens = NString::split($input, $this->re, PREG_SPLIT_NO_EMPTY); if ($this->tokens && !NString::match(end($this->tokens), $this->re)) { $tmp = NString::split($this->input, $this->re, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE); list(, $errorOffset) = end($tmp); } } if (isset($errorOffset)) { $line = $errorOffset ? substr_count($this->input, "\n", 0, $errorOffset) + 1 : 1; $col = $errorOffset - strrpos(substr($this->input, 0, $errorOffset), "\n") + 1; $token = str_replace("\n", '\\n', substr($input, $errorOffset, 10)); throw new NTokenizerException("Unexpected '{$token}' on line {$line}, column {$col}."); } return $this->tokens; }
function macroWidget($content) { $pair = NLatteFilter::fetchToken($content); if ($pair === NULL) { throw new InvalidStateException("Missing widget name in {widget} on line {$this->filter->line}."); } $pair = explode(':', $pair, 2); $widget = NLatteFilter::formatString($pair[0]); $method = isset($pair[1]) ? ucfirst($pair[1]) : ''; $method = NString::match($method, '#^(' . NLatteFilter::RE_IDENTIFIER . '|)$#') ? "render{$method}" : "{\"render{$method}\"}"; $param = NLatteFilter::formatArray($content); if (strpos($content, '=>') === FALSE) { $param = substr($param, 6, -1); } return ($widget[0] === '$' ? "if (is_object({$widget})) {$widget}->{$method}({$param}); else " : '') . "\$control->getWidget({$widget})->{$method}({$param})"; }