Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 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 | 42f3de9 | 2014-10-03 17:44:14 +0000 | [diff] [blame] | 35 | mp_uint_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") |
| 60 | ; |
| 61 | |
Alex March | bfb272b | 2015-09-17 18:02:53 +0100 | [diff] [blame] | 62 | // check for unmatched open bracket, quote or escape quote |
Damien George | 28596ed | 2015-07-29 15:21:42 +0000 | [diff] [blame] | 63 | #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) |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 68 | int n_paren = 0; |
| 69 | int n_brack = 0; |
| 70 | int n_brace = 0; |
Damien George | 28596ed | 2015-07-29 15:21:42 +0000 | [diff] [blame] | 71 | int in_quote = Q_NONE; |
Damien George | 9779045 | 2014-04-08 11:04:29 +0000 | [diff] [blame] | 72 | const char *i; |
| 73 | for (i = input; *i; i++) { |
Damien George | 28596ed | 2015-07-29 15:21:42 +0000 | [diff] [blame] | 74 | 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 March | bfb272b | 2015-09-17 18:02:53 +0100 | [diff] [blame] | 88 | } else if (*i == '\\' && (i[1] == '\'' || i[1] == '"')) { |
| 89 | if (in_quote != Q_NONE) { |
| 90 | i++; |
| 91 | } |
Damien George | 28596ed | 2015-07-29 15:21:42 +0000 | [diff] [blame] | 92 | } 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 | } |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 102 | } |
| 103 | } |
Damien George | 9779045 | 2014-04-08 11:04:29 +0000 | [diff] [blame] | 104 | |
| 105 | // continue if unmatched brackets or quotes |
Damien George | 28596ed | 2015-07-29 15:21:42 +0000 | [diff] [blame] | 106 | 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] | 107 | return true; |
| 108 | } |
| 109 | |
Damien George | 73c79b9 | 2014-04-08 11:33:28 +0000 | [diff] [blame] | 110 | // continue if last character was backslash (for line continuation) |
| 111 | if (i[-1] == '\\') { |
| 112 | return true; |
| 113 | } |
| 114 | |
Damien George | 9779045 | 2014-04-08 11:04:29 +0000 | [diff] [blame] | 115 | // 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; |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 122 | } |
Damien George | 136f675 | 2014-01-07 14:54:15 +0000 | [diff] [blame] | 123 | |
Damien George | a1a2c41 | 2015-04-26 17:55:31 +0100 | [diff] [blame] | 124 | mp_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 George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 172 | type = MP_OBJ_TO_PTR(obj); |
Damien George | a1a2c41 | 2015-04-26 17:55:31 +0100 | [diff] [blame] | 173 | } else { |
| 174 | type = mp_obj_get_type(obj); |
| 175 | } |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 176 | 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] | 177 | 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 George | ef7dd8d | 2015-07-06 14:00:09 +0100 | [diff] [blame] | 203 | // 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 George | f27aa27 | 2015-04-29 01:01:48 +0100 | [diff] [blame] | 206 | if (match_str[j] != d_str[j]) { |
| 207 | match_len = j; |
Damien George | a1a2c41 | 2015-04-26 17:55:31 +0100 | [diff] [blame] | 208 | 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 George | f27aa27 | 2015-04-29 01:01:48 +0100 | [diff] [blame] | 245 | for (int j = 0; j < gap; ++j) { |
Damien George | a1a2c41 | 2015-04-26 17:55:31 +0100 | [diff] [blame] | 246 | 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 George | 58ebde4 | 2014-05-21 20:32:59 +0100 | [diff] [blame] | 264 | #endif // MICROPY_HELPER_REPL |