From 819235bac3990795f25e292910a3d76c0cda4e00 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 12 Sep 2017 12:00:00 -0700 Subject: [PATCH] Limit the number of stack nodes that are included in a summary --- src/runtime/parser.c | 5 +++-- src/runtime/stack.c | 35 +++++++++++++++++++---------------- src/runtime/stack.h | 2 +- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/runtime/parser.c b/src/runtime/parser.c index 60aa7da4..548becc3 100644 --- a/src/runtime/parser.c +++ b/src/runtime/parser.c @@ -34,7 +34,8 @@ #define SYM_NAME(symbol) ts_language_symbol_name(self->language, symbol) -static const uint32_t MAX_VERSION_COUNT = 10; +static const unsigned MAX_VERSION_COUNT = 10; +static const unsigned MAX_SUMMARY_DEPTH = 16; static void parser__log(Parser *self) { if (self->lexer.logger.log) { @@ -797,7 +798,7 @@ static void parser__handle_error(Parser *self, StackVersion version, TSSymbol lo ts_stack_force_merge(self->stack, version, previous_version_count); } - ts_stack_record_summary(self->stack, version); + ts_stack_record_summary(self->stack, version, MAX_SUMMARY_DEPTH); LOG_STACK(); } diff --git a/src/runtime/stack.c b/src/runtime/stack.c index f3907ffd..b4d3feb6 100644 --- a/src/runtime/stack.c +++ b/src/runtime/stack.c @@ -477,24 +477,33 @@ StackPopResult ts_stack_pop_all(Stack *self, StackVersion version) { return stack__iter(self, version, pop_all_callback, NULL, true); } +typedef struct { + StackSummary *summary; + unsigned max_depth; +} SummarizeStackSession; + inline StackIterateAction summarize_stack_callback(void *payload, const Iterator *iterator) { - StackSummary *summary = payload; + SummarizeStackSession *session = payload; TSStateId state = iterator->node->state; unsigned depth = iterator->tree_count; - for (unsigned i = summary->size - 1; i + 1 > 0; i--) { - StackSummaryEntry entry = summary->contents[i]; + if (depth > session->max_depth) return StackIterateStop; + for (unsigned i = session->summary->size - 1; i + 1 > 0; i--) { + StackSummaryEntry entry = session->summary->contents[i]; if (entry.depth < depth) break; if (entry.depth == depth && entry.state == state) return StackIterateNone; } - array_push(summary, ((StackSummaryEntry){.depth = depth, .state = state})); + array_push(session->summary, ((StackSummaryEntry){.depth = depth, .state = state})); return StackIterateNone; } -void ts_stack_record_summary(Stack *self, StackVersion version) { - StackSummary *result = ts_malloc(sizeof(StackSummary)); - array_init(result); - stack__iter(self, version, summarize_stack_callback, result, false); - self->heads.contents[version].summary = result; +void ts_stack_record_summary(Stack *self, StackVersion version, unsigned max_depth) { + SummarizeStackSession session = { + .summary = ts_malloc(sizeof(StackSummary)), + .max_depth = max_depth + }; + array_init(session.summary); + stack__iter(self, version, summarize_stack_callback, &session, false); + self->heads.contents[version].summary = session.summary; } StackSummary *ts_stack_get_summary(Stack *self, StackVersion version) { @@ -502,13 +511,7 @@ StackSummary *ts_stack_get_summary(Stack *self, StackVersion version) { } unsigned ts_stack_depth_since_error(Stack *self, StackVersion version) { - unsigned result = 0; - StackNode *node = array_get(&self->heads, version)->node; - while (node->state == 0) { - result++; - node = node->links[0].node; - } - return result - 1; + return array_get(&self->heads, version)->node->depth; } void ts_stack_remove_version(Stack *self, StackVersion version) { diff --git a/src/runtime/stack.h b/src/runtime/stack.h index 36e41c50..ce0a30ed 100644 --- a/src/runtime/stack.h +++ b/src/runtime/stack.h @@ -103,7 +103,7 @@ StackPopResult ts_stack_pop_all(Stack *, StackVersion); unsigned ts_stack_depth_since_error(Stack *, StackVersion); -void ts_stack_record_summary(Stack *, StackVersion); +void ts_stack_record_summary(Stack *, StackVersion, unsigned max_depth); StackSummary *ts_stack_get_summary(Stack *, StackVersion);