/** * Parses the Token passed or creates a root Token based on the $stream of this Parser * @param AbstractToken $token * @param string $dataBefore * @return Parser * @throws \RuntimeException */ public function parse(AbstractToken $token = null, $dataBefore = null) { // If this is the root parser if ($token === null) { $tokeners = $this->lexer->getCreatedTokeners($this->stream); $dataTker = $this->lexer->getCreatedDataTokener($this->stream); $token = Lexer::tokenize($tokeners, $dataTker); $this->token = $token; } else { if ($this->token === null) { $this->token = $this->lexer->getCreatedDataTokener($dataBefore)->setParent($token->getParent())->autoClose(); $this->token = $this->composeToken($this->token->getStream(true)); } } // processing the Token $token->process($this); if ($this->token instanceof ComposedToken) { $this->token->addToken($token); } // start parsing the identical level of remaining stream $remaining = $token->getRemainingStream(); while ($remaining !== '') { // create the appropiate Token by remaining stream $tokeners = $this->lexer->getCreatedTokeners($remaining); $dataTker = $this->lexer->getCreatedDataTokener($remaining); $token = Lexer::tokenize($tokeners, $dataTker)->setParent($this->token->getParent())->process($this); // switch to composed token first if more processable data found in stream if (!$this->token instanceof ComposedToken) { $this->token = $this->composeToken($this->token->getStream(true)); } // add this new Token to the composition $this->token->addToken($token); // getting new state of remaining stream based on processed Token above $remaining = $token->getRemainingStream(); } return $this; }
/** * This is the core iteration of process() method * @param Lexer $lexer * @return int The returning position-offset for next iteration (will be incremented by 1) */ protected function iteration(Lexer $lexer) { $offset = 0; // stop processing when simple closing sequence found if ($this->identifyCloseTag()) { $this->close(); return $offset; } // create a token on current position for checking if there's a sub-token $substream = $this->getSubStream($this->getCursor()); $tokeners = $lexer->getCreatedTokeners($substream); $dataTker = $lexer->getCreatedDataTokener($substream); $token = Lexer::tokenize($tokeners, $dataTker); // check if a sub-token found if (!$token->isDataStream()) { // getting a new instance of used Parser $dataBefore = $this->getSubStream(0, $this->getCursor()); $this->subparser = $this->getParser()->getNewInstance($substream)->parse($token->setParent($this), $dataBefore); // incrementing the position of processing to the end of the sub-token's closing tag $offset = $this->subparser->getToken()->getLength(); $this->setCursor($offset); $this->close(true); // next iteration increments position to continue the processing } return $offset; }