a9a093fca3493c5728e0ab2aa6680db6495e4963
[yaffs-website] / vendor / psy / psysh / src / TabCompletion / Matcher / AbstractMatcher.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2018 Justin Hileman
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Psy\TabCompletion\Matcher;
13
14 /**
15  * Abstract tab completion Matcher.
16  *
17  * @author Marc Garcia <markcial@gmail.com>
18  */
19 abstract class AbstractMatcher
20 {
21     /** Syntax types */
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 = '+-*/^|&';
25     /** Token values */
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';
38
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';
44
45     /**
46      * Check whether this matcher can provide completions for $tokens.
47      *
48      * @param array $tokens Tokenized readline input
49      *
50      * @return bool
51      */
52     public function hasMatched(array $tokens)
53     {
54         return false;
55     }
56
57     /**
58      * Get current readline input word.
59      *
60      * @param array $tokens Tokenized readline input (see token_get_all)
61      *
62      * @return string
63      */
64     protected function getInput(array $tokens)
65     {
66         $var = '';
67         $firstToken = array_pop($tokens);
68         if (self::tokenIs($firstToken, self::T_STRING)) {
69             $var = $firstToken[1];
70         }
71
72         return $var;
73     }
74
75     /**
76      * Get current namespace and class (if any) from readline input.
77      *
78      * @param array $tokens Tokenized readline input (see token_get_all)
79      *
80      * @return string
81      */
82     protected function getNamespaceAndClass($tokens)
83     {
84         $class = '';
85         while (self::hasToken(
86             [self::T_NS_SEPARATOR, self::T_STRING],
87             $token = array_pop($tokens)
88         )) {
89             $class = $token[1] . $class;
90         }
91
92         return $class;
93     }
94
95     /**
96      * Provide tab completion matches for readline input.
97      *
98      * @param array $tokens information substracted with get_token_all
99      * @param array $info   readline_info object
100      *
101      * @return array The matches resulting from the query
102      */
103     abstract public function getMatches(array $tokens, array $info = []);
104
105     /**
106      * Check whether $word starts with $prefix.
107      *
108      * @param string $prefix
109      * @param string $word
110      *
111      * @return bool
112      */
113     public static function startsWith($prefix, $word)
114     {
115         return preg_match(sprintf('#^%s#', $prefix), $word);
116     }
117
118     /**
119      * Check whether $token matches a given syntax pattern.
120      *
121      * @param mixed  $token  A PHP token (see token_get_all)
122      * @param string $syntax A syntax pattern (default: variable pattern)
123      *
124      * @return bool
125      */
126     public static function hasSyntax($token, $syntax = self::VAR_SYNTAX)
127     {
128         if (!is_array($token)) {
129             return false;
130         }
131
132         $regexp = sprintf('#%s#', $syntax);
133
134         return (bool) preg_match($regexp, $token[1]);
135     }
136
137     /**
138      * Check whether $token type is $which.
139      *
140      * @param string $which A PHP token type
141      * @param mixed  $token A PHP token (see token_get_all)
142      *
143      * @return bool
144      */
145     public static function tokenIs($token, $which)
146     {
147         if (!is_array($token)) {
148             return false;
149         }
150
151         return token_name($token[0]) === $which;
152     }
153
154     /**
155      * Check whether $token is an operator.
156      *
157      * @param mixed $token A PHP token (see token_get_all)
158      *
159      * @return bool
160      */
161     public static function isOperator($token)
162     {
163         if (!is_string($token)) {
164             return false;
165         }
166
167         return strpos(self::MISC_OPERATORS, $token) !== false;
168     }
169
170     /**
171      * Check whether $token type is present in $coll.
172      *
173      * @param array $coll  A list of token types
174      * @param mixed $token A PHP token (see token_get_all)
175      *
176      * @return bool
177      */
178     public static function hasToken(array $coll, $token)
179     {
180         if (!is_array($token)) {
181             return false;
182         }
183
184         return in_array(token_name($token[0]), $coll);
185     }
186 }