Numworks Epsilon  1.4.1
Graphing Calculator Operating System
repl.c
Go to the documentation of this file.
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 
27 #include <string.h>
28 #include "py/obj.h"
29 #include "py/runtime.h"
30 #include "py/repl.h"
31 
32 #if MICROPY_HELPER_REPL
33 
34 STATIC bool str_startswith_word(const char *str, const char *head) {
35  size_t i;
36  for (i = 0; str[i] && head[i]; i++) {
37  if (str[i] != head[i]) {
38  return false;
39  }
40  }
41  return head[i] == '\0' && (str[i] == '\0' || !unichar_isident(str[i]));
42 }
43 
44 bool mp_repl_continue_with_input(const char *input) {
45  // check for blank input
46  if (input[0] == '\0') {
47  return false;
48  }
49 
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  #if MICROPY_PY_ASYNC_AWAIT
61  || str_startswith_word(input, "async")
62  #endif
63  ;
64 
65  // check for unmatched open bracket, quote or escape quote
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)
71  int n_paren = 0;
72  int n_brack = 0;
73  int n_brace = 0;
74  int in_quote = Q_NONE;
75  const char *i;
76  for (i = input; *i; i++) {
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  }
91  } else if (*i == '\\' && (i[1] == '\'' || i[1] == '"' || i[1] == '\\')) {
92  if (in_quote != Q_NONE) {
93  i++;
94  }
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  }
105  }
106  }
107 
108  // continue if unmatched brackets or quotes
109  if (n_paren > 0 || n_brack > 0 || n_brace > 0 || in_quote == Q_3_SINGLE || in_quote == Q_3_DOUBLE) {
110  return true;
111  }
112 
113  // continue if last character was backslash (for line continuation)
114  if (i[-1] == '\\') {
115  return true;
116  }
117 
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;
125 }
126 
127 size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print, const char **compl_str) {
128  // scan backwards to find start of "a.b.c" chain
129  const char *org_str = str;
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  }
148  size_t s_len = str - s_start;
149 
150  if (str < top) {
151  // a complete word, lookup in current dict
152 
153  mp_obj_t obj = MP_OBJ_NULL;
154  for (size_t i = 0; i < dict->map.alloc; i++) {
155  if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) {
156  size_t d_len;
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)) {
176  type = MP_OBJ_TO_PTR(obj);
177  } else {
178  type = mp_obj_get_type(obj);
179  }
180  if (type->locals_dict != NULL && type->locals_dict->base.type == &mp_type_dict) {
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;
197  size_t match_len = 0;
198  for (size_t i = 0; i < dict->map.alloc; i++) {
199  if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) {
200  size_t d_len;
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 {
207  // search for longest common prefix of match_str and d_str
208  // (assumes these strings are null-terminated)
209  for (size_t j = s_len; j <= match_len && j <= d_len; ++j) {
210  if (match_str[j] != d_str[j]) {
211  match_len = j;
212  break;
213  }
214  }
215  }
216  ++n_found;
217  }
218  }
219  }
220 
221  // nothing found
222  if (n_found == 0) {
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) {
226  static const char import_str[] = "import ";
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 
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
248  for (size_t i = 0; i < dict->map.alloc; i++) {
249  if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) {
250  size_t d_len;
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?
259  for (int j = 0; j < gap; ++j) {
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 
273  return (size_t)(-1); // indicate many matches
274  }
275  }
276 }
277 
278 #endif // MICROPY_HELPER_REPL
bool unichar_isalpha(unichar c)
Definition: unicode.c:132
#define MP_OBJ_IS_TYPE(o, t)
Definition: obj.h:254
mp_obj_type_t * mp_obj_get_type(mp_const_obj_t o_in)
Definition: obj.c:40
int mp_print_str(const mp_print_t *print, const char *str)
Definition: mpprint.c:53
mp_obj_t key
Definition: obj.h:342
size_t alloc
Definition: obj.h:361
mp_obj_base_t base
Definition: obj.h:765
mp_obj_dict_t * mp_obj_module_get_globals(mp_obj_t self_in)
Definition: objmodule.c:125
bool unichar_isdigit(unichar c)
Definition: unicode.c:142
#define STATIC
Definition: mpconfig.h:1178
const char * mp_obj_str_get_data(mp_obj_t self_in, size_t *len)
Definition: objstr.c:2105
#define NULL
Definition: stddef.h:4
#define MP_OBJ_NULL
Definition: obj.h:73
mp_obj_t value
Definition: obj.h:343
bool unichar_isident(unichar c)
Definition: unicode.c:150
const mp_obj_type_t mp_type_type
Definition: objtype.c:969
#define MP_OBJ_TO_PTR(o)
Definition: obj.h:228
const mp_obj_type_t mp_type_dict
Definition: objdict.c:552
const mp_obj_type_t mp_type_module
Definition: objmodule.c:94
LIBA_BEGIN_DECLS int memcmp(const void *s1, const void *s2, size_t n)
Definition: memcmp.c:3
mp_map_t map
Definition: obj.h:766
uint64_t mp_obj_t
Definition: obj.h:39
mp_map_elem_t * table
Definition: obj.h:362
int mp_printf(const mp_print_t *print, const char *fmt,...)
Definition: mpprint.c:380
struct _mp_obj_dict_t * locals_dict
Definition: obj.h:536