Skip to content

Commit

Permalink
Don't use symbols in Crystal::Lexer#check_macro_opening_keyword (cr…
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil authored and Blacksmoke16 committed Dec 11, 2023
1 parent 9ef62d9 commit 26d4b59
Showing 1 changed file with 34 additions and 43 deletions.
77 changes: 34 additions & 43 deletions src/compiler/crystal/syntax/lexer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -2006,87 +2006,78 @@ 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
when 'b'
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

Expand Down

0 comments on commit 26d4b59

Please sign in to comment.