Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 087350a

Browse files
committed
Fold Dot2 and Dot3 into RangeLiteral
1 parent 5f64ba7 commit 087350a

File tree

6 files changed

+45
-117
lines changed

6 files changed

+45
-117
lines changed

CHANGELOG.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
99
### Changed
1010

1111
- Nodes no longer have a `comments:` keyword on their initializers. By default, they initialize to an empty array. If you were previously passing comments into the initializer, you should now create the node first, then call `node.comments.concat` to add your comments.
12-
- `IfMod` and `UnlessMod` are no longer nodes. Instead, they have been folded into `If` and `Unless`, respectively. The `If` and `Unless` nodes now have a `modifier?` method to tell you if they were original found in the modifier form.
13-
- `WhileMod` and `UntilMod` are no longer nodes. Instead, they have been folded into `While` and `Until`, respectively. The `While` and `Until` nodes now have a `modifier?` method to tell you if they were originally found in the modifier form.
14-
- `VarAlias` is no longer a node. Instead it has been folded into the `Alias` node. The `Alias` node now has a `var_alias?` method to tell you if it is aliasing a global variable.
15-
- `Yield0` is no longer a node. Instead if has been folded into the `Yield` node. The `Yield` node can now have its `arguments` field be `nil`.
16-
- `FCall` is no longer a node. Instead it has been folded into the `Call` node. The `Call` node can now have its `receiver` and `operator` fields be `nil`.
12+
- A lot of nodes have been folded into other nodes to make it easier to interact with the AST. This means that a lot of visit methods have been removed from the visitor and a lot of class definitions are no longer present. This also means that the nodes that received more function now have additional methods or fields to be able to differentiate them. Note that none of these changes have resulted in different formatting. The changes are listed below:
13+
- `IfMod`, `UnlessMod`, `WhileMod`, `UntilMod` have been folded into `If`, `Unless`, `While`, and `Until`. Each of the nodes now have a `modifier?` method to tell if it was originally in the modifier form. Consequently, the `visit_if_mod`, `visit_unless_mod`, `visit_while_mod`, and `visit_until_mod` methods have been removed from the visitor.
14+
- `VarAlias` is no longer a node. Instead it has been folded into the `Alias` node. The `Alias` node now has a `var_alias?` method to tell you if it is aliasing a global variable. Consequently, the `visit_var_alias` method has been removed from the visitor interface. If you were previously using this method, you should now use `visit_alias` instead.
15+
- `Yield0` is no longer a node. Instead if has been folded into the `Yield` node. The `Yield` node can now have its `arguments` field be `nil`. Consequently, the `visit_yield0` method has been removed from the visitor interface. If you were previously using this method, you should now use `visit_yield` instead.
16+
- `FCall` is no longer a node. Instead it has been folded into the `Call` node. The `Call` node can now have its `receiver` and `operator` fields be `nil`. Consequently, the `visit_fcall` method has been removed from the visitor interface. If you were previously using this method, you should now use `visit_call` instead.
17+
- `Dot2` and `Dot3` are no longer nodes. Instead they have become a single new `RangeLiteral` node. This node looks the same as `Dot2` and `Dot3`, except that it additionally has an `operator` field that contains the operator that created the node. Consequently, the `visit_dot2` and `visit_dot3` methods have been removed from the visitor interface. If you were previously using these methods, you should now use `visit_range_literal` instead.
1718

1819
## [4.3.0] - 2022-10-28
1920

lib/syntax_tree/node.rb

Lines changed: 19 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -3878,38 +3878,8 @@ def format(q)
38783878
end
38793879
end
38803880

3881-
# Responsible for formatting Dot2 and Dot3 nodes.
3882-
class DotFormatter
3883-
# [String] the operator to display
3884-
attr_reader :operator
3885-
3886-
# [Dot2 | Dot3] the node that is being formatter
3887-
attr_reader :node
3888-
3889-
def initialize(operator, node)
3890-
@operator = operator
3891-
@node = node
3892-
end
3893-
3894-
def format(q)
3895-
left = node.left
3896-
right = node.right
3897-
3898-
q.format(left) if left
3899-
3900-
case q.parent
3901-
when If, Unless
3902-
q.text(" #{operator} ")
3903-
else
3904-
q.text(operator)
3905-
end
3906-
3907-
q.format(right) if right
3908-
end
3909-
end
3910-
3911-
# Dot2 represents using the .. operator between two expressions. Usually this
3912-
# is to create a range object.
3881+
# RangeLiteral represents using the .. or the ... operator between two
3882+
# expressions. Usually this is to create a range object.
39133883
#
39143884
# 1..2
39153885
#
@@ -3919,25 +3889,29 @@ def format(q)
39193889
# end
39203890
#
39213891
# One of the sides of the expression may be nil, but not both.
3922-
class Dot2 < Node
3892+
class RangeLiteral < Node
39233893
# [nil | untyped] the left side of the expression
39243894
attr_reader :left
39253895

3896+
# [Op] the operator used for this range
3897+
attr_reader :operator
3898+
39263899
# [nil | untyped] the right side of the expression
39273900
attr_reader :right
39283901

39293902
# [Array[ Comment | EmbDoc ]] the comments attached to this node
39303903
attr_reader :comments
39313904

3932-
def initialize(left:, right:, location:)
3905+
def initialize(left:, operator:, right:, location:)
39333906
@left = left
3907+
@operator = operator
39343908
@right = right
39353909
@location = location
39363910
@comments = []
39373911
end
39383912

39393913
def accept(visitor)
3940-
visitor.visit_dot2(self)
3914+
visitor.visit_range_literal(self)
39413915
end
39423916

39433917
def child_nodes
@@ -3947,59 +3921,20 @@ def child_nodes
39473921
alias deconstruct child_nodes
39483922

39493923
def deconstruct_keys(_keys)
3950-
{ left: left, right: right, location: location, comments: comments }
3924+
{ left: left, operator: operator, right: right, location: location, comments: comments }
39513925
end
39523926

39533927
def format(q)
3954-
DotFormatter.new("..", self).format(q)
3955-
end
3956-
end
3957-
3958-
# Dot3 represents using the ... operator between two expressions. Usually this
3959-
# is to create a range object. It's effectively the same event as the Dot2
3960-
# node but with this operator you're asking Ruby to omit the final value.
3961-
#
3962-
# 1...2
3963-
#
3964-
# Like Dot2 it can also be used to create a flip-flop.
3965-
#
3966-
# if value == 5 ... value == 10
3967-
# end
3968-
#
3969-
# One of the sides of the expression may be nil, but not both.
3970-
class Dot3 < Node
3971-
# [nil | untyped] the left side of the expression
3972-
attr_reader :left
3973-
3974-
# [nil | untyped] the right side of the expression
3975-
attr_reader :right
3976-
3977-
# [Array[ Comment | EmbDoc ]] the comments attached to this node
3978-
attr_reader :comments
3979-
3980-
def initialize(left:, right:, location:)
3981-
@left = left
3982-
@right = right
3983-
@location = location
3984-
@comments = []
3985-
end
3986-
3987-
def accept(visitor)
3988-
visitor.visit_dot3(self)
3989-
end
3990-
3991-
def child_nodes
3992-
[left, right]
3993-
end
3994-
3995-
alias deconstruct child_nodes
3928+
q.format(left) if left
39963929

3997-
def deconstruct_keys(_keys)
3998-
{ left: left, right: right, location: location, comments: comments }
3999-
end
3930+
case q.parent
3931+
when If, Unless
3932+
q.text(" #{operator.value} ")
3933+
else
3934+
q.text(operator.value)
3935+
end
40003936

4001-
def format(q)
4002-
DotFormatter.new("...", self).format(q)
3937+
q.format(right) if right
40033938
end
40043939
end
40053940

@@ -9771,7 +9706,7 @@ def format(q)
97719706
# last argument to the predicate is and endless range, then you are
97729707
# forced to use the "then" keyword to make it parse properly.
97739708
last = arguments.parts.last
9774-
if (last.is_a?(Dot2) || last.is_a?(Dot3)) && !last.right
9709+
if last.is_a?(RangeLiteral) && !last.right
97759710
q.text(" then")
97769711
end
97779712
end

lib/syntax_tree/parser.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,30 +1357,32 @@ def on_do_block(block_var, bodystmt)
13571357
end
13581358

13591359
# :call-seq:
1360-
# on_dot2: ((nil | untyped) left, (nil | untyped) right) -> Dot2
1360+
# on_dot2: ((nil | untyped) left, (nil | untyped) right) -> RangeLiteral
13611361
def on_dot2(left, right)
13621362
operator = consume_operator(:"..")
13631363

13641364
beginning = left || operator
13651365
ending = right || operator
13661366

1367-
Dot2.new(
1367+
RangeLiteral.new(
13681368
left: left,
1369+
operator: operator,
13691370
right: right,
13701371
location: beginning.location.to(ending.location)
13711372
)
13721373
end
13731374

13741375
# :call-seq:
1375-
# on_dot3: ((nil | untyped) left, (nil | untyped) right) -> Dot3
1376+
# on_dot3: ((nil | untyped) left, (nil | untyped) right) -> RangeLiteral
13761377
def on_dot3(left, right)
13771378
operator = consume_operator(:"...")
13781379

13791380
beginning = left || operator
13801381
ending = right || operator
13811382

1382-
Dot3.new(
1383+
RangeLiteral.new(
13831384
left: left,
1385+
operator: operator,
13841386
right: right,
13851387
location: beginning.location.to(ending.location)
13861388
)

lib/syntax_tree/visitor.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,6 @@ class Visitor < BasicVisitor
131131
# Visit a DoBlock node.
132132
alias visit_do_block visit_child_nodes
133133

134-
# Visit a Dot2 node.
135-
alias visit_dot2 visit_child_nodes
136-
137-
# Visit a Dot3 node.
138-
alias visit_dot3 visit_child_nodes
139-
140134
# Visit a DynaSymbol node.
141135
alias visit_dyna_symbol visit_child_nodes
142136

@@ -305,6 +299,9 @@ class Visitor < BasicVisitor
305299
# Visit a QWordsBeg node.
306300
alias visit_qwords_beg visit_child_nodes
307301

302+
# Visit a RangeLiteral node
303+
alias visit_range_literal visit_child_nodes
304+
308305
# Visit a RAssign node.
309306
alias visit_rassign visit_child_nodes
310307

lib/syntax_tree/visitor/field_visitor.rb

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -362,22 +362,6 @@ def visit_do_block(node)
362362
end
363363
end
364364

365-
def visit_dot2(node)
366-
node(node, "dot2") do
367-
field("left", node.left) if node.left
368-
field("right", node.right) if node.right
369-
comments(node)
370-
end
371-
end
372-
373-
def visit_dot3(node)
374-
node(node, "dot3") do
375-
field("left", node.left) if node.left
376-
field("right", node.right) if node.right
377-
comments(node)
378-
end
379-
end
380-
381365
def visit_dyna_symbol(node)
382366
node(node, "dyna_symbol") do
383367
list("parts", node.parts)
@@ -739,6 +723,15 @@ def visit_qwords_beg(node)
739723
node(node, "qwords_beg") { field("value", node.value) }
740724
end
741725

726+
def visit_range_literal(node)
727+
node(node, "range_literal") do
728+
field("left", node.left) if node.left
729+
field("operator", node.operator)
730+
field("right", node.right) if node.right
731+
comments(node)
732+
end
733+
end
734+
742735
def visit_rassign(node)
743736
node(node, "rassign") do
744737
field("value", node.value)

test/node_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,11 +414,11 @@ def test_do_block
414414
end
415415

416416
def test_dot2
417-
assert_node(Dot2, "1..3")
417+
assert_node(RangeLiteral, "1..3")
418418
end
419419

420420
def test_dot3
421-
assert_node(Dot3, "1...3")
421+
assert_node(RangeLiteral, "1...3")
422422
end
423423

424424
def test_dyna_symbol

0 commit comments

Comments
 (0)