From 44c4bf5f5e949b7c7bc72b33218467692f02e32f Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 11 Jul 2014 13:21:44 -0700 Subject: [PATCH] Refactor add_ubiquitous_token_actions method --- examples/parsers/json.c | 1 - .../build_tables/build_parse_table.cc | 22 ++++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/examples/parsers/json.c b/examples/parsers/json.c index 2777c6c1..c7779a53 100644 --- a/examples/parsers/json.c +++ b/examples/parsers/json.c @@ -319,7 +319,6 @@ LEX_FN() { ADVANCE(27); LEX_ERROR(); case ts_lex_state_error: - START_TOKEN(); if (lookahead == '\0') ADVANCE(25); if (('\t' <= lookahead && lookahead <= '\n') || diff --git a/src/compiler/build_tables/build_parse_table.cc b/src/compiler/build_tables/build_parse_table.cc index 32878cff..3e685797 100644 --- a/src/compiler/build_tables/build_parse_table.cc +++ b/src/compiler/build_tables/build_parse_table.cc @@ -75,19 +75,21 @@ namespace tree_sitter { } void add_ubiquitous_token_actions(ParseStateId state_id) { - auto &actions = parse_table.states[state_id].actions; - for (const Symbol &symbol : grammar.ubiquitous_tokens) { - const auto &action = actions.find(symbol); - if (action != actions.end() && action->second.type == ParseActionTypeShift) { - size_t new_state_id = action->second.state_index; + const map &actions = parse_table.states[state_id].actions; + + for (const Symbol &ubiquitous_symbol : grammar.ubiquitous_tokens) { + const auto &pair_for_symbol = actions.find(ubiquitous_symbol); + + if (pair_for_symbol == actions.end()) { + parse_table.add_action(state_id, ubiquitous_symbol, ParseAction::ShiftExtra()); + } else if (pair_for_symbol->second.type == ParseActionTypeShift) { + size_t shift_state_id = pair_for_symbol->second.state_index; for (const auto &pair : actions) { const Symbol &lookahead_sym = pair.first; - ParseAction action = ParseAction::ReduceExtra(symbol); - if (should_add_action(new_state_id, lookahead_sym, action)) - parse_table.add_action(new_state_id, lookahead_sym, action); + ParseAction reduce_extra = ParseAction::ReduceExtra(ubiquitous_symbol); + if (should_add_action(shift_state_id, lookahead_sym, reduce_extra)) + parse_table.add_action(shift_state_id, lookahead_sym, reduce_extra); } - } else if (actions.find(symbol) == actions.end()) { - parse_table.add_action(state_id, symbol, ParseAction::ShiftExtra()); } } }