-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Parse integer division operator #6470
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,7 @@ module Crystal | |
| @count_whitespace = false | ||
| @slash_is_regex = true | ||
| @wants_raw = false | ||
| @wants_def_or_macro_name = false | ||
| @string_pool = string_pool || StringPool.new | ||
|
|
||
| # When lexing macro tokens, when we encounter `#{` inside | ||
|
|
@@ -287,8 +288,17 @@ module Crystal | |
| line = @line_number | ||
| column = @column_number | ||
| char = next_char | ||
| if !@slash_is_regex && char == '=' | ||
| if (@wants_def_or_macro_name || !@slash_is_regex) && char == '/' | ||
| case next_char | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be replaced by a smaller
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the case is more about following the pattern of the rest of the lexer.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is fine - this just makes the lexer looks a bit more complex that it is, that's it :) if next_char == '/'
next_char :"//="
else
@token.type = :"//"
end
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought you ment the At there is no real change the the actual code. So |
||
| when '=' | ||
| next_char :"//=" | ||
| else | ||
| @token.type = :"//" | ||
| end | ||
| elsif !@slash_is_regex && char == '=' | ||
| next_char :"/=" | ||
| elsif @wants_def_or_macro_name | ||
| @token.type = :"/" | ||
| elsif @slash_is_regex | ||
| @token.type = :DELIMITER_START | ||
| @token.delimiter_state = Token::DelimiterState.new(:regex, '/', '/') | ||
|
|
@@ -303,65 +313,69 @@ module Crystal | |
| @token.type = :"/" | ||
| end | ||
| when '%' | ||
| case next_char | ||
| when '=' | ||
| next_char :"%=" | ||
| when '(', '[', '{', '<', '|' | ||
| delimited_pair :string, current_char, closing_char, start | ||
| when 'i' | ||
| case peek_next_char | ||
| when '(', '{', '[', '<', '|' | ||
| start_char = next_char | ||
| next_char :SYMBOL_ARRAY_START | ||
| @token.raw = "%i#{start_char}" if @wants_raw | ||
| @token.delimiter_state = Token::DelimiterState.new(:symbol_array, start_char, closing_char(start_char)) | ||
| else | ||
| @token.type = :"%" | ||
| end | ||
| when 'q' | ||
| case peek_next_char | ||
| when '(', '{', '[', '<', '|' | ||
| next_char | ||
| delimited_pair :string, current_char, closing_char, start, allow_escapes: false | ||
| else | ||
| @token.type = :"%" | ||
| end | ||
| when 'Q' | ||
| case peek_next_char | ||
| when '(', '{', '[', '<', '|' | ||
| next_char | ||
| delimited_pair :string, current_char, closing_char, start | ||
| else | ||
| @token.type = :"%" | ||
| end | ||
| when 'r' | ||
| case next_char | ||
| when '(', '[', '{', '<', '|' | ||
| delimited_pair :regex, current_char, closing_char, start | ||
| else | ||
| raise "unknown %r char" | ||
| end | ||
| when 'x' | ||
| if @wants_def_or_macro_name | ||
| next_char :"%" | ||
| else | ||
| case next_char | ||
| when '=' | ||
| next_char :"%=" | ||
| when '(', '[', '{', '<', '|' | ||
| delimited_pair :command, current_char, closing_char, start | ||
| else | ||
| raise "unknown %x char" | ||
| end | ||
| when 'w' | ||
| case peek_next_char | ||
| when '(', '{', '[', '<', '|' | ||
| start_char = next_char | ||
| next_char :STRING_ARRAY_START | ||
| @token.raw = "%w#{start_char}" if @wants_raw | ||
| @token.delimiter_state = Token::DelimiterState.new(:string_array, start_char, closing_char(start_char)) | ||
| delimited_pair :string, current_char, closing_char, start | ||
| when 'i' | ||
| case peek_next_char | ||
| when '(', '{', '[', '<', '|' | ||
| start_char = next_char | ||
| next_char :SYMBOL_ARRAY_START | ||
| @token.raw = "%i#{start_char}" if @wants_raw | ||
| @token.delimiter_state = Token::DelimiterState.new(:symbol_array, start_char, closing_char(start_char)) | ||
| else | ||
| @token.type = :"%" | ||
| end | ||
| when 'q' | ||
| case peek_next_char | ||
| when '(', '{', '[', '<', '|' | ||
| next_char | ||
| delimited_pair :string, current_char, closing_char, start, allow_escapes: false | ||
| else | ||
| @token.type = :"%" | ||
| end | ||
| when 'Q' | ||
| case peek_next_char | ||
| when '(', '{', '[', '<', '|' | ||
| next_char | ||
| delimited_pair :string, current_char, closing_char, start | ||
| else | ||
| @token.type = :"%" | ||
| end | ||
| when 'r' | ||
| case next_char | ||
| when '(', '[', '{', '<', '|' | ||
| delimited_pair :regex, current_char, closing_char, start | ||
| else | ||
| raise "unknown %r char" | ||
| end | ||
| when 'x' | ||
| case next_char | ||
| when '(', '[', '{', '<', '|' | ||
| delimited_pair :command, current_char, closing_char, start | ||
| else | ||
| raise "unknown %x char" | ||
| end | ||
| when 'w' | ||
| case peek_next_char | ||
| when '(', '{', '[', '<', '|' | ||
| start_char = next_char | ||
| next_char :STRING_ARRAY_START | ||
| @token.raw = "%w#{start_char}" if @wants_raw | ||
| @token.delimiter_state = Token::DelimiterState.new(:string_array, start_char, closing_char(start_char)) | ||
| else | ||
| @token.type = :"%" | ||
| end | ||
| when '}' | ||
| next_char :"%}" | ||
| else | ||
| @token.type = :"%" | ||
| end | ||
| when '}' | ||
| next_char :"%}" | ||
| else | ||
| @token.type = :"%" | ||
| end | ||
| when '(' then next_char :"(" | ||
| when ')' then next_char :")" | ||
|
|
@@ -412,7 +426,12 @@ module Crystal | |
| symbol "*" | ||
| end | ||
| when '/' | ||
| next_char_and_symbol "/" | ||
| case next_char | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto ditto :-)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be here if next_char == '/'
next_char_and_symbol "//"
else
symbol "/"
end |
||
| when '/' | ||
| next_char_and_symbol "//" | ||
| else | ||
| symbol "/" | ||
| end | ||
| when '=' | ||
| case next_char | ||
| when '=' | ||
|
|
@@ -694,10 +713,14 @@ module Crystal | |
| set_token_raw_from_start(start) | ||
| when '"', '`' | ||
| delimiter = current_char | ||
| next_char | ||
| @token.type = :DELIMITER_START | ||
| @token.delimiter_state = Token::DelimiterState.new(delimiter == '`' ? :command : :string, delimiter, delimiter) | ||
| set_token_raw_from_start(start) | ||
| if delimiter == '`' && @wants_def_or_macro_name | ||
| next_char :"`" | ||
| else | ||
| next_char | ||
| @token.type = :DELIMITER_START | ||
| @token.delimiter_state = Token::DelimiterState.new(delimiter == '`' ? :command : :string, delimiter, delimiter) | ||
| set_token_raw_from_start(start) | ||
| end | ||
| when '0' | ||
| scan_zero_number(start) | ||
| when '1', '2', '3', '4', '5', '6', '7', '8', '9' | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace simply by
if v = slash_is_regexThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the same.
slash_is_regexisBool?so you need to test for.nil?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok 👍 - the language evaluate if the expression is true and not nil, and we just want to know if it isn't nil.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be abusing that the default value of slash_is_regex is false.