public function subparse($test = null) { $nodes = []; while (!$this->stream->isEOF()) { $token = $this->stream->getToken(); $node = null; if ($token->test([TokenTypes::T_CODE_BLOCK, TokenTypes::T_EOF])) { $this->stream->next(); continue; } if ($test && call_user_func($test, $this->stream)) { break; } $found = false; foreach ($this->parsers as $parser) { if ($parser->canParse($token, $this)) { $found = true; $node = $parser->parse($token, $this); break; } } if (!$found) { throw new Exception('Syntax error, invalid syntax \'%s\' (%s) on line %d column %d', $token->getValue(), $token->getTokenName(), $token->getLine(), $token->getColumn()); } if ($node) { $nodes[] = $node; } } return new Node($nodes, [], 0); }
protected function parsePrimary() { $node = null; $token = $this->stream->getCurrent(); switch ($token->getType()) { case TokenTypes::T_STR: $node = $this->parseString(); break; case TokenTypes::T_NUM: $node = $this->parseNumber(); break; case TokenTypes::T_BOOLEAN: $node = $this->parseBoolean(); break; case TokenTypes::T_NULL: $node = $this->parseNull(); break; case TokenTypes::T_NAME: if ($this->stream->nextIf(TokenTypes::T_OPEN_PARAN)) { // Call $node = $this->parseFunction($token->getName(), $token->getLine()); } else { // Variable $this->stream->next(); $node = new Node\Expression\Name($token->getValue(), $token->getLine()); } break; case TokenTypes::T_OPEN_BRACE: case TokenTypes::T_OPEN_BRACKET: $node = $this->parseArray(); break; } return $this->parsePostfix($node); }