syntax_tree 4.1.0 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 61bd75fb26dcf665293adb21c2cf21a5f0f26b56a2077d80ce5ff6806eaf434d
4
- data.tar.gz: 545306a76376f972805159e8eed622e825011b7fa9ef6b93cb9649c6caf9eccf
3
+ metadata.gz: 444c1c74319e55d9c3a656ff1796e6658237c0433a257eb6e2c66d8625d47243
4
+ data.tar.gz: 2662a60c92265c6b257f6545ee1947269e1990f96003c60caef790eeb871eb32
5
5
  SHA512:
6
- metadata.gz: 633701480cbe5c34bfcff52c4dd6c48f10cb22cba377b295ac56f2c288ce6a5dfdde57df5e682a3226f3d49ca7c3dd8d02db5d2d6d37acfc54df5c304be8a16f
7
- data.tar.gz: e4224136ab12c158c45bd6375308e01a4e7075c7da30536f82f77ebecd3d4c426c4798e80fb5eb6fef9a1771036d4ac7e5d709dc49c5b620cbe9a2c3f2dd15a0
6
+ metadata.gz: 9dddc08b2d7c2c3c7cec089f301b88220cf18a625ef7b155ad71810eaefd4cfe54c063279a4f817bc823b1eb586a222b106aa5889f3b736da901ca0404feedb5
7
+ data.tar.gz: eaabea185c2f53aaf62ea76b1bb89987d7c472538025ae3f3e457f97eafe38b2533895f504100ed534785ffebaa18a013dd8d1f27432da974beedf0b7202861c
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
  /vendor/
10
10
 
11
11
  test.rb
12
+ query.txt
data/CHANGELOG.md CHANGED
@@ -6,6 +6,17 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [4.2.0] - 2022-10-25
10
+
11
+ ### Added
12
+
13
+ - [#182](https://github.com/ruby-syntax-tree/syntax_tree/pull/182) - The new `stree expr` CLI command will function similarly to the `stree match` CLI command except that it only outputs the first expression of the program.
14
+ - [#182](https://github.com/ruby-syntax-tree/syntax_tree/pull/182) - Added the `SyntaxTree::Pattern` class for compiling `in` expressions into procs.
15
+
16
+ ### Changed
17
+
18
+ - [#182](https://github.com/ruby-syntax-tree/syntax_tree/pull/182) - Much more syntax is now supported by the search command.
19
+
9
20
  ## [4.1.0] - 2022-10-24
10
21
 
11
22
  ### Added
@@ -403,7 +414,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
403
414
 
404
415
  - 🎉 Initial release! 🎉
405
416
 
406
- [unreleased]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v4.1.0...HEAD
417
+ [unreleased]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v4.2.0...HEAD
418
+ [4.2.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v4.1.0...v4.2.0
407
419
  [4.1.0]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v4.0.2...v4.1.0
408
420
  [4.0.2]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v4.0.1...v4.0.2
409
421
  [4.0.1]: https://github.com/ruby-syntax-tree/syntax_tree/compare/v4.0.0...v4.0.1
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- syntax_tree (4.1.0)
4
+ syntax_tree (4.2.0)
5
5
  prettier_print (>= 1.0.2)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -15,6 +15,7 @@ It is built with only standard library dependencies. It additionally ships with
15
15
  - [CLI](#cli)
16
16
  - [ast](#ast)
17
17
  - [check](#check)
18
+ - [expr](#expr)
18
19
  - [format](#format)
19
20
  - [json](#json)
20
21
  - [match](#match)
@@ -26,6 +27,7 @@ It is built with only standard library dependencies. It additionally ships with
26
27
  - [SyntaxTree.read(filepath)](#syntaxtreereadfilepath)
27
28
  - [SyntaxTree.parse(source)](#syntaxtreeparsesource)
28
29
  - [SyntaxTree.format(source)](#syntaxtreeformatsource)
30
+ - [SyntaxTree.search(source, query, &block)](#syntaxtreesearchsource-query-block)
29
31
  - [Nodes](#nodes)
30
32
  - [child_nodes](#child_nodes)
31
33
  - [Pattern matching](#pattern-matching)
@@ -129,6 +131,24 @@ To change the print width that you are checking against, specify the `--print-wi
129
131
  stree check --print-width=100 path/to/file.rb
130
132
  ```
131
133
 
134
+ ### expr
135
+
136
+ This command will output a Ruby case-match expression that would match correctly against the first expression of the input.
137
+
138
+ ```sh
139
+ stree expr path/to/file.rb
140
+ ```
141
+
142
+ For a file that contains `1 + 1`, you will receive:
143
+
144
+ ```ruby
145
+ SyntaxTree::Binary[
146
+ left: SyntaxTree::Int[value: "1"],
147
+ operator: :+,
148
+ right: SyntaxTree::Int[value: "1"]
149
+ ]
150
+ ```
151
+
132
152
  ### format
133
153
 
134
154
  This command will output the formatted version of each of the listed files. Importantly, it will not write that content back to the source files. It is meant to display the formatted version only.
@@ -226,7 +246,7 @@ stree search VarRef path/to/file.rb
226
246
 
227
247
  For a file that contains `Foo + Bar` you will receive:
228
248
 
229
- ```ruby
249
+ ```
230
250
  path/to/file.rb:1:0: Foo + Bar
231
251
  path/to/file.rb:1:6: Foo + Bar
232
252
  ```
@@ -312,6 +332,10 @@ This function takes an input string containing Ruby code and returns the syntax
312
332
 
313
333
  This function takes an input string containing Ruby code, parses it into its underlying syntax tree, and formats it back out to a string. You can optionally pass a second argument to this method as well that is the maximum width to print. It defaults to `80`.
314
334
 
335
+ ### SyntaxTree.search(source, query, &block)
336
+
337
+ This function takes an input string containing Ruby code, an input string containing a valid Ruby `in` clause expression that can be used to match against nodes in the tree (can be generated using `stree expr`, `stree match`, or `Node#construct_keys`), and a block. Each node that matches the given query will be yielded to the block. The block will receive the node as its only argument.
338
+
315
339
  ## Nodes
316
340
 
317
341
  There are many different node types in the syntax tree. They are meant to be treated as immutable structs containing links to child nodes with minimal logic contained within their implementation. However, for the most part they all respond to a certain set of APIs, listed below.
@@ -188,6 +188,20 @@ module SyntaxTree
188
188
  end
189
189
  end
190
190
 
191
+ # An action of the CLI that outputs a pattern-matching Ruby expression that
192
+ # would match the first expression of the input given.
193
+ class Expr < Action
194
+ def run(item)
195
+ case item.handler.parse(item.source)
196
+ in Program[statements: Statements[body: [expression]]]
197
+ puts expression.construct_keys
198
+ else
199
+ warn("The input to `stree expr` must be a single expression.")
200
+ exit(1)
201
+ end
202
+ end
203
+ end
204
+
191
205
  # An action of the CLI that formats the input source and prints it out.
192
206
  class Format < Action
193
207
  def run(item)
@@ -219,10 +233,15 @@ module SyntaxTree
219
233
 
220
234
  def initialize(query)
221
235
  query = File.read(query) if File.readable?(query)
222
- @search = SyntaxTree::Search.new(query)
223
- rescue SyntaxTree::Search::UncompilableError => error
224
- warn(error.message)
225
- exit(1)
236
+ pattern =
237
+ begin
238
+ Pattern.new(query).compile
239
+ rescue Pattern::CompilationError => error
240
+ warn(error.message)
241
+ exit(1)
242
+ end
243
+
244
+ @search = SyntaxTree::Search.new(pattern)
226
245
  end
227
246
 
228
247
  def run(item)
@@ -281,6 +300,10 @@ module SyntaxTree
281
300
  #{Color.bold("stree doc [--plugins=...] [-e SCRIPT] FILE")}
282
301
  Print out the doc tree that would be used to format the given files
283
302
 
303
+ #{Color.bold("stree expr [-e SCRIPT] FILE")}
304
+ Print out a pattern-matching Ruby expression that would match the first
305
+ expression of the given files
306
+
284
307
  #{Color.bold("stree format [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE")}
285
308
  Print out the formatted version of the given files
286
309
 
@@ -436,6 +459,8 @@ module SyntaxTree
436
459
  Debug.new(options)
437
460
  when "doc"
438
461
  Doc.new(options)
462
+ when "e", "expr"
463
+ Expr.new(options)
439
464
  when "f", "format"
440
465
  Format.new(options)
441
466
  when "help"
@@ -0,0 +1,172 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SyntaxTree
4
+ # A pattern is an object that wraps a Ruby pattern matching expression. The
5
+ # expression would normally be passed to an `in` clause within a `case`
6
+ # expression or a rightward assignment expression. For example, in the
7
+ # following snippet:
8
+ #
9
+ # case node
10
+ # in Const[value: "SyntaxTree"]
11
+ # end
12
+ #
13
+ # the pattern is the `Const[value: "SyntaxTree"]` expression. Within Syntax
14
+ # Tree, every node generates these kinds of expressions using the
15
+ # #construct_keys method.
16
+ #
17
+ # The pattern gets compiled into an object that responds to call by running
18
+ # the #compile method. This method itself will run back through Syntax Tree to
19
+ # parse the expression into a tree, then walk the tree to generate the
20
+ # necessary callable objects. For example, if you wanted to compile the
21
+ # expression above into a callable, you would:
22
+ #
23
+ # callable = SyntaxTree::Pattern.new("Const[value: 'SyntaxTree']").compile
24
+ # callable.call(node)
25
+ #
26
+ # The callable object returned by #compile is guaranteed to respond to #call
27
+ # with a single argument, which is the node to match against. It also is
28
+ # guaranteed to respond to #===, which means it itself can be used in a `case`
29
+ # expression, as in:
30
+ #
31
+ # case node
32
+ # when callable
33
+ # end
34
+ #
35
+ # If the query given to the initializer cannot be compiled into a valid
36
+ # matcher (either because of a syntax error or because it is using syntax we
37
+ # do not yet support) then a SyntaxTree::Pattern::CompilationError will be
38
+ # raised.
39
+ class Pattern
40
+ # Raised when the query given to a pattern is either invalid Ruby syntax or
41
+ # is using syntax that we don't yet support.
42
+ class CompilationError < StandardError
43
+ def initialize(repr)
44
+ super(<<~ERROR)
45
+ Syntax Tree was unable to compile the pattern you provided to search
46
+ into a usable expression. It failed on to understand the node
47
+ represented by:
48
+
49
+ #{repr}
50
+
51
+ Note that not all syntax supported by Ruby's pattern matching syntax
52
+ is also supported by Syntax Tree's code search. If you're using some
53
+ syntax that you believe should be supported, please open an issue on
54
+ GitHub at https://github.com/ruby-syntax-tree/syntax_tree/issues/new.
55
+ ERROR
56
+ end
57
+ end
58
+
59
+ attr_reader :query
60
+
61
+ def initialize(query)
62
+ @query = query
63
+ end
64
+
65
+ def compile
66
+ program =
67
+ begin
68
+ SyntaxTree.parse("case nil\nin #{query}\nend")
69
+ rescue Parser::ParseError
70
+ raise CompilationError, query
71
+ end
72
+
73
+ compile_node(program.statements.body.first.consequent.pattern)
74
+ end
75
+
76
+ private
77
+
78
+ def combine_and(left, right)
79
+ ->(node) { left.call(node) && right.call(node) }
80
+ end
81
+
82
+ def combine_or(left, right)
83
+ ->(node) { left.call(node) || right.call(node) }
84
+ end
85
+
86
+ def compile_node(root)
87
+ case root
88
+ in AryPtn[constant:, requireds:, rest: nil, posts: []]
89
+ compiled_constant = compile_node(constant) if constant
90
+
91
+ preprocessed = requireds.map { |required| compile_node(required) }
92
+
93
+ compiled_requireds = ->(node) do
94
+ deconstructed = node.deconstruct
95
+
96
+ deconstructed.length == preprocessed.length &&
97
+ preprocessed
98
+ .zip(deconstructed)
99
+ .all? { |(matcher, value)| matcher.call(value) }
100
+ end
101
+
102
+ if compiled_constant
103
+ combine_and(compiled_constant, compiled_requireds)
104
+ else
105
+ compiled_requireds
106
+ end
107
+ in Binary[left:, operator: :|, right:]
108
+ combine_or(compile_node(left), compile_node(right))
109
+ in Const[value:] if SyntaxTree.const_defined?(value)
110
+ clazz = SyntaxTree.const_get(value)
111
+
112
+ ->(node) { node.is_a?(clazz) }
113
+ in Const[value:] if Object.const_defined?(value)
114
+ clazz = Object.const_get(value)
115
+
116
+ ->(node) { node.is_a?(clazz) }
117
+ in ConstPathRef[
118
+ parent: VarRef[value: Const[value: "SyntaxTree"]], constant:
119
+ ]
120
+ compile_node(constant)
121
+ in DynaSymbol[parts: []]
122
+ symbol = :""
123
+
124
+ ->(node) { node == symbol }
125
+ in DynaSymbol[parts: [TStringContent[value:]]]
126
+ symbol = value.to_sym
127
+
128
+ ->(attribute) { attribute == value }
129
+ in HshPtn[constant:, keywords:, keyword_rest: nil]
130
+ compiled_constant = compile_node(constant)
131
+
132
+ preprocessed =
133
+ keywords.to_h do |keyword, value|
134
+ raise NoMatchingPatternError unless keyword.is_a?(Label)
135
+ [keyword.value.chomp(":").to_sym, compile_node(value)]
136
+ end
137
+
138
+ compiled_keywords = ->(node) do
139
+ deconstructed = node.deconstruct_keys(preprocessed.keys)
140
+
141
+ preprocessed.all? do |keyword, matcher|
142
+ matcher.call(deconstructed[keyword])
143
+ end
144
+ end
145
+
146
+ if compiled_constant
147
+ combine_and(compiled_constant, compiled_keywords)
148
+ else
149
+ compiled_keywords
150
+ end
151
+ in RegexpLiteral[parts: [TStringContent[value:]]]
152
+ regexp = /#{value}/
153
+
154
+ ->(attribute) { regexp.match?(attribute) }
155
+ in StringLiteral[parts: []]
156
+ ->(attribute) { attribute == "" }
157
+ in StringLiteral[parts: [TStringContent[value:]]]
158
+ ->(attribute) { attribute == value }
159
+ in SymbolLiteral[value:]
160
+ symbol = value.value.to_sym
161
+
162
+ ->(attribute) { attribute == symbol }
163
+ in VarRef[value: Const => value]
164
+ compile_node(value)
165
+ in VarRef[value: Kw[value: "nil"]]
166
+ ->(attribute) { attribute.nil? }
167
+ end
168
+ rescue NoMatchingPatternError
169
+ raise CompilationError, PP.pp(root, +"").chomp
170
+ end
171
+ end
172
+ end
@@ -4,14 +4,10 @@ module SyntaxTree
4
4
  # Provides an interface for searching for a pattern of nodes against a
5
5
  # subtree of an AST.
6
6
  class Search
7
- class UncompilableError < StandardError
8
- end
9
-
10
- attr_reader :matcher
7
+ attr_reader :pattern
11
8
 
12
- def initialize(query)
13
- root = SyntaxTree.parse("case nil\nin #{query}\nend")
14
- @matcher = compile(root.statements.body.first.consequent.pattern)
9
+ def initialize(pattern)
10
+ @pattern = pattern
15
11
  end
16
12
 
17
13
  def scan(root)
@@ -22,71 +18,9 @@ module SyntaxTree
22
18
  node = queue.shift
23
19
  next unless node
24
20
 
25
- yield node if matcher.call(node)
21
+ yield node if pattern.call(node)
26
22
  queue += node.child_nodes
27
23
  end
28
24
  end
29
-
30
- private
31
-
32
- def compile(pattern)
33
- case pattern
34
- in Binary[left:, operator: :|, right:]
35
- compiled_left = compile(left)
36
- compiled_right = compile(right)
37
-
38
- ->(node) { compiled_left.call(node) || compiled_right.call(node) }
39
- in Const[value:] if SyntaxTree.const_defined?(value)
40
- clazz = SyntaxTree.const_get(value)
41
-
42
- ->(node) { node.is_a?(clazz) }
43
- in Const[value:] if Object.const_defined?(value)
44
- clazz = Object.const_get(value)
45
-
46
- ->(node) { node.is_a?(clazz) }
47
- in ConstPathRef[parent: VarRef[value: Const[value: "SyntaxTree"]]]
48
- compile(pattern.constant)
49
- in HshPtn[constant:, keywords:, keyword_rest: nil]
50
- compiled_constant = compile(constant)
51
-
52
- preprocessed_keywords =
53
- keywords.to_h do |keyword, value|
54
- raise NoMatchingPatternError unless keyword.is_a?(Label)
55
- [keyword.value.chomp(":").to_sym, compile(value)]
56
- end
57
-
58
- compiled_keywords = ->(node) do
59
- deconstructed = node.deconstruct_keys(preprocessed_keywords.keys)
60
- preprocessed_keywords.all? do |keyword, matcher|
61
- matcher.call(deconstructed[keyword])
62
- end
63
- end
64
-
65
- ->(node) do
66
- compiled_constant.call(node) && compiled_keywords.call(node)
67
- end
68
- in RegexpLiteral[parts: [TStringContent[value:]]]
69
- regexp = /#{value}/
70
-
71
- ->(attribute) { regexp.match?(attribute) }
72
- in StringLiteral[parts: [TStringContent[value:]]]
73
- ->(attribute) { attribute == value }
74
- in VarRef[value: Const => value]
75
- compile(value)
76
- end
77
- rescue NoMatchingPatternError
78
- raise UncompilableError, <<~ERROR
79
- Syntax Tree was unable to compile the pattern you provided to search
80
- into a usable expression. It failed on the node within the pattern
81
- matching expression represented by:
82
-
83
- #{PP.pp(pattern, +"").chomp}
84
-
85
- Note that not all syntax supported by Ruby's pattern matching syntax is
86
- also supported by Syntax Tree's code search. If you're using some syntax
87
- that you believe should be supported, please open an issue on the GitHub
88
- repository at https://github.com/ruby-syntax-tree/syntax_tree.
89
- ERROR
90
- end
91
25
  end
92
26
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SyntaxTree
4
- VERSION = "4.1.0"
4
+ VERSION = "4.2.0"
5
5
  end
data/lib/syntax_tree.rb CHANGED
@@ -21,6 +21,7 @@ require_relative "syntax_tree/visitor/environment"
21
21
  require_relative "syntax_tree/visitor/with_environment"
22
22
 
23
23
  require_relative "syntax_tree/parser"
24
+ require_relative "syntax_tree/pattern"
24
25
  require_relative "syntax_tree/search"
25
26
 
26
27
  # Syntax Tree is a suite of tools built on top of the internal CRuby parser. It
@@ -74,4 +75,10 @@ module SyntaxTree
74
75
 
75
76
  File.read(filepath, encoding: encoding)
76
77
  end
78
+
79
+ # Searches through the given source using the given pattern and yields each
80
+ # node in the tree that matches the pattern to the given block.
81
+ def self.search(source, query, &block)
82
+ Search.new(Pattern.new(query).compile).scan(parse(source), &block)
83
+ end
77
84
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: syntax_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.0
4
+ version: 4.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Newton
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-24 00:00:00.000000000 Z
11
+ date: 2022-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prettier_print
@@ -129,6 +129,7 @@ files:
129
129
  - lib/syntax_tree/language_server/inlay_hints.rb
130
130
  - lib/syntax_tree/node.rb
131
131
  - lib/syntax_tree/parser.rb
132
+ - lib/syntax_tree/pattern.rb
132
133
  - lib/syntax_tree/plugin/single_quotes.rb
133
134
  - lib/syntax_tree/plugin/trailing_comma.rb
134
135
  - lib/syntax_tree/rake/check_task.rb