repl.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013-2015 Damien P. George
  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. #include <string.h>
  27. #include "py/obj.h"
  28. #include "py/runtime.h"
  29. #include "py/repl.h"
  30. #if MICROPY_HELPER_REPL
  31. STATIC bool str_startswith_word(const char *str, const char *head) {
  32. size_t i;
  33. for (i = 0; str[i] && head[i]; i++) {
  34. if (str[i] != head[i]) {
  35. return false;
  36. }
  37. }
  38. return head[i] == '\0' && (str[i] == '\0' || !unichar_isident(str[i]));
  39. }
  40. bool mp_repl_continue_with_input(const char *input) {
  41. // check for blank input
  42. if (input[0] == '\0') {
  43. return false;
  44. }
  45. // check if input starts with a certain keyword
  46. bool starts_with_compound_keyword =
  47. input[0] == '@'
  48. || str_startswith_word(input, "if")
  49. || str_startswith_word(input, "while")
  50. || str_startswith_word(input, "for")
  51. || str_startswith_word(input, "try")
  52. || str_startswith_word(input, "with")
  53. || str_startswith_word(input, "def")
  54. || str_startswith_word(input, "class")
  55. #if MICROPY_PY_ASYNC_AWAIT
  56. || str_startswith_word(input, "async")
  57. #endif
  58. ;
  59. // check for unmatched open bracket, quote or escape quote
  60. #define Q_NONE (0)
  61. #define Q_1_SINGLE (1)
  62. #define Q_1_DOUBLE (2)
  63. #define Q_3_SINGLE (3)
  64. #define Q_3_DOUBLE (4)
  65. int n_paren = 0;
  66. int n_brack = 0;
  67. int n_brace = 0;
  68. int in_quote = Q_NONE;
  69. const char *i;
  70. for (i = input; *i; i++) {
  71. if (*i == '\'') {
  72. if ((in_quote == Q_NONE || in_quote == Q_3_SINGLE) && i[1] == '\'' && i[2] == '\'') {
  73. i += 2;
  74. in_quote = Q_3_SINGLE - in_quote;
  75. } else if (in_quote == Q_NONE || in_quote == Q_1_SINGLE) {
  76. in_quote = Q_1_SINGLE - in_quote;
  77. }
  78. } else if (*i == '"') {
  79. if ((in_quote == Q_NONE || in_quote == Q_3_DOUBLE) && i[1] == '"' && i[2] == '"') {
  80. i += 2;
  81. in_quote = Q_3_DOUBLE - in_quote;
  82. } else if (in_quote == Q_NONE || in_quote == Q_1_DOUBLE) {
  83. in_quote = Q_1_DOUBLE - in_quote;
  84. }
  85. } else if (*i == '\\' && (i[1] == '\'' || i[1] == '"' || i[1] == '\\')) {
  86. if (in_quote != Q_NONE) {
  87. i++;
  88. }
  89. } else if (in_quote == Q_NONE) {
  90. switch (*i) {
  91. case '(': n_paren += 1; break;
  92. case ')': n_paren -= 1; break;
  93. case '[': n_brack += 1; break;
  94. case ']': n_brack -= 1; break;
  95. case '{': n_brace += 1; break;
  96. case '}': n_brace -= 1; break;
  97. default: break;
  98. }
  99. }
  100. }
  101. // continue if unmatched brackets or quotes
  102. if (n_paren > 0 || n_brack > 0 || n_brace > 0 || in_quote == Q_3_SINGLE || in_quote == Q_3_DOUBLE) {
  103. return true;
  104. }
  105. // continue if last character was backslash (for line continuation)
  106. if (i[-1] == '\\') {
  107. return true;
  108. }
  109. // continue if compound keyword and last line was not empty
  110. if (starts_with_compound_keyword && i[-1] != '\n') {
  111. return true;
  112. }
  113. // otherwise, don't continue
  114. return false;
  115. }
  116. size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print, const char **compl_str) {
  117. // scan backwards to find start of "a.b.c" chain
  118. const char *org_str = str;
  119. const char *top = str + len;
  120. for (const char *s = top; --s >= str;) {
  121. if (!(unichar_isalpha(*s) || unichar_isdigit(*s) || *s == '_' || *s == '.')) {
  122. ++s;
  123. str = s;
  124. break;
  125. }
  126. }
  127. // begin search in locals dict
  128. mp_obj_dict_t *dict = mp_locals_get();
  129. for (;;) {
  130. // get next word in string to complete
  131. const char *s_start = str;
  132. while (str < top && *str != '.') {
  133. ++str;
  134. }
  135. size_t s_len = str - s_start;
  136. if (str < top) {
  137. // a complete word, lookup in current dict
  138. mp_obj_t obj = MP_OBJ_NULL;
  139. for (size_t i = 0; i < dict->map.alloc; i++) {
  140. if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) {
  141. size_t d_len;
  142. const char *d_str = mp_obj_str_get_data(dict->map.table[i].key, &d_len);
  143. if (s_len == d_len && strncmp(s_start, d_str, d_len) == 0) {
  144. obj = dict->map.table[i].value;
  145. break;
  146. }
  147. }
  148. }
  149. if (obj == MP_OBJ_NULL) {
  150. // lookup failed
  151. return 0;
  152. }
  153. // found an object of this name; try to get its dict
  154. if (MP_OBJ_IS_TYPE(obj, &mp_type_module)) {
  155. dict = mp_obj_module_get_globals(obj);
  156. } else {
  157. mp_obj_type_t *type;
  158. if (MP_OBJ_IS_TYPE(obj, &mp_type_type)) {
  159. type = MP_OBJ_TO_PTR(obj);
  160. } else {
  161. type = mp_obj_get_type(obj);
  162. }
  163. if (type->locals_dict != NULL && type->locals_dict->base.type == &mp_type_dict) {
  164. dict = type->locals_dict;
  165. } else {
  166. // obj has no dict
  167. return 0;
  168. }
  169. }
  170. // skip '.' to move to next word
  171. ++str;
  172. } else {
  173. // end of string, do completion on this partial name
  174. // look for matches
  175. int n_found = 0;
  176. const char *match_str = NULL;
  177. size_t match_len = 0;
  178. for (size_t i = 0; i < dict->map.alloc; i++) {
  179. if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) {
  180. size_t d_len;
  181. const char *d_str = mp_obj_str_get_data(dict->map.table[i].key, &d_len);
  182. if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0) {
  183. if (match_str == NULL) {
  184. match_str = d_str;
  185. match_len = d_len;
  186. } else {
  187. // search for longest common prefix of match_str and d_str
  188. // (assumes these strings are null-terminated)
  189. for (size_t j = s_len; j <= match_len && j <= d_len; ++j) {
  190. if (match_str[j] != d_str[j]) {
  191. match_len = j;
  192. break;
  193. }
  194. }
  195. }
  196. ++n_found;
  197. }
  198. }
  199. }
  200. // nothing found
  201. if (n_found == 0) {
  202. // If there're no better alternatives, and if it's first word
  203. // in the line, try to complete "import".
  204. if (s_start == org_str) {
  205. static const char import_str[] = "import ";
  206. if (memcmp(s_start, import_str, s_len) == 0) {
  207. *compl_str = import_str + s_len;
  208. return sizeof(import_str) - 1 - s_len;
  209. }
  210. }
  211. return 0;
  212. }
  213. // 1 match found, or multiple matches with a common prefix
  214. if (n_found == 1 || match_len > s_len) {
  215. *compl_str = match_str + s_len;
  216. return match_len - s_len;
  217. }
  218. // multiple matches found, print them out
  219. #define WORD_SLOT_LEN (16)
  220. #define MAX_LINE_LEN (4 * WORD_SLOT_LEN)
  221. int line_len = MAX_LINE_LEN; // force a newline for first word
  222. for (size_t i = 0; i < dict->map.alloc; i++) {
  223. if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) {
  224. size_t d_len;
  225. const char *d_str = mp_obj_str_get_data(dict->map.table[i].key, &d_len);
  226. if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0) {
  227. int gap = (line_len + WORD_SLOT_LEN - 1) / WORD_SLOT_LEN * WORD_SLOT_LEN - line_len;
  228. if (gap < 2) {
  229. gap += WORD_SLOT_LEN;
  230. }
  231. if (line_len + gap + d_len <= MAX_LINE_LEN) {
  232. // TODO optimise printing of gap?
  233. for (int j = 0; j < gap; ++j) {
  234. mp_print_str(print, " ");
  235. }
  236. mp_print_str(print, d_str);
  237. line_len += gap + d_len;
  238. } else {
  239. mp_printf(print, "\n%s", d_str);
  240. line_len = d_len;
  241. }
  242. }
  243. }
  244. }
  245. mp_print_str(print, "\n");
  246. return (size_t)(-1); // indicate many matches
  247. }
  248. }
  249. }
  250. #endif // MICROPY_HELPER_REPL