Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
Alexander Steffen | 55f3324 | 2017-06-30 09:22:17 +0200 | [diff] [blame] | 2 | * This file is part of the MicroPython project, http://micropython.org/ |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
Damien George | a1a2c41 | 2015-04-26 17:55:31 +0100 | [diff] [blame] | 6 | * Copyright (c) 2013-2015 Damien P. George |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 7 | * |
| 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 George | a1a2c41 | 2015-04-26 17:55:31 +0100 | [diff] [blame] | 27 | #include <string.h> |
| 28 | #include "py/obj.h" |
| 29 | #include "py/runtime.h" |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 30 | #include "py/repl.h" |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 31 | |
Damien George | 58ebde4 | 2014-05-21 20:32:59 +0100 | [diff] [blame] | 32 | #if MICROPY_HELPER_REPL |
Damien George | 136f675 | 2014-01-07 14:54:15 +0000 | [diff] [blame] | 33 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 34 | STATIC bool str_startswith_word(const char *str, const char *head) { |
Damien George | e66fd56 | 2017-07-04 23:44:54 +1000 | [diff] [blame] | 35 | size_t i; |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 36 | for (i = 0; str[i] && head[i]; i++) { |
| 37 | if (str[i] != head[i]) { |
| 38 | return false; |
| 39 | } |
| 40 | } |
Alex March | 69d9e7d | 2016-02-16 13:36:18 +0000 | [diff] [blame] | 41 | return head[i] == '\0' && (str[i] == '\0' || !unichar_isident(str[i])); |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 42 | } |
| 43 | |
Damien George | 9779045 | 2014-04-08 11:04:29 +0000 | [diff] [blame] | 44 | bool mp_repl_continue_with_input(const char *input) { |
| 45 | // check for blank input |
| 46 | if (input[0] == '\0') { |
| 47 | return false; |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 48 | } |
| 49 | |
Damien George | 9779045 | 2014-04-08 11:04:29 +0000 | [diff] [blame] | 50 | // 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") |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 60 | #if MICROPY_PY_ASYNC_AWAIT |
| 61 | || str_startswith_word(input, "async") |
| 62 | #endif |
Damien George | 9779045 | 2014-04-08 11:04:29 +0000 | [diff] [blame] | 63 | ; |
| 64 | |
Alex March | bfb272b | 2015-09-17 18:02:53 +0100 | [diff] [blame] | 65 | // check for unmatched open bracket, quote or escape quote |
Damien George | 28596ed | 2015-07-29 15:21:42 +0000 | [diff] [blame] | 66 | #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) |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 71 | int n_paren = 0; |
| 72 | int n_brack = 0; |
| 73 | int n_brace = 0; |
Damien George | 28596ed | 2015-07-29 15:21:42 +0000 | [diff] [blame] | 74 | int in_quote = Q_NONE; |
Damien George | 9779045 | 2014-04-08 11:04:29 +0000 | [diff] [blame] | 75 | const char *i; |
| 76 | for (i = input; *i; i++) { |
Damien George | 28596ed | 2015-07-29 15:21:42 +0000 | [diff] [blame] | 77 | 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 George | 80a8d47 | 2016-05-11 16:05:22 +0100 | [diff] [blame] | 91 | } else if (*i == '\\' && (i[1] == '\'' || i[1] == '"' || i[1] == '\\')) { |
Alex March | bfb272b | 2015-09-17 18:02:53 +0100 | [diff] [blame] | 92 | if (in_quote != Q_NONE) { |
| 93 | i++; |
| 94 | } |
Damien George | 28596ed | 2015-07-29 15:21:42 +0000 | [diff] [blame] | 95 | } 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 | } |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 105 | } |
| 106 | } |
Damien George | 9779045 | 2014-04-08 11:04:29 +0000 | [diff] [blame] | 107 | |
| 108 | // continue if unmatched brackets or quotes |
Damien George | 28596ed | 2015-07-29 15:21:42 +0000 | [diff] [blame] | 109 | if (n_paren > 0 || n_brack > 0 || n_brace > 0 || in_quote == Q_3_SINGLE || in_quote == Q_3_DOUBLE) { |
Damien George | 9779045 | 2014-04-08 11:04:29 +0000 | [diff] [blame] | 110 | return true; |
| 111 | } |
| 112 | |
Damien George | 73c79b9 | 2014-04-08 11:33:28 +0000 | [diff] [blame] | 113 | // continue if last character was backslash (for line continuation) |
| 114 | if (i[-1] == '\\') { |
| 115 | return true; |
| 116 | } |
| 117 | |
Damien George | 9779045 | 2014-04-08 11:04:29 +0000 | [diff] [blame] | 118 | // 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; |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 125 | } |
Damien George | 136f675 | 2014-01-07 14:54:15 +0000 | [diff] [blame] | 126 | |
Damien George | e66fd56 | 2017-07-04 23:44:54 +1000 | [diff] [blame] | 127 | size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print, const char **compl_str) { |
Damien George | a1a2c41 | 2015-04-26 17:55:31 +0100 | [diff] [blame] | 128 | // scan backwards to find start of "a.b.c" chain |
Paul Sokolovsky | d59c2e5 | 2016-05-08 19:31:06 +0300 | [diff] [blame] | 129 | const char *org_str = str; |
Damien George | a1a2c41 | 2015-04-26 17:55:31 +0100 | [diff] [blame] | 130 | 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 George | e66fd56 | 2017-07-04 23:44:54 +1000 | [diff] [blame] | 148 | size_t s_len = str - s_start; |
Damien George | a1a2c41 | 2015-04-26 17:55:31 +0100 | [diff] [blame] | 149 | |
| 150 | if (str < top) { |
| 151 | // a complete word, lookup in current dict |
| 152 | |
| 153 | mp_obj_t obj = MP_OBJ_NULL; |
Damien George | e66fd56 | 2017-07-04 23:44:54 +1000 | [diff] [blame] | 154 | for (size_t i = 0; i < dict->map.alloc; i++) { |
Damien George | a1a2c41 | 2015-04-26 17:55:31 +0100 | [diff] [blame] | 155 | if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) { |
Damien George | 6b34107 | 2017-03-25 19:48:18 +1100 | [diff] [blame] | 156 | size_t d_len; |
Damien George | a1a2c41 | 2015-04-26 17:55:31 +0100 | [diff] [blame] | 157 | 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 George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 176 | type = MP_OBJ_TO_PTR(obj); |
Damien George | a1a2c41 | 2015-04-26 17:55:31 +0100 | [diff] [blame] | 177 | } else { |
| 178 | type = mp_obj_get_type(obj); |
| 179 | } |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 180 | if (type->locals_dict != NULL && type->locals_dict->base.type == &mp_type_dict) { |
Damien George | a1a2c41 | 2015-04-26 17:55:31 +0100 | [diff] [blame] | 181 | 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 George | e66fd56 | 2017-07-04 23:44:54 +1000 | [diff] [blame] | 197 | size_t match_len = 0; |
| 198 | for (size_t i = 0; i < dict->map.alloc; i++) { |
Damien George | a1a2c41 | 2015-04-26 17:55:31 +0100 | [diff] [blame] | 199 | if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) { |
Damien George | 6b34107 | 2017-03-25 19:48:18 +1100 | [diff] [blame] | 200 | size_t d_len; |
Damien George | a1a2c41 | 2015-04-26 17:55:31 +0100 | [diff] [blame] | 201 | 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 George | ef7dd8d | 2015-07-06 14:00:09 +0100 | [diff] [blame] | 207 | // search for longest common prefix of match_str and d_str |
| 208 | // (assumes these strings are null-terminated) |
Damien George | e66fd56 | 2017-07-04 23:44:54 +1000 | [diff] [blame] | 209 | for (size_t j = s_len; j <= match_len && j <= d_len; ++j) { |
Damien George | f27aa27 | 2015-04-29 01:01:48 +0100 | [diff] [blame] | 210 | if (match_str[j] != d_str[j]) { |
| 211 | match_len = j; |
Damien George | a1a2c41 | 2015-04-26 17:55:31 +0100 | [diff] [blame] | 212 | break; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | ++n_found; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | // nothing found |
| 222 | if (n_found == 0) { |
Paul Sokolovsky | d59c2e5 | 2016-05-08 19:31:06 +0300 | [diff] [blame] | 223 | // 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 George | 3ff16ff | 2016-05-20 12:38:15 +0100 | [diff] [blame] | 226 | static const char import_str[] = "import "; |
Paul Sokolovsky | d59c2e5 | 2016-05-08 19:31:06 +0300 | [diff] [blame] | 227 | 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 George | a1a2c41 | 2015-04-26 17:55:31 +0100 | [diff] [blame] | 233 | 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 George | e66fd56 | 2017-07-04 23:44:54 +1000 | [diff] [blame] | 248 | for (size_t i = 0; i < dict->map.alloc; i++) { |
Damien George | a1a2c41 | 2015-04-26 17:55:31 +0100 | [diff] [blame] | 249 | if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) { |
Damien George | 6b34107 | 2017-03-25 19:48:18 +1100 | [diff] [blame] | 250 | size_t d_len; |
Damien George | a1a2c41 | 2015-04-26 17:55:31 +0100 | [diff] [blame] | 251 | 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 George | f27aa27 | 2015-04-29 01:01:48 +0100 | [diff] [blame] | 259 | for (int j = 0; j < gap; ++j) { |
Damien George | a1a2c41 | 2015-04-26 17:55:31 +0100 | [diff] [blame] | 260 | 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 George | e66fd56 | 2017-07-04 23:44:54 +1000 | [diff] [blame] | 273 | return (size_t)(-1); // indicate many matches |
Damien George | a1a2c41 | 2015-04-26 17:55:31 +0100 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
Damien George | 58ebde4 | 2014-05-21 20:32:59 +0100 | [diff] [blame] | 278 | #endif // MICROPY_HELPER_REPL |