diff options
author | Kevin Newton <[email protected]> | 2024-11-04 11:07:37 -0500 |
---|---|---|
committer | Kevin Newton <[email protected]> | 2024-12-16 10:51:22 -0500 |
commit | 2ab1b07b84a9f2947f52b8b7bb758eb00a51b47f (patch) | |
tree | 3c278eb4929d878b0fafa1f620a91909d70cdc4f /lib/prism | |
parent | cc967a470b65b3fe4bae00d930a42e213eca6687 (diff) |
[ruby/prism] Simplify srange_find in parser compiler
https://github.com/ruby/prism/commit/34efacc618
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/12358
Diffstat (limited to 'lib/prism')
-rw-r--r-- | lib/prism/translation/parser/compiler.rb | 38 |
1 files changed, 20 insertions, 18 deletions
diff --git a/lib/prism/translation/parser/compiler.rb b/lib/prism/translation/parser/compiler.rb index fa51f33bca..d66e553fa3 100644 --- a/lib/prism/translation/parser/compiler.rb +++ b/lib/prism/translation/parser/compiler.rb @@ -188,7 +188,7 @@ module Prism rescue_clause.exceptions.any? ? builder.array(nil, visit_all(rescue_clause.exceptions), nil) : nil, token(rescue_clause.operator_loc), visit(rescue_clause.reference), - srange_find(find_start_offset, find_end_offset, [";"]), + srange_find(find_start_offset, find_end_offset, ";"), visit(rescue_clause.statements) ) end until (rescue_clause = rescue_clause.subsequent).nil? @@ -294,7 +294,7 @@ module Prism visit_all(arguments), token(node.closing_loc), ), - srange_find(node.message_loc.end_offset, node.arguments.arguments.last.location.start_offset, ["="]), + srange_find(node.message_loc.end_offset, node.arguments.arguments.last.location.start_offset, "="), visit(node.arguments.arguments.last) ), block @@ -311,7 +311,7 @@ module Prism if name.end_with?("=") && !message_loc.slice.end_with?("=") && node.arguments && block.nil? builder.assign( builder.attr_asgn(visit(node.receiver), call_operator, token(message_loc)), - srange_find(message_loc.end_offset, node.arguments.location.start_offset, ["="]), + srange_find(message_loc.end_offset, node.arguments.location.start_offset, "="), visit(node.arguments.arguments.last) ) else @@ -736,7 +736,7 @@ module Prism if (do_keyword_loc = node.do_keyword_loc) token(do_keyword_loc) else - srange_find(node.collection.location.end_offset, (node.statements&.location || node.end_keyword_loc).start_offset, [";"]) + srange_find(node.collection.location.end_offset, (node.statements&.location || node.end_keyword_loc).start_offset, ";") end, visit(node.statements), token(node.end_keyword_loc) @@ -868,7 +868,7 @@ module Prism if (then_keyword_loc = node.then_keyword_loc) token(then_keyword_loc) else - srange_find(node.predicate.location.end_offset, (node.statements&.location || node.subsequent&.location || node.end_keyword_loc).start_offset, [";"]) + srange_find(node.predicate.location.end_offset, (node.statements&.location || node.subsequent&.location || node.end_keyword_loc).start_offset, ";") end, visit(node.statements), case node.subsequent @@ -931,7 +931,11 @@ module Prism token(node.in_loc), pattern, guard, - srange_find(node.pattern.location.end_offset, node.statements&.location&.start_offset, [";", "then"]), + if (then_loc = node.then_loc) + token(then_loc) + else + srange_find(node.pattern.location.end_offset, node.statements&.location&.start_offset, ";") + end, visit(node.statements) ) end @@ -1784,7 +1788,7 @@ module Prism if (then_keyword_loc = node.then_keyword_loc) token(then_keyword_loc) else - srange_find(node.predicate.location.end_offset, (node.statements&.location || node.else_clause&.location || node.end_keyword_loc).start_offset, [";"]) + srange_find(node.predicate.location.end_offset, (node.statements&.location || node.else_clause&.location || node.end_keyword_loc).start_offset, ";") end, visit(node.else_clause), token(node.else_clause&.else_keyword_loc), @@ -1815,7 +1819,7 @@ module Prism if (do_keyword_loc = node.do_keyword_loc) token(do_keyword_loc) else - srange_find(node.predicate.location.end_offset, (node.statements&.location || node.closing_loc).start_offset, [";"]) + srange_find(node.predicate.location.end_offset, (node.statements&.location || node.closing_loc).start_offset, ";") end, visit(node.statements), token(node.closing_loc) @@ -1839,7 +1843,7 @@ module Prism if (then_keyword_loc = node.then_keyword_loc) token(then_keyword_loc) else - srange_find(node.conditions.last.location.end_offset, node.statements&.location&.start_offset, [";"]) + srange_find(node.conditions.last.location.end_offset, node.statements&.location&.start_offset, ";") end, visit(node.statements) ) @@ -1859,7 +1863,7 @@ module Prism if (do_keyword_loc = node.do_keyword_loc) token(do_keyword_loc) else - srange_find(node.predicate.location.end_offset, (node.statements&.location || node.closing_loc).start_offset, [";"]) + srange_find(node.predicate.location.end_offset, (node.statements&.location || node.closing_loc).start_offset, ";") end, visit(node.statements), token(node.closing_loc) @@ -1993,18 +1997,16 @@ module Prism Range.new(source_buffer, offset_cache[start_offset], offset_cache[end_offset]) end - # Constructs a new source range by finding the given tokens between the - # given start offset and end offset. If the needle is not found, it + # Constructs a new source range by finding the given character between + # the given start offset and end offset. If the needle is not found, it # returns nil. Importantly it does not search past newlines or comments. # # Note that end_offset is allowed to be nil, in which case this will # search until the end of the string. - def srange_find(start_offset, end_offset, tokens) - if (match = source_buffer.source.byteslice(start_offset...end_offset).match(/\A(\s*)(#{tokens.join("|")})/)) - _, whitespace, token = *match - token_offset = start_offset + whitespace.bytesize - - [token, Range.new(source_buffer, offset_cache[token_offset], offset_cache[token_offset + token.bytesize])] + def srange_find(start_offset, end_offset, character) + if (match = source_buffer.source.byteslice(start_offset...end_offset)[/\A\s*#{character}/]) + final_offset = start_offset + match.bytesize + [character, Range.new(source_buffer, offset_cache[final_offset - character.bytesize], offset_cache[final_offset])] end end |