/** * @return \Carrooi\Tokenizer\Matching\Matcher */ public function newInstance() { $parenthesisMatcher = new Matcher(); $parenthesisMatcher->select($parenthesisMatcher->expr()->notRequired(Lexer::T_WHITESPACE), $this->parenthesis()); $matcher = new Matcher(); $matcher->select(Lexer::T_NEW, Lexer::T_WHITESPACE, $matcher->expr()->anyOf($this->className(), Lexer::T_VARIABLE), $matcher->expr()->notRequired($parenthesisMatcher)); $matcher->map(new ResultMapping(function ($instance) { if (!$instance) { return null; } $parenthesis = null; // with parenthesis if (!Helpers::isValidToken($instance[count($instance) - 1])) { $parenthesis = $instance[count($instance) - 1][1]; } // ClassNameExpression or $variable $name = is_array($instance[2]) ? $instance[2]['value'] : $instance[2]; $class = new AST\NewInstanceExpression(Helpers::flattenTokens($instance), $name); $class->parenthesis = $parenthesis; return $class; })); return $matcher; }
/** * @param array $result * @param array $match * @return array */ private function mergeMatchToResult(array $result, $match) { if (is_array($match) && Helpers::isValidToken($match)) { $result[] = $match; } elseif (is_array($match)) { $result = array_merge($result, $match); } return $result; }
/** * @param \Carrooi\Tokenizer\Parsing\Lexer $lexer * @return array|null */ private function doMatch(Lexer $lexer) { reset($this->select); $result = []; $initPosition = $lexer->position; while (list($key, $select) = each($this->select)) { if ($lexer->position === $initPosition) { $match = $this->skipUntil($lexer, $select); } else { $match = $this->_matchToken($lexer, $select); } // not a first occurrence, reset searching from current position if ($match === false) { $result = []; $select = reset($this->select); next($this->select); $match = $this->skipUntil($lexer, $select); } if ($match === false) { return null; } elseif ($match === true) { $result[] = $lexer->lookahead; } elseif ($match === null) { $result[] = null; } elseif (is_array($match)) { if (Helpers::isValidToken($match)) { $result[] = $match; } else { $result = array_merge($result, $match); } } } return count($result) ? $this->mapResult($result) : null; }