Skip to content

Commit e6cea4f

Browse files
committed
Fixes so bundle exec rake can run on JRuby and TruffleRuby
1 parent 8bab199 commit e6cea4f

File tree

10 files changed

+106
-54
lines changed

10 files changed

+106
-54
lines changed

Rakefile

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,14 @@ task build: [:templates, :check_manifest]
3737
# the C extension
3838
task "compile:yarp" => ["templates"] # must be before the ExtensionTask is created
3939

40-
if RUBY_ENGINE == 'jruby'
40+
if RUBY_ENGINE == 'ruby' and ENV["YARP_FIDDLE_BACKEND"] != "true"
41+
Rake::ExtensionTask.new(:compile) do |ext|
42+
ext.name = "yarp"
43+
ext.ext_dir = "ext/yarp"
44+
ext.lib_dir = "lib/yarp"
45+
ext.gem_spec = Gem::Specification.load("yarp.gemspec")
46+
end
47+
elsif RUBY_ENGINE == 'jruby'
4148
require 'rake/javaextensiontask'
4249

4350
# This compiles java to make sure any templating changes produces valid code.
@@ -49,13 +56,6 @@ if RUBY_ENGINE == 'jruby'
4956
ext.target_version = "1.8"
5057
ext.gem_spec = Gem::Specification.load("yarp.gemspec")
5158
end
52-
else
53-
Rake::ExtensionTask.new(:compile) do |ext|
54-
ext.name = "yarp"
55-
ext.ext_dir = "ext/yarp"
56-
ext.lib_dir = "lib/yarp"
57-
ext.gem_spec = Gem::Specification.load("yarp.gemspec")
58-
end
5959
end
6060

6161
# So `rake clobber` will delete generated files

lib/yarp/ripper_compat.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ def _dispatch_event_push(list, item)
4747
Ripper::PARSER_EVENT_TABLE.each do |event, arity|
4848
case event
4949
when /_new\z/
50-
alias :"on_#{event}" :_dispatch_event_new if arity == 0
50+
alias_method :"on_#{event}", :_dispatch_event_new if arity == 0
5151
when /_add\z/
52-
alias :"on_#{event}" :_dispatch_event_push
52+
alias_method :"on_#{event}", :_dispatch_event_push
5353
end
5454
end
5555
end
@@ -168,7 +168,7 @@ def _dispatch5(_, _, _, _, _); end
168168
def _dispatch7(_, _, _, _, _, _, _); end
169169

170170
(Ripper::SCANNER_EVENT_TABLE.merge(Ripper::PARSER_EVENT_TABLE)).each do |event, arity|
171-
alias :"on_#{event}" :"_dispatch#{arity}"
171+
alias_method :"on_#{event}", :"_dispatch#{arity}"
172172
end
173173
end
174174
end

templates/include/yarp/ast.h.erb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,15 @@ typedef struct yp_<%= node.human %> {
8484
yp_node_t base;
8585
<%- node.params.grep_v(FlagsParam).each do |param| -%>
8686
<%= case param
87-
in NodeParam | OptionalNodeParam then "struct #{param.c_type} *#{param.name}"
88-
in NodeListParam then "struct yp_node_list #{param.name}"
89-
in LocationListParam then "yp_location_list_t #{param.name}"
90-
in ConstantParam then "yp_constant_id_t #{param.name}"
91-
in ConstantListParam then "yp_constant_id_list_t #{param.name}"
92-
in StringParam then "yp_string_t #{param.name}"
93-
in LocationParam | OptionalLocationParam then "yp_location_t #{param.name}"
94-
in UInt32Param then "uint32_t #{param.name}"
87+
when NodeParam, OptionalNodeParam then "struct #{param.c_type} *#{param.name}"
88+
when NodeListParam then "struct yp_node_list #{param.name}"
89+
when LocationListParam then "yp_location_list_t #{param.name}"
90+
when ConstantParam then "yp_constant_id_t #{param.name}"
91+
when ConstantListParam then "yp_constant_id_list_t #{param.name}"
92+
when StringParam then "yp_string_t #{param.name}"
93+
when LocationParam, OptionalLocationParam then "yp_location_t #{param.name}"
94+
when UInt32Param then "uint32_t #{param.name}"
95+
else raise param.class.name
9596
end
9697
%>;
9798
<%- end -%>

templates/java/org/yarp/Nodes.java.erb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,14 @@ public abstract class Nodes {
231231
public void setNewLineFlag(Source source, boolean[] newlineMarked) {
232232
<%- param = node.params.find { |p| p.name == node.newline } or raise node.newline -%>
233233
<%- case param -%>
234-
<%- in SingleNodeParam -%>
234+
<%- when SingleNodeParam -%>
235235
this.<%= param.name %>.setNewLineFlag(source, newlineMarked);
236-
<%- in NodeListParam -%>
236+
<%- when NodeListParam -%>
237237
Node first = this.<%= param.name %>.length > 0 ? this.<%= param.name %>[0] : null;
238238
if (first != null) {
239239
first.setNewLineFlag(source, newlineMarked);
240240
}
241+
<%- else raise param.class.name -%>
241242
<%- end -%>
242243
}
243244
<%- end -%>

templates/lib/yarp/node.rb.erb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ module YARP
2929
def set_newline_flag(newline_marked)
3030
<%- param = node.params.find { |p| p.name == node.newline } or raise node.newline -%>
3131
<%- case param -%>
32-
<%- in SingleNodeParam -%>
32+
<%- when SingleNodeParam -%>
3333
<%= param.name %>.set_newline_flag(newline_marked)
34-
<%- in NodeListParam -%>
34+
<%- when NodeListParam -%>
3535
first = <%= param.name %>.first
3636
first.set_newline_flag(newline_marked) if first
37+
<%- else raise param.class.name -%>
3738
<%- end -%>
3839
end
3940
<%- end -%>

templates/template.rb

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ def c_type
2323
end
2424
end
2525

26-
def java_type = options[:kind] || "Node"
26+
def java_type
27+
options[:kind] || "Node"
28+
end
2729

2830
def java_cast
2931
if options[:kind]
@@ -39,77 +41,119 @@ def java_cast
3941
class NodeParam < Param
4042
include KindTypes
4143

42-
def rbs_class = "Node"
44+
def rbs_class
45+
"Node"
46+
end
4347
end
4448

4549
# This represents a parameter to a node that is itself a node and can be
4650
# optionally null. We pass them as references and store them as references.
4751
class OptionalNodeParam < Param
4852
include KindTypes
4953

50-
def rbs_class = "Node?"
54+
def rbs_class
55+
"Node?"
56+
end
5157
end
5258

5359
SingleNodeParam = -> (node) { NodeParam === node or OptionalNodeParam === node }
5460

5561
# This represents a parameter to a node that is a list of nodes. We pass them as
5662
# references and store them as references.
5763
class NodeListParam < Param
58-
def rbs_class = "Array[Node]"
59-
def java_type = "Node[]"
64+
def rbs_class
65+
"Array[Node]"
66+
end
67+
def java_type
68+
"Node[]"
69+
end
6070
end
6171

6272
# This represents a parameter to a node that is a list of locations.
6373
class LocationListParam < Param
64-
def rbs_class = "Array[Location]"
65-
def java_type = "Location[]"
74+
def rbs_class
75+
"Array[Location]"
76+
end
77+
def java_type
78+
"Location[]"
79+
end
6680
end
6781

6882
# This represents a parameter to a node that is the ID of a string interned
6983
# through the parser's constant pool.
7084
class ConstantParam < Param
71-
def rbs_class = "Symbol"
72-
def java_type = "byte[]"
85+
def rbs_class
86+
"Symbol"
87+
end
88+
def java_type
89+
"byte[]"
90+
end
7391
end
7492

7593
# This represents a parameter to a node that is a list of IDs that are
7694
# associated with strings interned through the parser's constant pool.
7795
class ConstantListParam < Param
78-
def rbs_class = "Array[Symbol]"
79-
def java_type = "byte[][]"
96+
def rbs_class
97+
"Array[Symbol]"
98+
end
99+
def java_type
100+
"byte[][]"
101+
end
80102
end
81103

82104
# This represents a parameter to a node that is a string.
83105
class StringParam < Param
84-
def rbs_class = "String"
85-
def java_type = "byte[]"
106+
def rbs_class
107+
"String"
108+
end
109+
def java_type
110+
"byte[]"
111+
end
86112
end
87113

88114
# This represents a parameter to a node that is a location.
89115
class LocationParam < Param
90-
def rbs_class = "Location"
91-
def java_type = "Location"
116+
def rbs_class
117+
"Location"
118+
end
119+
def java_type
120+
"Location"
121+
end
92122
end
93123

94124
# This represents a parameter to a node that is a location that is optional.
95125
class OptionalLocationParam < Param
96-
def rbs_class = "Location?"
97-
def java_type = "Location"
126+
def rbs_class
127+
"Location?"
128+
end
129+
def java_type
130+
"Location"
131+
end
98132
end
99133

100134
# This represents an integer parameter.
101135
class UInt32Param < Param
102-
def rbs_class = "Integer"
103-
def java_type = "int"
136+
def rbs_class
137+
"Integer"
138+
end
139+
def java_type
140+
"int"
141+
end
104142
end
105143

106144
# This represents a set of flags. It is very similar to the UInt32Param, but can
107145
# be directly embedded into the flags field on the struct and provides
108146
# convenient methods for checking if a flag is set.
109147
class FlagsParam < Param
110-
def rbs_class = "Integer"
111-
def java_type = "short"
112-
def kind = options.fetch(:kind)
148+
def rbs_class
149+
"Integer"
150+
end
151+
def java_type
152+
"short"
153+
end
154+
def kind
155+
options.fetch(:kind)
156+
end
113157
end
114158

115159
PARAM_TYPES = {

test/comments_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def test_comment_embedded_document_with_content_on_same_line
5252
def assert_comment(source, type, location)
5353
result = YARP.parse(source)
5454
assert result.errors.empty?, result.errors.map(&:message).join("\n")
55-
result => YARP::ParseResult[comments: [YARP::Comment[type: type]]]
55+
assert_equal result.comments.first.type, type
5656
assert_equal result.comments.first.location.start_offset, location.begin
5757
assert_equal result.comments.first.location.end_offset, location.end
5858
end

test/errors_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,10 +1080,11 @@ def test_duplicated_parameter_names
10801080
private
10811081

10821082
def assert_errors(expected, source, errors)
1083-
assert_nil Ripper.sexp_raw(source)
1083+
# Ripper behaves differently on JRuby/TruffleRuby, so only check this on CRuby
1084+
assert_nil Ripper.sexp_raw(source) if RUBY_ENGINE == "ruby"
10841085

10851086
result = YARP.parse(source)
1086-
result => YARP::ParseResult[value: YARP::ProgramNode[statements: YARP::StatementsNode[body: [*, node]]]]
1087+
node = result.value.statements.body.last
10871088

10881089
assert_equal_nodes(expected, node, compare_location: false)
10891090
assert_equal(errors, result.errors.map { |e| [e.message, e.location.start_offset..e.location.end_offset] })
@@ -1096,7 +1097,6 @@ def assert_error_messages(source, errors)
10961097
end
10971098

10981099
def expression(source)
1099-
YARP.parse(source) => YARP::ParseResult[value: YARP::ProgramNode[statements: YARP::StatementsNode[body: [*, node]]]]
1100-
node
1100+
YARP.parse(source).value.statements.body.last
11011101
end
11021102
end

test/location_test.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -753,9 +753,11 @@ def test_YieldNode
753753
private
754754

755755
def assert_location(kind, source, expected = 0...source.length)
756-
YARP.parse(source) => ParseResult[comments: [], errors: [], value: node]
756+
result = YARP.parse(source)
757+
assert_equal [], result.comments
758+
assert_equal [], result.errors
757759

758-
node => ProgramNode[statements: [*, node]]
760+
node = result.value.statements.body.last
759761
node = yield node if block_given?
760762

761763
assert_kind_of kind, node

test/parse_test.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ def teardown
3030
end
3131

3232
def test_empty_string
33-
YARP.parse("") => YARP::ParseResult[value: YARP::ProgramNode[statements: YARP::StatementsNode[body: []]]]
33+
result = YARP.parse("")
34+
assert_equal [], result.value.statements.body
3435
end
3536

3637
known_failures = %w[seattlerb/heredoc_nested.txt]
@@ -98,7 +99,9 @@ def test_parse_takes_file_path
9899

99100
# Finally, assert that we can lex the source and get the same tokens as
100101
# Ripper.
101-
YARP.lex_compat(source) => { errors: [], value: tokens }
102+
lex_result = YARP.lex_compat(source)
103+
assert_equal [], lex_result.errors
104+
tokens = lex_result.value
102105

103106
begin
104107
YARP.lex_ripper(source).zip(tokens).each do |(ripper, yarp)|

0 commit comments

Comments
 (0)