Skip to content

Commit 3d34404

Browse files
committed
Move DSL into its own file
1 parent d510145 commit 3d34404

File tree

7 files changed

+50
-16
lines changed

7 files changed

+50
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ a.out
2929
/java/org/yarp/Loader.java
3030
/java/org/yarp/Nodes.java
3131
/lib/yarp/dispatcher.rb
32+
/lib/yarp/dsl.rb
3233
/lib/yarp/mutation_visitor.rb
3334
/lib/yarp/node.rb
3435
/lib/yarp/serialize.rb

docs/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ A lot of code in YARP's repository is templated from a single configuration file
88
* `java/org/yarp/Loader.java` - for defining how to deserialize the nodes in Java
99
* `java/org/yarp/Nodes.java` - for defining the nodes in Java
1010
* `lib/yarp/dispatcher.rb` - for defining the dispatch visitors for the nodes in Ruby
11+
* `lib/yarp/dsl.rb` - for defining the DSL for the nodes in Ruby
1112
* `lib/yarp/mutation_visitor.rb` - for defining the mutation visitor for the nodes in Ruby
1213
* `lib/yarp/node.rb` - for defining the nodes in Ruby
1314
* `lib/yarp/serialize.rb` - for defining how to deserialize the nodes in Ruby

lib/yarp.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@ def self.parse_serialize_file(filepath)
541541
# of these features.
542542
autoload :DesugarVisitor, "yarp/desugar_visitor"
543543
autoload :Dispatcher, "yarp/dispatcher"
544+
autoload :DSL, "yarp/dsl"
544545
autoload :MutationVisitor, "yarp/mutation_visitor"
545546
autoload :RipperCompat, "yarp/ripper_compat"
546547
autoload :Pack, "yarp/pack"

templates/lib/yarp/dsl.rb.erb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module YARP
2+
# The DSL module provides a set of methods that can be used to create YARP
3+
# nodes in a more concise manner. For example, instead of writing:
4+
#
5+
# source = YARP::Source.new("[1]")
6+
#
7+
# YARP::ArrayNode.new(
8+
# [
9+
# YARP::IntegerNode.new(
10+
# YARP::IntegerBaseFlags::DECIMAL,
11+
# YARP::Location.new(source, 1, 1),
12+
# )
13+
# ],
14+
# YARP::Location.new(source, 0, 1),
15+
# YARP::Location.new(source, 2, 1)
16+
# )
17+
#
18+
# you could instead write:
19+
#
20+
# source = YARP::Source.new("[1]")
21+
#
22+
# ArrayNode(
23+
# IntegerNode(YARP::IntegerBaseFlags::DECIMAL, Location(source, 1, 1))),
24+
# Location(source, 0, 1),
25+
# Location(source, 2, 1)
26+
# )
27+
#
28+
# This is mostly helpful in the context of writing tests, but can also be used
29+
# to generate trees programmatically.
30+
module DSL
31+
private
32+
33+
# Create a new Location object
34+
def Location(source = nil, start_offset = 0, length = 0)
35+
Location.new(source, start_offset, length)
36+
end
37+
<%- nodes.each do |node| -%>
38+
39+
# Create a new <%= node.name %> node
40+
def <%= node.name %>(<%= (node.fields.map(&:name) + ["location = Location()"]).join(", ") %>)
41+
<%= node.name %>.new(<%= (node.fields.map(&:name) + ["location"]).join(", ") %>)
42+
end
43+
<%- end -%>
44+
end
45+
end

templates/lib/yarp/node.rb.erb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -181,20 +181,4 @@ module YARP
181181
<%= "\n" if node != nodes.last -%>
182182
<%- end -%>
183183
end
184-
185-
module DSL
186-
private
187-
188-
# Create a new Location object
189-
def Location(source = nil, start_offset = 0, length = 0)
190-
Location.new(source, start_offset, length)
191-
end
192-
<%- nodes.each do |node| -%>
193-
194-
# Create a new <%= node.name %> node
195-
def <%= node.name %>(<%= (node.fields.map(&:name) + ["location = Location()"]).join(", ") %>)
196-
<%= node.name %>.new(<%= (node.fields.map(&:name) + ["location"]).join(", ") %>)
197-
end
198-
<%- end -%>
199-
end
200184
end

templates/template.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ def locals
367367
"java/org/yarp/Nodes.java",
368368
"java/org/yarp/AbstractNodeVisitor.java",
369369
"lib/yarp/dispatcher.rb",
370+
"lib/yarp/dsl.rb",
370371
"lib/yarp/mutation_visitor.rb",
371372
"lib/yarp/node.rb",
372373
"lib/yarp/serialize.rb",

yarp.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Gem::Specification.new do |spec|
6161
"lib/yarp.rb",
6262
"lib/yarp/desugar_visitor.rb",
6363
"lib/yarp/dispatcher.rb",
64+
"lib/yarp/dsl.rb",
6465
"lib/yarp/ffi.rb",
6566
"lib/yarp/lex_compat.rb",
6667
"lib/yarp/mutation_visitor.rb",

0 commit comments

Comments
 (0)