summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2018-01-24 18:50:26 -0800
committerGitHub <noreply@github.com>2018-01-24 18:50:26 -0800
commit271c4bead6807cbde8fcd5141042fc5b91cf06a7 (patch)
treec52f60135a4cd88f77b41041fc8dcf09a011f907 /py
parenta1254f125503e871fbb72ad6440299a0e943c7e6 (diff)
parentdff744558b32de05bbcf2e7a4804cb3c89e6658e (diff)
Merge pull request #552 from tannewt/flexible_parser
Make parsing more memory flexible.
Diffstat (limited to 'py')
-rw-r--r--py/parse.c23
1 files changed, 21 insertions, 2 deletions
diff --git a/py/parse.c b/py/parse.c
index 3a108964b..08a403401 100644
--- a/py/parse.c
+++ b/py/parse.c
@@ -742,11 +742,30 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
parser.rule_stack_alloc = MICROPY_ALLOC_PARSE_RULE_INIT;
parser.rule_stack_top = 0;
- parser.rule_stack = m_new(rule_stack_t, parser.rule_stack_alloc);
+ parser.rule_stack = NULL;
+ while (parser.rule_stack_alloc > 1) {
+ parser.rule_stack = m_new_maybe(rule_stack_t, parser.rule_stack_alloc);
+ if (parser.rule_stack != NULL) {
+ break;
+ } else {
+ parser.rule_stack_alloc /= 2;
+ }
+ }
parser.result_stack_alloc = MICROPY_ALLOC_PARSE_RESULT_INIT;
parser.result_stack_top = 0;
- parser.result_stack = m_new(mp_parse_node_t, parser.result_stack_alloc);
+ parser.result_stack = NULL;
+ while (parser.result_stack_alloc > 1) {
+ parser.result_stack = m_new_maybe(mp_parse_node_t, parser.result_stack_alloc);
+ if (parser.result_stack != NULL) {
+ break;
+ } else {
+ parser.result_stack_alloc /= 2;
+ }
+ }
+ if (parser.rule_stack == NULL || parser.result_stack == NULL) {
+ mp_raise_msg(&mp_type_MemoryError, "Unable to init parser");
+ }
parser.lexer = lex;