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