ruby_json_parser 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rubocop.yml +35 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE +21 -0
- data/README.md +143 -0
- data/Rakefile +12 -0
- data/lib/ruby_json_parser/ast.rb +312 -0
- data/lib/ruby_json_parser/evaluator.rb +81 -0
- data/lib/ruby_json_parser/lexer.rb +358 -0
- data/lib/ruby_json_parser/parser.rb +205 -0
- data/lib/ruby_json_parser/result.rb +43 -0
- data/lib/ruby_json_parser/token.rb +171 -0
- data/lib/ruby_json_parser/version.rb +6 -0
- data/lib/ruby_json_parser.rb +77 -0
- data/sorbet/config +4 -0
- data/sorbet/rbi/annotations/.gitattributes +1 -0
- data/sorbet/rbi/annotations/minitest.rbi +119 -0
- data/sorbet/rbi/annotations/rainbow.rbi +269 -0
- data/sorbet/rbi/gems/.gitattributes +1 -0
- data/sorbet/rbi/gems/ast@2.4.2.rbi +585 -0
- data/sorbet/rbi/gems/erubi@1.13.0.rbi +150 -0
- data/sorbet/rbi/gems/json@2.7.2.rbi +1562 -0
- data/sorbet/rbi/gems/language_server-protocol@3.17.0.3.rbi +14238 -0
- data/sorbet/rbi/gems/minitest@5.24.1.rbi +1563 -0
- data/sorbet/rbi/gems/netrc@0.11.0.rbi +159 -0
- data/sorbet/rbi/gems/parallel@1.25.1.rbi +287 -0
- data/sorbet/rbi/gems/parser@3.3.4.0.rbi +5519 -0
- data/sorbet/rbi/gems/prism@0.30.0.rbi +39212 -0
- data/sorbet/rbi/gems/racc@1.8.0.rbi +162 -0
- data/sorbet/rbi/gems/rainbow@3.1.1.rbi +403 -0
- data/sorbet/rbi/gems/rake@13.2.1.rbi +3028 -0
- data/sorbet/rbi/gems/rbi@0.1.13.rbi +3078 -0
- data/sorbet/rbi/gems/regexp_parser@2.9.2.rbi +3772 -0
- data/sorbet/rbi/gems/rexml@3.3.1.rbi +4813 -0
- data/sorbet/rbi/gems/rubocop-ast@1.31.3.rbi +7015 -0
- data/sorbet/rbi/gems/rubocop@1.65.0.rbi +58191 -0
- data/sorbet/rbi/gems/ruby-progressbar@1.13.0.rbi +1318 -0
- data/sorbet/rbi/gems/spoom@1.3.3.rbi +4926 -0
- data/sorbet/rbi/gems/strscan@3.1.0.rbi +9 -0
- data/sorbet/rbi/gems/tapioca@0.15.1.rbi +3566 -0
- data/sorbet/rbi/gems/thor@1.3.1.rbi +4352 -0
- data/sorbet/rbi/gems/unicode-display_width@2.5.0.rbi +66 -0
- data/sorbet/rbi/gems/yard-sorbet@0.9.0.rbi +435 -0
- data/sorbet/rbi/gems/yard@0.9.36.rbi +18221 -0
- data/sorbet/tapioca/config.yml +13 -0
- data/sorbet/tapioca/require.rb +4 -0
- metadata +105 -0
@@ -0,0 +1,585 @@
|
|
1
|
+
# typed: true
|
2
|
+
|
3
|
+
# DO NOT EDIT MANUALLY
|
4
|
+
# This is an autogenerated file for types exported from the `ast` gem.
|
5
|
+
# Please instead update this file by running `bin/tapioca gem ast`.
|
6
|
+
|
7
|
+
|
8
|
+
# {AST} is a library for manipulating abstract syntax trees.
|
9
|
+
#
|
10
|
+
# It embraces immutability; each AST node is inherently frozen at
|
11
|
+
# creation, and updating a child node requires recreating that node
|
12
|
+
# and its every parent, recursively.
|
13
|
+
# This is a design choice. It does create some pressure on
|
14
|
+
# garbage collector, but completely eliminates all concurrency
|
15
|
+
# and aliasing problems.
|
16
|
+
#
|
17
|
+
# See also {AST::Node}, {AST::Processor::Mixin} and {AST::Sexp} for
|
18
|
+
# additional recommendations and design patterns.
|
19
|
+
#
|
20
|
+
# source://ast//lib/ast.rb#13
|
21
|
+
module AST; end
|
22
|
+
|
23
|
+
# Node is an immutable class, instances of which represent abstract
|
24
|
+
# syntax tree nodes. It combines semantic information (i.e. anything
|
25
|
+
# that affects the algorithmic properties of a program) with
|
26
|
+
# meta-information (line numbers or compiler intermediates).
|
27
|
+
#
|
28
|
+
# Notes on inheritance
|
29
|
+
# ====================
|
30
|
+
#
|
31
|
+
# The distinction between semantics and metadata is important. Complete
|
32
|
+
# semantic information should be contained within just the {#type} and
|
33
|
+
# {#children} of a Node instance; in other words, if an AST was to be
|
34
|
+
# stripped of all meta-information, it should remain a valid AST which
|
35
|
+
# could be successfully processed to yield a result with the same
|
36
|
+
# algorithmic properties.
|
37
|
+
#
|
38
|
+
# Thus, Node should never be inherited in order to define methods which
|
39
|
+
# affect or return semantic information, such as getters for `class_name`,
|
40
|
+
# `superclass` and `body` in the case of a hypothetical `ClassNode`. The
|
41
|
+
# correct solution is to use a generic Node with a {#type} of `:class`
|
42
|
+
# and three children. See also {Processor} for tips on working with such
|
43
|
+
# ASTs.
|
44
|
+
#
|
45
|
+
# On the other hand, Node can and should be inherited to define
|
46
|
+
# application-specific metadata (see also {#initialize}) or customize the
|
47
|
+
# printing format. It is expected that an application would have one or two
|
48
|
+
# such classes and use them across the entire codebase.
|
49
|
+
#
|
50
|
+
# The rationale for this pattern is extensibility and maintainability.
|
51
|
+
# Unlike static ones, dynamic languages do not require the presence of a
|
52
|
+
# predefined, rigid structure, nor does it improve dispatch efficiency,
|
53
|
+
# and while such a structure can certainly be defined, it does not add
|
54
|
+
# any value but incurs a maintaining cost.
|
55
|
+
# For example, extending the AST even with a transformation-local
|
56
|
+
# temporary node type requires making globally visible changes to
|
57
|
+
# the codebase.
|
58
|
+
#
|
59
|
+
# source://ast//lib/ast/node.rb#40
|
60
|
+
class AST::Node
|
61
|
+
# Constructs a new instance of Node.
|
62
|
+
#
|
63
|
+
# The arguments `type` and `children` are converted with `to_sym` and
|
64
|
+
# `to_a` respectively. Additionally, the result of converting `children`
|
65
|
+
# is frozen. While mutating the arguments is generally considered harmful,
|
66
|
+
# the most common case is to pass an array literal to the constructor. If
|
67
|
+
# your code does not expect the argument to be frozen, use `#dup`.
|
68
|
+
#
|
69
|
+
# The `properties` hash is passed to {#assign_properties}.
|
70
|
+
#
|
71
|
+
# @return [Node] a new instance of Node
|
72
|
+
#
|
73
|
+
# source://ast//lib/ast/node.rb#72
|
74
|
+
def initialize(type, children = T.unsafe(nil), properties = T.unsafe(nil)); end
|
75
|
+
|
76
|
+
# Concatenates `array` with `children` and returns the resulting node.
|
77
|
+
#
|
78
|
+
# @return [AST::Node]
|
79
|
+
#
|
80
|
+
# source://ast//lib/ast/node.rb#168
|
81
|
+
def +(array); end
|
82
|
+
|
83
|
+
# Appends `element` to `children` and returns the resulting node.
|
84
|
+
#
|
85
|
+
# @return [AST::Node]
|
86
|
+
#
|
87
|
+
# source://ast//lib/ast/node.rb#177
|
88
|
+
def <<(element); end
|
89
|
+
|
90
|
+
# Compares `self` to `other`, possibly converting with `to_ast`. Only
|
91
|
+
# `type` and `children` are compared; metadata is deliberately ignored.
|
92
|
+
#
|
93
|
+
# @return [Boolean]
|
94
|
+
#
|
95
|
+
# source://ast//lib/ast/node.rb#153
|
96
|
+
def ==(other); end
|
97
|
+
|
98
|
+
# Appends `element` to `children` and returns the resulting node.
|
99
|
+
#
|
100
|
+
# @return [AST::Node]
|
101
|
+
#
|
102
|
+
# source://ast//lib/ast/node.rb#177
|
103
|
+
def append(element); end
|
104
|
+
|
105
|
+
# Returns the children of this node.
|
106
|
+
# The returned value is frozen.
|
107
|
+
# The to_a alias is useful for decomposing nodes concisely.
|
108
|
+
# For example:
|
109
|
+
#
|
110
|
+
# node = s(:gasgn, :$foo, s(:integer, 1))
|
111
|
+
# var_name, value = *node
|
112
|
+
# p var_name # => :$foo
|
113
|
+
# p value # => (integer 1)
|
114
|
+
#
|
115
|
+
# @return [Array]
|
116
|
+
#
|
117
|
+
# source://ast//lib/ast/node.rb#56
|
118
|
+
def children; end
|
119
|
+
|
120
|
+
# Nodes are already frozen, so there is no harm in returning the
|
121
|
+
# current node as opposed to initializing from scratch and freezing
|
122
|
+
# another one.
|
123
|
+
#
|
124
|
+
# @return self
|
125
|
+
#
|
126
|
+
# source://ast//lib/ast/node.rb#115
|
127
|
+
def clone; end
|
128
|
+
|
129
|
+
# Concatenates `array` with `children` and returns the resulting node.
|
130
|
+
#
|
131
|
+
# @return [AST::Node]
|
132
|
+
#
|
133
|
+
# source://ast//lib/ast/node.rb#168
|
134
|
+
def concat(array); end
|
135
|
+
|
136
|
+
# Enables matching for Node, where type is the first element
|
137
|
+
# and the children are remaining items.
|
138
|
+
#
|
139
|
+
# @return [Array]
|
140
|
+
#
|
141
|
+
# source://ast//lib/ast/node.rb#253
|
142
|
+
def deconstruct; end
|
143
|
+
|
144
|
+
# Nodes are already frozen, so there is no harm in returning the
|
145
|
+
# current node as opposed to initializing from scratch and freezing
|
146
|
+
# another one.
|
147
|
+
#
|
148
|
+
# @return self
|
149
|
+
#
|
150
|
+
# source://ast//lib/ast/node.rb#115
|
151
|
+
def dup; end
|
152
|
+
|
153
|
+
# Test if other object is equal to
|
154
|
+
#
|
155
|
+
# @param other [Object]
|
156
|
+
# @return [Boolean]
|
157
|
+
#
|
158
|
+
# source://ast//lib/ast/node.rb#85
|
159
|
+
def eql?(other); end
|
160
|
+
|
161
|
+
# Returns the precomputed hash value for this node
|
162
|
+
#
|
163
|
+
# @return [Fixnum]
|
164
|
+
#
|
165
|
+
# source://ast//lib/ast/node.rb#61
|
166
|
+
def hash; end
|
167
|
+
|
168
|
+
# Converts `self` to a s-expression ruby string.
|
169
|
+
# The code return will recreate the node, using the sexp module s()
|
170
|
+
#
|
171
|
+
# @param indent [Integer] Base indentation level.
|
172
|
+
# @return [String]
|
173
|
+
#
|
174
|
+
# source://ast//lib/ast/node.rb#211
|
175
|
+
def inspect(indent = T.unsafe(nil)); end
|
176
|
+
|
177
|
+
# Returns the children of this node.
|
178
|
+
# The returned value is frozen.
|
179
|
+
# The to_a alias is useful for decomposing nodes concisely.
|
180
|
+
# For example:
|
181
|
+
#
|
182
|
+
# node = s(:gasgn, :$foo, s(:integer, 1))
|
183
|
+
# var_name, value = *node
|
184
|
+
# p var_name # => :$foo
|
185
|
+
# p value # => (integer 1)
|
186
|
+
#
|
187
|
+
# @return [Array]
|
188
|
+
#
|
189
|
+
# source://ast//lib/ast/node.rb#56
|
190
|
+
def to_a; end
|
191
|
+
|
192
|
+
# @return [AST::Node] self
|
193
|
+
#
|
194
|
+
# source://ast//lib/ast/node.rb#229
|
195
|
+
def to_ast; end
|
196
|
+
|
197
|
+
# Converts `self` to a pretty-printed s-expression.
|
198
|
+
#
|
199
|
+
# @param indent [Integer] Base indentation level.
|
200
|
+
# @return [String]
|
201
|
+
#
|
202
|
+
# source://ast//lib/ast/node.rb#187
|
203
|
+
def to_s(indent = T.unsafe(nil)); end
|
204
|
+
|
205
|
+
# Converts `self` to a pretty-printed s-expression.
|
206
|
+
#
|
207
|
+
# @param indent [Integer] Base indentation level.
|
208
|
+
# @return [String]
|
209
|
+
#
|
210
|
+
# source://ast//lib/ast/node.rb#187
|
211
|
+
def to_sexp(indent = T.unsafe(nil)); end
|
212
|
+
|
213
|
+
# Converts `self` to an Array where the first element is the type as a Symbol,
|
214
|
+
# and subsequent elements are the same representation of its children.
|
215
|
+
#
|
216
|
+
# @return [Array<Symbol, [...Array]>]
|
217
|
+
#
|
218
|
+
# source://ast//lib/ast/node.rb#237
|
219
|
+
def to_sexp_array; end
|
220
|
+
|
221
|
+
# Returns the type of this node.
|
222
|
+
#
|
223
|
+
# @return [Symbol]
|
224
|
+
#
|
225
|
+
# source://ast//lib/ast/node.rb#43
|
226
|
+
def type; end
|
227
|
+
|
228
|
+
# Returns a new instance of Node where non-nil arguments replace the
|
229
|
+
# corresponding fields of `self`.
|
230
|
+
#
|
231
|
+
# For example, `Node.new(:foo, [ 1, 2 ]).updated(:bar)` would yield
|
232
|
+
# `(bar 1 2)`, and `Node.new(:foo, [ 1, 2 ]).updated(nil, [])` would
|
233
|
+
# yield `(foo)`.
|
234
|
+
#
|
235
|
+
# If the resulting node would be identical to `self`, does nothing.
|
236
|
+
#
|
237
|
+
# @param type [Symbol, nil]
|
238
|
+
# @param children [Array, nil]
|
239
|
+
# @param properties [Hash, nil]
|
240
|
+
# @return [AST::Node]
|
241
|
+
#
|
242
|
+
# source://ast//lib/ast/node.rb#133
|
243
|
+
def updated(type = T.unsafe(nil), children = T.unsafe(nil), properties = T.unsafe(nil)); end
|
244
|
+
|
245
|
+
protected
|
246
|
+
|
247
|
+
# By default, each entry in the `properties` hash is assigned to
|
248
|
+
# an instance variable in this instance of Node. A subclass should define
|
249
|
+
# attribute readers for such variables. The values passed in the hash
|
250
|
+
# are not frozen or whitelisted; such behavior can also be implemented
|
251
|
+
# by subclassing Node and overriding this method.
|
252
|
+
#
|
253
|
+
# @return [nil]
|
254
|
+
#
|
255
|
+
# source://ast//lib/ast/node.rb#98
|
256
|
+
def assign_properties(properties); end
|
257
|
+
|
258
|
+
# Returns `@type` with all underscores replaced by dashes. This allows
|
259
|
+
# to write symbol literals without quotes in Ruby sources and yet have
|
260
|
+
# nicely looking s-expressions.
|
261
|
+
#
|
262
|
+
# @return [String]
|
263
|
+
#
|
264
|
+
# source://ast//lib/ast/node.rb#264
|
265
|
+
def fancy_type; end
|
266
|
+
|
267
|
+
private
|
268
|
+
|
269
|
+
def original_dup; end
|
270
|
+
end
|
271
|
+
|
272
|
+
# This class includes {AST::Processor::Mixin}; however, it is
|
273
|
+
# deprecated, since the module defines all of the behaviors that
|
274
|
+
# the processor includes. Any new libraries should use
|
275
|
+
# {AST::Processor::Mixin} instead of subclassing this.
|
276
|
+
#
|
277
|
+
# @deprecated Use {AST::Processor::Mixin} instead.
|
278
|
+
#
|
279
|
+
# source://ast//lib/ast/processor.rb#8
|
280
|
+
class AST::Processor
|
281
|
+
include ::AST::Processor::Mixin
|
282
|
+
end
|
283
|
+
|
284
|
+
# The processor module is a module which helps transforming one
|
285
|
+
# AST into another. In a nutshell, the {#process} method accepts
|
286
|
+
# a {Node} and dispatches it to a handler corresponding to its
|
287
|
+
# type, and returns a (possibly) updated variant of the node.
|
288
|
+
#
|
289
|
+
# The processor module has a set of associated design patterns.
|
290
|
+
# They are best explained with a concrete example. Let's define a
|
291
|
+
# simple arithmetic language and an AST format for it:
|
292
|
+
#
|
293
|
+
# Terminals (AST nodes which do not have other AST nodes inside):
|
294
|
+
#
|
295
|
+
# * `(integer <int-literal>)`,
|
296
|
+
#
|
297
|
+
# Nonterminals (AST nodes with other nodes as children):
|
298
|
+
#
|
299
|
+
# * `(add <node> <node>)`,
|
300
|
+
# * `(multiply <node> <node>)`,
|
301
|
+
# * `(divide <node> <node>)`,
|
302
|
+
# * `(negate <node>)`,
|
303
|
+
# * `(store <node> <string-literal>)`: stores value of `<node>`
|
304
|
+
# into a variable named `<string-literal>`,
|
305
|
+
# * `(load <string-literal>)`: loads value of a variable named
|
306
|
+
# `<string-literal>`,
|
307
|
+
# * `(each <node> ...)`: computes each of the `<node>`s and
|
308
|
+
# prints the result.
|
309
|
+
#
|
310
|
+
# All AST nodes have the same Ruby class, and therefore they don't
|
311
|
+
# know how to traverse themselves. (A solution which dynamically
|
312
|
+
# checks the type of children is possible, but is slow and
|
313
|
+
# error-prone.) So, a class including the module which knows how
|
314
|
+
# to traverse the entire tree should be defined. Such classes
|
315
|
+
# have a handler for each nonterminal node which recursively
|
316
|
+
# processes children nodes:
|
317
|
+
#
|
318
|
+
# require 'ast'
|
319
|
+
#
|
320
|
+
# class ArithmeticsProcessor
|
321
|
+
# include AST::Processor::Mixin
|
322
|
+
# # This method traverses any binary operators such as (add)
|
323
|
+
# # or (multiply).
|
324
|
+
# def process_binary_op(node)
|
325
|
+
# # Children aren't decomposed automatically; it is
|
326
|
+
# # suggested to use Ruby multiple assignment expansion,
|
327
|
+
# # as it is very convenient here.
|
328
|
+
# left_expr, right_expr = *node
|
329
|
+
#
|
330
|
+
# # AST::Node#updated won't change node type if nil is
|
331
|
+
# # passed as a first argument, which allows to reuse the
|
332
|
+
# # same handler for multiple node types using `alias'
|
333
|
+
# # (below).
|
334
|
+
# node.updated(nil, [
|
335
|
+
# process(left_expr),
|
336
|
+
# process(right_expr)
|
337
|
+
# ])
|
338
|
+
# end
|
339
|
+
# alias_method :on_add, :process_binary_op
|
340
|
+
# alias_method :on_multiply, :process_binary_op
|
341
|
+
# alias_method :on_divide, :process_binary_op
|
342
|
+
#
|
343
|
+
# def on_negate(node)
|
344
|
+
# # It is also possible to use #process_all for more
|
345
|
+
# # compact code if every child is a Node.
|
346
|
+
# node.updated(nil, process_all(node))
|
347
|
+
# end
|
348
|
+
#
|
349
|
+
# def on_store(node)
|
350
|
+
# expr, variable_name = *node
|
351
|
+
#
|
352
|
+
# # Note that variable_name is not a Node and thus isn't
|
353
|
+
# # passed to #process.
|
354
|
+
# node.updated(nil, [
|
355
|
+
# process(expr),
|
356
|
+
# variable_name
|
357
|
+
# ])
|
358
|
+
# end
|
359
|
+
#
|
360
|
+
# # (load) is effectively a terminal node, and so it does
|
361
|
+
# # not need an explicit handler, as the following is the
|
362
|
+
# # default behavior. Essentially, for any nodes that don't
|
363
|
+
# # have a defined handler, the node remains unchanged.
|
364
|
+
# def on_load(node)
|
365
|
+
# nil
|
366
|
+
# end
|
367
|
+
#
|
368
|
+
# def on_each(node)
|
369
|
+
# node.updated(nil, process_all(node))
|
370
|
+
# end
|
371
|
+
# end
|
372
|
+
#
|
373
|
+
# Let's test our ArithmeticsProcessor:
|
374
|
+
#
|
375
|
+
# include AST::Sexp
|
376
|
+
# expr = s(:add, s(:integer, 2), s(:integer, 2))
|
377
|
+
#
|
378
|
+
# p ArithmeticsProcessor.new.process(expr) == expr # => true
|
379
|
+
#
|
380
|
+
# As expected, it does not change anything at all. This isn't
|
381
|
+
# actually very useful, so let's now define a Calculator, which
|
382
|
+
# will compute the expression values:
|
383
|
+
#
|
384
|
+
# # This Processor folds nonterminal nodes and returns an
|
385
|
+
# # (integer) terminal node.
|
386
|
+
# class ArithmeticsCalculator < ArithmeticsProcessor
|
387
|
+
# def compute_op(node)
|
388
|
+
# # First, node children are processed and then unpacked
|
389
|
+
# # to local variables.
|
390
|
+
# nodes = process_all(node)
|
391
|
+
#
|
392
|
+
# if nodes.all? { |node| node.type == :integer }
|
393
|
+
# # If each of those nodes represents a literal, we can
|
394
|
+
# # fold this node!
|
395
|
+
# values = nodes.map { |node| node.children.first }
|
396
|
+
# AST::Node.new(:integer, [
|
397
|
+
# yield(values)
|
398
|
+
# ])
|
399
|
+
# else
|
400
|
+
# # Otherwise, we can just leave the current node in the
|
401
|
+
# # tree and only update it with processed children
|
402
|
+
# # nodes, which can be partially folded.
|
403
|
+
# node.updated(nil, nodes)
|
404
|
+
# end
|
405
|
+
# end
|
406
|
+
#
|
407
|
+
# def on_add(node)
|
408
|
+
# compute_op(node) { |left, right| left + right }
|
409
|
+
# end
|
410
|
+
#
|
411
|
+
# def on_multiply(node)
|
412
|
+
# compute_op(node) { |left, right| left * right }
|
413
|
+
# end
|
414
|
+
# end
|
415
|
+
#
|
416
|
+
# Let's check:
|
417
|
+
#
|
418
|
+
# p ArithmeticsCalculator.new.process(expr) # => (integer 4)
|
419
|
+
#
|
420
|
+
# Excellent, the calculator works! Now, a careful reader could
|
421
|
+
# notice that the ArithmeticsCalculator does not know how to
|
422
|
+
# divide numbers. What if we pass an expression with division to
|
423
|
+
# it?
|
424
|
+
#
|
425
|
+
# expr_with_division = \
|
426
|
+
# s(:add,
|
427
|
+
# s(:integer, 1),
|
428
|
+
# s(:divide,
|
429
|
+
# s(:add, s(:integer, 8), s(:integer, 4)),
|
430
|
+
# s(:integer, 3))) # 1 + (8 + 4) / 3
|
431
|
+
#
|
432
|
+
# folded_expr_with_division = ArithmeticsCalculator.new.process(expr_with_division)
|
433
|
+
# p folded_expr_with_division
|
434
|
+
# # => (add
|
435
|
+
# # (integer 1)
|
436
|
+
# # (divide
|
437
|
+
# # (integer 12)
|
438
|
+
# # (integer 3)))
|
439
|
+
#
|
440
|
+
# As you can see, the expression was folded _partially_: the inner
|
441
|
+
# `(add)` node which could be computed was folded to
|
442
|
+
# `(integer 12)`, the `(divide)` node is left as-is because there
|
443
|
+
# is no computing handler for it, and the root `(add)` node was
|
444
|
+
# also left as it is because some of its children were not
|
445
|
+
# literals.
|
446
|
+
#
|
447
|
+
# Note that this partial folding is only possible because the
|
448
|
+
# _data_ format, i.e. the format in which the computed values of
|
449
|
+
# the nodes are represented, is the same as the AST itself.
|
450
|
+
#
|
451
|
+
# Let's extend our ArithmeticsCalculator class further.
|
452
|
+
#
|
453
|
+
# class ArithmeticsCalculator
|
454
|
+
# def on_divide(node)
|
455
|
+
# compute_op(node) { |left, right| left / right }
|
456
|
+
# end
|
457
|
+
#
|
458
|
+
# def on_negate(node)
|
459
|
+
# # Note how #compute_op works regardless of the operator
|
460
|
+
# # arity.
|
461
|
+
# compute_op(node) { |value| -value }
|
462
|
+
# end
|
463
|
+
# end
|
464
|
+
#
|
465
|
+
# Now, let's apply our renewed ArithmeticsCalculator to a partial
|
466
|
+
# result of previous evaluation:
|
467
|
+
#
|
468
|
+
# p ArithmeticsCalculator.new.process(expr_with_division) # => (integer 5)
|
469
|
+
#
|
470
|
+
# Five! Excellent. This is also pretty much how CRuby 1.8 executed
|
471
|
+
# its programs.
|
472
|
+
#
|
473
|
+
# Now, let's do some automated bug searching. Division by zero is
|
474
|
+
# an error, right? So if we could detect that someone has divided
|
475
|
+
# by zero before the program is even run, that could save some
|
476
|
+
# debugging time.
|
477
|
+
#
|
478
|
+
# class DivisionByZeroVerifier < ArithmeticsProcessor
|
479
|
+
# class VerificationFailure < Exception; end
|
480
|
+
#
|
481
|
+
# def on_divide(node)
|
482
|
+
# # You need to process the children to handle nested divisions
|
483
|
+
# # such as:
|
484
|
+
# # (divide
|
485
|
+
# # (integer 1)
|
486
|
+
# # (divide (integer 1) (integer 0))
|
487
|
+
# left, right = process_all(node)
|
488
|
+
#
|
489
|
+
# if right.type == :integer &&
|
490
|
+
# right.children.first == 0
|
491
|
+
# raise VerificationFailure, "Ouch! This code divides by zero."
|
492
|
+
# end
|
493
|
+
# end
|
494
|
+
#
|
495
|
+
# def divides_by_zero?(ast)
|
496
|
+
# process(ast)
|
497
|
+
# false
|
498
|
+
# rescue VerificationFailure
|
499
|
+
# true
|
500
|
+
# end
|
501
|
+
# end
|
502
|
+
#
|
503
|
+
# nice_expr = \
|
504
|
+
# s(:divide,
|
505
|
+
# s(:add, s(:integer, 10), s(:integer, 2)),
|
506
|
+
# s(:integer, 4))
|
507
|
+
#
|
508
|
+
# p DivisionByZeroVerifier.new.divides_by_zero?(nice_expr)
|
509
|
+
# # => false. Good.
|
510
|
+
#
|
511
|
+
# bad_expr = \
|
512
|
+
# s(:add, s(:integer, 10),
|
513
|
+
# s(:divide, s(:integer, 1), s(:integer, 0)))
|
514
|
+
#
|
515
|
+
# p DivisionByZeroVerifier.new.divides_by_zero?(bad_expr)
|
516
|
+
# # => true. WHOOPS. DO NOT RUN THIS.
|
517
|
+
#
|
518
|
+
# Of course, this won't detect more complex cases... unless you
|
519
|
+
# use some partial evaluation before! The possibilites are
|
520
|
+
# endless. Have fun.
|
521
|
+
#
|
522
|
+
# source://ast//lib/ast/processor/mixin.rb#240
|
523
|
+
module AST::Processor::Mixin
|
524
|
+
# Default handler. Does nothing.
|
525
|
+
#
|
526
|
+
# @param node [AST::Node]
|
527
|
+
# @return [AST::Node, nil]
|
528
|
+
#
|
529
|
+
# source://ast//lib/ast/processor/mixin.rb#284
|
530
|
+
def handler_missing(node); end
|
531
|
+
|
532
|
+
# Dispatches `node`. If a node has type `:foo`, then a handler
|
533
|
+
# named `on_foo` is invoked with one argument, the `node`; if
|
534
|
+
# there isn't such a handler, {#handler_missing} is invoked
|
535
|
+
# with the same argument.
|
536
|
+
#
|
537
|
+
# If the handler returns `nil`, `node` is returned; otherwise,
|
538
|
+
# the return value of the handler is passed along.
|
539
|
+
#
|
540
|
+
# @param node [AST::Node, nil]
|
541
|
+
# @return [AST::Node, nil]
|
542
|
+
#
|
543
|
+
# source://ast//lib/ast/processor/mixin.rb#251
|
544
|
+
def process(node); end
|
545
|
+
|
546
|
+
# {#process}es each node from `nodes` and returns an array of
|
547
|
+
# results.
|
548
|
+
#
|
549
|
+
# @param nodes [Array<AST::Node>]
|
550
|
+
# @return [Array<AST::Node>]
|
551
|
+
#
|
552
|
+
# source://ast//lib/ast/processor/mixin.rb#274
|
553
|
+
def process_all(nodes); end
|
554
|
+
end
|
555
|
+
|
556
|
+
# This simple module is very useful in the cases where one needs
|
557
|
+
# to define deeply nested ASTs from Ruby code, for example, in
|
558
|
+
# tests. It should be used like this:
|
559
|
+
#
|
560
|
+
# describe YourLanguage::AST do
|
561
|
+
# include Sexp
|
562
|
+
#
|
563
|
+
# it "should correctly parse expressions" do
|
564
|
+
# YourLanguage.parse("1 + 2 * 3").should ==
|
565
|
+
# s(:add,
|
566
|
+
# s(:integer, 1),
|
567
|
+
# s(:multiply,
|
568
|
+
# s(:integer, 2),
|
569
|
+
# s(:integer, 3)))
|
570
|
+
# end
|
571
|
+
# end
|
572
|
+
#
|
573
|
+
# This way the amount of boilerplate code is greatly reduced.
|
574
|
+
#
|
575
|
+
# source://ast//lib/ast/sexp.rb#20
|
576
|
+
module AST::Sexp
|
577
|
+
# Creates a {Node} with type `type` and children `children`.
|
578
|
+
# Note that the resulting node is of the type AST::Node and not a
|
579
|
+
# subclass.
|
580
|
+
# This would not pose a problem with comparisons, as {Node#==}
|
581
|
+
# ignores metadata.
|
582
|
+
#
|
583
|
+
# source://ast//lib/ast/sexp.rb#26
|
584
|
+
def s(type, *children); end
|
585
|
+
end
|