forked from rubocop/rubocop-ast
/
node.rb
260 lines (206 loc) · 6.35 KB
/
node.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# frozen_string_literal: true
module RuboCop
module AST
class NodePattern
# Base class for AST Nodes of a `NodePattern`
class Node < ::Parser::AST::Node
extend Forwardable
include ::RuboCop::AST::Descendence
using Ext::RangeMinMax
MATCHES_WITHIN_SET = %i[symbol number string].to_set.freeze
private_constant :MATCHES_WITHIN_SET
###
# To be overridden by subclasses
###
def rest?
false
end
def capture?
false
end
# @return [Integer, Range] An Integer for fixed length terms, otherwise a Range.
# Note: `arity.end` may be `Float::INFINITY`
def arity
1
end
# @return [Array<Node>, nil] replace node with result, or `nil` if no change requested.
def in_sequence_head
nil
end
###
# Utilities
###
# @return [Array<Node>]
def children_nodes
children.grep(Node)
end
# @return [Node] most nodes have only one child
def child
children[0]
end
# @return [Integer] nb of captures of that node and its descendants
def nb_captures
children_nodes.sum(&:nb_captures)
end
# @return [Boolean] returns whether it matches a variable number of elements
def variadic?
arity.is_a?(Range)
end
# @return [Boolean] returns true for nodes having a Ruby literal equivalent
# that matches within a Set (e.g. `42`, `:sym` but not `/regexp/`)
def matches_within_set?
MATCHES_WITHIN_SET.include?(type)
end
# @return [Range] arity as a Range
def arity_range
a = arity
a.is_a?(Range) ? a : INT_TO_RANGE[a]
end
def with(type: @type, children: @children, location: @location)
self.class.new(type, children, { location: location })
end
INT_TO_RANGE = Hash.new { |h, k| h[k] = k..k }
private_constant :INT_TO_RANGE
# :nodoc:
module ForbidInSeqHead
def in_sequence_head
raise NodePattern::Invalid, "A sequence can not start with a #{type}"
end
end
###
# Subclasses for specific node types
###
# Node class for `$something`
class Capture < Node
# Delegate most introspection methods to it's only child
def_delegators :child, :arity, :rest?
def capture?
true
end
def nb_captures
1 + super
end
def in_sequence_head
wildcard, original_child = child.in_sequence_head
return unless original_child
[wildcard, self] # ($...) => (_ $...)
end
end
# Node class for `(type first second ...)`
class Sequence < Node
include ForbidInSeqHead
def initialize(type, children = [], properties = {})
if (replace = children.first.in_sequence_head)
children = [*replace, *children[1..-1]]
end
super
end
end
# Node class for `predicate?(:arg, :list)`
class Predicate < Node
def method_name
children.first
end
def arg_list
children[1..-1]
end
end
FunctionCall = Predicate
# Node class for `int+`
class Repetition < Node
include ForbidInSeqHead
def operator
children[1]
end
ARITIES = {
'*': 0..Float::INFINITY,
'+': 1..Float::INFINITY,
'?': 0..1
}.freeze
def arity
ARITIES[operator]
end
end
# Node class for `...`
class Rest < Node
ARITY = (0..Float::INFINITY).freeze
private_constant :ARITY
def rest?
true
end
def arity
ARITY
end
def in_sequence_head
[Node.new(:wildcard), self]
end
end
# Node class for `<int str ...>`
class AnyOrder < Node
include ForbidInSeqHead
ARITIES = Hash.new { |h, k| h[k] = k - 1..Float::INFINITY }
private_constant :ARITIES
def term_nodes
ends_with_rest? ? children[0...-1] : children
end
def ends_with_rest?
children.last.rest?
end
def rest_node
children.last if ends_with_rest?
end
def arity
return children.size unless ends_with_rest?
ARITIES[children.size]
end
end
# A list (potentially empty) of nodes; part of a Union
class Subsequence < Node
include ForbidInSeqHead
def arity
min, max = children.map(&:arity_range).map(&:minmax).transpose.map(&:sum)
min == max ? min || 0 : min..max # NOTE: || 0 for empty case, where min == max == nil.
end
def in_sequence_head
super if children.empty?
return unless (replace = children.first.in_sequence_head)
[with(children: [*replace, *children[1..-1]])]
end
end
# Node class for `{ ... }`
class Union < Node
def arity
minima, maxima = children.map(&:arity_range).map(&:minmax).transpose
min = minima.min
max = maxima.max
min == max ? min : min..max
end
def in_sequence_head
return unless children.any?(&:in_sequence_head)
new_children = children.map do |child|
next child unless (replace = child.in_sequence_head)
if replace.size > 1
Subsequence.new(:subsequence, replace, loc: child.loc)
else
replace.first
end
end
[with(children: new_children)]
end
end
# Registry
MAP = Hash.new(Node).merge!(
sequence: Sequence,
repetition: Repetition,
rest: Rest,
capture: Capture,
predicate: Predicate,
any_order: AnyOrder,
function_call: FunctionCall,
subsequence: Subsequence,
union: Union
).freeze
end
end
end
end