Skip to content
Open
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3016,9 +3016,9 @@ nodes:
- name: statements
type: node?
kind: StatementsNode
- name: in_loc
- name: in_keyword_loc
type: location
- name: then_loc
- name: then_keyword_loc
type: location?
comment: |
Represents the use of the `in` keyword in a case statement.
Expand Down Expand Up @@ -3603,7 +3603,7 @@ nodes:
- name: pattern
type: node
kind: pattern expression
- name: operator_loc
- name: keyword_loc
type: location
comment: |
Represents the use of the modifier `in` operator.
Expand Down Expand Up @@ -4576,7 +4576,7 @@ nodes:
^^^^^^^^^^^^^^^^^^^^^^
- name: UnlessNode
fields:
- name: keyword_loc
- name: unless_keyword_loc
type: location
comment: |
The Location of the `unless` keyword.
Expand Down Expand Up @@ -4640,11 +4640,11 @@ nodes:
- name: UntilNode
flags: LoopFlags
fields:
- name: keyword_loc
- name: until_keyword_loc
type: location
- name: do_keyword_loc
type: location?
- name: closing_loc
- name: end_keyword_loc
type: location?
- name: predicate
type: node
Expand All @@ -4663,7 +4663,7 @@ nodes:
^^^^^^^^^^^^^^^^^^^^
- name: WhenNode
fields:
- name: keyword_loc
- name: when_keyword_loc
type: location
- name: conditions
type: node[]
Expand All @@ -4683,11 +4683,11 @@ nodes:
- name: WhileNode
flags: LoopFlags
fields:
- name: keyword_loc
- name: while_keyword_loc
type: location
- name: do_keyword_loc
type: location?
- name: closing_loc
- name: end_keyword_loc
type: location?
- name: predicate
type: node
Expand Down
102 changes: 102 additions & 0 deletions lib/prism/node_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,106 @@ def full_message_loc
attribute_write? ? message_loc&.adjoin("=") : message_loc
end
end

class InNode < Node
#: () -> String
def in # :nodoc
in_keyword
end

#: () -> Location
def in_loc # :nodoc
in_keyword_loc
end

#: () -> String?
def then # :nodoc
then_keyword
end

#: () -> Location?
def then_loc # :nodoc
then_keyword_loc
end
end

class MatchPredicateNode < Node
#: () -> String
def operator # :nodoc
keyword
end

#: () -> Location
def operator_loc # :nodoc
keyword_loc
end
end

class UnlessNode < Node
#: () -> String
def keyword # :nodoc
unless_keyword
end

#: () -> Location
def keyword_loc # :nodoc
unless_keyword_loc
end
end

class UntilNode < Node
#: () -> String
def keyword # :nodoc
until_keyword
end

#: () -> Location
def keyword_loc # :nodoc
until_keyword_loc
end

#: () -> String?
def closing # :nodoc
end_keyword
end

#: () -> Location?
def closing_loc # :nodoc
end_keyword_loc
end
end

class WhenNode < Node
#: () -> String
def keyword # :nodoc
when_keyword
end

#: () -> Location
def keyword_loc # :nodoc
when_keyword_loc
end
end

class WhileNode < Node
#: () -> String
def keyword # :nodoc
while_keyword
end

#: () -> Location
def keyword_loc # :nodoc
while_keyword_loc
end

#: () -> String?
def closing # :nodoc
end_keyword
end

#: () -> Location?
def closing_loc # :nodoc
end_keyword_loc
end
end
end
38 changes: 19 additions & 19 deletions lib/prism/translation/parser/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -975,17 +975,17 @@ def visit_in_node(node)
guard = builder.if_guard(token(node.pattern.if_keyword_loc), visit(node.pattern.predicate))
when UnlessNode
pattern = within_pattern { |compiler| node.pattern.statements.accept(compiler) }
guard = builder.unless_guard(token(node.pattern.keyword_loc), visit(node.pattern.predicate))
guard = builder.unless_guard(token(node.pattern.unless_keyword_loc), visit(node.pattern.predicate))
else
pattern = within_pattern { |compiler| node.pattern.accept(compiler) }
end

builder.in_pattern(
token(node.in_loc),
token(node.in_keyword_loc),
pattern,
guard,
if (then_loc = node.then_loc)
token(then_loc)
if (then_keyword_loc = node.then_keyword_loc)
token(then_keyword_loc)
else
srange_semicolon(node.pattern.location.end_offset, node.statements&.location&.start_offset)
end,
Expand Down Expand Up @@ -1296,7 +1296,7 @@ def visit_local_variable_target_node(node)
def visit_match_predicate_node(node)
builder.match_pattern_p(
visit(node.value),
token(node.operator_loc),
token(node.keyword_loc),
within_pattern { |compiler| node.pattern.accept(compiler) }
)
end
Expand Down Expand Up @@ -1807,9 +1807,9 @@ def visit_undef_node(node)
# bar unless foo
# ^^^^^^^^^^^^^^
def visit_unless_node(node)
if node.keyword_loc.start_offset == node.location.start_offset
if node.unless_keyword_loc.start_offset == node.location.start_offset
builder.condition(
token(node.keyword_loc),
token(node.unless_keyword_loc),
visit(node.predicate),
if (then_keyword_loc = node.then_keyword_loc)
token(then_keyword_loc)
Expand All @@ -1825,7 +1825,7 @@ def visit_unless_node(node)
builder.condition_mod(
visit(node.else_clause),
visit(node.statements),
token(node.keyword_loc),
token(node.unless_keyword_loc),
visit(node.predicate)
)
end
Expand All @@ -1837,24 +1837,24 @@ def visit_unless_node(node)
# bar until foo
# ^^^^^^^^^^^^^
def visit_until_node(node)
if node.location.start_offset == node.keyword_loc.start_offset
if node.location.start_offset == node.until_keyword_loc.start_offset
builder.loop(
:until,
token(node.keyword_loc),
token(node.until_keyword_loc),
visit(node.predicate),
if (do_keyword_loc = node.do_keyword_loc)
token(do_keyword_loc)
else
srange_semicolon(node.predicate.location.end_offset, (node.statements&.location || node.closing_loc).start_offset)
srange_semicolon(node.predicate.location.end_offset, (node.statements&.location || node.end_keyword_loc).start_offset)
end,
visit(node.statements),
token(node.closing_loc)
token(node.end_keyword_loc)
)
else
builder.loop_mod(
:until,
visit(node.statements),
token(node.keyword_loc),
token(node.until_keyword_loc),
visit(node.predicate)
)
end
Expand All @@ -1864,7 +1864,7 @@ def visit_until_node(node)
# ^^^^^^^^^^^^^
def visit_when_node(node)
builder.when(
token(node.keyword_loc),
token(node.when_keyword_loc),
visit_all(node.conditions),
if (then_keyword_loc = node.then_keyword_loc)
token(then_keyword_loc)
Expand All @@ -1881,24 +1881,24 @@ def visit_when_node(node)
# bar while foo
# ^^^^^^^^^^^^^
def visit_while_node(node)
if node.location.start_offset == node.keyword_loc.start_offset
if node.location.start_offset == node.while_keyword_loc.start_offset
builder.loop(
:while,
token(node.keyword_loc),
token(node.while_keyword_loc),
visit(node.predicate),
if (do_keyword_loc = node.do_keyword_loc)
token(do_keyword_loc)
else
srange_semicolon(node.predicate.location.end_offset, (node.statements&.location || node.closing_loc).start_offset)
srange_semicolon(node.predicate.location.end_offset, (node.statements&.location || node.end_keyword_loc).start_offset)
end,
visit(node.statements),
token(node.closing_loc)
token(node.end_keyword_loc)
)
else
builder.loop_mod(
:while,
visit(node.statements),
token(node.keyword_loc),
token(node.while_keyword_loc),
visit(node.predicate)
)
end
Expand Down
28 changes: 14 additions & 14 deletions lib/prism/translation/ripper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2169,12 +2169,12 @@ def visit_in_node(node)
# This is a special case where we're not going to call on_in directly
# because we don't have access to the subsequent. Instead, we'll return
# the component parts and let the parent node handle it.
bounds(node.in_loc)
bounds(node.in_keyword_loc)
on_kw("in")

pattern = visit_pattern_node(node.pattern)
if node.then_loc
bounds(node.then_loc)
if node.then_keyword_loc
bounds(node.then_keyword_loc)
on_kw("then")
end
statements =
Expand Down Expand Up @@ -2633,7 +2633,7 @@ def visit_match_last_line_node(node)
# ^^^^^^^^^^
def visit_match_predicate_node(node)
value = visit(node.value)
bounds(node.operator_loc)
bounds(node.keyword_loc)
on_kw("in")
pattern = on_in(visit_pattern_node(node.pattern), nil, nil)

Expand Down Expand Up @@ -3430,7 +3430,7 @@ def visit_undef_node(node)
# ^^^^^^^^^^^^^^
def visit_unless_node(node)
if node.statements.nil? || (node.predicate.location.start_offset < node.statements.location.start_offset)
bounds(node.keyword_loc)
bounds(node.unless_keyword_loc)
on_kw("unless")
predicate = visit(node.predicate)
if node.then_keyword_loc
Expand All @@ -3455,7 +3455,7 @@ def visit_unless_node(node)
on_unless(predicate, statements, else_clause)
else
statements = visit(node.statements.body.first)
bounds(node.keyword_loc)
bounds(node.unless_keyword_loc)
on_kw("unless")
predicate = visit(node.predicate)

Expand All @@ -3470,7 +3470,7 @@ def visit_unless_node(node)
# bar until foo
# ^^^^^^^^^^^^^
def visit_until_node(node)
bounds(node.keyword_loc)
bounds(node.until_keyword_loc)
on_kw("until")

if node.statements.nil? || (node.predicate.location.start_offset < node.statements.location.start_offset)
Expand All @@ -3487,8 +3487,8 @@ def visit_until_node(node)
visit(node.statements)
end

if node.closing_loc
bounds(node.closing_loc)
if node.end_keyword_loc
bounds(node.end_keyword_loc)
on_kw("end")
end

Expand All @@ -3509,7 +3509,7 @@ def visit_when_node(node)
# This is a special case where we're not going to call on_when directly
# because we don't have access to the subsequent. Instead, we'll return
# the component parts and let the parent node handle it.
bounds(node.keyword_loc)
bounds(node.when_keyword_loc)
on_kw("when")

conditions = visit_arguments(node.conditions)
Expand All @@ -3535,15 +3535,15 @@ def visit_when_node(node)
# ^^^^^^^^^^^^^
def visit_while_node(node)
if node.statements.nil? || (node.predicate.location.start_offset < node.statements.location.start_offset)
bounds(node.keyword_loc)
bounds(node.while_keyword_loc)
on_kw("while")
if node.do_keyword_loc
bounds(node.do_keyword_loc)
on_kw("do")
end
predicate = visit(node.predicate)
if node.closing_loc
bounds(node.closing_loc)
if node.end_keyword_loc
bounds(node.end_keyword_loc)
on_kw("end")
end
statements =
Expand All @@ -3558,7 +3558,7 @@ def visit_while_node(node)
on_while(predicate, statements)
else
statements = visit(node.statements.body.first)
bounds(node.keyword_loc)
bounds(node.while_keyword_loc)
on_kw("while")
predicate = visit(node.predicate)

Expand Down
Loading