blob: de0e8a4e761dbd98c1a638a0c8efd404f08ef679 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
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 George42f3de92014-10-03 17:44:14 +000035 mp_uint_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")
60 ;
61
Alex Marchbfb272b2015-09-17 18:02:53 +010062 // check for unmatched open bracket, quote or escape quote
Damien George28596ed2015-07-29 15:21:42 +000063 #define Q_NONE (0)
64 #define Q_1_SINGLE (1)
65 #define Q_1_DOUBLE (2)
66 #define Q_3_SINGLE (3)
67 #define Q_3_DOUBLE (4)
Damien92c06562013-10-22 22:32:27 +010068 int n_paren = 0;
69 int n_brack = 0;
70 int n_brace = 0;
Damien George28596ed2015-07-29 15:21:42 +000071 int in_quote = Q_NONE;
Damien George97790452014-04-08 11:04:29 +000072 const char *i;
73 for (i = input; *i; i++) {
Damien George28596ed2015-07-29 15:21:42 +000074 if (*i == '\'') {
75 if ((in_quote == Q_NONE || in_quote == Q_3_SINGLE) && i[1] == '\'' && i[2] == '\'') {
76 i += 2;
77 in_quote = Q_3_SINGLE - in_quote;
78 } else if (in_quote == Q_NONE || in_quote == Q_1_SINGLE) {
79 in_quote = Q_1_SINGLE - in_quote;
80 }
81 } else if (*i == '"') {
82 if ((in_quote == Q_NONE || in_quote == Q_3_DOUBLE) && i[1] == '"' && i[2] == '"') {
83 i += 2;
84 in_quote = Q_3_DOUBLE - in_quote;
85 } else if (in_quote == Q_NONE || in_quote == Q_1_DOUBLE) {
86 in_quote = Q_1_DOUBLE - in_quote;
87 }
Alex Marchbfb272b2015-09-17 18:02:53 +010088 } else if (*i == '\\' && (i[1] == '\'' || i[1] == '"')) {
89 if (in_quote != Q_NONE) {
90 i++;
91 }
Damien George28596ed2015-07-29 15:21:42 +000092 } else if (in_quote == Q_NONE) {
93 switch (*i) {
94 case '(': n_paren += 1; break;
95 case ')': n_paren -= 1; break;
96 case '[': n_brack += 1; break;
97 case ']': n_brack -= 1; break;
98 case '{': n_brace += 1; break;
99 case '}': n_brace -= 1; break;
100 default: break;
101 }
Damien92c06562013-10-22 22:32:27 +0100102 }
103 }
Damien George97790452014-04-08 11:04:29 +0000104
105 // continue if unmatched brackets or quotes
Damien George28596ed2015-07-29 15:21:42 +0000106 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 +0000107 return true;
108 }
109
Damien George73c79b92014-04-08 11:33:28 +0000110 // continue if last character was backslash (for line continuation)
111 if (i[-1] == '\\') {
112 return true;
113 }
114
Damien George97790452014-04-08 11:04:29 +0000115 // continue if compound keyword and last line was not empty
116 if (starts_with_compound_keyword && i[-1] != '\n') {
117 return true;
118 }
119
120 // otherwise, don't continue
121 return false;
Damien92c06562013-10-22 22:32:27 +0100122}
Damien George136f6752014-01-07 14:54:15 +0000123
Damien Georgea1a2c412015-04-26 17:55:31 +0100124mp_uint_t mp_repl_autocomplete(const char *str, mp_uint_t len, const mp_print_t *print, const char **compl_str) {
125 // scan backwards to find start of "a.b.c" chain
126 const char *top = str + len;
127 for (const char *s = top; --s >= str;) {
128 if (!(unichar_isalpha(*s) || unichar_isdigit(*s) || *s == '_' || *s == '.')) {
129 ++s;
130 str = s;
131 break;
132 }
133 }
134
135 // begin search in locals dict
136 mp_obj_dict_t *dict = mp_locals_get();
137
138 for (;;) {
139 // get next word in string to complete
140 const char *s_start = str;
141 while (str < top && *str != '.') {
142 ++str;
143 }
144 mp_uint_t s_len = str - s_start;
145
146 if (str < top) {
147 // a complete word, lookup in current dict
148
149 mp_obj_t obj = MP_OBJ_NULL;
150 for (mp_uint_t i = 0; i < dict->map.alloc; i++) {
151 if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) {
152 mp_uint_t d_len;
153 const char *d_str = mp_obj_str_get_data(dict->map.table[i].key, &d_len);
154 if (s_len == d_len && strncmp(s_start, d_str, d_len) == 0) {
155 obj = dict->map.table[i].value;
156 break;
157 }
158 }
159 }
160
161 if (obj == MP_OBJ_NULL) {
162 // lookup failed
163 return 0;
164 }
165
166 // found an object of this name; try to get its dict
167 if (MP_OBJ_IS_TYPE(obj, &mp_type_module)) {
168 dict = mp_obj_module_get_globals(obj);
169 } else {
170 mp_obj_type_t *type;
171 if (MP_OBJ_IS_TYPE(obj, &mp_type_type)) {
Damien George999cedb2015-11-27 17:01:44 +0000172 type = MP_OBJ_TO_PTR(obj);
Damien Georgea1a2c412015-04-26 17:55:31 +0100173 } else {
174 type = mp_obj_get_type(obj);
175 }
Damien George999cedb2015-11-27 17:01:44 +0000176 if (type->locals_dict != NULL && type->locals_dict->base.type == &mp_type_dict) {
Damien Georgea1a2c412015-04-26 17:55:31 +0100177 dict = type->locals_dict;
178 } else {
179 // obj has no dict
180 return 0;
181 }
182 }
183
184 // skip '.' to move to next word
185 ++str;
186
187 } else {
188 // end of string, do completion on this partial name
189
190 // look for matches
191 int n_found = 0;
192 const char *match_str = NULL;
193 mp_uint_t match_len = 0;
194 for (mp_uint_t i = 0; i < dict->map.alloc; i++) {
195 if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) {
196 mp_uint_t d_len;
197 const char *d_str = mp_obj_str_get_data(dict->map.table[i].key, &d_len);
198 if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0) {
199 if (match_str == NULL) {
200 match_str = d_str;
201 match_len = d_len;
202 } else {
Damien Georgeef7dd8d2015-07-06 14:00:09 +0100203 // search for longest common prefix of match_str and d_str
204 // (assumes these strings are null-terminated)
205 for (mp_uint_t j = s_len; j <= match_len && j <= d_len; ++j) {
Damien Georgef27aa272015-04-29 01:01:48 +0100206 if (match_str[j] != d_str[j]) {
207 match_len = j;
Damien Georgea1a2c412015-04-26 17:55:31 +0100208 break;
209 }
210 }
211 }
212 ++n_found;
213 }
214 }
215 }
216
217 // nothing found
218 if (n_found == 0) {
219 return 0;
220 }
221
222 // 1 match found, or multiple matches with a common prefix
223 if (n_found == 1 || match_len > s_len) {
224 *compl_str = match_str + s_len;
225 return match_len - s_len;
226 }
227
228 // multiple matches found, print them out
229
230 #define WORD_SLOT_LEN (16)
231 #define MAX_LINE_LEN (4 * WORD_SLOT_LEN)
232
233 int line_len = MAX_LINE_LEN; // force a newline for first word
234 for (mp_uint_t i = 0; i < dict->map.alloc; i++) {
235 if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) {
236 mp_uint_t d_len;
237 const char *d_str = mp_obj_str_get_data(dict->map.table[i].key, &d_len);
238 if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0) {
239 int gap = (line_len + WORD_SLOT_LEN - 1) / WORD_SLOT_LEN * WORD_SLOT_LEN - line_len;
240 if (gap < 2) {
241 gap += WORD_SLOT_LEN;
242 }
243 if (line_len + gap + d_len <= MAX_LINE_LEN) {
244 // TODO optimise printing of gap?
Damien Georgef27aa272015-04-29 01:01:48 +0100245 for (int j = 0; j < gap; ++j) {
Damien Georgea1a2c412015-04-26 17:55:31 +0100246 mp_print_str(print, " ");
247 }
248 mp_print_str(print, d_str);
249 line_len += gap + d_len;
250 } else {
251 mp_printf(print, "\n%s", d_str);
252 line_len = d_len;
253 }
254 }
255 }
256 }
257 mp_print_str(print, "\n");
258
259 return (mp_uint_t)(-1); // indicate many matches
260 }
261 }
262}
263
Damien George58ebde42014-05-21 20:32:59 +0100264#endif // MICROPY_HELPER_REPL