From 26d4b5910b0bc723254de7c661e9b6ee950a78bf Mon Sep 17 00:00:00 2001 From: Quinton Miller Date: Wed, 7 Jun 2023 15:27:32 +0800 Subject: [PATCH] Don't use symbols in `Crystal::Lexer#check_macro_opening_keyword` (#13277) --- src/compiler/crystal/syntax/lexer.cr | 77 ++++++++++++---------------- 1 file changed, 34 insertions(+), 43 deletions(-) diff --git a/src/compiler/crystal/syntax/lexer.cr b/src/compiler/crystal/syntax/lexer.cr index 7bafb7ddae04..33f991c5fc0f 100644 --- a/src/compiler/crystal/syntax/lexer.cr +++ b/src/compiler/crystal/syntax/lexer.cr @@ -1925,10 +1925,10 @@ module Crystal char = current_char whitespace = true beginning_of_line = false - elsif !delimiter_state && whitespace && (keyword = lookahead { check_macro_opening_keyword(beginning_of_line) }) + elsif !delimiter_state && whitespace && (keyword = lookahead { macro_starts_with_keyword?(beginning_of_line) }) char = current_char - nest += 1 unless keyword == :abstract_def + nest += 1 unless keyword.abstract_def? whitespace = true beginning_of_line = false next @@ -2006,7 +2006,12 @@ module Crystal end end - def check_macro_opening_keyword(beginning_of_line) + enum MacroKeywordState + AbstractDef + Other + end + + def macro_starts_with_keyword?(beginning_of_line) : MacroKeywordState? case char = current_char when 'a' case next_char @@ -2014,79 +2019,65 @@ module Crystal if char_sequence?('s', 't', 'r', 'a', 'c', 't') && next_char.whitespace? case next_char when 'd' - char_sequence?('e', 'f') && peek_not_ident_part_or_end_next_char && :abstract_def + MacroKeywordState::AbstractDef if char_sequence?('e', 'f') && peek_not_ident_part_or_end_next_char when 'c' - char_sequence?('l', 'a', 's', 's') && peek_not_ident_part_or_end_next_char && :abstract_class + MacroKeywordState::Other if char_sequence?('l', 'a', 's', 's') && peek_not_ident_part_or_end_next_char when 's' - char_sequence?('t', 'r', 'u', 'c', 't') && peek_not_ident_part_or_end_next_char && :abstract_struct - else - false + MacroKeywordState::Other if char_sequence?('t', 'r', 'u', 'c', 't') && peek_not_ident_part_or_end_next_char end end when 'n' - char_sequence?('n', 'o', 't', 'a', 't', 'i', 'o', 'n') && peek_not_ident_part_or_end_next_char && :annotation - else - false + MacroKeywordState::Other if char_sequence?('n', 'o', 't', 'a', 't', 'i', 'o', 'n') && peek_not_ident_part_or_end_next_char end when 'b' - char_sequence?('e', 'g', 'i', 'n') && peek_not_ident_part_or_end_next_char && :begin + MacroKeywordState::Other if char_sequence?('e', 'g', 'i', 'n') && peek_not_ident_part_or_end_next_char when 'c' case next_char when 'a' - char_sequence?('s', 'e') && peek_not_ident_part_or_end_next_char && :case + MacroKeywordState::Other if char_sequence?('s', 'e') && peek_not_ident_part_or_end_next_char when 'l' - char_sequence?('a', 's', 's') && peek_not_ident_part_or_end_next_char && :class - else - false + MacroKeywordState::Other if char_sequence?('a', 's', 's') && peek_not_ident_part_or_end_next_char end when 'd' case next_char when 'o' - peek_not_ident_part_or_end_next_char && :do + MacroKeywordState::Other if peek_not_ident_part_or_end_next_char when 'e' - next_char == 'f' && peek_not_ident_part_or_end_next_char && :def - else - false + MacroKeywordState::Other if next_char == 'f' && peek_not_ident_part_or_end_next_char end when 'f' - char_sequence?('u', 'n') && peek_not_ident_part_or_end_next_char && :fun + MacroKeywordState::Other if char_sequence?('u', 'n') && peek_not_ident_part_or_end_next_char when 'i' - beginning_of_line && next_char == 'f' && peek_not_ident_part_or_end_next_char && :if + MacroKeywordState::Other if beginning_of_line && next_char == 'f' && peek_not_ident_part_or_end_next_char when 'l' - char_sequence?('i', 'b') && peek_not_ident_part_or_end_next_char && :lib + MacroKeywordState::Other if char_sequence?('i', 'b') && peek_not_ident_part_or_end_next_char when 'm' case next_char when 'a' - char_sequence?('c', 'r', 'o') && peek_not_ident_part_or_end_next_char && :macro + MacroKeywordState::Other if char_sequence?('c', 'r', 'o') && peek_not_ident_part_or_end_next_char when 'o' - char_sequence?('d', 'u', 'l', 'e') && peek_not_ident_part_or_end_next_char && :module - else - false + MacroKeywordState::Other if char_sequence?('d', 'u', 'l', 'e') && peek_not_ident_part_or_end_next_char end when 's' case next_char when 'e' - char_sequence?('l', 'e', 'c', 't') && !ident_part_or_end?(peek_next_char) && next_char && :select + MacroKeywordState::Other if char_sequence?('l', 'e', 'c', 't') && !ident_part_or_end?(peek_next_char) && next_char when 't' - char_sequence?('r', 'u', 'c', 't') && !ident_part_or_end?(peek_next_char) && next_char && :struct - else - false + MacroKeywordState::Other if char_sequence?('r', 'u', 'c', 't') && !ident_part_or_end?(peek_next_char) && next_char end when 'u' - next_char == 'n' && case next_char - when 'i' - char_sequence?('o', 'n') && peek_not_ident_part_or_end_next_char && :union - when 'l' - beginning_of_line && char_sequence?('e', 's', 's') && peek_not_ident_part_or_end_next_char && :unless - when 't' - beginning_of_line && char_sequence?('i', 'l') && peek_not_ident_part_or_end_next_char && :until - else - false + if next_char == 'n' + case next_char + when 'i' + MacroKeywordState::Other if char_sequence?('o', 'n') && peek_not_ident_part_or_end_next_char + when 'l' + MacroKeywordState::Other if beginning_of_line && char_sequence?('e', 's', 's') && peek_not_ident_part_or_end_next_char + when 't' + MacroKeywordState::Other if beginning_of_line && char_sequence?('i', 'l') && peek_not_ident_part_or_end_next_char + end end when 'w' - beginning_of_line && char_sequence?('h', 'i', 'l', 'e') && peek_not_ident_part_or_end_next_char && :while - else - false + MacroKeywordState::Other if beginning_of_line && char_sequence?('h', 'i', 'l', 'e') && peek_not_ident_part_or_end_next_char end end