summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Newton <[email protected]>2023-09-22 10:57:00 -0400
committerKevin Newton <[email protected]>2023-09-27 12:10:23 -0400
commit2a4a55f896c44896e10ad1e905c9e8a6a0f4ae92 (patch)
treebea6e38253cc8b46c9cc5064bfa8a00949c7e11c
parent1c049c64c06552198678b29f9e235830e3d17e5a (diff)
[ruby/yarp] Move DSL into its own file
https://github.com/ruby/yarp/commit/3d34404d80
-rw-r--r--lib/yarp.rb1
-rw-r--r--lib/yarp/yarp.gemspec1
-rw-r--r--yarp/templates/lib/yarp/dsl.rb.erb45
-rw-r--r--yarp/templates/lib/yarp/node.rb.erb16
-rwxr-xr-xyarp/templates/template.rb1
5 files changed, 48 insertions, 16 deletions
diff --git a/lib/yarp.rb b/lib/yarp.rb
index 8c73c46b28..9b9df081d8 100644
--- a/lib/yarp.rb
+++ b/lib/yarp.rb
@@ -541,6 +541,7 @@ module YARP
# of these features.
autoload :DesugarVisitor, "yarp/desugar_visitor"
autoload :Dispatcher, "yarp/dispatcher"
+ autoload :DSL, "yarp/dsl"
autoload :MutationVisitor, "yarp/mutation_visitor"
autoload :RipperCompat, "yarp/ripper_compat"
autoload :Pack, "yarp/pack"
diff --git a/lib/yarp/yarp.gemspec b/lib/yarp/yarp.gemspec
index 8696204ccf..16e8196140 100644
--- a/lib/yarp/yarp.gemspec
+++ b/lib/yarp/yarp.gemspec
@@ -61,6 +61,7 @@ Gem::Specification.new do |spec|
"lib/yarp.rb",
"lib/yarp/desugar_visitor.rb",
"lib/yarp/dispatcher.rb",
+ "lib/yarp/dsl.rb",
"lib/yarp/ffi.rb",
"lib/yarp/lex_compat.rb",
"lib/yarp/mutation_visitor.rb",
diff --git a/yarp/templates/lib/yarp/dsl.rb.erb b/yarp/templates/lib/yarp/dsl.rb.erb
new file mode 100644
index 0000000000..cc96d15e6c
--- /dev/null
+++ b/yarp/templates/lib/yarp/dsl.rb.erb
@@ -0,0 +1,45 @@
+module YARP
+ # The DSL module provides a set of methods that can be used to create YARP
+ # nodes in a more concise manner. For example, instead of writing:
+ #
+ # source = YARP::Source.new("[1]")
+ #
+ # YARP::ArrayNode.new(
+ # [
+ # YARP::IntegerNode.new(
+ # YARP::IntegerBaseFlags::DECIMAL,
+ # YARP::Location.new(source, 1, 1),
+ # )
+ # ],
+ # YARP::Location.new(source, 0, 1),
+ # YARP::Location.new(source, 2, 1)
+ # )
+ #
+ # you could instead write:
+ #
+ # source = YARP::Source.new("[1]")
+ #
+ # ArrayNode(
+ # IntegerNode(YARP::IntegerBaseFlags::DECIMAL, Location(source, 1, 1))),
+ # Location(source, 0, 1),
+ # Location(source, 2, 1)
+ # )
+ #
+ # This is mostly helpful in the context of writing tests, but can also be used
+ # to generate trees programmatically.
+ module DSL
+ private
+
+ # Create a new Location object
+ def Location(source = nil, start_offset = 0, length = 0)
+ Location.new(source, start_offset, length)
+ end
+ <%- nodes.each do |node| -%>
+
+ # Create a new <%= node.name %> node
+ def <%= node.name %>(<%= (node.fields.map(&:name) + ["location = Location()"]).join(", ") %>)
+ <%= node.name %>.new(<%= (node.fields.map(&:name) + ["location"]).join(", ") %>)
+ end
+ <%- end -%>
+ end
+end
diff --git a/yarp/templates/lib/yarp/node.rb.erb b/yarp/templates/lib/yarp/node.rb.erb
index 91de6f46f4..7d52d823da 100644
--- a/yarp/templates/lib/yarp/node.rb.erb
+++ b/yarp/templates/lib/yarp/node.rb.erb
@@ -181,20 +181,4 @@ module YARP
<%= "\n" if node != nodes.last -%>
<%- end -%>
end
-
- module DSL
- private
-
- # Create a new Location object
- def Location(source = nil, start_offset = 0, length = 0)
- Location.new(source, start_offset, length)
- end
- <%- nodes.each do |node| -%>
-
- # Create a new <%= node.name %> node
- def <%= node.name %>(<%= (node.fields.map(&:name) + ["location = Location()"]).join(", ") %>)
- <%= node.name %>.new(<%= (node.fields.map(&:name) + ["location"]).join(", ") %>)
- end
- <%- end -%>
- end
end
diff --git a/yarp/templates/template.rb b/yarp/templates/template.rb
index 17cb52a2ff..ea5946761e 100755
--- a/yarp/templates/template.rb
+++ b/yarp/templates/template.rb
@@ -367,6 +367,7 @@ module YARP
"java/org/yarp/Nodes.java",
"java/org/yarp/AbstractNodeVisitor.java",
"lib/yarp/dispatcher.rb",
+ "lib/yarp/dsl.rb",
"lib/yarp/mutation_visitor.rb",
"lib/yarp/node.rb",
"lib/yarp/serialize.rb",