From 57036b4f8a6f431a79aa6f1ea8333e53263cd97a Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 9 Mar 2021 15:01:26 -0800 Subject: [PATCH] Extract lexer helper functions for all large char sets No need to restrict it to char sets used in multiple places. This is important because the helper functions are now implemented more efficiently than the inline comparisons (using a binary search). --- cli/src/generate/render.rs | 61 +++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 34 deletions(-) diff --git a/cli/src/generate/render.rs b/cli/src/generate/render.rs index 460dfc24..46914629 100644 --- a/cli/src/generate/render.rs +++ b/cli/src/generate/render.rs @@ -82,7 +82,6 @@ struct LargeCharacterSetInfo { ranges: Vec>, symbol: Symbol, index: usize, - usage_count: usize, } impl Generator { @@ -682,7 +681,6 @@ impl Generator { for (i, info) in large_character_sets.iter_mut().enumerate() { if info.ranges == ranges { call_id = Some(i); - info.usage_count += 1; break; } if info.symbol == char_set_symbol { @@ -695,7 +693,6 @@ impl Generator { symbol: char_set_symbol, index: count_for_symbol + 1, ranges: ranges.clone(), - usage_count: 1, }); } } @@ -714,23 +711,21 @@ impl Generator { let mut sorted_large_char_sets: Vec<_> = large_character_sets.iter().map(|e| e).collect(); sorted_large_char_sets.sort_unstable_by_key(|info| (info.symbol, info.index)); for info in sorted_large_char_sets { - if info.usage_count > 1 { - add_line!( - self, - "static inline bool {}_character_set_{}(int32_t c) {{", - self.symbol_ids[&info.symbol], - info.index - ); - indent!(self); - add_whitespace!(self); - add!(self, "return "); - let tree = CharacterTree::from_ranges(&info.ranges); - self.add_character_tree(tree.as_ref()); - add!(self, ";\n"); - dedent!(self); - add_line!(self, "}}"); - add_line!(self, ""); - } + add_line!( + self, + "static inline bool {}_character_set_{}(int32_t c) {{", + self.symbol_ids[&info.symbol], + info.index + ); + indent!(self); + add_whitespace!(self); + add!(self, "return "); + let tree = CharacterTree::from_ranges(&info.ranges); + self.add_character_tree(tree.as_ref()); + add!(self, ";\n"); + dedent!(self); + add_line!(self, "}}"); + add_line!(self, ""); } add_line!( @@ -810,21 +805,19 @@ impl Generator { // set, then generate a call to that helper function. if let Some(call_id) = transition.call_id { let info = &large_character_sets[call_id]; - if info.usage_count > 1 { - add!(self, "if ("); - if !transition.is_included { - add!(self, "!"); - } - add!( - self, - "{}_character_set_{}(lookahead)) ", - self.symbol_ids[&info.symbol], - info.index - ); - self.add_advance_action(&action); - add!(self, "\n"); - continue; + add!(self, "if ("); + if !transition.is_included { + add!(self, "!"); } + add!( + self, + "{}_character_set_{}(lookahead)) ", + self.symbol_ids[&info.symbol], + info.index + ); + self.add_advance_action(&action); + add!(self, "\n"); + continue; } // Otherwise, generate code to compare the lookahead character