repl.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 '(':
  93. n_paren += 1;
  94. break;
  95. case ')':
  96. n_paren -= 1;
  97. break;
  98. case '[':
  99. n_brack += 1;
  100. break;
  101. case ']':
  102. n_brack -= 1;
  103. break;
  104. case '{':
  105. n_brace += 1;
  106. break;
  107. case '}':
  108. n_brace -= 1;
  109. break;
  110. default:
  111. break;
  112. }
  113. }
  114. }
  115. // continue if unmatched 3-quotes
  116. if (in_quote == Q_3_SINGLE || in_quote == Q_3_DOUBLE) {
  117. return true;
  118. }
  119. // continue if unmatched brackets, but only if not in a 1-quote
  120. if ((n_paren > 0 || n_brack > 0 || n_brace > 0) && in_quote == Q_NONE) {
  121. return true;
  122. }
  123. // continue if last character was backslash (for line continuation)
  124. if (i[-1] == '\\') {
  125. return true;
  126. }
  127. // continue if compound keyword and last line was not empty
  128. if (starts_with_compound_keyword && i[-1] != '\n') {
  129. return true;
  130. }
  131. // otherwise, don't continue
  132. return false;
  133. }
  134. size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print, const char **compl_str) {
  135. // scan backwards to find start of "a.b.c" chain
  136. const char *org_str = str;
  137. const char *top = str + len;
  138. for (const char *s = top; --s >= str;) {
  139. if (!(unichar_isalpha(*s) || unichar_isdigit(*s) || *s == '_' || *s == '.')) {
  140. ++s;
  141. str = s;
  142. break;
  143. }
  144. }
  145. size_t nqstr = QSTR_TOTAL();
  146. // begin search in outer global dict which is accessed from __main__
  147. mp_obj_t obj = MP_OBJ_FROM_PTR(&mp_module___main__);
  148. mp_obj_t dest[2];
  149. for (;;) {
  150. // get next word in string to complete
  151. const char *s_start = str;
  152. while (str < top && *str != '.') {
  153. ++str;
  154. }
  155. size_t s_len = str - s_start;
  156. if (str < top) {
  157. // a complete word, lookup in current object
  158. qstr q = qstr_find_strn(s_start, s_len);
  159. if (q == MP_QSTRnull) {
  160. // lookup will fail
  161. return 0;
  162. }
  163. mp_load_method_protected(obj, q, dest, true);
  164. obj = dest[0]; // attribute, method, or MP_OBJ_NULL if nothing found
  165. if (obj == MP_OBJ_NULL) {
  166. // lookup failed
  167. return 0;
  168. }
  169. // skip '.' to move to next word
  170. ++str;
  171. } else {
  172. // end of string, do completion on this partial name
  173. // look for matches
  174. const char *match_str = NULL;
  175. size_t match_len = 0;
  176. qstr q_first = 0, q_last = 0;
  177. for (qstr q = MP_QSTR_ + 1; q < nqstr; ++q) {
  178. size_t d_len;
  179. const char *d_str = (const char *)qstr_data(q, &d_len);
  180. if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0) {
  181. mp_load_method_protected(obj, q, dest, true);
  182. if (dest[0] != MP_OBJ_NULL) {
  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. if (q_first == 0) {
  197. q_first = q;
  198. }
  199. q_last = q;
  200. }
  201. }
  202. }
  203. // nothing found
  204. if (q_first == 0) {
  205. // If there're no better alternatives, and if it's first word
  206. // in the line, try to complete "import".
  207. if (s_start == org_str) {
  208. static const char import_str[] = "import ";
  209. if (memcmp(s_start, import_str, s_len) == 0) {
  210. *compl_str = import_str + s_len;
  211. return sizeof(import_str) - 1 - s_len;
  212. }
  213. }
  214. return 0;
  215. }
  216. // 1 match found, or multiple matches with a common prefix
  217. if (q_first == q_last || match_len > s_len) {
  218. *compl_str = match_str + s_len;
  219. return match_len - s_len;
  220. }
  221. // multiple matches found, print them out
  222. #define WORD_SLOT_LEN (16)
  223. #define MAX_LINE_LEN (4 * WORD_SLOT_LEN)
  224. int line_len = MAX_LINE_LEN; // force a newline for first word
  225. for (qstr q = q_first; q <= q_last; ++q) {
  226. size_t d_len;
  227. const char *d_str = (const char *)qstr_data(q, &d_len);
  228. if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0) {
  229. mp_load_method_protected(obj, q, dest, true);
  230. if (dest[0] != MP_OBJ_NULL) {
  231. int gap = (line_len + WORD_SLOT_LEN - 1) / WORD_SLOT_LEN * WORD_SLOT_LEN - line_len;
  232. if (gap < 2) {
  233. gap += WORD_SLOT_LEN;
  234. }
  235. if (line_len + gap + d_len <= MAX_LINE_LEN) {
  236. // TODO optimise printing of gap?
  237. for (int j = 0; j < gap; ++j) {
  238. mp_print_str(print, " ");
  239. }
  240. mp_print_str(print, d_str);
  241. line_len += gap + d_len;
  242. } else {
  243. mp_printf(print, "\n%s", d_str);
  244. line_len = d_len;
  245. }
  246. }
  247. }
  248. }
  249. mp_print_str(print, "\n");
  250. return (size_t)(-1); // indicate many matches
  251. }
  252. }
  253. }
  254. #endif // MICROPY_HELPER_REPL