repl.c 9.5 KB

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