diff options
author | Kevin Newton <[email protected]> | 2023-09-22 10:57:00 -0400 |
---|---|---|
committer | Kevin Newton <[email protected]> | 2023-09-27 12:10:23 -0400 |
commit | 2a4a55f896c44896e10ad1e905c9e8a6a0f4ae92 (patch) | |
tree | bea6e38253cc8b46c9cc5064bfa8a00949c7e11c /yarp | |
parent | 1c049c64c06552198678b29f9e235830e3d17e5a (diff) |
[ruby/yarp] Move DSL into its own file
https://github.com/ruby/yarp/commit/3d34404d80
Diffstat (limited to 'yarp')
-rw-r--r-- | yarp/templates/lib/yarp/dsl.rb.erb | 45 | ||||
-rw-r--r-- | yarp/templates/lib/yarp/node.rb.erb | 16 | ||||
-rwxr-xr-x | yarp/templates/template.rb | 1 |
3 files changed, 46 insertions, 16 deletions
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", |