4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\TabCompletion\Matcher;
15 * Abstract tab completion Matcher.
17 * @author Marc Garcia <markcial@gmail.com>
19 abstract class AbstractMatcher
22 const CONSTANT_SYNTAX = '^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$';
23 const VAR_SYNTAX = '^\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$';
24 const MISC_OPERATORS = '+-*/^|&';
26 const T_OPEN_TAG = 'T_OPEN_TAG';
27 const T_VARIABLE = 'T_VARIABLE';
28 const T_OBJECT_OPERATOR = 'T_OBJECT_OPERATOR';
29 const T_DOUBLE_COLON = 'T_DOUBLE_COLON';
30 const T_NEW = 'T_NEW';
31 const T_CLONE = 'T_CLONE';
32 const T_NS_SEPARATOR = 'T_NS_SEPARATOR';
33 const T_STRING = 'T_STRING';
34 const T_WHITESPACE = 'T_WHITESPACE';
35 const T_AND_EQUAL = 'T_AND_EQUAL';
36 const T_BOOLEAN_AND = 'T_BOOLEAN_AND';
37 const T_BOOLEAN_OR = 'T_BOOLEAN_OR';
39 const T_ENCAPSED_AND_WHITESPACE = 'T_ENCAPSED_AND_WHITESPACE';
40 const T_REQUIRE = 'T_REQUIRE';
41 const T_REQUIRE_ONCE = 'T_REQUIRE_ONCE';
42 const T_INCLUDE = 'T_INCLUDE';
43 const T_INCLUDE_ONCE = 'T_INCLUDE_ONCE';
46 * Check whether this matcher can provide completions for $tokens.
48 * @param array $tokens Tokenized readline input
52 public function hasMatched(array $tokens)
58 * Get current readline input word.
60 * @param array $tokens Tokenized readline input (see token_get_all)
64 protected function getInput(array $tokens)
67 $firstToken = array_pop($tokens);
68 if (self::tokenIs($firstToken, self::T_STRING)) {
69 $var = $firstToken[1];
76 * Get current namespace and class (if any) from readline input.
78 * @param array $tokens Tokenized readline input (see token_get_all)
82 protected function getNamespaceAndClass($tokens)
85 while (self::hasToken(
86 [self::T_NS_SEPARATOR, self::T_STRING],
87 $token = array_pop($tokens)
89 $class = $token[1] . $class;
96 * Provide tab completion matches for readline input.
98 * @param array $tokens information substracted with get_token_all
99 * @param array $info readline_info object
101 * @return array The matches resulting from the query
103 abstract public function getMatches(array $tokens, array $info = []);
106 * Check whether $word starts with $prefix.
108 * @param string $prefix
109 * @param string $word
113 public static function startsWith($prefix, $word)
115 return preg_match(sprintf('#^%s#', $prefix), $word);
119 * Check whether $token matches a given syntax pattern.
121 * @param mixed $token A PHP token (see token_get_all)
122 * @param string $syntax A syntax pattern (default: variable pattern)
126 public static function hasSyntax($token, $syntax = self::VAR_SYNTAX)
128 if (!is_array($token)) {
132 $regexp = sprintf('#%s#', $syntax);
134 return (bool) preg_match($regexp, $token[1]);
138 * Check whether $token type is $which.
140 * @param string $which A PHP token type
141 * @param mixed $token A PHP token (see token_get_all)
145 public static function tokenIs($token, $which)
147 if (!is_array($token)) {
151 return token_name($token[0]) === $which;
155 * Check whether $token is an operator.
157 * @param mixed $token A PHP token (see token_get_all)
161 public static function isOperator($token)
163 if (!is_string($token)) {
167 return strpos(self::MISC_OPERATORS, $token) !== false;
171 * Check whether $token type is present in $coll.
173 * @param array $coll A list of token types
174 * @param mixed $token A PHP token (see token_get_all)
178 public static function hasToken(array $coll, $token)
180 if (!is_array($token)) {
184 return in_array(token_name($token[0]), $coll);