Skip to content

Commit 09248a1

Browse files
committed
Add a frozen string flag
1 parent c61658c commit 09248a1

File tree

173 files changed

+644
-5
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

173 files changed

+644
-5
lines changed

config.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ flags:
369369
comment: "u - forces the UTF-8 encoding"
370370
- name: ONCE
371371
comment: "o - only interpolates values into the regular expression once"
372+
- name: StringFlags
373+
values:
374+
- name: FROZEN
375+
comment: "frozen by virtue of a frozen_string_literal comment"
372376
nodes:
373377
- name: AliasGlobalVariableNode
374378
fields:
@@ -2249,6 +2253,9 @@ nodes:
22492253
^^^^^^^^^^^
22502254
- name: StringNode
22512255
fields:
2256+
- name: flags
2257+
type: flags
2258+
kind: StringFlags
22522259
- name: opening_loc
22532260
type: location?
22542261
- name: content_loc

src/yarp.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4197,10 +4197,16 @@ yp_string_concat_node_create(yp_parser_t *parser, yp_node_t *left, yp_node_t *ri
41974197
static yp_string_node_t *
41984198
yp_string_node_create(yp_parser_t *parser, const yp_token_t *opening, const yp_token_t *content, const yp_token_t *closing) {
41994199
yp_string_node_t *node = YP_ALLOC_NODE(parser, yp_string_node_t);
4200+
yp_node_flags_t flags = 0;
4201+
4202+
if (parser->frozen_string_literal) {
4203+
flags = YP_NODE_FLAG_STATIC_LITERAL | YP_STRING_FLAGS_FROZEN;
4204+
}
42004205

42014206
*node = (yp_string_node_t) {
42024207
{
42034208
.type = YP_STRING_NODE,
4209+
.flags = flags,
42044210
.location = {
42054211
.start = (opening->type == YP_TOKEN_NOT_PROVIDED ? content->start : opening->start),
42064212
.end = (closing->type == YP_TOKEN_NOT_PROVIDED ? content->end : closing->end)
@@ -4361,10 +4367,16 @@ yp_string_node_to_symbol_node(yp_parser_t *parser, yp_string_node_t *node, const
43614367
static yp_string_node_t *
43624368
yp_symbol_node_to_string_node(yp_parser_t *parser, yp_symbol_node_t *node) {
43634369
yp_string_node_t *new_node = YP_ALLOC_NODE(parser, yp_string_node_t);
4370+
yp_node_flags_t flags = 0;
4371+
4372+
if (parser->frozen_string_literal) {
4373+
flags = YP_NODE_FLAG_STATIC_LITERAL | YP_STRING_FLAGS_FROZEN;
4374+
}
43644375

43654376
*new_node = (yp_string_node_t) {
43664377
{
43674378
.type = YP_STRING_NODE,
4379+
.flags = flags,
43684380
.location = node->base.location
43694381
},
43704382
.opening_loc = node->opening_loc,
@@ -5111,7 +5123,8 @@ parser_lex_frozen_string_literal_comment(yp_parser_t *parser) {
51115123

51125124
while ((cursor = yp_memchr(cursor, 'f', (size_t) (cursor_limit - cursor), parser->encoding_changed, &parser->encoding)) != NULL) {
51135125
if (memcmp(cursor, "frozen_string_literal", key_length) == 0) {
5114-
cursor += yp_strspn_inline_whitespace(cursor + key_length, end - (cursor + key_length));
5126+
cursor += key_length;
5127+
cursor += yp_strspn_inline_whitespace(cursor, end - cursor);
51155128

51165129
if (*cursor == ':' || *cursor == '=') {
51175130
cursor++;
@@ -6174,7 +6187,7 @@ parser_lex(yp_parser_t *parser) {
61746187
/* fallthrough */
61756188
case '\r':
61766189
case '\n': {
6177-
parser->semantic_token_seen = semantic_token_seen;
6190+
parser->semantic_token_seen = semantic_token_seen & 0x1;
61786191
size_t eol_length = match_eol_at(parser, parser->current.end - 1);
61796192

61806193
if (eol_length) {

test/yarp/errors_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ def test_do_not_allow_trailing_commas_in_lambda_parameters
601601
end
602602

603603
def test_do_not_allow_multiple_codepoints_in_a_single_character_literal
604-
expected = StringNode(Location(), Location(), nil, "\u0001\u0002")
604+
expected = StringNode(0, Location(), Location(), nil, "\u0001\u0002")
605605

606606
assert_errors expected, '?\u{0001 0002}', [
607607
["Invalid Unicode escape sequence; multiple codepoints are not allowed in a character literal", 9..12]
@@ -615,15 +615,15 @@ def test_invalid_hex_escape
615615
end
616616

617617
def test_do_not_allow_more_than_6_hexadecimal_digits_in_u_Unicode_character_notation
618-
expected = StringNode(Location(), Location(), Location(), "\u0001")
618+
expected = StringNode(0, Location(), Location(), Location(), "\u0001")
619619

620620
assert_errors expected, '"\u{0000001}"', [
621621
["Invalid Unicode escape sequence; maximum length is 6 digits", 4..11],
622622
]
623623
end
624624

625625
def test_do_not_allow_characters_other_than_0_9_a_f_and_A_F_in_u_Unicode_character_notation
626-
expected = StringNode(Location(), Location(), Location(), "\u0000z}")
626+
expected = StringNode(0, Location(), Location(), Location(), "\u0000z}")
627627

628628
assert_errors expected, '"\u{000z}"', [
629629
["Invalid Unicode escape sequence", 7..7],

test/yarp/snapshots/alias.txt

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/yarp/snapshots/arrays.txt

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/yarp/snapshots/begin_rescue.txt

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/yarp/snapshots/dash_heredocs.txt

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/yarp/snapshots/dos_endings.txt

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)