/** * Parse the next token * * @return void */ protected function next() { $token = $this->currentToken(); if ($token->type !== 'foreach') { throw $this->errorUnexpectedToken($token); } $this->skipToken(); // If the next key is a comma we have to assign both key and value if ($this->nextToken()->type === 'comma') { $this->loop->setKeyVariable($this->parseChild('Variable')); // skip the comma $this->skipToken(); } $this->loop->setValueVariable($this->parseChild('Variable')); // skip so that we can parse the upcoming array or variable $this->skipToken(); // parse the upcoming expresssion $this->loop->setCollection($this->parseChild('Expression')); // we might have some linebreak $this->skipTokensOfType('linebreak'); // the current token now has to be a scope open so lets parse that scope if ($this->currentToken()->type !== 'scopeOpen') { throw $this->errorUnexpectedToken($this->currentToken()); } // now parse the rest of the scope if ($scopeTokens = $this->getTokensUntilClosingScope()) { $scopeParser = new Scope($scopeTokens); // assign the parsed children to the current tag foreach ($scopeParser->parse()->getChildren() as $child) { $this->loop->addChild($child); } $this->skipToken(); } return $this->node(); }
/** * Parse the next token * * @return void */ protected function next() { // check that a tag is being opend if ($this->currentToken()->type !== 'tagOpen') { throw $this->errorUnexpectedToken($this->currentToken()); } // the normal tag is baiscally a short tag with out the // tag tokens so we simply strip them $this->skipToken(); // get all tokens until tag close $tokens = $this->getTokensUntil('tagClose'); $this->skipToken(); // we might have tag recursion if ($this->currentToken() && $this->currentToken()->type === 'tagOpen') { $this->tag = $this->parseChild('ShortTag', $tokens, false); $this->tag->addChild($this->parseChild('Tag')); return $this->node(); } // and add the tokens until linebreak $tokens = array_merge($tokens, $this->getTokensUntil(array('linebreak', 'scopeOpen'))); // parse the short tag as base $this->tag = $this->parseChild('ShortTag', $tokens, false); // we might have some linebreak $this->skipTokensOfType('linebreak'); // is there a following scope if ($this->currentToken() && $this->currentToken()->type === 'scopeOpen') { // now parse the rest of the scope if ($scopeTokens = $this->getTokensUntilClosingScope()) { $scopeParse = new Scope($scopeTokens); // assign the parsed children to the current tag foreach ($scopeParse->parse()->getChildren() as $child) { $this->tag->addChild($child); } } } return $this->node(); }
/** * Parse tattoo code * * @throws Tattoo\Exception * * @param string $code * @return Node\Scope */ public function parse($code) { $lexer = new Lexer($code); $parser = new ScopeParser($lexer->tokens()); return $parser->parse(); }