32 #if MICROPY_HELPER_REPL 34 STATIC bool str_startswith_word(
const char *str,
const char *head) {
36 for (i = 0; str[i] && head[i]; i++) {
37 if (str[i] != head[i]) {
44 bool mp_repl_continue_with_input(
const char *input) {
46 if (input[0] ==
'\0') {
51 bool starts_with_compound_keyword =
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")
67 #define Q_1_SINGLE (1) 68 #define Q_1_DOUBLE (2) 69 #define Q_3_SINGLE (3) 70 #define Q_3_DOUBLE (4) 74 int in_quote = Q_NONE;
76 for (i = input; *i; i++) {
78 if ((in_quote == Q_NONE || in_quote == Q_3_SINGLE) && i[1] ==
'\'' && 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;
84 }
else if (*i ==
'"') {
85 if ((in_quote == Q_NONE || in_quote == Q_3_DOUBLE) && i[1] ==
'"' && 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;
91 }
else if (*i ==
'\\' && (i[1] ==
'\'' || i[1] ==
'"' || i[1] ==
'\\')) {
92 if (in_quote != Q_NONE) {
95 }
else if (in_quote == Q_NONE) {
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;
109 if (n_paren > 0 || n_brack > 0 || n_brace > 0 || in_quote == Q_3_SINGLE || in_quote == Q_3_DOUBLE) {
119 if (starts_with_compound_keyword && i[-1] !=
'\n') {
127 size_t mp_repl_autocomplete(
const char *str,
size_t len,
const mp_print_t *print,
const char **compl_str) {
129 const char *org_str = str;
130 const char *top = str + len;
131 for (
const char *s = top; --s >= str;) {
144 const char *s_start = str;
145 while (str < top && *str !=
'.') {
148 size_t s_len = str - s_start;
154 for (
size_t i = 0; i < dict->
map.
alloc; i++) {
155 if (MP_MAP_SLOT_IS_FILLED(&dict->
map, i)) {
158 if (s_len == d_len && strncmp(s_start, d_str, d_len) == 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)) {
202 if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0) {
203 if (match_str ==
NULL) {
209 for (
size_t j = s_len; j <= match_len && j <= d_len; ++j) {
210 if (match_str[j] != d_str[j]) {
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;
237 if (n_found == 1 || match_len > s_len) {
238 *compl_str = match_str + s_len;
239 return match_len - s_len;
244 #define WORD_SLOT_LEN (16) 245 #define MAX_LINE_LEN (4 * WORD_SLOT_LEN) 247 int line_len = MAX_LINE_LEN;
248 for (
size_t i = 0; i < dict->
map.
alloc; i++) {
249 if (MP_MAP_SLOT_IS_FILLED(&dict->
map, i)) {
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;
255 gap += WORD_SLOT_LEN;
257 if (line_len + gap + d_len <= MAX_LINE_LEN) {
259 for (
int j = 0; j < gap; ++j) {
263 line_len += gap + d_len;
278 #endif // MICROPY_HELPER_REPL
bool unichar_isalpha(unichar c)
#define MP_OBJ_IS_TYPE(o, t)
mp_obj_type_t * mp_obj_get_type(mp_const_obj_t o_in)
int mp_print_str(const mp_print_t *print, const char *str)
mp_obj_dict_t * mp_obj_module_get_globals(mp_obj_t self_in)
bool unichar_isdigit(unichar c)
const char * mp_obj_str_get_data(mp_obj_t self_in, size_t *len)
bool unichar_isident(unichar c)
const mp_obj_type_t mp_type_type
const mp_obj_type_t mp_type_dict
const mp_obj_type_t mp_type_module
LIBA_BEGIN_DECLS int memcmp(const void *s1, const void *s2, size_t n)
int mp_printf(const mp_print_t *print, const char *fmt,...)
struct _mp_obj_dict_t * locals_dict