@@ -9551,21 +9551,7 @@ escape_write_unicode(pm_parser_t *parser, pm_buffer_t *buffer, const uint8_t fla
95519551 parser->explicit_encoding = PM_ENCODING_UTF_8_ENTRY;
95529552 }
95539553
9554- if (value <= 0x7F) { // 0xxxxxxx
9555- pm_buffer_append_byte(buffer, (uint8_t) value);
9556- } else if (value <= 0x7FF) { // 110xxxxx 10xxxxxx
9557- pm_buffer_append_byte(buffer, (uint8_t) (0xC0 | (value >> 6)));
9558- pm_buffer_append_byte(buffer, (uint8_t) (0x80 | (value & 0x3F)));
9559- } else if (value <= 0xFFFF) { // 1110xxxx 10xxxxxx 10xxxxxx
9560- pm_buffer_append_byte(buffer, (uint8_t) (0xE0 | (value >> 12)));
9561- pm_buffer_append_byte(buffer, (uint8_t) (0x80 | ((value >> 6) & 0x3F)));
9562- pm_buffer_append_byte(buffer, (uint8_t) (0x80 | (value & 0x3F)));
9563- } else if (value <= 0x10FFFF) { // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
9564- pm_buffer_append_byte(buffer, (uint8_t) (0xF0 | (value >> 18)));
9565- pm_buffer_append_byte(buffer, (uint8_t) (0x80 | ((value >> 12) & 0x3F)));
9566- pm_buffer_append_byte(buffer, (uint8_t) (0x80 | ((value >> 6) & 0x3F)));
9567- pm_buffer_append_byte(buffer, (uint8_t) (0x80 | (value & 0x3F)));
9568- } else {
9554+ if (!pm_buffer_append_unicode_codepoint(buffer, value)) {
95699555 pm_parser_err(parser, start, end, PM_ERR_ESCAPE_INVALID_UNICODE);
95709556 pm_buffer_append_byte(buffer, 0xEF);
95719557 pm_buffer_append_byte(buffer, 0xBF);
@@ -20865,6 +20851,123 @@ typedef struct {
2086520851 bool shared;
2086620852} parse_regular_expression_named_capture_data_t;
2086720853
20854+ static inline const uint8_t *
20855+ pm_named_capture_escape_hex(pm_buffer_t *unescaped, const uint8_t *cursor, const uint8_t *end) {
20856+ cursor++;
20857+
20858+ if (cursor < end && pm_char_is_hexadecimal_digit(*cursor)) {
20859+ uint8_t value = escape_hexadecimal_digit(*cursor);
20860+ cursor++;
20861+
20862+ if (cursor < end && pm_char_is_hexadecimal_digit(*cursor)) {
20863+ value = (uint8_t) ((value << 4) | escape_hexadecimal_digit(*cursor));
20864+ cursor++;
20865+ }
20866+
20867+ pm_buffer_append_byte(unescaped, value);
20868+ } else {
20869+ pm_buffer_append_string(unescaped, "\\x", 2);
20870+ }
20871+
20872+ return cursor;
20873+ }
20874+
20875+ static inline const uint8_t *
20876+ pm_named_capture_escape_octal(pm_buffer_t *unescaped, const uint8_t *cursor, const uint8_t *end) {
20877+ uint8_t value = (uint8_t) (*cursor - '0');
20878+ cursor++;
20879+
20880+ if (cursor < end && pm_char_is_octal_digit(*cursor)) {
20881+ value = ((uint8_t) (value << 3)) | ((uint8_t) (*cursor - '0'));
20882+ cursor++;
20883+
20884+ if (cursor < end && pm_char_is_octal_digit(*cursor)) {
20885+ value = ((uint8_t) (value << 3)) | ((uint8_t) (*cursor - '0'));
20886+ cursor++;
20887+ }
20888+ }
20889+
20890+ pm_buffer_append_byte(unescaped, value);
20891+ return cursor;
20892+ }
20893+
20894+ static inline const uint8_t *
20895+ pm_named_capture_escape_unicode(pm_parser_t *parser, pm_buffer_t *unescaped, const uint8_t *cursor, const uint8_t *end) {
20896+ const uint8_t *start = cursor - 1;
20897+ cursor++;
20898+
20899+ if (cursor >= end) {
20900+ pm_buffer_append_string(unescaped, "\\u", 2);
20901+ return cursor;
20902+ }
20903+
20904+ if (*cursor != '{') {
20905+ size_t length = pm_strspn_hexadecimal_digit(cursor, MIN(end - cursor, 4));
20906+ uint32_t value = escape_unicode(parser, cursor, length);
20907+
20908+ if (!pm_buffer_append_unicode_codepoint(unescaped, value)) {
20909+ pm_buffer_append_string(unescaped, (const char *) start, (size_t) ((cursor + length) - start));
20910+ }
20911+
20912+ return cursor + length;
20913+ }
20914+
20915+ cursor++;
20916+ for (;;) {
20917+ while (cursor < end && *cursor == ' ') cursor++;
20918+
20919+ if (cursor >= end) break;
20920+ if (*cursor == '}') {
20921+ cursor++;
20922+ break;
20923+ }
20924+
20925+ size_t length = pm_strspn_hexadecimal_digit(cursor, end - cursor);
20926+ uint32_t value = escape_unicode(parser, cursor, length);
20927+
20928+ (void) pm_buffer_append_unicode_codepoint(unescaped, value);
20929+ cursor += length;
20930+ }
20931+
20932+ return cursor;
20933+ }
20934+
20935+ static void
20936+ pm_named_capture_escape(pm_parser_t *parser, pm_buffer_t *unescaped, const uint8_t *source, const size_t length, const uint8_t *cursor) {
20937+ const uint8_t *end = source + length;
20938+ pm_buffer_append_string(unescaped, (const char *) source, (size_t) (cursor - source));
20939+
20940+ for (;;) {
20941+ if (++cursor >= end) {
20942+ pm_buffer_append_byte(unescaped, '\\');
20943+ return;
20944+ }
20945+
20946+ switch (*cursor) {
20947+ case 'x':
20948+ cursor = pm_named_capture_escape_hex(unescaped, cursor, end);
20949+ break;
20950+ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7':
20951+ cursor = pm_named_capture_escape_octal(unescaped, cursor, end);
20952+ break;
20953+ case 'u':
20954+ cursor = pm_named_capture_escape_unicode(parser, unescaped, cursor, end);
20955+ break;
20956+ default:
20957+ pm_buffer_append_byte(unescaped, '\\');
20958+ break;
20959+ }
20960+
20961+ const uint8_t *next_cursor = pm_memchr(cursor, '\\', (size_t) (end - cursor), parser->encoding_changed, parser->encoding);
20962+ if (next_cursor == NULL) break;
20963+
20964+ pm_buffer_append_string(unescaped, (const char *) cursor, (size_t) (next_cursor - cursor));
20965+ cursor = next_cursor;
20966+ }
20967+
20968+ pm_buffer_append_string(unescaped, (const char *) cursor, (size_t) (end - cursor));
20969+ }
20970+
2086820971/**
2086920972 * This callback is called when the regular expression parser encounters a named
2087020973 * capture group.
@@ -20879,13 +20982,32 @@ parse_regular_expression_named_capture(const pm_string_t *capture, void *data) {
2087920982
2088020983 const uint8_t *source = pm_string_source(capture);
2088120984 size_t length = pm_string_length(capture);
20985+ pm_buffer_t unescaped = { 0 };
20986+
20987+ // First, we need to handle escapes within the name of the capture group.
20988+ // This is because regular expressions have three different representations
20989+ // in prism. The first is the plain source code. The second is the
20990+ // representation that will be sent to the regular expression engine, which
20991+ // is the value of the "unescaped" field. This is poorly named, because it
20992+ // actually still contains escapes, just a subset of them that the regular
20993+ // expression engine knows how to handle. The third representation is fully
20994+ // unescaped, which is what we need.
20995+ const uint8_t *cursor = pm_memchr(source, '\\', length, parser->encoding_changed, parser->encoding);
20996+ if (PRISM_UNLIKELY(cursor != NULL)) {
20997+ pm_named_capture_escape(parser, &unescaped, source, length, cursor);
20998+ source = (const uint8_t *) pm_buffer_value(&unescaped);
20999+ length = pm_buffer_length(&unescaped);
21000+ }
2088221001
2088321002 pm_location_t location;
2088421003 pm_constant_id_t name;
2088521004
2088621005 // If the name of the capture group isn't a valid identifier, we do
2088721006 // not add it to the local table.
20888- if (!pm_slice_is_valid_local(parser, source, source + length)) return;
21007+ if (!pm_slice_is_valid_local(parser, source, source + length)) {
21008+ pm_buffer_free(&unescaped);
21009+ return;
21010+ }
2088921011
2089021012 if (callback_data->shared) {
2089121013 // If the unescaped string is a slice of the source, then we can
@@ -20913,7 +21035,10 @@ parse_regular_expression_named_capture(const pm_string_t *capture, void *data) {
2091321035 if ((depth = pm_parser_local_depth_constant_id(parser, name)) == -1) {
2091421036 // If the local is not already a local but it is a keyword, then we
2091521037 // do not want to add a capture for this.
20916- if (pm_local_is_keyword((const char *) source, length)) return;
21038+ if (pm_local_is_keyword((const char *) source, length)) {
21039+ pm_buffer_free(&unescaped);
21040+ return;
21041+ }
2091721042
2091821043 // If the identifier is not already a local, then we will add it to
2091921044 // the local table.
@@ -20931,6 +21056,8 @@ parse_regular_expression_named_capture(const pm_string_t *capture, void *data) {
2093121056 pm_node_t *target = (pm_node_t *) pm_local_variable_target_node_create(parser, &location, name, depth == -1 ? 0 : (uint32_t) depth);
2093221057 pm_node_list_append(&callback_data->match->targets, target);
2093321058 }
21059+
21060+ pm_buffer_free(&unescaped);
2093421061}
2093521062
2093621063/**
0 commit comments