summaryrefslogtreecommitdiffstatshomepage
path: root/py/parsehelper.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-02-07 18:33:58 +0000
committerDamien George <damien.p.george@gmail.com>2015-02-07 18:33:58 +0000
commit0bfc7638baa4c5a4a2351364ab770a188dcab302 (patch)
tree0127fcea13a875d37dd9cfa07dc6921bf387c578 /py/parsehelper.c
parente1e359ff59d6bbf09441cc1f3965be63f1046182 (diff)
downloadmicropython-0bfc7638baa4c5a4a2351364ab770a188dcab302.tar.gz
micropython-0bfc7638baa4c5a4a2351364ab770a188dcab302.zip
py: Protect mp_parse and mp_compile with nlr push/pop block.
To enable parsing constants more efficiently, mp_parse should be allowed to raise an exception, and mp_compile can already raise a MemoryError. So these functions need to be protected by an nlr push/pop block. This patch adds that feature in all places. This allows to simplify how mp_parse and mp_compile are called: they now raise an exception if they have an error and so explicit checking is not needed anymore.
Diffstat (limited to 'py/parsehelper.c')
-rw-r--r--py/parsehelper.c87
1 files changed, 0 insertions, 87 deletions
diff --git a/py/parsehelper.c b/py/parsehelper.c
deleted file mode 100644
index 904268109b..0000000000
--- a/py/parsehelper.c
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
- * This file is part of the Micro Python project, http://micropython.org/
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2013, 2014 Damien P. George
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-// these functions are separate from parse.c to keep parser independent of mp_obj_t
-
-#include <stdio.h>
-
-#include "py/parsehelper.h"
-
-#define STR_MEMORY "parser could not allocate enough memory"
-#define STR_UNEXPECTED_INDENT "unexpected indent"
-#define STR_UNMATCHED_UNINDENT "unindent does not match any outer indentation level"
-#define STR_INVALID_SYNTAX "invalid syntax"
-
-void mp_parse_show_exception(mp_lexer_t *lex, mp_parse_error_kind_t parse_error_kind) {
- printf(" File \"%s\", line " UINT_FMT ", column " UINT_FMT "\n", qstr_str(lex->source_name), lex->tok_line, lex->tok_column);
- switch (parse_error_kind) {
- case MP_PARSE_ERROR_MEMORY:
- printf("MemoryError: %s\n", STR_MEMORY);
- break;
-
- case MP_PARSE_ERROR_UNEXPECTED_INDENT:
- printf("IndentationError: %s\n", STR_UNEXPECTED_INDENT);
- break;
-
- case MP_PARSE_ERROR_UNMATCHED_UNINDENT:
- printf("IndentationError: %s\n", STR_UNMATCHED_UNINDENT);
- break;
-
- case MP_PARSE_ERROR_INVALID_SYNTAX:
- default:
- printf("SyntaxError: %s\n", STR_INVALID_SYNTAX);
- break;
- }
-}
-
-mp_obj_t mp_parse_make_exception(mp_lexer_t *lex, mp_parse_error_kind_t parse_error_kind) {
- // make exception object
- mp_obj_t exc;
- switch (parse_error_kind) {
- case MP_PARSE_ERROR_MEMORY:
- exc = mp_obj_new_exception_msg(&mp_type_MemoryError, STR_MEMORY);
- break;
-
- case MP_PARSE_ERROR_UNEXPECTED_INDENT:
- exc = mp_obj_new_exception_msg(&mp_type_IndentationError, STR_UNEXPECTED_INDENT);
- break;
-
- case MP_PARSE_ERROR_UNMATCHED_UNINDENT:
- exc = mp_obj_new_exception_msg(&mp_type_IndentationError, STR_UNMATCHED_UNINDENT);
- break;
-
- case MP_PARSE_ERROR_INVALID_SYNTAX:
- default:
- exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, STR_INVALID_SYNTAX);
- break;
- }
-
- // add traceback to give info about file name and location
- // we don't have a 'block' name, so just pass the NULL qstr to indicate this
- mp_obj_exception_add_traceback(exc, lex->source_name, lex->tok_line, MP_QSTR_NULL);
-
- return exc;
-}