|
13 | 13 | require "syntax_tree" |
14 | 14 | require "syntax_tree/cli" |
15 | 15 |
|
16 | | -# Here we are going to establish type verification whenever a new node is |
17 | | -# created. We do this through the reflection module, which in turn parses the |
18 | | -# source code of the node classes. |
19 | | -require "syntax_tree/reflection" |
20 | | -SyntaxTree::Reflection.nodes.each do |name, node| |
21 | | - next if name == :Statements |
22 | | - |
23 | | - clazz = SyntaxTree.const_get(name) |
24 | | - parameters = clazz.instance_method(:initialize).parameters |
25 | | - |
26 | | - # First, verify that all of the parameters listed in the list of attributes. |
27 | | - # If there are any parameters that aren't listed in the attributes, then |
28 | | - # something went wrong with the parsing in the reflection module. |
29 | | - raise unless (parameters.map(&:last) - node.attributes.keys).empty? |
30 | | - |
31 | | - # Now we're going to use an alias chain to redefine the initialize method to |
32 | | - # include type checking. |
33 | | - clazz.alias_method(:initialize_without_verify, :initialize) |
34 | | - clazz.define_method(:initialize) do |**kwargs| |
35 | | - kwargs.each do |kwarg, value| |
36 | | - attribute = node.attributes.fetch(kwarg) |
37 | | - |
38 | | - unless attribute.type === value |
39 | | - raise TypeError, |
40 | | - "invalid type for #{name}##{kwarg}, expected " \ |
41 | | - "#{attribute.type.inspect}, got #{value.inspect}" |
| 16 | +unless RUBY_ENGINE == "truffleruby" |
| 17 | + # Here we are going to establish type verification whenever a new node is |
| 18 | + # created. We do this through the reflection module, which in turn parses the |
| 19 | + # source code of the node classes. |
| 20 | + require "syntax_tree/reflection" |
| 21 | + SyntaxTree::Reflection.nodes.each do |name, node| |
| 22 | + next if name == :Statements |
| 23 | + |
| 24 | + clazz = SyntaxTree.const_get(name) |
| 25 | + parameters = clazz.instance_method(:initialize).parameters |
| 26 | + |
| 27 | + # First, verify that all of the parameters listed in the list of attributes. |
| 28 | + # If there are any parameters that aren't listed in the attributes, then |
| 29 | + # something went wrong with the parsing in the reflection module. |
| 30 | + raise unless (parameters.map(&:last) - node.attributes.keys).empty? |
| 31 | + |
| 32 | + # Now we're going to use an alias chain to redefine the initialize method to |
| 33 | + # include type checking. |
| 34 | + clazz.alias_method(:initialize_without_verify, :initialize) |
| 35 | + clazz.define_method(:initialize) do |**kwargs| |
| 36 | + kwargs.each do |kwarg, value| |
| 37 | + attribute = node.attributes.fetch(kwarg) |
| 38 | + |
| 39 | + unless attribute.type === value |
| 40 | + raise TypeError, |
| 41 | + "invalid type for #{name}##{kwarg}, expected " \ |
| 42 | + "#{attribute.type.inspect}, got #{value.inspect}" |
| 43 | + end |
42 | 44 | end |
43 | | - end |
44 | 45 |
|
45 | | - initialize_without_verify(**kwargs) |
| 46 | + initialize_without_verify(**kwargs) |
| 47 | + end |
46 | 48 | end |
47 | 49 | end |
48 | 50 |
|
|
0 commit comments