Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 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 | |
Damien George | 136f675 | 2014-01-07 14:54:15 +0000 | [diff] [blame] | 27 | #include "mpconfig.h" |
Paul Sokolovsky | 59c675a | 2014-06-21 22:43:22 +0300 | [diff] [blame] | 28 | #include "misc.h" |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 29 | #include "repl.h" |
| 30 | |
Damien George | 58ebde4 | 2014-05-21 20:32:59 +0100 | [diff] [blame] | 31 | #if MICROPY_HELPER_REPL |
Damien George | 136f675 | 2014-01-07 14:54:15 +0000 | [diff] [blame] | 32 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame^] | 33 | STATIC bool str_startswith_word(const char *str, const char *head) { |
Damien George | 42f3de9 | 2014-10-03 17:44:14 +0000 | [diff] [blame] | 34 | mp_uint_t i; |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 35 | for (i = 0; str[i] && head[i]; i++) { |
| 36 | if (str[i] != head[i]) { |
| 37 | return false; |
| 38 | } |
| 39 | } |
Damien George | 8cc96a3 | 2013-12-30 18:23:50 +0000 | [diff] [blame] | 40 | return head[i] == '\0' && (str[i] == '\0' || !unichar_isalpha(str[i])); |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 41 | } |
| 42 | |
Damien George | 9779045 | 2014-04-08 11:04:29 +0000 | [diff] [blame] | 43 | bool mp_repl_continue_with_input(const char *input) { |
| 44 | // check for blank input |
| 45 | if (input[0] == '\0') { |
| 46 | return false; |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 47 | } |
| 48 | |
Damien George | 9779045 | 2014-04-08 11:04:29 +0000 | [diff] [blame] | 49 | // check if input starts with a certain keyword |
| 50 | bool starts_with_compound_keyword = |
| 51 | input[0] == '@' |
| 52 | || str_startswith_word(input, "if") |
| 53 | || str_startswith_word(input, "while") |
| 54 | || str_startswith_word(input, "for") |
| 55 | || str_startswith_word(input, "try") |
| 56 | || str_startswith_word(input, "with") |
| 57 | || str_startswith_word(input, "def") |
| 58 | || str_startswith_word(input, "class") |
| 59 | ; |
| 60 | |
| 61 | // check for unmatched open bracket or triple quote |
| 62 | // TODO don't look at triple quotes inside single quotes |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 63 | int n_paren = 0; |
| 64 | int n_brack = 0; |
| 65 | int n_brace = 0; |
Damien George | a28507a | 2014-04-07 13:01:30 +0100 | [diff] [blame] | 66 | int in_triple_quote = 0; |
Damien George | 9779045 | 2014-04-08 11:04:29 +0000 | [diff] [blame] | 67 | const char *i; |
| 68 | for (i = input; *i; i++) { |
| 69 | switch (*i) { |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 70 | case '(': n_paren += 1; break; |
| 71 | case ')': n_paren -= 1; break; |
| 72 | case '[': n_brack += 1; break; |
| 73 | case ']': n_brack -= 1; break; |
| 74 | case '{': n_brace += 1; break; |
| 75 | case '}': n_brace -= 1; break; |
Damien George | 9779045 | 2014-04-08 11:04:29 +0000 | [diff] [blame] | 76 | case '\'': |
| 77 | if (in_triple_quote != '"' && i[1] == '\'' && i[2] == '\'') { |
| 78 | i += 2; |
| 79 | in_triple_quote = '\'' - in_triple_quote; |
| 80 | } |
| 81 | break; |
Damien George | a28507a | 2014-04-07 13:01:30 +0100 | [diff] [blame] | 82 | case '"': |
Damien George | 9779045 | 2014-04-08 11:04:29 +0000 | [diff] [blame] | 83 | if (in_triple_quote != '\'' && i[1] == '"' && i[2] == '"') { |
| 84 | i += 2; |
| 85 | in_triple_quote = '"' - in_triple_quote; |
Damien George | a28507a | 2014-04-07 13:01:30 +0100 | [diff] [blame] | 86 | } |
| 87 | break; |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 88 | } |
| 89 | } |
Damien George | 9779045 | 2014-04-08 11:04:29 +0000 | [diff] [blame] | 90 | |
| 91 | // continue if unmatched brackets or quotes |
| 92 | if (n_paren > 0 || n_brack > 0 || n_brace > 0 || in_triple_quote != 0) { |
| 93 | return true; |
| 94 | } |
| 95 | |
Damien George | 73c79b9 | 2014-04-08 11:33:28 +0000 | [diff] [blame] | 96 | // continue if last character was backslash (for line continuation) |
| 97 | if (i[-1] == '\\') { |
| 98 | return true; |
| 99 | } |
| 100 | |
Damien George | 9779045 | 2014-04-08 11:04:29 +0000 | [diff] [blame] | 101 | // continue if compound keyword and last line was not empty |
| 102 | if (starts_with_compound_keyword && i[-1] != '\n') { |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | // otherwise, don't continue |
| 107 | return false; |
Damien | 92c0656 | 2013-10-22 22:32:27 +0100 | [diff] [blame] | 108 | } |
Damien George | 136f675 | 2014-01-07 14:54:15 +0000 | [diff] [blame] | 109 | |
Damien George | 58ebde4 | 2014-05-21 20:32:59 +0100 | [diff] [blame] | 110 | #endif // MICROPY_HELPER_REPL |