blob: 7e8922e19ac69ac54907542a64874b33f270d5a9 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
4 * The MIT License (MIT)
5 *
Damien Georgea1a2c412015-04-26 17:55:31 +01006 * Copyright (c) 2013-2015 Damien P. George
Damien George04b91472014-05-03 23:27:38 +01007 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien Georgea1a2c412015-04-26 17:55:31 +010027#include <string.h>
28#include "py/obj.h"
29#include "py/runtime.h"
Damien George51dfcb42015-01-01 20:27:54 +000030#include "py/repl.h"
Damien92c06562013-10-22 22:32:27 +010031
Damien George58ebde42014-05-21 20:32:59 +010032#if MICROPY_HELPER_REPL
Damien George136f6752014-01-07 14:54:15 +000033
Damien George969a6b32014-12-10 22:07:04 +000034STATIC bool str_startswith_word(const char *str, const char *head) {
Damien Georgee66fd562017-07-04 23:44:54 +100035 size_t i;
Damien92c06562013-10-22 22:32:27 +010036 for (i = 0; str[i] && head[i]; i++) {
37 if (str[i] != head[i]) {
38 return false;
39 }
40 }
Alex March69d9e7d2016-02-16 13:36:18 +000041 return head[i] == '\0' && (str[i] == '\0' || !unichar_isident(str[i]));
Damien92c06562013-10-22 22:32:27 +010042}
43
Damien George97790452014-04-08 11:04:29 +000044bool mp_repl_continue_with_input(const char *input) {
45 // check for blank input
46 if (input[0] == '\0') {
47 return false;
Damien92c06562013-10-22 22:32:27 +010048 }
49
Damien George97790452014-04-08 11:04:29 +000050 // check if input starts with a certain keyword
51 bool starts_with_compound_keyword =
52 input[0] == '@'
53 || str_startswith_word(input, "if")
54 || str_startswith_word(input, "while")
55 || str_startswith_word(input, "for")
56 || str_startswith_word(input, "try")
57 || str_startswith_word(input, "with")
58 || str_startswith_word(input, "def")
59 || str_startswith_word(input, "class")
pohmelie81ebba72016-01-27 23:23:11 +030060 #if MICROPY_PY_ASYNC_AWAIT
61 || str_startswith_word(input, "async")
62 #endif
Damien George97790452014-04-08 11:04:29 +000063 ;
64
Alex Marchbfb272b2015-09-17 18:02:53 +010065 // check for unmatched open bracket, quote or escape quote
Damien George28596ed2015-07-29 15:21:42 +000066 #define Q_NONE (0)
67 #define Q_1_SINGLE (1)
68 #define Q_1_DOUBLE (2)
69 #define Q_3_SINGLE (3)
70 #define Q_3_DOUBLE (4)
Damien92c06562013-10-22 22:32:27 +010071 int n_paren = 0;
72 int n_brack = 0;
73 int n_brace = 0;
Damien George28596ed2015-07-29 15:21:42 +000074 int in_quote = Q_NONE;
Damien George97790452014-04-08 11:04:29 +000075 const char *i;
76 for (i = input; *i; i++) {
Damien George28596ed2015-07-29 15:21:42 +000077 if (*i == '\'') {
78 if ((in_quote == Q_NONE || in_quote == Q_3_SINGLE) && i[1] == '\'' && i[2] == '\'') {
79 i += 2;
80 in_quote = Q_3_SINGLE - in_quote;
81 } else if (in_quote == Q_NONE || in_quote == Q_1_SINGLE) {
82 in_quote = Q_1_SINGLE - in_quote;
83 }
84 } else if (*i == '"') {
85 if ((in_quote == Q_NONE || in_quote == Q_3_DOUBLE) && i[1] == '"' && i[2] == '"') {
86 i += 2;
87 in_quote = Q_3_DOUBLE - in_quote;
88 } else if (in_quote == Q_NONE || in_quote == Q_1_DOUBLE) {
89 in_quote = Q_1_DOUBLE - in_quote;
90 }
Damien George80a8d472016-05-11 16:05:22 +010091 } else if (*i == '\\' && (i[1] == '\'' || i[1] == '"' || i[1] == '\\')) {
Alex Marchbfb272b2015-09-17 18:02:53 +010092 if (in_quote != Q_NONE) {
93 i++;
94 }
Damien George28596ed2015-07-29 15:21:42 +000095 } else if (in_quote == Q_NONE) {
96 switch (*i) {
97 case '(': n_paren += 1; break;
98 case ')': n_paren -= 1; break;
99 case '[': n_brack += 1; break;
100 case ']': n_brack -= 1; break;
101 case '{': n_brace += 1; break;
102 case '}': n_brace -= 1; break;
103 default: break;
104 }
Damien92c06562013-10-22 22:32:27 +0100105 }
106 }
Damien George97790452014-04-08 11:04:29 +0000107
108 // continue if unmatched brackets or quotes
Damien George28596ed2015-07-29 15:21:42 +0000109 if (n_paren > 0 || n_brack > 0 || n_brace > 0 || in_quote == Q_3_SINGLE || in_quote == Q_3_DOUBLE) {
Damien George97790452014-04-08 11:04:29 +0000110 return true;
111 }
112
Damien George73c79b92014-04-08 11:33:28 +0000113 // continue if last character was backslash (for line continuation)
114 if (i[-1] == '\\') {
115 return true;
116 }
117
Damien George97790452014-04-08 11:04:29 +0000118 // continue if compound keyword and last line was not empty
119 if (starts_with_compound_keyword && i[-1] != '\n') {
120 return true;
121 }
122
123 // otherwise, don't continue
124 return false;
Damien92c06562013-10-22 22:32:27 +0100125}
Damien George136f6752014-01-07 14:54:15 +0000126
Damien Georgee66fd562017-07-04 23:44:54 +1000127size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print, const char **compl_str) {
Damien Georgea1a2c412015-04-26 17:55:31 +0100128 // scan backwards to find start of "a.b.c" chain
Paul Sokolovskyd59c2e52016-05-08 19:31:06 +0300129 const char *org_str = str;
Damien Georgea1a2c412015-04-26 17:55:31 +0100130 const char *top = str + len;
131 for (const char *s = top; --s >= str;) {
132 if (!(unichar_isalpha(*s) || unichar_isdigit(*s) || *s == '_' || *s == '.')) {
133 ++s;
134 str = s;
135 break;
136 }
137 }
138
139 // begin search in locals dict
140 mp_obj_dict_t *dict = mp_locals_get();
141
142 for (;;) {
143 // get next word in string to complete
144 const char *s_start = str;
145 while (str < top && *str != '.') {
146 ++str;
147 }
Damien Georgee66fd562017-07-04 23:44:54 +1000148 size_t s_len = str - s_start;
Damien Georgea1a2c412015-04-26 17:55:31 +0100149
150 if (str < top) {
151 // a complete word, lookup in current dict
152
153 mp_obj_t obj = MP_OBJ_NULL;
Damien Georgee66fd562017-07-04 23:44:54 +1000154 for (size_t i = 0; i < dict->map.alloc; i++) {
Damien Georgea1a2c412015-04-26 17:55:31 +0100155 if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) {
Damien George6b341072017-03-25 19:48:18 +1100156 size_t d_len;
Damien Georgea1a2c412015-04-26 17:55:31 +0100157 const char *d_str = mp_obj_str_get_data(dict->map.table[i].key, &d_len);
158 if (s_len == d_len && strncmp(s_start, d_str, d_len) == 0) {
159 obj = dict->map.table[i].value;
160 break;
161 }
162 }
163 }
164
165 if (obj == MP_OBJ_NULL) {
166 // lookup failed
167 return 0;
168 }
169
170 // found an object of this name; try to get its dict
171 if (MP_OBJ_IS_TYPE(obj, &mp_type_module)) {
172 dict = mp_obj_module_get_globals(obj);
173 } else {
174 mp_obj_type_t *type;
175 if (MP_OBJ_IS_TYPE(obj, &mp_type_type)) {
Damien George999cedb2015-11-27 17:01:44 +0000176 type = MP_OBJ_TO_PTR(obj);
Damien Georgea1a2c412015-04-26 17:55:31 +0100177 } else {
178 type = mp_obj_get_type(obj);
179 }
Damien George999cedb2015-11-27 17:01:44 +0000180 if (type->locals_dict != NULL && type->locals_dict->base.type == &mp_type_dict) {
Damien Georgea1a2c412015-04-26 17:55:31 +0100181 dict = type->locals_dict;
182 } else {
183 // obj has no dict
184 return 0;
185 }
186 }
187
188 // skip '.' to move to next word
189 ++str;
190
191 } else {
192 // end of string, do completion on this partial name
193
194 // look for matches
195 int n_found = 0;
196 const char *match_str = NULL;
Damien Georgee66fd562017-07-04 23:44:54 +1000197 size_t match_len = 0;
198 for (size_t i = 0; i < dict->map.alloc; i++) {
Damien Georgea1a2c412015-04-26 17:55:31 +0100199 if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) {
Damien George6b341072017-03-25 19:48:18 +1100200 size_t d_len;
Damien Georgea1a2c412015-04-26 17:55:31 +0100201 const char *d_str = mp_obj_str_get_data(dict->map.table[i].key, &d_len);
202 if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0) {
203 if (match_str == NULL) {
204 match_str = d_str;
205 match_len = d_len;
206 } else {
Damien Georgeef7dd8d2015-07-06 14:00:09 +0100207 // search for longest common prefix of match_str and d_str
208 // (assumes these strings are null-terminated)
Damien Georgee66fd562017-07-04 23:44:54 +1000209 for (size_t j = s_len; j <= match_len && j <= d_len; ++j) {
Damien Georgef27aa272015-04-29 01:01:48 +0100210 if (match_str[j] != d_str[j]) {
211 match_len = j;
Damien Georgea1a2c412015-04-26 17:55:31 +0100212 break;
213 }
214 }
215 }
216 ++n_found;
217 }
218 }
219 }
220
221 // nothing found
222 if (n_found == 0) {
Paul Sokolovskyd59c2e52016-05-08 19:31:06 +0300223 // If there're no better alternatives, and if it's first word
224 // in the line, try to complete "import".
225 if (s_start == org_str) {
Damien George3ff16ff2016-05-20 12:38:15 +0100226 static const char import_str[] = "import ";
Paul Sokolovskyd59c2e52016-05-08 19:31:06 +0300227 if (memcmp(s_start, import_str, s_len) == 0) {
228 *compl_str = import_str + s_len;
229 return sizeof(import_str) - 1 - s_len;
230 }
231 }
232
Damien Georgea1a2c412015-04-26 17:55:31 +0100233 return 0;
234 }
235
236 // 1 match found, or multiple matches with a common prefix
237 if (n_found == 1 || match_len > s_len) {
238 *compl_str = match_str + s_len;
239 return match_len - s_len;
240 }
241
242 // multiple matches found, print them out
243
244 #define WORD_SLOT_LEN (16)
245 #define MAX_LINE_LEN (4 * WORD_SLOT_LEN)
246
247 int line_len = MAX_LINE_LEN; // force a newline for first word
Damien Georgee66fd562017-07-04 23:44:54 +1000248 for (size_t i = 0; i < dict->map.alloc; i++) {
Damien Georgea1a2c412015-04-26 17:55:31 +0100249 if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) {
Damien George6b341072017-03-25 19:48:18 +1100250 size_t d_len;
Damien Georgea1a2c412015-04-26 17:55:31 +0100251 const char *d_str = mp_obj_str_get_data(dict->map.table[i].key, &d_len);
252 if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0) {
253 int gap = (line_len + WORD_SLOT_LEN - 1) / WORD_SLOT_LEN * WORD_SLOT_LEN - line_len;
254 if (gap < 2) {
255 gap += WORD_SLOT_LEN;
256 }
257 if (line_len + gap + d_len <= MAX_LINE_LEN) {
258 // TODO optimise printing of gap?
Damien Georgef27aa272015-04-29 01:01:48 +0100259 for (int j = 0; j < gap; ++j) {
Damien Georgea1a2c412015-04-26 17:55:31 +0100260 mp_print_str(print, " ");
261 }
262 mp_print_str(print, d_str);
263 line_len += gap + d_len;
264 } else {
265 mp_printf(print, "\n%s", d_str);
266 line_len = d_len;
267 }
268 }
269 }
270 }
271 mp_print_str(print, "\n");
272
Damien Georgee66fd562017-07-04 23:44:54 +1000273 return (size_t)(-1); // indicate many matches
Damien Georgea1a2c412015-04-26 17:55:31 +0100274 }
275 }
276}
277
Damien George58ebde42014-05-21 20:32:59 +0100278#endif // MICROPY_HELPER_REPL