/** * * @param TokenStream $stream * @param int $indention * @return TokenStreamBuilder */ public function addStream(TokenStream $stream, $indention = 1) { if ($indention > 0) { $stream->indent($indention); } $this->stream->append($stream); return $this; }
function scan($source) { //todo: track indentation $stream = new TokenStream(); $depth = 0; foreach (token_get_all($source) as $token) { if (is_array($token)) { list($token, $text) = $token; } elseif (is_string($token)) { $text = $token; $token = -1; } if ($token === T_CURLY_OPEN || $token === T_DOLLAR_OPEN_CURLY_BRACES || $text === '{') { ++$depth; } elseif ($text == '}') { --$depth; } $stream->append(new Token($text, $token, $depth)); } return $stream; }
/** * get substream * @param int $from * @param int $to */ public function get($from, $to) { $tokenStream = new TokenStream(); $tokenStream->append(array_slice($this->tokens, $from, $to - $from + 1)); return $tokenStream; }