Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
10 changes: 10 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,16 @@ nodes:

foo(bar)
^
- name: equal_loc
type: location?
comment: |
Represents the location of the equal sign, in the case that this is an attribute write.

foo.bar = value
^

foo[bar] = value
^
- name: block
type: node?
kind:
Expand Down
32 changes: 16 additions & 16 deletions lib/prism/translation/parser/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def visit_begin_node(node)
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_semicolon(find_start_offset, find_end_offset),
visit(rescue_clause.statements)
)
end until (rescue_clause = rescue_clause.subsequent).nil?
Expand Down Expand Up @@ -323,7 +323,7 @@ def visit_call_node(node)
visit_all(arguments),
token(node.closing_loc),
),
srange_find(node.message_loc.end_offset, node.arguments.arguments.last.location.start_offset, "="),
token(node.equal_loc),
visit(node.arguments.arguments.last)
),
block
Expand All @@ -340,7 +340,7 @@ def visit_call_node(node)
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, "="),
token(node.equal_loc),
visit(node.arguments.arguments.last)
)
else
Expand Down Expand Up @@ -789,7 +789,7 @@ def visit_for_node(node)
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_semicolon(node.collection.location.end_offset, (node.statements&.location || node.end_keyword_loc).start_offset)
end,
visit(node.statements),
token(node.end_keyword_loc)
Expand Down Expand Up @@ -921,7 +921,7 @@ def visit_if_node(node)
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_semicolon(node.predicate.location.end_offset, (node.statements&.location || node.subsequent&.location || node.end_keyword_loc).start_offset)
end,
visit(node.statements),
case node.subsequent
Expand Down Expand Up @@ -987,7 +987,7 @@ def visit_in_node(node)
if (then_loc = node.then_loc)
token(then_loc)
else
srange_find(node.pattern.location.end_offset, node.statements&.location&.start_offset, ";")
srange_semicolon(node.pattern.location.end_offset, node.statements&.location&.start_offset)
end,
visit(node.statements)
)
Expand Down Expand Up @@ -1808,7 +1808,7 @@ def visit_unless_node(node)
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_semicolon(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),
Expand Down Expand Up @@ -1839,7 +1839,7 @@ def visit_until_node(node)
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_semicolon(node.predicate.location.end_offset, (node.statements&.location || node.closing_loc).start_offset)
end,
visit(node.statements),
token(node.closing_loc)
Expand All @@ -1863,7 +1863,7 @@ def visit_when_node(node)
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_semicolon(node.conditions.last.location.end_offset, node.statements&.location&.start_offset)
end,
visit(node.statements)
)
Expand All @@ -1883,7 +1883,7 @@ def visit_while_node(node)
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_semicolon(node.predicate.location.end_offset, (node.statements&.location || node.closing_loc).start_offset)
end,
visit(node.statements),
token(node.closing_loc)
Expand Down Expand Up @@ -2012,16 +2012,16 @@ def srange_offsets(start_offset, end_offset)
Range.new(source_buffer, offset_cache[start_offset], offset_cache[end_offset])
end

# 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.
# Constructs a new source range by finding a semicolon between the given
# start offset and end offset. If the semicolon 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, character)
if (match = source_buffer.source.byteslice(start_offset...end_offset)[/\A\s*#{character}/])
def srange_semicolon(start_offset, end_offset)
if (match = source_buffer.source.byteslice(start_offset...end_offset)[/\A\s*;/])
final_offset = start_offset + match.bytesize
[character, Range.new(source_buffer, offset_cache[final_offset - character.bytesize], offset_cache[final_offset])]
[";", Range.new(source_buffer, offset_cache[final_offset - 1], offset_cache[final_offset])]
end
end

Expand Down
3 changes: 3 additions & 0 deletions snapshots/3.3-3.3/block_args_in_array_assignment.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
│ ├── opening_loc: ∅
│ ├── arguments: ∅
│ ├── closing_loc: ∅
│ ├── equal_loc: ∅
│ └── block: ∅
├── call_operator_loc: ∅
├── name: :[]=
Expand All @@ -33,6 +34,7 @@
│ ├── flags: static_literal, decimal
│ └── value: 8
├── closing_loc: (1,16)-(1,17) = "]"
├── equal_loc: (1,18)-(1,19) = "="
└── block:
@ BlockArgumentNode (location: (1,10)-(1,16))
├── flags: ∅
Expand All @@ -46,5 +48,6 @@
│ ├── opening_loc: ∅
│ ├── arguments: ∅
│ ├── closing_loc: ∅
│ ├── equal_loc: ∅
│ └── block: ∅
└── operator_loc: (1,10)-(1,11) = "&"
3 changes: 3 additions & 0 deletions snapshots/3.3-3.3/it.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
│ ├── opening_loc: ∅
│ ├── arguments: ∅
│ ├── closing_loc: ∅
│ ├── equal_loc: ∅
│ └── block:
│ @ BlockNode (location: (1,2)-(3,3))
│ ├── flags: ∅
Expand All @@ -32,6 +33,7 @@
│ │ ├── opening_loc: ∅
│ │ ├── arguments: ∅
│ │ ├── closing_loc: ∅
│ │ ├── equal_loc: ∅
│ │ └── block: ∅
│ ├── opening_loc: (1,2)-(1,4) = "do"
│ └── closing_loc: (3,0)-(3,3) = "end"
Expand All @@ -55,4 +57,5 @@
├── opening_loc: ∅
├── arguments: ∅
├── closing_loc: ∅
├── equal_loc: ∅
└── block: ∅
18 changes: 18 additions & 0 deletions snapshots/3.3-3.3/it_indirect_writes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
│ ├── opening_loc: ∅
│ ├── arguments: ∅
│ ├── closing_loc: ∅
│ ├── equal_loc: ∅
│ └── block:
│ @ BlockNode (location: (1,4)-(1,15))
│ ├── flags: ∅
Expand Down Expand Up @@ -45,6 +46,7 @@
│ ├── opening_loc: ∅
│ ├── arguments: ∅
│ ├── closing_loc: ∅
│ ├── equal_loc: ∅
│ └── block:
│ @ BlockNode (location: (3,4)-(3,16))
│ ├── flags: ∅
Expand Down Expand Up @@ -75,6 +77,7 @@
│ ├── opening_loc: ∅
│ ├── arguments: ∅
│ ├── closing_loc: ∅
│ ├── equal_loc: ∅
│ └── block:
│ @ BlockNode (location: (5,4)-(5,16))
│ ├── flags: ∅
Expand Down Expand Up @@ -105,6 +108,7 @@
│ ├── opening_loc: ∅
│ ├── arguments: ∅
│ ├── closing_loc: ∅
│ ├── equal_loc: ∅
│ └── block:
│ @ BlockNode (location: (7,4)-(7,19))
│ ├── flags: ∅
Expand All @@ -123,6 +127,7 @@
│ │ │ ├── opening_loc: ∅
│ │ │ ├── arguments: ∅
│ │ │ ├── closing_loc: ∅
│ │ │ ├── equal_loc: ∅
│ │ │ └── block: ∅
│ │ └── @ LocalVariableOperatorWriteNode (location: (7,10)-(7,17))
│ │ ├── flags: newline
Expand All @@ -146,6 +151,7 @@
│ ├── opening_loc: ∅
│ ├── arguments: ∅
│ ├── closing_loc: ∅
│ ├── equal_loc: ∅
│ └── block:
│ @ BlockNode (location: (9,4)-(9,20))
│ ├── flags: ∅
Expand All @@ -164,6 +170,7 @@
│ │ │ ├── opening_loc: ∅
│ │ │ ├── arguments: ∅
│ │ │ ├── closing_loc: ∅
│ │ │ ├── equal_loc: ∅
│ │ │ └── block: ∅
│ │ └── @ LocalVariableOrWriteNode (location: (9,10)-(9,18))
│ │ ├── flags: newline
Expand All @@ -186,6 +193,7 @@
│ ├── opening_loc: ∅
│ ├── arguments: ∅
│ ├── closing_loc: ∅
│ ├── equal_loc: ∅
│ └── block:
│ @ BlockNode (location: (11,4)-(11,20))
│ ├── flags: ∅
Expand All @@ -204,6 +212,7 @@
│ │ │ ├── opening_loc: ∅
│ │ │ ├── arguments: ∅
│ │ │ ├── closing_loc: ∅
│ │ │ ├── equal_loc: ∅
│ │ │ └── block: ∅
│ │ └── @ LocalVariableAndWriteNode (location: (11,10)-(11,18))
│ │ ├── flags: newline
Expand All @@ -226,6 +235,7 @@
│ ├── opening_loc: ∅
│ ├── arguments: ∅
│ ├── closing_loc: ∅
│ ├── equal_loc: ∅
│ └── block:
│ @ BlockNode (location: (13,4)-(13,19))
│ ├── flags: ∅
Expand Down Expand Up @@ -261,6 +271,7 @@
│ ├── opening_loc: ∅
│ ├── arguments: ∅
│ ├── closing_loc: ∅
│ ├── equal_loc: ∅
│ └── block:
│ @ BlockNode (location: (15,4)-(15,20))
│ ├── flags: ∅
Expand Down Expand Up @@ -295,6 +306,7 @@
│ ├── opening_loc: ∅
│ ├── arguments: ∅
│ ├── closing_loc: ∅
│ ├── equal_loc: ∅
│ └── block:
│ @ BlockNode (location: (17,4)-(17,20))
│ ├── flags: ∅
Expand Down Expand Up @@ -329,6 +341,7 @@
│ ├── opening_loc: ∅
│ ├── arguments: ∅
│ ├── closing_loc: ∅
│ ├── equal_loc: ∅
│ └── block:
│ @ BlockNode (location: (19,4)-(19,23))
│ ├── flags: ∅
Expand All @@ -347,6 +360,7 @@
│ │ │ ├── opening_loc: ∅
│ │ │ ├── arguments: ∅
│ │ │ ├── closing_loc: ∅
│ │ │ ├── equal_loc: ∅
│ │ │ └── block: ∅
│ │ ├── @ LocalVariableOperatorWriteNode (location: (19,10)-(19,17))
│ │ │ ├── flags: newline
Expand Down Expand Up @@ -374,6 +388,7 @@
│ ├── opening_loc: ∅
│ ├── arguments: ∅
│ ├── closing_loc: ∅
│ ├── equal_loc: ∅
│ └── block:
│ @ BlockNode (location: (21,4)-(21,24))
│ ├── flags: ∅
Expand All @@ -392,6 +407,7 @@
│ │ │ ├── opening_loc: ∅
│ │ │ ├── arguments: ∅
│ │ │ ├── closing_loc: ∅
│ │ │ ├── equal_loc: ∅
│ │ │ └── block: ∅
│ │ ├── @ LocalVariableOrWriteNode (location: (21,10)-(21,18))
│ │ │ ├── flags: newline
Expand All @@ -418,6 +434,7 @@
├── opening_loc: ∅
├── arguments: ∅
├── closing_loc: ∅
├── equal_loc: ∅
└── block:
@ BlockNode (location: (23,4)-(23,24))
├── flags: ∅
Expand All @@ -436,6 +453,7 @@
│ │ ├── opening_loc: ∅
│ │ ├── arguments: ∅
│ │ ├── closing_loc: ∅
│ │ ├── equal_loc: ∅
│ │ └── block: ∅
│ ├── @ LocalVariableAndWriteNode (location: (23,10)-(23,18))
│ │ ├── flags: newline
Expand Down
4 changes: 4 additions & 0 deletions snapshots/3.3-3.3/it_read_and_assignment.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
├── opening_loc: ∅
├── arguments: ∅
├── closing_loc: ∅
├── equal_loc: ∅
└── block:
@ BlockNode (location: (1,7)-(1,30))
├── flags: ∅
Expand Down Expand Up @@ -46,8 +47,10 @@
│ │ │ ├── opening_loc: ∅
│ │ │ ├── arguments: ∅
│ │ │ ├── closing_loc: ∅
│ │ │ ├── equal_loc: ∅
│ │ │ └── block: ∅
│ │ ├── closing_loc: ∅
│ │ ├── equal_loc: ∅
│ │ └── block: ∅
│ ├── @ LocalVariableWriteNode (location: (1,15)-(1,22))
│ │ ├── flags: newline
Expand Down Expand Up @@ -76,6 +79,7 @@
│ │ ├── name: :it
│ │ └── depth: 0
│ ├── closing_loc: ∅
│ ├── equal_loc: ∅
│ └── block: ∅
├── opening_loc: (1,7)-(1,8) = "{"
└── closing_loc: (1,29)-(1,30) = "}"
2 changes: 2 additions & 0 deletions snapshots/3.3-3.3/it_with_ordinary_parameter.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
├── opening_loc: ∅
├── arguments: ∅
├── closing_loc: ∅
├── equal_loc: ∅
└── block:
@ BlockNode (location: (1,5)-(1,14))
├── flags: ∅
Expand All @@ -38,6 +39,7 @@
│ ├── opening_loc: ∅
│ ├── arguments: ∅
│ ├── closing_loc: ∅
│ ├── equal_loc: ∅
│ └── block: ∅
├── opening_loc: (1,5)-(1,6) = "{"
└── closing_loc: (1,13)-(1,14) = "}"
2 changes: 2 additions & 0 deletions snapshots/3.3-3.3/keyword_args_in_array_assignment.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
│ ├── opening_loc: ∅
│ ├── arguments: ∅
│ ├── closing_loc: ∅
│ ├── equal_loc: ∅
│ └── block: ∅
├── call_operator_loc: ∅
├── name: :[]=
Expand Down Expand Up @@ -53,4 +54,5 @@
│ ├── flags: static_literal, decimal
│ └── value: 8
├── closing_loc: (1,18)-(1,19) = "]"
├── equal_loc: (1,20)-(1,21) = "="
└── block: ∅
2 changes: 2 additions & 0 deletions snapshots/3.4/circular_parameters.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
│ ├── opening_loc: ∅
│ ├── arguments: ∅
│ ├── closing_loc: ∅
│ ├── equal_loc: ∅
│ └── block:
│ @ BlockNode (location: (3,5)-(3,20))
│ ├── flags: ∅
Expand Down Expand Up @@ -133,6 +134,7 @@
├── opening_loc: ∅
├── arguments: ∅
├── closing_loc: ∅
├── equal_loc: ∅
└── block:
@ BlockNode (location: (4,5)-(4,19))
├── flags: ∅
Expand Down
1 change: 1 addition & 0 deletions snapshots/3.4/it.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
│ ├── opening_loc: ∅
│ ├── arguments: ∅
│ ├── closing_loc: ∅
│ ├── equal_loc: ∅
│ └── block:
│ @ BlockNode (location: (1,2)-(3,3))
│ ├── flags: ∅
Expand Down
Loading