summaryrefslogtreecommitdiff
path: root/prism
diff options
context:
space:
mode:
authoreileencodes <[email protected]>2023-10-13 13:30:02 -0400
committergit <[email protected]>2023-10-13 19:38:57 +0000
commit42484d1281868a50dd06cd819aaa3d9f977b3ae2 (patch)
tree0fcf3097e0f839c2d9c9c0520fc02d6d41ddc95d /prism
parent17697c968e8cd00cdd581459c11868d1f2025f3a (diff)
[ruby/prism] Move common flags to top bits
Moves the common flag bits to the top. This lets us eliminate the `COMMON` constant, and also allows us to group encoding flags on a nibble so we can more easily mask them. https://github.com/ruby/prism/commit/895508659e
Diffstat (limited to 'prism')
-rw-r--r--prism/config.yml4
-rw-r--r--prism/templates/ext/prism/api_node.c.erb2
-rw-r--r--prism/templates/include/prism/ast.h.erb8
-rw-r--r--prism/templates/src/prettyprint.c.erb2
-rw-r--r--prism/templates/src/serialize.c.erb2
-rwxr-xr-xprism/templates/template.rb2
6 files changed, 10 insertions, 10 deletions
diff --git a/prism/config.yml b/prism/config.yml
index ad6ec1b707..f299754d82 100644
--- a/prism/config.yml
+++ b/prism/config.yml
@@ -361,6 +361,8 @@ flags:
comment: "x - ignores whitespace and allows comments in regular expressions"
- name: MULTI_LINE
comment: "m - allows $ to match the end of lines within strings"
+ - name: ONCE
+ comment: "o - only interpolates values into the regular expression once"
- name: EUC_JP
comment: "e - forces the EUC-JP encoding"
- name: ASCII_8BIT
@@ -369,8 +371,6 @@ flags:
comment: "s - forces the Windows-31J encoding"
- name: UTF_8
comment: "u - forces the UTF-8 encoding"
- - name: ONCE
- comment: "o - only interpolates values into the regular expression once"
- name: StringFlags
values:
- name: FROZEN
diff --git a/prism/templates/ext/prism/api_node.c.erb b/prism/templates/ext/prism/api_node.c.erb
index 76eb50a7bd..6da5832185 100644
--- a/prism/templates/ext/prism/api_node.c.erb
+++ b/prism/templates/ext/prism/api_node.c.erb
@@ -176,7 +176,7 @@ pm_ast_new(pm_parser_t *parser, pm_node_t *node, rb_encoding *encoding) {
argv[<%= index %>] = ULONG2NUM(cast-><%= field.name %>);
<%- when Prism::FlagsField -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
- argv[<%= index %>] = ULONG2NUM(node->flags >> <%= Prism::COMMON_FLAGS %>);
+ argv[<%= index %>] = ULONG2NUM(node->flags & ~PM_NODE_FLAG_COMMON_MASK);
<%- else -%>
<%- raise -%>
<%- end -%>
diff --git a/prism/templates/include/prism/ast.h.erb b/prism/templates/include/prism/ast.h.erb
index 31c2fb2e0d..3a95676c95 100644
--- a/prism/templates/include/prism/ast.h.erb
+++ b/prism/templates/include/prism/ast.h.erb
@@ -52,8 +52,10 @@ typedef uint16_t pm_node_flags_t;
// We store the flags enum in every node in the tree. Some flags are common to
// all nodes (the ones listed below). Others are specific to certain node types.
-static const pm_node_flags_t PM_NODE_FLAG_NEWLINE = 0x1;
-static const pm_node_flags_t PM_NODE_FLAG_STATIC_LITERAL = 0x2;
+#define PM_NODE_FLAG_BITS (sizeof(pm_node_flags_t) * 8)
+static const pm_node_flags_t PM_NODE_FLAG_NEWLINE = (1 << (PM_NODE_FLAG_BITS - 1));
+static const pm_node_flags_t PM_NODE_FLAG_STATIC_LITERAL = (1 << (PM_NODE_FLAG_BITS - 2));
+static const pm_node_flags_t PM_NODE_FLAG_COMMON_MASK = PM_NODE_FLAG_NEWLINE | PM_NODE_FLAG_STATIC_LITERAL;
// For easy access, we define some macros to check node type
#define PM_NODE_TYPE(node) ((enum pm_node_type)node->type)
@@ -105,7 +107,7 @@ typedef struct pm_<%= node.human %> {
// <%= flag.name %>
typedef enum pm_<%= flag.human %> {
- <%- flag.values.each.with_index(Prism::COMMON_FLAGS) do |value, index| -%>
+ <%- flag.values.each_with_index do |value, index| -%>
PM_<%= flag.human.upcase %>_<%= value.name %> = 1 << <%= index %>,
<%- end -%>
} pm_<%= flag.human %>_t;
diff --git a/prism/templates/src/prettyprint.c.erb b/prism/templates/src/prettyprint.c.erb
index 0dd8632616..c5d05485b8 100644
--- a/prism/templates/src/prettyprint.c.erb
+++ b/prism/templates/src/prettyprint.c.erb
@@ -80,7 +80,7 @@ prettyprint_node(pm_buffer_t *buffer, pm_parser_t *parser, pm_node_t *node) {
pm_buffer_append_str(buffer, <%= field.name %>_buffer, strlen(<%= field.name %>_buffer));
<%- when Prism::FlagsField -%>
char <%= field.name %>_buffer[12];
- snprintf(<%= field.name %>_buffer, sizeof(<%= field.name %>_buffer), "+%d", node->flags >> <%= Prism::COMMON_FLAGS %>);
+ snprintf(<%= field.name %>_buffer, sizeof(<%= field.name %>_buffer), "+%d", (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
pm_buffer_append_str(buffer, <%= field.name %>_buffer, strlen(<%= field.name %>_buffer));
<%- else -%>
<%- raise -%>
diff --git a/prism/templates/src/serialize.c.erb b/prism/templates/src/serialize.c.erb
index cc2d4db7a0..e9ebc31590 100644
--- a/prism/templates/src/serialize.c.erb
+++ b/prism/templates/src/serialize.c.erb
@@ -110,7 +110,7 @@ pm_serialize_node(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer) {
<%- when Prism::UInt32Field -%>
pm_buffer_append_u32(buffer, ((pm_<%= node.human %>_t *)node)-><%= field.name %>);
<%- when Prism::FlagsField -%>
- pm_buffer_append_u32(buffer, node->flags >> <%= Prism::COMMON_FLAGS %>);
+ pm_buffer_append_u32(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
<%- else -%>
<%- raise -%>
<%- end -%>
diff --git a/prism/templates/template.rb b/prism/templates/template.rb
index ac71191d77..79ad84c732 100755
--- a/prism/templates/template.rb
+++ b/prism/templates/template.rb
@@ -5,8 +5,6 @@ require "fileutils"
require "yaml"
module Prism
- COMMON_FLAGS = 2
-
SERIALIZE_ONLY_SEMANTICS_FIELDS = ENV.fetch("PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS", false)
JAVA_BACKEND = ENV["PRISM_JAVA_BACKEND"] || "truffleruby"