summaryrefslogtreecommitdiff
path: root/yarp
diff options
context:
space:
mode:
authorKevin Newton <kddnewton@gmail.com>2023-09-06 10:46:20 -0400
committergit <svn-admin@ruby-lang.org>2023-09-06 18:18:10 +0000
commit746eede412f083dc93923e39e3299c69fce46c15 (patch)
tree6a4f3e43dc50425f39133b9242fbf18a8e6575cd /yarp
parent08929b344d89fb12fb3a12eccf686458dc820e6d (diff)
[ruby/yarp] Constant on block parameter node
https://github.com/ruby/yarp/commit/2cd9a67150
Diffstat (limited to 'yarp')
-rw-r--r--yarp/config.yml2
-rw-r--r--yarp/templates/ext/yarp/api_node.c.erb2
-rw-r--r--yarp/templates/include/yarp/ast.h.erb2
-rw-r--r--yarp/templates/lib/yarp/node.rb.erb2
-rw-r--r--yarp/templates/lib/yarp/serialize.rb.erb17
-rw-r--r--yarp/templates/src/node.c.erb6
-rw-r--r--yarp/templates/src/prettyprint.c.erb8
-rw-r--r--yarp/templates/src/serialize.c.erb2
-rwxr-xr-xyarp/templates/template.rb16
-rw-r--r--yarp/yarp.c8
10 files changed, 54 insertions, 11 deletions
diff --git a/yarp/config.yml b/yarp/config.yml
index 164e96101f..5ccdabab16 100644
--- a/yarp/config.yml
+++ b/yarp/config.yml
@@ -549,6 +549,8 @@ nodes:
^^^^^^^^^^^^^^
- name: BlockParameterNode
fields:
+ - name: name
+ type: constant?
- name: name_loc
type: location?
- name: operator_loc
diff --git a/yarp/templates/ext/yarp/api_node.c.erb b/yarp/templates/ext/yarp/api_node.c.erb
index c3a232ec5f..8fb2d2e507 100644
--- a/yarp/templates/ext/yarp/api_node.c.erb
+++ b/yarp/templates/ext/yarp/api_node.c.erb
@@ -154,6 +154,8 @@ yp_ast_new(yp_parser_t *parser, yp_node_t *node, rb_encoding *encoding) {
argv[<%= index %>] = yp_string_new(&cast-><%= field.name %>, encoding);
<%- when YARP::ConstantField -%>
argv[<%= index %>] = rb_id2sym(constants[cast-><%= field.name %> - 1]);
+ <%- when YARP::OptionalConstantField -%>
+ argv[<%= index %>] = cast-><%= field.name %> == 0 ? Qnil : rb_id2sym(constants[cast-><%= field.name %> - 1]);
<%- when YARP::ConstantListField -%>
argv[<%= index %>] = rb_ary_new_capa(cast-><%= field.name %>.size);
for (size_t index = 0; index < cast-><%= field.name %>.size; index++) {
diff --git a/yarp/templates/include/yarp/ast.h.erb b/yarp/templates/include/yarp/ast.h.erb
index 82fb758440..09841408eb 100644
--- a/yarp/templates/include/yarp/ast.h.erb
+++ b/yarp/templates/include/yarp/ast.h.erb
@@ -95,7 +95,7 @@ typedef struct yp_<%= node.human %> {
<%= case field
when YARP::NodeField, YARP::OptionalNodeField then "struct #{field.c_type} *#{field.name}"
when YARP::NodeListField then "struct yp_node_list #{field.name}"
- when YARP::ConstantField then "yp_constant_id_t #{field.name}"
+ when YARP::ConstantField, YARP::OptionalConstantField then "yp_constant_id_t #{field.name}"
when YARP::ConstantListField then "yp_constant_id_list_t #{field.name}"
when YARP::StringField then "yp_string_t #{field.name}"
when YARP::LocationField, YARP::OptionalLocationField then "yp_location_t #{field.name}"
diff --git a/yarp/templates/lib/yarp/node.rb.erb b/yarp/templates/lib/yarp/node.rb.erb
index 105830836f..0d40406360 100644
--- a/yarp/templates/lib/yarp/node.rb.erb
+++ b/yarp/templates/lib/yarp/node.rb.erb
@@ -112,7 +112,7 @@ module YARP
inspector << "<%= pointer %><%= field.name %>:\n"
inspector << <%= field.name %>.inspect(inspector.child_inspector("<%= preadd %>")).delete_prefix(inspector.prefix)
end
- <%- when YARP::ConstantField, YARP::StringField, YARP::UInt32Field -%>
+ <%- when YARP::ConstantField, YARP::OptionalConstantField, YARP::StringField, YARP::UInt32Field -%>
inspector << "<%= pointer %><%= field.name %>: #{<%= field.name %>.inspect}\n"
<%- when YARP::FlagsField -%>
<%- flag = flags.find { |flag| flag.name == field.kind }.tap { |flag| raise unless flag } -%>
diff --git a/yarp/templates/lib/yarp/serialize.rb.erb b/yarp/templates/lib/yarp/serialize.rb.erb
index ee0b8666bf..2f6d6421a9 100644
--- a/yarp/templates/lib/yarp/serialize.rb.erb
+++ b/yarp/templates/lib/yarp/serialize.rb.erb
@@ -152,8 +152,7 @@ module YARP
load_location if io.getbyte != 0
end
- def load_constant
- index = load_varint - 1
+ def load_constant(index)
constant = constant_pool[index]
unless constant
@@ -169,6 +168,15 @@ module YARP
constant
end
+ def load_required_constant
+ load_constant(load_varint - 1)
+ end
+
+ def load_optional_constant
+ index = load_varint
+ load_constant(index - 1) if index != 0
+ end
+
def load_node
type = io.getbyte
location = load_location
@@ -185,8 +193,9 @@ module YARP
when YARP::OptionalNodeField then "load_optional_node"
when YARP::StringField then "load_string"
when YARP::NodeListField then "Array.new(load_varint) { load_node }"
- when YARP::ConstantField then "load_constant"
- when YARP::ConstantListField then "Array.new(load_varint) { load_constant }"
+ when YARP::ConstantField then "load_required_constant"
+ when YARP::OptionalConstantField then "load_optional_constant"
+ when YARP::ConstantListField then "Array.new(load_varint) { load_required_constant }"
when YARP::LocationField then "load_location"
when YARP::OptionalLocationField then "load_optional_location"
when YARP::UInt32Field, YARP::FlagsField then "load_varint"
diff --git a/yarp/templates/src/node.c.erb b/yarp/templates/src/node.c.erb
index 732749394a..9f23123914 100644
--- a/yarp/templates/src/node.c.erb
+++ b/yarp/templates/src/node.c.erb
@@ -55,12 +55,12 @@ yp_node_destroy(yp_parser_t *parser, yp_node_t *node) {
<%- nodes.each do |node| -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
case <%= node.type %>: {
- <%- if node.fields.any? { |field| ![YARP::LocationField, YARP::OptionalLocationField, YARP::UInt32Field, YARP::FlagsField, YARP::ConstantField].include?(field.class) } -%>
+ <%- if node.fields.any? { |field| ![YARP::LocationField, YARP::OptionalLocationField, YARP::UInt32Field, YARP::FlagsField, YARP::ConstantField, YARP::OptionalConstantField].include?(field.class) } -%>
yp_<%= node.human %>_t *cast = (yp_<%= node.human %>_t *) node;
<%- end -%>
<%- node.fields.each do |field| -%>
<%- case field -%>
- <%- when YARP::LocationField, YARP::OptionalLocationField, YARP::UInt32Field, YARP::FlagsField, YARP::ConstantField -%>
+ <%- when YARP::LocationField, YARP::OptionalLocationField, YARP::UInt32Field, YARP::FlagsField, YARP::ConstantField, YARP::OptionalConstantField -%>
<%- when YARP::NodeField -%>
yp_node_destroy(parser, (yp_node_t *)cast-><%= field.name %>);
<%- when YARP::OptionalNodeField -%>
@@ -104,7 +104,7 @@ yp_node_memsize_node(yp_node_t *node, yp_memsize_t *memsize) {
memsize->memsize += sizeof(*cast);
<%- node.fields.each do |field| -%>
<%- case field -%>
- <%- when YARP::ConstantField, YARP::UInt32Field, YARP::FlagsField, YARP::LocationField, YARP::OptionalLocationField -%>
+ <%- when YARP::ConstantField, YARP::OptionalConstantField, YARP::UInt32Field, YARP::FlagsField, YARP::LocationField, YARP::OptionalLocationField -%>
<%- when YARP::NodeField -%>
yp_node_memsize_node((yp_node_t *)cast-><%= field.name %>, memsize);
<%- when YARP::OptionalNodeField -%>
diff --git a/yarp/templates/src/prettyprint.c.erb b/yarp/templates/src/prettyprint.c.erb
index 774720a245..796d8ccafb 100644
--- a/yarp/templates/src/prettyprint.c.erb
+++ b/yarp/templates/src/prettyprint.c.erb
@@ -49,6 +49,14 @@ prettyprint_node(yp_buffer_t *buffer, yp_parser_t *parser, yp_node_t *node) {
char <%= field.name %>_buffer[12];
snprintf(<%= field.name %>_buffer, sizeof(<%= field.name %>_buffer), "%u", ((yp_<%= node.human %>_t *)node)-><%= field.name %>);
yp_buffer_append_str(buffer, <%= field.name %>_buffer, strlen(<%= field.name %>_buffer));
+ <%- when YARP::OptionalConstantField -%>
+ if (((yp_<%= node.human %>_t *)node)-><%= field.name %> == 0) {
+ yp_buffer_append_str(buffer, "nil", 3);
+ } else {
+ char <%= field.name %>_buffer[12];
+ snprintf(<%= field.name %>_buffer, sizeof(<%= field.name %>_buffer), "%u", ((yp_<%= node.human %>_t *)node)-><%= field.name %>);
+ yp_buffer_append_str(buffer, <%= field.name %>_buffer, strlen(<%= field.name %>_buffer));
+ }
<%- when YARP::ConstantListField -%>
yp_buffer_append_str(buffer, "[", 1);
for (uint32_t index = 0; index < ((yp_<%= node.human %>_t *)node)-><%= field.name %>.size; index++) {
diff --git a/yarp/templates/src/serialize.c.erb b/yarp/templates/src/serialize.c.erb
index 4816f154ba..8e0b0905dc 100644
--- a/yarp/templates/src/serialize.c.erb
+++ b/yarp/templates/src/serialize.c.erb
@@ -86,7 +86,7 @@ yp_serialize_node(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer) {
for (uint32_t index = 0; index < <%= field.name %>_size; index++) {
yp_serialize_node(parser, (yp_node_t *) ((yp_<%= node.human %>_t *)node)-><%= field.name %>.nodes[index], buffer);
}
- <%- when YARP::ConstantField -%>
+ <%- when YARP::ConstantField, YARP::OptionalConstantField -%>
yp_buffer_append_u32(buffer, yp_sizet_to_u32(((yp_<%= node.human %>_t *)node)-><%= field.name %>));
<%- when YARP::ConstantListField -%>
uint32_t <%= field.name %>_size = yp_sizet_to_u32(((yp_<%= node.human %>_t *)node)-><%= field.name %>.size);
diff --git a/yarp/templates/template.rb b/yarp/templates/template.rb
index d73951cb53..928ade61a6 100755
--- a/yarp/templates/template.rb
+++ b/yarp/templates/template.rb
@@ -85,6 +85,18 @@ module YARP
end
end
+ # This represents a field on a node that is the ID of a string interned
+ # through the parser's constant pool and can be optionally null.
+ class OptionalConstantField < Field
+ def rbs_class
+ "Symbol?"
+ end
+
+ def java_type
+ "byte[]"
+ end
+ end
+
# This represents a field on a node that is a list of IDs that are associated
# with strings interned through the parser's constant pool.
class ConstantListField < Field
@@ -195,6 +207,7 @@ module YARP
when "node[]" then NodeListField
when "string" then StringField
when "constant" then ConstantField
+ when "constant?" then OptionalConstantField
when "constant[]" then ConstantListField
when "location" then LocationField
when "location?" then OptionalLocationField
@@ -275,7 +288,8 @@ module YARP
HEADING
- heading = if File.extname(filepath.gsub(".erb", "")) == ".rb"
+ heading =
+ if File.extname(filepath.gsub(".erb", "")) == ".rb"
ruby_heading
else
non_ruby_heading
diff --git a/yarp/yarp.c b/yarp/yarp.c
index 4e291637f1..2bce80abad 100644
--- a/yarp/yarp.c
+++ b/yarp/yarp.c
@@ -437,6 +437,13 @@ yp_parser_constant_id_token(yp_parser_t *parser, const yp_token_t *token) {
return yp_parser_constant_id_location(parser, token->start, token->end);
}
+// Retrieve the constant pool id for the given token. If the token is not
+// provided, then return 0.
+static inline yp_constant_id_t
+yp_parser_optional_constant_id_token(yp_parser_t *parser, const yp_token_t *token) {
+ return token->type == YP_TOKEN_NOT_PROVIDED ? 0 : yp_parser_constant_id_token(parser, token);
+}
+
// Mark any range nodes in this subtree as flipflops.
static void
yp_flip_flop(yp_node_t *node) {
@@ -1134,6 +1141,7 @@ yp_block_parameter_node_create(yp_parser_t *parser, const yp_token_t *name, cons
.end = (name->type == YP_TOKEN_NOT_PROVIDED ? operator->end : name->end)
},
},
+ .name = yp_parser_optional_constant_id_token(parser, name),
.name_loc = YP_OPTIONAL_LOCATION_TOKEN_VALUE(name),
.operator_loc = YP_LOCATION_TOKEN_VALUE(operator)
};