From 40bc05ee1eafcc2f0b3ed48f212a1526b65163bc Mon Sep 17 00:00:00 2001 From: Josh Klar Date: Thu, 26 Sep 2019 16:48:02 -0700 Subject: [PATCH] Address dpgeorge feedback - largely simplifications --- py/lexer.c | 26 +++++++++----------------- py/parse.c | 12 ++++++++++++ tests/basics/string_pep498_fstring.py | 13 ------------- 3 files changed, 21 insertions(+), 30 deletions(-) diff --git a/py/lexer.c b/py/lexer.c index 6894ada47..c3f4e6506 100644 --- a/py/lexer.c +++ b/py/lexer.c @@ -129,29 +129,21 @@ STATIC bool is_tail_of_identifier(mp_lexer_t *lex) { STATIC void swap_char_banks(mp_lexer_t *lex) { if (lex->vstr_postfix_processing) { - unichar h0, h1, h2; - - h0 = lex->chr0; - h1 = lex->chr1; - h2 = lex->chr2; - - lex->chr0 = lex->vstr_postfix.len > 0 ? lex->vstr_postfix.buf[0] : 0; - lex->chr1 = lex->vstr_postfix.len > 1 ? lex->vstr_postfix.buf[1] : 0; - lex->chr2 = lex->vstr_postfix.len > 2 ? lex->vstr_postfix.buf[2] : 0; - lex->chr3 = h0; - lex->chr4 = h1; - lex->chr5 = h2; - - lex->vstr_postfix_idx = lex->vstr_postfix.len > 2 ? 3 : lex->vstr_postfix.len; + lex->chr3 = lex->chr0; + lex->chr4 = lex->chr1; + lex->chr5 = lex->chr2; + lex->chr0 = lex->vstr_postfix.buf[0]; + lex->chr1 = lex->vstr_postfix.buf[1]; + lex->chr2 = lex->vstr_postfix.buf[2]; + + lex->vstr_postfix_idx = 3; } else { // blindly reset to the "backup" bank when done postfix processing // this restores control to the mp_reader lex->chr0 = lex->chr3; lex->chr1 = lex->chr4; lex->chr2 = lex->chr5; - lex->chr3 = 0; - lex->chr4 = 0; - lex->chr5 = 0; + // willfully ignoring setting chr3-5 here - WARNING consider those garbage data now vstr_reset(&lex->vstr_postfix); lex->vstr_postfix_idx = 0; diff --git a/py/parse.c b/py/parse.c index d30d1b22f..f86ee25fe 100644 --- a/py/parse.c +++ b/py/parse.c @@ -1178,6 +1178,7 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) { exc = mp_obj_new_exception_msg(&mp_type_IndentationError, translate("unindent does not match any outer indentation level")); break; +#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED case MP_TOKEN_FSTRING_BACKSLASH: exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, translate("f-string expression part cannot include a backslash")); @@ -1202,6 +1203,17 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) { exc = mp_obj_new_exception_msg(&mp_type_NotImplementedError, translate("raw f-strings are not implemented")); break; +#else + case MP_TOKEN_FSTRING_BACKSLASH: + case MP_TOKEN_FSTRING_COMMENT: + case MP_TOKEN_FSTRING_UNCLOSED: + case MP_TOKEN_FSTRING_UNOPENED: + case MP_TOKEN_FSTRING_EMPTY_EXP: + case MP_TOKEN_FSTRING_RAW: + exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, + translate("malformed f-string")); + break; +#endif default: exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, translate("invalid syntax")); diff --git a/tests/basics/string_pep498_fstring.py b/tests/basics/string_pep498_fstring.py index f1ad1f19b..f4e76c831 100644 --- a/tests/basics/string_pep498_fstring.py +++ b/tests/basics/string_pep498_fstring.py @@ -111,16 +111,3 @@ assert ('a' 'b' f'{x}' f'str<{y:^4}>' 'd' 'e') == 'ab10str< hi >de' # Other tests assert f'{{{4*10}}}' == '{40}' - -try: - eval("fr''") -except NotImplementedError: - pass -else: - raise RuntimeError('expected raw f-string to raise NotImplementedError') -try: - eval("rf''") -except NotImplementedError: - pass -else: - raise RuntimeError('expected raw f-string to raise NotImplementedError')