/** * Matches all occurrences and returns token list * * @param string $source Source to match tokens * * @param TokenFactoryInterface $factory * * @return \Generator */ public function match($source, TokenFactoryInterface $factory) { $all = []; foreach ($this->multiLine as $name => $comment) { $comment = array_map(function ($e) { return preg_quote($e, '/'); }, $comment); $all[] = [$name, "/({$comment[0]}(.*?){$comment[1]})/ms"]; } foreach ($this->singleLine as $name => $comment) { $comment = preg_quote($comment, '/'); $all[] = [$name, "/({$comment}[^\r\n]*)/"]; } foreach ($all as $i => $comment) { $matches = []; $name = $comment[0]; $name = is_string($name) ? $name : null; $regex = $comment[1]; if (preg_match_all($regex, $source, $matches, PREG_OFFSET_CAPTURE)) { foreach ($matches[1] as $match) { (yield $factory->create($name, ['pos' => $match[1], 'length' => strlen($match[0])])); } } } }
/** * Matches all occurrences and returns token list * * @param string $source Source to match tokens * * @param TokenFactoryInterface $factory * * @return \Generator */ public function match($source, TokenFactoryInterface $factory) { $pos = 0; $len = strlen($this->_substr); while (($pos = strpos($source, $this->_substr, $pos)) !== false) { (yield $factory->create(Token::NAME, ['pos' => $pos, 'length' => $len])); $pos += $len; } }
/** * Matches all occurrences and returns token list * * @param string $source Source to match tokens * * @param TokenFactoryInterface $factory * * @return \Generator */ public function match($source, TokenFactoryInterface $factory) { preg_match_all($this->regex, $source, $matches, PREG_OFFSET_CAPTURE); $matches = array_intersect_key($matches, $this->groups); foreach ($matches as $id => $group) { $name = $this->groups[$id]; foreach ($group as $match) { if (empty($match) || $match[1] === -1) { continue; } (yield $factory->create($name, ['pos' => $match[1], 'length' => strlen($match[0])])); } } }
/** * Matches all occurrences and returns token list * * @param string $source Source to match tokens * * @param TokenFactoryInterface $factory * * @return \Generator */ public function match($source, TokenFactoryInterface $factory) { (yield $factory->create(Token::NAME, ['pos' => 0, 'length' => strlen($source)])); }