public function addToken(Token &$token) { $this->children[] = $token; $token->setParent($this); return $token; }
/** * @param Stream $stream * @return Token[] */ public function compile(Stream &$stream) { $syntaxClassName = $this->getClassName(); $tokens = []; $tokensPropertyName = 'tokens'; $tokensOptions = $syntaxClassName::${$tokensPropertyName}; $tokenDefaultConfig = isset($tokensOptions[$this->defaultToken]) ? $tokensOptions[$this->defaultToken] : []; $token = new Token($this->defaultToken, $tokenDefaultConfig); $this->setCurrentToken($token); $parent = false; while ($charSet = $stream->selectCharSet()) { $current = $charSet->getCurrent(); //echo '[current: '.$current->getValue().', code: '.$current->getCode().']<br>' ."\n"; foreach ($this->getCurrentToken()->getIncludes() as $include) { $tokenConfig = $this->getToken($include); // Haven't configuration if (!$tokenConfig) { continue; } /* @var array $tokenConfig */ // Don't available this token as next if (isset($tokenConfig[Syntax::PROPERTY_PREVIOUS_TOKENS]) && !in_array($this->getCurrentToken()->getName(), $tokenConfig[Syntax::PROPERTY_PREVIOUS_TOKENS])) { continue; } // Start character not equal available if (isset($tokenConfig[Syntax::PROPERTY_START]) && !$charSet->getCurrent()->is($tokenConfig[Syntax::PROPERTY_START])) { continue; } // Check if second character not equal available if (isset($tokenConfig[Syntax::PROPERTY_AFTER_START]) && !$charSet->getNext()->is($tokenConfig[Syntax::PROPERTY_AFTER_START])) { continue; } $parent = $this->getCurrentToken(); $token = new Token($include, $tokenConfig); $token->setParent($parent); $this->setCurrentToken($token); $currentToken = $this->getCurrentToken(); $parent->addToken($currentToken); } foreach ($this->getCurrentToken()->getNext() as $next) { //echo('[next: '.$next.']'); $tokenConfig = $this->getToken($next); // Haven't configuration if (!$tokenConfig) { continue; } /* @var array $tokenConfig */ // Don't available this token as next if (!in_array($this->getCurrentToken()->getName(), $tokenConfig[Syntax::PROPERTY_PREVIOUS_TOKENS])) { continue; } // Start character not equal available if (isset($tokenConfig[Syntax::PROPERTY_START]) && !$charSet->getCurrent()->is($tokenConfig[Syntax::PROPERTY_START])) { continue; } // Check if second character not equal available if (isset($tokenConfig[Syntax::PROPERTY_AFTER_START]) && !$charSet->getNext()->is($tokenConfig[Syntax::PROPERTY_AFTER_START])) { continue; } $tokens[] = $this->getCurrentToken(); $this->setCurrentToken(new Token($next, $tokenConfig)); } if ($ends = $this->getCurrentToken()->getEnd()) { foreach ($ends as $end) { //echo '[end: '.$end.']<br>' ."\n"; if ($current->is($end)) { $this->getCurrentToken()->appendChar($current); if ($this->getCurrentToken()->isValid()) { if ($parent = $this->getCurrentToken()->getParent()) { $token = $parent; } else { $token = new Token($this->defaultToken, $tokenDefaultConfig); } $this->setCurrentToken($token); continue 2; } break; } } } $this->getCurrentToken()->appendChar($current); } $tokens[] = $this->getCurrentToken(); //foreach($tokens as $token){ /* @var Token $token */ // echo '['.$token->getName().':' . htmlentities($token->getValue()) . "]<br>\n"; //} return $tokens; }