public function parsePrimary() { //unary if ($this->inUnaryOperator($this->tokenStream->lookNext())) { return $this->parseUnary(); } //bracket if ($this->tokenStream->test(Token::TYPE_PUNCTUATION, '(')) { return $this->parseBracket(); } //array if ($this->tokenStream->test(Token::TYPE_PUNCTUATION, '[') || $this->tokenStream->test(Token::TYPE_PUNCTUATION, '{')) { return $this->parseArray(); } if ($this->tokenStream->test(Token::TYPE_NAME)) { //constant val eg: null if (in_array(strtolower($this->tokenStream->lookNext()->getValue()), ['null', 'true', 'false'])) { return new ConstantNode($this->tokenStream->next()); } else { //function eg: test() return $this->parseFunction(); } } //attribute eg: $x | $x.varName | $x.func(1,2) | $x["y"] if ($this->tokenStream->test(Token::TYPE_VARIABLE)) { return $this->parseAttributeOrMethod(); } $token = $this->tokenStream->next(); switch ($token->getType()) { case Token::TYPE_NUMBER: case Token::TYPE_STRING: return new ConstantNode($token); default: throw new ParseException($this->tokenStream->getFileName(), 'unexpected ' . $token->getValue(), $token->line, $token->col); } }
/** * @param $tokenStream TokenStream * @return AbstractNode */ public function parse(TokenStream $tokenStream) { $this->tokenStream = $tokenStream; $bodyNode = $this->parseBody(null); return new RootNode($tokenStream->getFileName(), $bodyNode->childNodes); }