Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use symbols in Crystal::Lexer#check_macro_opening_keyword #13277

Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 29 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,73 @@ module Crystal
end
end

def check_macro_opening_keyword(beginning_of_line)
def macro_starts_with_keyword?(beginning_of_line) : {abstract_def: Bool}?
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
{abstract_def: true} 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
{abstract_def: false} 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
{abstract_def: false} 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
{abstract_def: false} 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
{abstract_def: false} 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
{abstract_def: false} 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
{abstract_def: false} 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
{abstract_def: false} 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
{abstract_def: false} 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
{abstract_def: false} 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
{abstract_def: false} 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
{abstract_def: false} 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
{abstract_def: false} 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
{abstract_def: false} 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
{abstract_def: false} 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
{abstract_def: false} 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'
{abstract_def: false} if char_sequence?('o', 'n') && peek_not_ident_part_or_end_next_char
when 'l'
{abstract_def: false} if beginning_of_line && char_sequence?('e', 's', 's') && peek_not_ident_part_or_end_next_char
when 't'
{abstract_def: false} 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
{abstract_def: false} if beginning_of_line && char_sequence?('h', 'i', 'l', 'e') && peek_not_ident_part_or_end_next_char
end
end

Expand Down