From 1cd7b04bd2545b8726b52e34c723510ebecdb9b2 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Tue, 24 Nov 2026 10:56:12 -0500 Subject: [PATCH 1/8] Track leading semicolon on statements When statements have a leading semicolon flag, it changes when something is considered a static literal, or when something flips to a flip flop or implicit match. --- config.yml | 2 + rbi/generated/prism/node.rbi | 7 +++ rust/ruby-prism-sys/src/bindings.rs | 9 +++- sig/generated/prism/node.rbs | 9 ++++ snapshots/arrays.txt | 12 +++--- snapshots/begin_ensure.txt | 4 +- snapshots/begin_rescue.txt | 14 +++--- snapshots/classes.txt | 4 +- snapshots/method_calls.txt | 2 +- snapshots/methods.txt | 12 +++--- snapshots/procs.txt | 2 +- snapshots/rescue.txt | 2 +- snapshots/rescue_ensure_value.txt | 6 +-- snapshots/seattlerb/defn_arg_forward_args.txt | 2 +- .../seattlerb/defn_args_forward_args.txt | 2 +- snapshots/seattlerb/defn_forward_args.txt | 2 +- snapshots/seattlerb/defn_kwarg_lvar.txt | 2 +- .../seattlerb/magic_encoding_comment.txt | 2 +- snapshots/whitequark/anonymous_blockarg.txt | 2 +- snapshots/whitequark/ensure.txt | 4 +- snapshots/whitequark/forward_arg.txt | 2 +- snapshots/whitequark/forward_args_legacy.txt | 4 +- .../forwarded_argument_with_kwrestarg.txt | 2 +- .../forwarded_argument_with_restarg.txt | 2 +- snapshots/whitequark/forwarded_kwrestarg.txt | 2 +- ...warded_kwrestarg_with_additional_kwarg.txt | 2 +- snapshots/whitequark/forwarded_restarg.txt | 2 +- ...targ_with_kwrestarg_and_forwarded_args.txt | 2 +- .../whitequark/numparam_outside_block.txt | 4 +- snapshots/whitequark/parser_bug_525.txt | 2 +- .../whitequark/pattern_matching_else.txt | 2 +- snapshots/whitequark/resbody_list.txt | 2 +- snapshots/whitequark/resbody_list_mrhs.txt | 2 +- snapshots/whitequark/resbody_list_var.txt | 2 +- snapshots/whitequark/resbody_var.txt | 4 +- snapshots/whitequark/rescue.txt | 2 +- snapshots/whitequark/rescue_else.txt | 4 +- snapshots/whitequark/rescue_else_ensure.txt | 4 +- snapshots/whitequark/rescue_ensure.txt | 4 +- .../whitequark/rescue_without_begin_end.txt | 2 +- snapshots/whitequark/trailing_forward_arg.txt | 2 +- src/prism.c | 43 ++++++++----------- test/prism/api/parse_test.rb | 27 ++++++++++++ 43 files changed, 134 insertions(+), 91 deletions(-) diff --git a/config.yml b/config.yml index 3abefab5c6..9dac53a72a 100644 --- a/config.yml +++ b/config.yml @@ -1330,6 +1330,8 @@ flags: values: - name: CONTAINS_PRE_EXECUTION comment: "the statement list directly contains a `BEGIN` block" + - name: LEADING_SEMICOLON + comment: "a semicolon precedes the first statement in the list" comment: Flags for statement lists. - name: StringFlags values: diff --git a/rbi/generated/prism/node.rbi b/rbi/generated/prism/node.rbi index bc14b8f2a0..e8dc343808 100644 --- a/rbi/generated/prism/node.rbi +++ b/rbi/generated/prism/node.rbi @@ -13812,6 +13812,10 @@ module Prism sig { returns(T::Boolean) } def contains_pre_execution?; end + # a semicolon precedes the first statement in the list + sig { returns(T::Boolean) } + def leading_semicolon?; end + # Returns the `body` attribute. sig { returns(T::Array[Node]) } def body; end @@ -15111,6 +15115,9 @@ module Prism module StatementsFlags # the statement list directly contains a `BEGIN` block CONTAINS_PRE_EXECUTION = T.let(nil, ::T.untyped) + + # a semicolon precedes the first statement in the list + LEADING_SEMICOLON = T.let(nil, ::T.untyped) end # Flags for string nodes. diff --git a/rust/ruby-prism-sys/src/bindings.rs b/rust/ruby-prism-sys/src/bindings.rs index a527839a6a..a88f467c44 100644 --- a/rust/ruby-prism-sys/src/bindings.rs +++ b/rust/ruby-prism-sys/src/bindings.rs @@ -2368,6 +2368,7 @@ pub type pm_splat_node_t = pm_splat_node; Flags (#pm_statements_flags): * ::PM_STATEMENTS_FLAGS_CONTAINS_PRE_EXECUTION + * ::PM_STATEMENTS_FLAGS_LEADING_SEMICOLON @extends pm_node_t */ @@ -9113,6 +9114,7 @@ pub struct pm_splat_node { Flags (#pm_statements_flags): * ::PM_STATEMENTS_FLAGS_CONTAINS_PRE_EXECUTION + * ::PM_STATEMENTS_FLAGS_LEADING_SEMICOLON @extends pm_node_t */ @@ -10428,9 +10430,12 @@ pub enum pm_statements_flags { /** the statement list directly contains a `BEGIN` block */ PM_STATEMENTS_FLAGS_CONTAINS_PRE_EXECUTION = 4, - /** the statement list directly contains a `BEGIN` block + /** a semicolon precedes the first statement in the list +*/ + PM_STATEMENTS_FLAGS_LEADING_SEMICOLON = 8, + /** a semicolon precedes the first statement in the list */ - PM_STATEMENTS_FLAGS_LAST = 5, + PM_STATEMENTS_FLAGS_LAST = 9, } #[repr(u32)] #[non_exhaustive] diff --git a/sig/generated/prism/node.rbs b/sig/generated/prism/node.rbs index 50eaf287dc..9c9c5339c5 100644 --- a/sig/generated/prism/node.rbs +++ b/sig/generated/prism/node.rbs @@ -19708,6 +19708,12 @@ module Prism # : () -> bool def contains_pre_execution?: () -> bool + # :category: Flags + # a semicolon precedes the first statement in the list + # + # : () -> bool + def leading_semicolon?: () -> bool + # :call-seq: # body -> Array[Node] # @@ -21602,6 +21608,9 @@ module Prism module StatementsFlags # the statement list directly contains a `BEGIN` block CONTAINS_PRE_EXECUTION: untyped + + # a semicolon precedes the first statement in the list + LEADING_SEMICOLON: untyped end # Flags for string nodes. diff --git a/snapshots/arrays.txt b/snapshots/arrays.txt index 8d530fe166..bd73a4f961 100644 --- a/snapshots/arrays.txt +++ b/snapshots/arrays.txt @@ -1469,7 +1469,7 @@ │ │ └── block: ∅ │ ├── body: │ │ @ StatementsNode (location: (108,10)-(108,14)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ CallNode (location: (108,10)-(108,14)) │ │ ├── flags: newline @@ -1529,7 +1529,7 @@ │ │ └── block: ∅ │ ├── body: │ │ @ StatementsNode (location: (110,10)-(110,17)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ CallNode (location: (110,10)-(110,17)) │ │ ├── flags: newline @@ -1592,7 +1592,7 @@ │ │ └── block: ∅ │ ├── body: │ │ @ StatementsNode (location: (112,10)-(112,18)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ CallNode (location: (112,10)-(112,18)) │ │ ├── flags: newline, attribute_write @@ -1655,7 +1655,7 @@ │ │ └── block: ∅ │ ├── body: │ │ @ StatementsNode (location: (114,10)-(114,21)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ CallNode (location: (114,10)-(114,21)) │ │ ├── flags: newline, attribute_write @@ -1721,7 +1721,7 @@ │ │ └── block: ∅ │ ├── body: │ │ @ StatementsNode (location: (116,10)-(116,19)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ IndexOperatorWriteNode (location: (116,10)-(116,19)) │ │ ├── flags: newline @@ -1784,7 +1784,7 @@ │ │ └── block: ∅ │ ├── body: │ │ @ StatementsNode (location: (118,10)-(118,23)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ IndexAndWriteNode (location: (118,10)-(118,23)) │ │ ├── flags: newline diff --git a/snapshots/begin_ensure.txt b/snapshots/begin_ensure.txt index e0a027b1c6..eaf8dc69a2 100644 --- a/snapshots/begin_ensure.txt +++ b/snapshots/begin_ensure.txt @@ -51,7 +51,7 @@ │ ├── begin_keyword_loc: (7,0)-(7,5) = "begin" │ ├── statements: │ │ @ StatementsNode (location: (7,7)-(7,8)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ CallNode (location: (7,7)-(7,8)) │ │ ├── flags: newline, variable_call, ignore_visibility @@ -72,7 +72,7 @@ │ │ ├── ensure_keyword_loc: (7,10)-(7,16) = "ensure" │ │ ├── statements: │ │ │ @ StatementsNode (location: (7,18)-(7,19)) - │ │ │ ├── flags: ∅ + │ │ │ ├── flags: leading_semicolon │ │ │ └── body: (length: 1) │ │ │ └── @ CallNode (location: (7,18)-(7,19)) │ │ │ ├── flags: newline, variable_call, ignore_visibility diff --git a/snapshots/begin_rescue.txt b/snapshots/begin_rescue.txt index 7e1ae368a1..c4ac36d983 100644 --- a/snapshots/begin_rescue.txt +++ b/snapshots/begin_rescue.txt @@ -10,7 +10,7 @@ │ ├── begin_keyword_loc: (1,0)-(1,5) = "begin" │ ├── statements: │ │ @ StatementsNode (location: (1,7)-(1,8)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ CallNode (location: (1,7)-(1,8)) │ │ ├── flags: newline, variable_call, ignore_visibility @@ -53,7 +53,7 @@ │ │ ├── else_keyword_loc: (1,21)-(1,25) = "else" │ │ ├── statements: │ │ │ @ StatementsNode (location: (1,27)-(1,28)) - │ │ │ ├── flags: ∅ + │ │ │ ├── flags: leading_semicolon │ │ │ └── body: (length: 1) │ │ │ └── @ CallNode (location: (1,27)-(1,28)) │ │ │ ├── flags: newline, variable_call, ignore_visibility @@ -74,7 +74,7 @@ │ ├── begin_keyword_loc: (3,0)-(3,5) = "begin" │ ├── statements: │ │ @ StatementsNode (location: (3,7)-(3,8)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ CallNode (location: (3,7)-(3,8)) │ │ ├── flags: newline, variable_call, ignore_visibility @@ -117,7 +117,7 @@ │ │ ├── else_keyword_loc: (3,21)-(3,25) = "else" │ │ ├── statements: │ │ │ @ StatementsNode (location: (3,27)-(3,28)) - │ │ │ ├── flags: ∅ + │ │ │ ├── flags: leading_semicolon │ │ │ └── body: (length: 1) │ │ │ └── @ CallNode (location: (3,27)-(3,28)) │ │ │ ├── flags: newline, variable_call, ignore_visibility @@ -137,7 +137,7 @@ │ │ ├── ensure_keyword_loc: (3,30)-(3,36) = "ensure" │ │ ├── statements: │ │ │ @ StatementsNode (location: (3,38)-(3,39)) - │ │ │ ├── flags: ∅ + │ │ │ ├── flags: leading_semicolon │ │ │ └── body: (length: 1) │ │ │ └── @ CallNode (location: (3,38)-(3,39)) │ │ │ ├── flags: newline, variable_call, ignore_visibility @@ -240,7 +240,7 @@ │ ├── begin_keyword_loc: (15,0)-(15,5) = "begin" │ ├── statements: │ │ @ StatementsNode (location: (15,7)-(15,8)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ CallNode (location: (15,7)-(15,8)) │ │ ├── flags: newline, variable_call, ignore_visibility @@ -604,7 +604,7 @@ │ ├── begin_keyword_loc: (56,0)-(56,5) = "begin" │ ├── statements: │ │ @ StatementsNode (location: (56,6)-(56,7)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ CallNode (location: (56,6)-(56,7)) │ │ ├── flags: newline, variable_call, ignore_visibility diff --git a/snapshots/classes.txt b/snapshots/classes.txt index 8633bb0f9c..1f1561e8d3 100644 --- a/snapshots/classes.txt +++ b/snapshots/classes.txt @@ -168,7 +168,7 @@ │ ├── superclass: ∅ │ ├── body: │ │ @ StatementsNode (location: (14,9)-(14,35)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ SingletonClassNode (location: (14,9)-(14,35)) │ │ ├── flags: newline @@ -207,7 +207,7 @@ │ ├── superclass: ∅ │ ├── body: │ │ @ StatementsNode (location: (16,9)-(16,49)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ SingletonClassNode (location: (16,9)-(16,49)) │ │ ├── flags: newline diff --git a/snapshots/method_calls.txt b/snapshots/method_calls.txt index ac36be0fdf..036664bcdd 100644 --- a/snapshots/method_calls.txt +++ b/snapshots/method_calls.txt @@ -2564,7 +2564,7 @@ │ │ └── block: ∅ │ ├── body: │ │ @ StatementsNode (location: (149,10)-(149,13)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ CallNode (location: (149,10)-(149,13)) │ │ ├── flags: newline, ignore_visibility diff --git a/snapshots/methods.txt b/snapshots/methods.txt index 80c6fc637e..2e4767dea2 100644 --- a/snapshots/methods.txt +++ b/snapshots/methods.txt @@ -1034,7 +1034,7 @@ │ │ └── block: ∅ │ ├── body: │ │ @ StatementsNode (location: (110,10)-(110,14)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ CallNode (location: (110,10)-(110,14)) │ │ ├── flags: newline, ignore_visibility @@ -1088,7 +1088,7 @@ │ │ └── block: ∅ │ ├── body: │ │ @ StatementsNode (location: (112,14)-(112,28)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ CallNode (location: (112,14)-(112,28)) │ │ ├── flags: newline, ignore_visibility @@ -1162,7 +1162,7 @@ │ │ └── block: ∅ │ ├── body: │ │ @ StatementsNode (location: (114,12)-(114,18)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ CallNode (location: (114,12)-(114,18)) │ │ ├── flags: newline, ignore_visibility @@ -1206,7 +1206,7 @@ │ │ └── block: ∅ │ ├── body: │ │ @ StatementsNode (location: (116,12)-(116,24)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ CallNode (location: (116,12)-(116,24)) │ │ ├── flags: newline, ignore_visibility @@ -1445,7 +1445,7 @@ │ │ └── block: ∅ │ ├── body: │ │ @ StatementsNode (location: (138,12)-(138,26)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ InterpolatedStringNode (location: (138,12)-(138,26)) │ │ ├── flags: newline @@ -1968,7 +1968,7 @@ │ │ └── block: ∅ │ ├── body: │ │ @ StatementsNode (location: (169,10)-(169,13)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ ArrayNode (location: (169,10)-(169,13)) │ │ ├── flags: newline, contains_splat diff --git a/snapshots/procs.txt b/snapshots/procs.txt index a52eb78484..630a65fc62 100644 --- a/snapshots/procs.txt +++ b/snapshots/procs.txt @@ -134,7 +134,7 @@ │ ├── parameters: ∅ │ └── body: │ @ StatementsNode (location: (15,7)-(15,10)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (15,7)-(15,10)) │ ├── flags: newline, variable_call, ignore_visibility diff --git a/snapshots/rescue.txt b/snapshots/rescue.txt index e93d48e33e..563812e774 100644 --- a/snapshots/rescue.txt +++ b/snapshots/rescue.txt @@ -190,7 +190,7 @@ │ ├── begin_keyword_loc: (16,0)-(16,5) = "begin" │ ├── statements: │ │ @ StatementsNode (location: (16,7)-(16,8)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ CallNode (location: (16,7)-(16,8)) │ │ ├── flags: newline, variable_call, ignore_visibility diff --git a/snapshots/rescue_ensure_value.txt b/snapshots/rescue_ensure_value.txt index e508009232..05ac40346e 100644 --- a/snapshots/rescue_ensure_value.txt +++ b/snapshots/rescue_ensure_value.txt @@ -26,7 +26,7 @@ │ │ │ ├── begin_keyword_loc: (1,11)-(1,16) = "begin" │ │ │ ├── statements: │ │ │ │ @ StatementsNode (location: (1,18)-(1,24)) - │ │ │ │ ├── flags: ∅ + │ │ │ │ ├── flags: leading_semicolon │ │ │ │ └── body: (length: 1) │ │ │ │ └── @ ReturnNode (location: (1,18)-(1,24)) │ │ │ │ ├── flags: newline @@ -79,7 +79,7 @@ │ │ │ ├── begin_keyword_loc: (3,11)-(3,16) = "begin" │ │ │ ├── statements: │ │ │ │ @ StatementsNode (location: (3,18)-(3,24)) - │ │ │ │ ├── flags: ∅ + │ │ │ │ ├── flags: leading_semicolon │ │ │ │ └── body: (length: 1) │ │ │ │ └── @ ReturnNode (location: (3,18)-(3,24)) │ │ │ │ ├── flags: newline @@ -157,7 +157,7 @@ │ │ ├── begin_keyword_loc: (5,11)-(5,16) = "begin" │ │ ├── statements: │ │ │ @ StatementsNode (location: (5,18)-(5,19)) - │ │ │ ├── flags: ∅ + │ │ │ ├── flags: leading_semicolon │ │ │ └── body: (length: 1) │ │ │ └── @ IntegerNode (location: (5,18)-(5,19)) │ │ │ ├── flags: newline, static_literal, decimal diff --git a/snapshots/seattlerb/defn_arg_forward_args.txt b/snapshots/seattlerb/defn_arg_forward_args.txt index 76ffa41db5..2f7945444f 100644 --- a/snapshots/seattlerb/defn_arg_forward_args.txt +++ b/snapshots/seattlerb/defn_arg_forward_args.txt @@ -27,7 +27,7 @@ │ └── block: ∅ ├── body: │ @ StatementsNode (location: (1,15)-(1,24)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (1,15)-(1,24)) │ ├── flags: newline, ignore_visibility diff --git a/snapshots/seattlerb/defn_args_forward_args.txt b/snapshots/seattlerb/defn_args_forward_args.txt index 2a49fa24cf..691d73e5c0 100644 --- a/snapshots/seattlerb/defn_args_forward_args.txt +++ b/snapshots/seattlerb/defn_args_forward_args.txt @@ -33,7 +33,7 @@ │ └── block: ∅ ├── body: │ @ StatementsNode (location: (1,21)-(1,36)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (1,21)-(1,36)) │ ├── flags: newline, ignore_visibility diff --git a/snapshots/seattlerb/defn_forward_args.txt b/snapshots/seattlerb/defn_forward_args.txt index b912c58721..326a0f5f40 100644 --- a/snapshots/seattlerb/defn_forward_args.txt +++ b/snapshots/seattlerb/defn_forward_args.txt @@ -24,7 +24,7 @@ │ └── block: ∅ ├── body: │ @ StatementsNode (location: (1,12)-(1,18)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (1,12)-(1,18)) │ ├── flags: newline, ignore_visibility diff --git a/snapshots/seattlerb/defn_kwarg_lvar.txt b/snapshots/seattlerb/defn_kwarg_lvar.txt index 17069bc20f..74f7f99ae2 100644 --- a/snapshots/seattlerb/defn_kwarg_lvar.txt +++ b/snapshots/seattlerb/defn_kwarg_lvar.txt @@ -33,7 +33,7 @@ │ └── block: ∅ ├── body: │ @ StatementsNode (location: (1,19)-(1,21)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ LocalVariableReadNode (location: (1,19)-(1,21)) │ ├── flags: newline diff --git a/snapshots/seattlerb/magic_encoding_comment.txt b/snapshots/seattlerb/magic_encoding_comment.txt index fd0e14c732..9e1df0d77f 100644 --- a/snapshots/seattlerb/magic_encoding_comment.txt +++ b/snapshots/seattlerb/magic_encoding_comment.txt @@ -17,7 +17,7 @@ ├── superclass: ∅ ├── body: │ @ StatementsNode (location: (2,36)-(2,68)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ DefNode (location: (2,36)-(2,68)) │ ├── flags: newline diff --git a/snapshots/whitequark/anonymous_blockarg.txt b/snapshots/whitequark/anonymous_blockarg.txt index 3d180d6910..6b0f972a35 100644 --- a/snapshots/whitequark/anonymous_blockarg.txt +++ b/snapshots/whitequark/anonymous_blockarg.txt @@ -27,7 +27,7 @@ │ └── operator_loc: (1,8)-(1,9) = "&" ├── body: │ @ StatementsNode (location: (1,12)-(1,18)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (1,12)-(1,18)) │ ├── flags: newline, ignore_visibility diff --git a/snapshots/whitequark/ensure.txt b/snapshots/whitequark/ensure.txt index 30791fb254..779f01dc01 100644 --- a/snapshots/whitequark/ensure.txt +++ b/snapshots/whitequark/ensure.txt @@ -10,7 +10,7 @@ ├── begin_keyword_loc: (1,0)-(1,5) = "begin" ├── statements: │ @ StatementsNode (location: (1,7)-(1,11)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (1,7)-(1,11)) │ ├── flags: newline, variable_call, ignore_visibility @@ -31,7 +31,7 @@ │ ├── ensure_keyword_loc: (1,13)-(1,19) = "ensure" │ ├── statements: │ │ @ StatementsNode (location: (1,21)-(1,24)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ CallNode (location: (1,21)-(1,24)) │ │ ├── flags: newline, variable_call, ignore_visibility diff --git a/snapshots/whitequark/forward_arg.txt b/snapshots/whitequark/forward_arg.txt index e24ae1f3a8..f1b157538d 100644 --- a/snapshots/whitequark/forward_arg.txt +++ b/snapshots/whitequark/forward_arg.txt @@ -24,7 +24,7 @@ │ └── block: ∅ ├── body: │ @ StatementsNode (location: (1,14)-(1,22)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (1,14)-(1,22)) │ ├── flags: newline, ignore_visibility diff --git a/snapshots/whitequark/forward_args_legacy.txt b/snapshots/whitequark/forward_args_legacy.txt index a719c84924..b304b760d1 100644 --- a/snapshots/whitequark/forward_args_legacy.txt +++ b/snapshots/whitequark/forward_args_legacy.txt @@ -24,7 +24,7 @@ │ │ └── block: ∅ │ ├── body: │ │ @ StatementsNode (location: (1,14)-(1,22)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ CallNode (location: (1,14)-(1,22)) │ │ ├── flags: newline, ignore_visibility @@ -93,7 +93,7 @@ │ └── block: ∅ ├── body: │ @ StatementsNode (location: (5,14)-(5,24)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ SuperNode (location: (5,14)-(5,24)) │ ├── flags: newline diff --git a/snapshots/whitequark/forwarded_argument_with_kwrestarg.txt b/snapshots/whitequark/forwarded_argument_with_kwrestarg.txt index 329920e876..ba97b51ff1 100644 --- a/snapshots/whitequark/forwarded_argument_with_kwrestarg.txt +++ b/snapshots/whitequark/forwarded_argument_with_kwrestarg.txt @@ -30,7 +30,7 @@ │ └── block: ∅ ├── body: │ @ StatementsNode (location: (1,23)-(1,40)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (1,23)-(1,40)) │ ├── flags: newline, ignore_visibility diff --git a/snapshots/whitequark/forwarded_argument_with_restarg.txt b/snapshots/whitequark/forwarded_argument_with_restarg.txt index f71e0dcc59..d6ded91bf6 100644 --- a/snapshots/whitequark/forwarded_argument_with_restarg.txt +++ b/snapshots/whitequark/forwarded_argument_with_restarg.txt @@ -30,7 +30,7 @@ │ └── block: ∅ ├── body: │ @ StatementsNode (location: (1,22)-(1,38)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (1,22)-(1,38)) │ ├── flags: newline, ignore_visibility diff --git a/snapshots/whitequark/forwarded_kwrestarg.txt b/snapshots/whitequark/forwarded_kwrestarg.txt index 2ad0bf5c34..10216f1f6d 100644 --- a/snapshots/whitequark/forwarded_kwrestarg.txt +++ b/snapshots/whitequark/forwarded_kwrestarg.txt @@ -27,7 +27,7 @@ │ └── block: ∅ ├── body: │ @ StatementsNode (location: (1,13)-(1,20)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (1,13)-(1,20)) │ ├── flags: newline, ignore_visibility diff --git a/snapshots/whitequark/forwarded_kwrestarg_with_additional_kwarg.txt b/snapshots/whitequark/forwarded_kwrestarg_with_additional_kwarg.txt index 7cd0368be4..ac35659919 100644 --- a/snapshots/whitequark/forwarded_kwrestarg_with_additional_kwarg.txt +++ b/snapshots/whitequark/forwarded_kwrestarg_with_additional_kwarg.txt @@ -27,7 +27,7 @@ │ └── block: ∅ ├── body: │ @ StatementsNode (location: (1,13)-(1,36)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (1,13)-(1,36)) │ ├── flags: newline, ignore_visibility diff --git a/snapshots/whitequark/forwarded_restarg.txt b/snapshots/whitequark/forwarded_restarg.txt index 86db7c5cf1..1343ef99a8 100644 --- a/snapshots/whitequark/forwarded_restarg.txt +++ b/snapshots/whitequark/forwarded_restarg.txt @@ -27,7 +27,7 @@ │ └── block: ∅ ├── body: │ @ StatementsNode (location: (1,12)-(1,18)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (1,12)-(1,18)) │ ├── flags: newline, ignore_visibility diff --git a/snapshots/whitequark/kwoptarg_with_kwrestarg_and_forwarded_args.txt b/snapshots/whitequark/kwoptarg_with_kwrestarg_and_forwarded_args.txt index 8f8e1a0c8b..446a7cdc3b 100644 --- a/snapshots/whitequark/kwoptarg_with_kwrestarg_and_forwarded_args.txt +++ b/snapshots/whitequark/kwoptarg_with_kwrestarg_and_forwarded_args.txt @@ -34,7 +34,7 @@ │ └── block: ∅ ├── body: │ @ StatementsNode (location: (1,19)-(1,24)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (1,19)-(1,24)) │ ├── flags: newline, ignore_visibility diff --git a/snapshots/whitequark/numparam_outside_block.txt b/snapshots/whitequark/numparam_outside_block.txt index 7fd3bf6805..5cf6788b14 100644 --- a/snapshots/whitequark/numparam_outside_block.txt +++ b/snapshots/whitequark/numparam_outside_block.txt @@ -61,7 +61,7 @@ │ ├── superclass: ∅ │ ├── body: │ │ @ StatementsNode (location: (5,9)-(5,11)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ CallNode (location: (5,9)-(5,11)) │ │ ├── flags: newline, variable_call, ignore_visibility @@ -116,7 +116,7 @@ │ └── name: :A ├── body: │ @ StatementsNode (location: (9,10)-(9,12)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (9,10)-(9,12)) │ ├── flags: newline, variable_call, ignore_visibility diff --git a/snapshots/whitequark/parser_bug_525.txt b/snapshots/whitequark/parser_bug_525.txt index 2e84e9c336..6b602141dd 100644 --- a/snapshots/whitequark/parser_bug_525.txt +++ b/snapshots/whitequark/parser_bug_525.txt @@ -50,7 +50,7 @@ ├── parameters: ∅ ├── body: │ @ StatementsNode (location: (1,16)-(1,27)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (1,16)-(1,27)) │ ├── flags: newline, ignore_visibility diff --git a/snapshots/whitequark/pattern_matching_else.txt b/snapshots/whitequark/pattern_matching_else.txt index 98ec86b5e1..3ea34aa340 100644 --- a/snapshots/whitequark/pattern_matching_else.txt +++ b/snapshots/whitequark/pattern_matching_else.txt @@ -33,7 +33,7 @@ │ ├── else_keyword_loc: (1,17)-(1,21) = "else" │ ├── statements: │ │ @ StatementsNode (location: (1,23)-(1,24)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ IntegerNode (location: (1,23)-(1,24)) │ │ ├── flags: newline, static_literal, decimal diff --git a/snapshots/whitequark/resbody_list.txt b/snapshots/whitequark/resbody_list.txt index 70a488d9ae..e51c9b1f54 100644 --- a/snapshots/whitequark/resbody_list.txt +++ b/snapshots/whitequark/resbody_list.txt @@ -10,7 +10,7 @@ ├── begin_keyword_loc: (1,0)-(1,5) = "begin" ├── statements: │ @ StatementsNode (location: (1,7)-(1,11)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (1,7)-(1,11)) │ ├── flags: newline, variable_call, ignore_visibility diff --git a/snapshots/whitequark/resbody_list_mrhs.txt b/snapshots/whitequark/resbody_list_mrhs.txt index cf0736fb20..9ac74c4738 100644 --- a/snapshots/whitequark/resbody_list_mrhs.txt +++ b/snapshots/whitequark/resbody_list_mrhs.txt @@ -10,7 +10,7 @@ ├── begin_keyword_loc: (1,0)-(1,5) = "begin" ├── statements: │ @ StatementsNode (location: (1,7)-(1,11)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (1,7)-(1,11)) │ ├── flags: newline, variable_call, ignore_visibility diff --git a/snapshots/whitequark/resbody_list_var.txt b/snapshots/whitequark/resbody_list_var.txt index 95f81a8d49..d6f5b42dfc 100644 --- a/snapshots/whitequark/resbody_list_var.txt +++ b/snapshots/whitequark/resbody_list_var.txt @@ -10,7 +10,7 @@ ├── begin_keyword_loc: (1,0)-(1,5) = "begin" ├── statements: │ @ StatementsNode (location: (1,7)-(1,11)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (1,7)-(1,11)) │ ├── flags: newline, variable_call, ignore_visibility diff --git a/snapshots/whitequark/resbody_var.txt b/snapshots/whitequark/resbody_var.txt index f5f0bb0931..c68a112c64 100644 --- a/snapshots/whitequark/resbody_var.txt +++ b/snapshots/whitequark/resbody_var.txt @@ -10,7 +10,7 @@ │ ├── begin_keyword_loc: (1,0)-(1,5) = "begin" │ ├── statements: │ │ @ StatementsNode (location: (1,7)-(1,11)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ CallNode (location: (1,7)-(1,11)) │ │ ├── flags: newline, variable_call, ignore_visibility @@ -58,7 +58,7 @@ ├── begin_keyword_loc: (3,0)-(3,5) = "begin" ├── statements: │ @ StatementsNode (location: (3,7)-(3,11)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (3,7)-(3,11)) │ ├── flags: newline, variable_call, ignore_visibility diff --git a/snapshots/whitequark/rescue.txt b/snapshots/whitequark/rescue.txt index e16a2d4415..826f6ee198 100644 --- a/snapshots/whitequark/rescue.txt +++ b/snapshots/whitequark/rescue.txt @@ -10,7 +10,7 @@ ├── begin_keyword_loc: (1,0)-(1,5) = "begin" ├── statements: │ @ StatementsNode (location: (1,7)-(1,11)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (1,7)-(1,11)) │ ├── flags: newline, variable_call, ignore_visibility diff --git a/snapshots/whitequark/rescue_else.txt b/snapshots/whitequark/rescue_else.txt index 00fdfee598..085b9c79c9 100644 --- a/snapshots/whitequark/rescue_else.txt +++ b/snapshots/whitequark/rescue_else.txt @@ -10,7 +10,7 @@ ├── begin_keyword_loc: (1,0)-(1,5) = "begin" ├── statements: │ @ StatementsNode (location: (1,7)-(1,11)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (1,7)-(1,11)) │ ├── flags: newline, variable_call, ignore_visibility @@ -53,7 +53,7 @@ │ ├── else_keyword_loc: (1,26)-(1,30) = "else" │ ├── statements: │ │ @ StatementsNode (location: (1,32)-(1,35)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ CallNode (location: (1,32)-(1,35)) │ │ ├── flags: newline, variable_call, ignore_visibility diff --git a/snapshots/whitequark/rescue_else_ensure.txt b/snapshots/whitequark/rescue_else_ensure.txt index 813e881dc5..17741457ea 100644 --- a/snapshots/whitequark/rescue_else_ensure.txt +++ b/snapshots/whitequark/rescue_else_ensure.txt @@ -10,7 +10,7 @@ ├── begin_keyword_loc: (1,0)-(1,5) = "begin" ├── statements: │ @ StatementsNode (location: (1,7)-(1,11)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (1,7)-(1,11)) │ ├── flags: newline, variable_call, ignore_visibility @@ -73,7 +73,7 @@ │ ├── ensure_keyword_loc: (1,36)-(1,42) = "ensure" │ ├── statements: │ │ @ StatementsNode (location: (1,44)-(1,47)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ CallNode (location: (1,44)-(1,47)) │ │ ├── flags: newline, variable_call, ignore_visibility diff --git a/snapshots/whitequark/rescue_ensure.txt b/snapshots/whitequark/rescue_ensure.txt index c519b956ea..70a26269c7 100644 --- a/snapshots/whitequark/rescue_ensure.txt +++ b/snapshots/whitequark/rescue_ensure.txt @@ -10,7 +10,7 @@ ├── begin_keyword_loc: (1,0)-(1,5) = "begin" ├── statements: │ @ StatementsNode (location: (1,7)-(1,11)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (1,7)-(1,11)) │ ├── flags: newline, variable_call, ignore_visibility @@ -54,7 +54,7 @@ │ ├── ensure_keyword_loc: (1,26)-(1,32) = "ensure" │ ├── statements: │ │ @ StatementsNode (location: (1,34)-(1,37)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ CallNode (location: (1,34)-(1,37)) │ │ ├── flags: newline, variable_call, ignore_visibility diff --git a/snapshots/whitequark/rescue_without_begin_end.txt b/snapshots/whitequark/rescue_without_begin_end.txt index 773cc64bc9..2a572bb669 100644 --- a/snapshots/whitequark/rescue_without_begin_end.txt +++ b/snapshots/whitequark/rescue_without_begin_end.txt @@ -26,7 +26,7 @@ │ ├── begin_keyword_loc: ∅ │ ├── statements: │ │ @ StatementsNode (location: (1,9)-(1,12)) - │ │ ├── flags: ∅ + │ │ ├── flags: leading_semicolon │ │ └── body: (length: 1) │ │ └── @ CallNode (location: (1,9)-(1,12)) │ │ ├── flags: newline, variable_call, ignore_visibility diff --git a/snapshots/whitequark/trailing_forward_arg.txt b/snapshots/whitequark/trailing_forward_arg.txt index 7c4a04c73e..a686db7243 100644 --- a/snapshots/whitequark/trailing_forward_arg.txt +++ b/snapshots/whitequark/trailing_forward_arg.txt @@ -30,7 +30,7 @@ │ └── block: ∅ ├── body: │ @ StatementsNode (location: (1,20)-(1,35)) - │ ├── flags: ∅ + │ ├── flags: leading_semicolon │ └── body: (length: 1) │ └── @ CallNode (location: (1,20)-(1,35)) │ ├── flags: newline, ignore_visibility diff --git a/src/prism.c b/src/prism.c index 11e880880c..2c9b28970e 100644 --- a/src/prism.c +++ b/src/prism.c @@ -13775,20 +13775,27 @@ parse_targets_validate(pm_parser_t *parser, pm_node_t *first_target, pm_binding_ return result; } +/* Consume optional terminators before a statement list and record whether + * they include a leading empty statement. */ +static pm_node_flags_t +parse_statements_leading_terminators(pm_parser_t *parser) { + pm_node_flags_t flags = 0; + while (accept2(parser, PM_TOKEN_NEWLINE, PM_TOKEN_SEMICOLON)) { + if (parser->previous.type == PM_TOKEN_SEMICOLON) flags = PM_STATEMENTS_FLAGS_LEADING_SEMICOLON; + } + return flags; +} + /** * Parse a list of statements separated by newlines or semicolons. */ static pm_statements_node_t * parse_statements(pm_parser_t *parser, pm_context_t context, uint16_t depth) { - // First, skip past any optional terminators that might be at the beginning - // of the statements. - while (accept2(parser, PM_TOKEN_SEMICOLON, PM_TOKEN_NEWLINE)); + pm_statements_t body = { .flags = parse_statements_leading_terminators(parser) }; // If we have a terminator, then we can just return NULL. if (context_terminator(context, &parser->current)) return NULL; - pm_statements_t body = { 0 }; - // At this point we know we have at least one statement, and that it // immediately follows the current token. context_push(parser, context); @@ -15368,7 +15375,6 @@ parse_rescues(pm_parser_t *parser, size_t opening_newline_index, const pm_token_ opening = &else_keyword; parser_lex(parser); - accept2(parser, PM_TOKEN_NEWLINE, PM_TOKEN_SEMICOLON); pm_statements_node_t *else_statements = NULL; if (!match2(parser, PM_TOKEN_KEYWORD_END, PM_TOKEN_KEYWORD_ENSURE)) { @@ -15405,7 +15411,6 @@ parse_rescues(pm_parser_t *parser, size_t opening_newline_index, const pm_token_ pm_token_t ensure_keyword = parser->current; parser_lex(parser); - accept2(parser, PM_TOKEN_NEWLINE, PM_TOKEN_SEMICOLON); pm_statements_node_t *ensure_statements = NULL; if (!match1(parser, PM_TOKEN_KEYWORD_END)) { @@ -18573,7 +18578,7 @@ parse_class(pm_parser_t *parser, uint8_t flags, uint16_t depth) { /* The header uses the enclosing loop context. The body starts a new one. */ pm_do_loop_stack_push(parser, false); pm_parser_scope_push(parser, true); - if (!match2(parser, PM_TOKEN_NEWLINE, PM_TOKEN_SEMICOLON)) { + if (!accept2(parser, PM_TOKEN_NEWLINE, PM_TOKEN_SEMICOLON)) { pm_parser_err_token_type(parser, &parser->current, PM_ERR_EXPECT_SINGLETON_CLASS_DELIMITER, parser->current.type); } @@ -18629,8 +18634,6 @@ parse_class(pm_parser_t *parser, uint8_t flags, uint16_t depth) { if (inheritance_operator.start != NULL) { expect2(parser, PM_TOKEN_NEWLINE, PM_TOKEN_SEMICOLON, PM_ERR_CLASS_UNEXPECTED_END); - } else { - accept2(parser, PM_TOKEN_NEWLINE, PM_TOKEN_SEMICOLON); } pm_node_t *statements = NULL; @@ -18999,8 +19002,6 @@ parse_def(pm_parser_t *parser, uint8_t flags, uint16_t depth) { lex_state_set(parser, PM_LEX_STATE_BEG); parser->command_start = true; expect2(parser, PM_TOKEN_NEWLINE, PM_TOKEN_SEMICOLON, PM_ERR_DEF_PARAMS_TERM); - } else { - accept2(parser, PM_TOKEN_NEWLINE, PM_TOKEN_SEMICOLON); } pm_accepts_block_stack_push(parser, true); @@ -19101,7 +19102,6 @@ parse_module(pm_parser_t *parser, uint8_t flags, uint16_t depth) { } pm_parser_scope_push(parser, true); - accept2(parser, PM_TOKEN_SEMICOLON, PM_TOKEN_NEWLINE); pm_node_t *statements = NULL; if (!match4(parser, PM_TOKEN_KEYWORD_RESCUE, PM_TOKEN_KEYWORD_ENSURE, PM_TOKEN_KEYWORD_ELSE, PM_TOKEN_KEYWORD_END)) { @@ -19139,19 +19139,13 @@ parse_module(pm_parser_t *parser, uint8_t flags, uint16_t depth) { static pm_node_t * parse_parentheses(pm_parser_t *parser, pm_binding_power_t binding_power, uint8_t flags, uint16_t depth) { pm_token_t opening = parser->current; - pm_node_flags_t paren_flags = 0; pm_scratch_node_list_t current_block_exits = { 0 }; pm_scratch_node_list_t *previous_block_exits = push_block_exits(parser, ¤t_block_exits); parser_lex(parser); - while (true) { - if (accept1(parser, PM_TOKEN_SEMICOLON)) { - paren_flags |= PM_PARENTHESES_NODE_FLAGS_MULTIPLE_STATEMENTS; - } else if (!accept1(parser, PM_TOKEN_NEWLINE)) { - break; - } - } + pm_node_flags_t statement_flags = parse_statements_leading_terminators(parser); + pm_node_flags_t paren_flags = statement_flags ? PM_PARENTHESES_NODE_FLAGS_MULTIPLE_STATEMENTS : 0; /* If this is the end of the file or we match a right parenthesis, then we * have an empty parentheses node, and we can immediately return. */ @@ -19263,6 +19257,7 @@ parse_parentheses(pm_parser_t *parser, pm_binding_power_t binding_power, uint8_t * and we didn't return a multiple assignment node, then we can return a * regular parentheses node now. */ pm_statements_node_t *statements = pm_statements_node_single_create(parser, statement, true); + pm_node_flag_set(UP(statements), statement_flags); return UP(pm_parentheses_node_create(parser, &opening, UP(statements), &parser->previous, paren_flags)); } @@ -19272,7 +19267,7 @@ parse_parentheses(pm_parser_t *parser, pm_binding_power_t binding_power, uint8_t context_push(parser, PM_CONTEXT_PARENS); paren_flags |= PM_PARENTHESES_NODE_FLAGS_MULTIPLE_STATEMENTS; - pm_statements_t body = { 0 }; + pm_statements_t body = { .flags = statement_flags }; pm_statements_body_append(parser, &body, statement, true); /* If we didn't find a terminator and we didn't find a right parenthesis, @@ -19648,8 +19643,6 @@ parse_begin(pm_parser_t *parser, uint16_t depth) { size_t opening_newline_index = token_newline_index(parser); parser_lex(parser); - accept2(parser, PM_TOKEN_NEWLINE, PM_TOKEN_SEMICOLON); - pm_scratch_node_list_t current_block_exits = { 0 }; pm_scratch_node_list_t *previous_block_exits = push_block_exits(parser, ¤t_block_exits); pm_statements_node_t *begin_statements = NULL; @@ -19739,7 +19732,7 @@ parse_for(pm_parser_t *parser, uint8_t flags, uint16_t depth) { if (accept1(parser, PM_TOKEN_KEYWORD_DO_LOOP)) { do_keyword = parser->previous; } else { - if (!match2(parser, PM_TOKEN_SEMICOLON, PM_TOKEN_NEWLINE)) { + if (!accept2(parser, PM_TOKEN_SEMICOLON, PM_TOKEN_NEWLINE)) { pm_parser_err_token_type(parser, &parser->current, PM_ERR_EXPECT_FOR_DELIMITER, parser->current.type); } } diff --git a/test/prism/api/parse_test.rb b/test/prism/api/parse_test.rb index 0890a5e4bb..8b7b71b2c7 100644 --- a/test/prism/api/parse_test.rb +++ b/test/prism/api/parse_test.rb @@ -17,6 +17,33 @@ def test_parse_empty_string assert_equal [], result.value.statements.body end + def test_statements_leading_semicolon + assert_predicate Prism.parse(";x").value.statements, :leading_semicolon? + assert_predicate Prism.parse_statement('"#{;x}"').parts.first.statements, :leading_semicolon? + assert_predicate Prism.parse_statement("begin; x end").statements, :leading_semicolon? + refute_predicate Prism.parse_statement("begin x; end").statements, :leading_semicolon? + + # The first semicolon terminates the condition. + refute_predicate Prism.parse_statement("if x; y end").statements, :leading_semicolon? + assert_predicate Prism.parse_statement("if x;; y end").statements, :leading_semicolon? + end + + def test_parenthesized_statements_leading_semicolon + ["(;x)", "(\n; x; y)"].each do |source| + group = Prism.parse_statement(source) + assert_predicate group, :multiple_statements?, source + assert_predicate group.body, :leading_semicolon?, source + end + + ["(x;)", "(x; y)"].each do |source| + group = Prism.parse_statement(source) + assert_predicate group, :multiple_statements?, source + refute_predicate group.body, :leading_semicolon?, source + end + + refute_predicate Prism.parse_statement("(\nx\n)").body, :leading_semicolon? + end + def test_parse_takes_file_path filepath = "filepath.rb" result = Prism.parse("def foo; __FILE__; end", filepath: filepath) From f82a9126adc56d6c6a46be5d1b500f05cc4ad738 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Tue, 24 Nov 2026 12:09:03 -0500 Subject: [PATCH 2/8] Extract the encoding compatibility check from regexp interpolation --- include/prism/internal/encoding.h | 9 ++++++ include/prism/internal/node.h | 9 ++++++ src/regexp.c | 47 +++++++++++-------------------- 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/include/prism/internal/encoding.h b/include/prism/internal/encoding.h index 2f3c8bba92..da4efab9a3 100644 --- a/include/prism/internal/encoding.h +++ b/include/prism/internal/encoding.h @@ -91,6 +91,15 @@ pm_encoding_string(const pm_encoding_t *encoding) { return name; } +/* Join the encodings of two strings' non-ASCII bytes. NULL means all ASCII. */ +static PRISM_INLINE bool +pm_encoding_concat(const pm_encoding_t **left, const pm_encoding_t *right) { + if (right == NULL) return true; + if (*left != NULL && *left != right) return false; + *left = right; + return true; +} + /* * Return true if the encoding is a multibyte encoding. Every kind other than * single-byte and ASCII has characters that span more than one byte. diff --git a/include/prism/internal/node.h b/include/prism/internal/node.h index 9519a3ed01..5a43bb7c1c 100644 --- a/include/prism/internal/node.h +++ b/include/prism/internal/node.h @@ -8,6 +8,7 @@ #include "prism/arena.h" #include "prism/internal/assert.h" +#include "prism/internal/encoding.h" #include #include @@ -31,6 +32,14 @@ pm_location_join(pm_location_t left, pm_location_t right) { return location; } +/* A string literal uses the source encoding unless an escape forced one. */ +static PRISM_INLINE const pm_encoding_t * +pm_string_node_encoding(const pm_string_node_t *node, const pm_encoding_t *source_encoding) { + if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_FORCED_UTF8_ENCODING)) return PM_ENCODING_UTF_8_ENTRY; + if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_FORCED_BINARY_ENCODING)) return PM_ENCODING_ASCII_8BIT_ENTRY; + return source_encoding; +} + /* * Marks an interpolated string whose value is known while parsing. That is * true when every part is a string literal or another string with this flag, diff --git a/src/regexp.c b/src/regexp.c index 616bcecdd6..92265bf1c2 100644 --- a/src/regexp.c +++ b/src/regexp.c @@ -3571,11 +3571,11 @@ typedef struct { /** The bytes of the content. */ pm_buffer_t buffer; - /** The encoding of the content so far, or NULL before its first string. */ - const pm_encoding_t *encoding; + /** The first part determines the encoding while every part is ASCII. */ + const pm_encoding_t *first_encoding; - /** Whether the content so far is all ASCII. */ - bool ascii_only; + /** The encoding of the non-ASCII bytes encountered so far. */ + const pm_encoding_t *encoding; /** * The encoding of the first string that is not ASCII and is in another @@ -3585,15 +3585,11 @@ typedef struct { const pm_encoding_t *incompatible; } pm_regexp_joined_t; -/** - * Return the encoding of a string node. That is UTF-8 or binary if the string - * forces one, and the source encoding otherwise. - */ static const pm_encoding_t * -pm_regexp_string_encoding(const pm_parser_t *parser, const pm_string_node_t *node) { - if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_FORCED_UTF8_ENCODING)) return PM_ENCODING_UTF_8_ENTRY; - if (PM_NODE_FLAG_P(node, PM_STRING_FLAGS_FORCED_BINARY_ENCODING)) return PM_ENCODING_ASCII_8BIT_ENTRY; - return parser->encoding; +pm_regexp_joined_encoding(const pm_regexp_joined_t *joined) { + if (joined->encoding != NULL) return joined->encoding; + if (joined->first_encoding != NULL) return joined->first_encoding; + return joined->parser->encoding; } /** @@ -3603,18 +3599,9 @@ static void pm_regexp_join(pm_regexp_joined_t *joined, const uint8_t *source, size_t size, const pm_encoding_t *encoding) { pm_string_t content; pm_string_shared_init(&content, source, source + size); - bool ascii_only = pm_string_ascii_only_p(&content); - - if (joined->encoding == NULL) { - joined->encoding = encoding; - joined->ascii_only = ascii_only; - } else if (!ascii_only) { - if (joined->ascii_only) { - joined->encoding = encoding; - joined->ascii_only = false; - } else if (encoding != joined->encoding && joined->incompatible == NULL) { - joined->incompatible = encoding; - } + if (joined->first_encoding == NULL) joined->first_encoding = encoding; + if (joined->encoding != encoding && joined->incompatible == NULL && !pm_string_ascii_only_p(&content)) { + if (!pm_encoding_concat(&joined->encoding, encoding)) joined->incompatible = encoding; } pm_buffer_append_string(&joined->buffer, (const char *) source, size); @@ -3632,7 +3619,7 @@ pm_regexp_join_string_parts(pm_regexp_joined_t *joined, const pm_node_list_t *pa switch (PM_NODE_TYPE(part)) { case PM_STRING_NODE: { const pm_string_node_t *string = (const pm_string_node_t *) part; - pm_regexp_join(joined, pm_string_source(&string->unescaped), pm_string_length(&string->unescaped), pm_regexp_string_encoding(joined->parser, string)); + pm_regexp_join(joined, pm_string_source(&string->unescaped), pm_string_length(&string->unescaped), pm_string_node_encoding(string, joined->parser->encoding)); break; } case PM_INTERPOLATED_STRING_NODE: @@ -3688,7 +3675,7 @@ typedef struct { */ static bool pm_regexp_part_init(pm_regexp_part_t *part, pm_parser_t *parser, pm_node_flags_t flags, const pm_node_t *node) { - part->joined = (pm_regexp_joined_t) { .parser = parser, .buffer = { 0 }, .encoding = NULL, .ascii_only = true, .incompatible = NULL }; + part->joined = (pm_regexp_joined_t) { .parser = parser }; /* The lexer already reported invalid characters in the regular * expression's own text. Nothing has checked the value of a string that @@ -3711,7 +3698,7 @@ pm_regexp_part_init(pm_regexp_part_t *part, pm_parser_t *parser, pm_node_flags_t const pm_string_node_t *string = (const pm_string_node_t *) node; source = pm_string_source(&string->unescaped); size = pm_string_length(&string->unescaped); - encoding = pm_regexp_string_encoding(parser, string); + encoding = pm_string_node_encoding(string, parser->encoding); break; } case PM_INTERPOLATED_STRING_NODE: { @@ -3720,7 +3707,7 @@ pm_regexp_part_init(pm_regexp_part_t *part, pm_parser_t *parser, pm_node_flags_t source = (const uint8_t *) pm_buffer_value(&part->joined.buffer); size = pm_buffer_length(&part->joined.buffer); - encoding = (part->joined.encoding == NULL) ? parser->encoding : part->joined.encoding; + encoding = pm_regexp_joined_encoding(&part->joined); break; } default: @@ -3815,7 +3802,7 @@ pm_regexp_parse_interpolated(pm_parser_t *parser, const pm_interpolated_regular_ const pm_encoding_t *option_encoding; bool check_option = !is_static && pm_regexp_encoding_option(flags, &option_encoding) != '\0'; - pm_regexp_joined_t joined = { .parser = parser, .buffer = { 0 }, .encoding = NULL, .ascii_only = true, .incompatible = NULL }; + pm_regexp_joined_t joined = { .parser = parser }; bool valid = true; for (size_t index = 0; index < parts->size && (valid || check_option); index++) { @@ -3852,7 +3839,7 @@ pm_regexp_parse_interpolated(pm_parser_t *parser, const pm_interpolated_regular_ .parser = parser, .source = (const uint8_t *) pm_buffer_value(&joined.buffer), .size = pm_buffer_length(&joined.buffer), - .encoding = (joined.encoding == NULL) ? parser->encoding : joined.encoding, + .encoding = pm_regexp_joined_encoding(&joined), .flags = flags, .location = node->base.location, .unlexed = false From ed6f97248d3ce5c44e556709fddc6bf33eeac44e Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Tue, 24 Nov 2026 12:29:13 -0500 Subject: [PATCH 3/8] Reject adjacent string literals with incompatible encodings --- config.yml | 2 + src/prism.c | 44 ++++++++++++++++++++- test/prism/encoding/string_encoding_test.rb | 36 +++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/config.yml b/config.yml index 9dac53a72a..2eeba7c050 100644 --- a/config.yml +++ b/config.yml @@ -548,6 +548,8 @@ errors: message: "unexpected an `undef` at a non-statement position" - name: STRING_CONCATENATION message: "expected a string for concatenation" + - name: STRING_CONCATENATION_ENCODING + message: "string literal encodings differ ({slice} / {slice})" - name: STRING_INTERPOLATED_TERM message: "unterminated string; expected a closing delimiter for the interpolated string" - name: STRING_LITERAL_EOF diff --git a/src/prism.c b/src/prism.c index 2c9b28970e..1fdaa26edf 100644 --- a/src/prism.c +++ b/src/prism.c @@ -16890,6 +16890,28 @@ parse_strings_empty_content(const uint8_t *location) { return (pm_token_t) { .type = PM_TOKEN_STRING_CONTENT, .start = location, .end = location }; } +/* Check one operand in a run of adjacent plain string literals. */ +static bool +pm_string_concat_encoding(pm_parser_t *parser, const pm_node_t *part, const pm_encoding_t **left) { + if (!PM_NODE_TYPE_P(part, PM_STRING_NODE)) { + *left = NULL; + return true; + } + + const pm_string_node_t *string = (const pm_string_node_t *) part; + const pm_encoding_t *right = pm_string_node_encoding(string, parser->encoding); + if (*left == right || pm_string_ascii_only_p(&string->unescaped) || pm_encoding_concat(left, right)) return true; + + pm_diagnostic_list_append_slice_slice( + &parser->metadata_arena, &parser->error_list, part->location.start, part->location.length, + PM_ERR_STRING_CONCATENATION_ENCODING, pm_encoding_string(*left), pm_encoding_string(right) + ); + + /* The next operand starts a fresh run after an incompatible pair. */ + *left = NULL; + return false; +} + /** * Parse a set of strings that could be concatenated together. */ @@ -16897,6 +16919,9 @@ static PRISM_INLINE pm_node_t * parse_strings(pm_parser_t *parser, pm_node_t *current, bool accepts_label, uint16_t depth) { PRISM_ASSERT(parser->current.type == PM_TOKEN_STRING_BEGIN); bool concating = false; + bool valid_encoding = true; + + const pm_encoding_t *encoding = NULL; pm_node_list_t container = { 0 }; while (match1(parser, PM_TOKEN_STRING_BEGIN)) { @@ -17095,15 +17120,32 @@ parse_strings(pm_parser_t *parser, pm_node_t *current, bool accepts_label, uint1 } concating = true; + if (parser->encoding_changed) { + pm_string_concat_encoding(parser, current, &encoding); + } + pm_node_list_open_append(&parser->node_chunks, &container, current); } + if (parser->encoding_changed && !pm_string_concat_encoding(parser, node, &encoding)) { + valid_encoding = false; + } + pm_node_list_open_append(&parser->node_chunks, &container, node); } } if (concating) { - return UP(pm_interpolated_string_node_create(parser, NULL, &container, NULL)); + pm_node_t *node = UP(pm_interpolated_string_node_create(parser, NULL, &container, NULL)); + if (!valid_encoding) { + pm_node_flag_unset( + node, + PM_NODE_FLAG_STATIC_LITERAL | PM_INTERPOLATED_STRING_NODE_FLAGS_STATIC_VALUE | + PM_INTERPOLATED_STRING_NODE_FLAGS_FROZEN | PM_INTERPOLATED_STRING_NODE_FLAGS_MUTABLE + ); + } + + return node; } return current; diff --git a/test/prism/encoding/string_encoding_test.rb b/test/prism/encoding/string_encoding_test.rb index 42e4481fa9..ef9ab57b17 100644 --- a/test/prism/encoding/string_encoding_test.rb +++ b/test/prism/encoding/string_encoding_test.rb @@ -60,6 +60,42 @@ def test_first_lexed_token assert_equal Encoding::ASCII_8BIT, encoding end + def test_adjacent_literals_with_incompatible_encodings + source = "# coding: binary\nif false\n" + '"\u3042" "\xD4"' + "\nend" + message = "string literal encodings differ (UTF-8 / ASCII-8BIT)" + errors = Prism.parse(source).errors + + assert_equal [:string_concatenation_encoding], errors.map(&:type) + assert_equal :syntax, errors.first.level + assert_equal message, errors.first.message + assert_equal '"\xD4"', errors.first.location.slice + error = assert_raise(SyntaxError) { Prism.parse(source, raise_error: :plain) } + assert_include error.message, message + end + + def test_incompatible_adjacent_literals_inside_a_regexp + [true, false].each do |frozen| + result = Prism.parse('/#{"\u3042" "\xD4"}/', encoding: "ASCII-8BIT", frozen_string_literal: frozen) + assert_equal [:string_concatenation_encoding], result.errors.map(&:type) + + string = result.statement.parts.first.statements.body.first + refute_predicate string, :static_literal? + refute_predicate string, :frozen? + refute_predicate string, :mutable? + end + end + + def test_adjacent_literals_use_the_encoding_of_the_non_ascii_bytes + ['?\xD4 "ascii" "" "\u3042"', '"\xD4" "\u3042"', "<<~END \"\\u3042\"\n \\xD4\nEND"].each do |source| + errors = Prism.parse(source, encoding: "US-ASCII").errors + assert_equal ["string literal encodings differ (ASCII-8BIT / UTF-8)"], errors.map(&:message) + end + + ['?\u0061 "\xD4"', '"\u3042" "ascii"', '"ascii" "\u3042"', '"\xD4" "\xFF"'].each do |source| + assert_predicate Prism.parse(source, encoding: "ASCII-8BIT"), :success?, source + end + end + # The part of a literal after an interpolation is in the source encoding, # whatever encoding the literals inside the interpolation fixed. def test_part_after_interpolation From 5d601ae3970fd651bc44a8e2217f470117127ebb Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Tue, 24 Nov 2026 12:33:10 -0500 Subject: [PATCH 4/8] Respect leading semicolons when folding interpolation --- include/prism/internal/node.h | 10 ++++++++++ src/prism.c | 6 +++--- src/regexp.c | 6 ++---- test/prism/regexp_test.rb | 6 ++++++ test/prism/result/static_literal_flag_test.rb | 9 +++++++++ 5 files changed, 30 insertions(+), 7 deletions(-) diff --git a/include/prism/internal/node.h b/include/prism/internal/node.h index 5a43bb7c1c..fcdeb5f5e1 100644 --- a/include/prism/internal/node.h +++ b/include/prism/internal/node.h @@ -32,6 +32,16 @@ pm_location_join(pm_location_t left, pm_location_t right) { return location; } +/* + * Return the sole expression in a statements node, if there is one. + * A leading semicolon introduces an empty statement before the expression. + */ +static PRISM_INLINE pm_node_t * +pm_statements_node_single_expression(const pm_statements_node_t *node) { + if (node == NULL || node->body.size != 1 || PM_NODE_FLAG_P(node, PM_STATEMENTS_FLAGS_LEADING_SEMICOLON)) return NULL; + return node->body.nodes[0]; +} + /* A string literal uses the source encoding unless an escape forced one. */ static PRISM_INLINE const pm_encoding_t * pm_string_node_encoding(const pm_string_node_t *node, const pm_encoding_t *source_encoding) { diff --git a/src/prism.c b/src/prism.c index 1fdaa26edf..98a0cc7ef2 100644 --- a/src/prism.c +++ b/src/prism.c @@ -4821,7 +4821,7 @@ pm_interpolated_part_flags(pm_node_flags_t *flags, pm_node_t *part) { break; case PM_EMBEDDED_STATEMENTS_NODE: { pm_embedded_statements_node_t *cast = (pm_embedded_statements_node_t *) part; - pm_node_t *embedded = (cast->statements != NULL && cast->statements->body.size == 1) ? cast->statements->body.nodes[0] : NULL; + pm_node_t *embedded = pm_statements_node_single_expression(cast->statements); if (embedded == NULL) { // If there are no statements or more than one statement, then @@ -4979,11 +4979,11 @@ pm_interpolated_string_node_create(pm_parser_t *parser, const pm_token_t *openin break; case PM_EMBEDDED_STATEMENTS_NODE: { pm_embedded_statements_node_t *cast = (pm_embedded_statements_node_t *) part; - pm_node_t *embedded = (cast->statements != NULL && cast->statements->body.size == 1) ? cast->statements->body.nodes[0] : NULL; + pm_node_t *embedded = pm_statements_node_single_expression(cast->statements); if (embedded == NULL) { // If we're embedding multiple statements or no statements, then - // the string is not longer a static literal. + // the string is no longer a static literal. CLEAR_FLAGS(); } else if (PM_NODE_TYPE_P(embedded, PM_STRING_NODE)) { // If the embedded statement is a string, then we can make that diff --git a/src/regexp.c b/src/regexp.c index 92265bf1c2..4a9a01745e 100644 --- a/src/regexp.c +++ b/src/regexp.c @@ -3682,10 +3682,8 @@ pm_regexp_part_init(pm_regexp_part_t *part, pm_parser_t *parser, pm_node_flags_t * it interpolates yet. */ bool unlexed = false; if (PM_NODE_TYPE_P(node, PM_EMBEDDED_STATEMENTS_NODE)) { - const pm_statements_node_t *statements = ((const pm_embedded_statements_node_t *) node)->statements; - if (statements == NULL || statements->body.size != 1) return false; - - node = statements->body.nodes[0]; + node = pm_statements_node_single_expression(((const pm_embedded_statements_node_t *) node)->statements); + if (node == NULL) return false; unlexed = true; } diff --git a/test/prism/regexp_test.rb b/test/prism/regexp_test.rb index 1dcb1cbbb8..e752f1f898 100644 --- a/test/prism/regexp_test.rb +++ b/test/prism/regexp_test.rb @@ -368,6 +368,12 @@ def test_static_interpolation_of_interpolated_string assert_equal ["end pattern with unmatched parenthesis: /abc(/"], errors.map(&:message) end + def test_leading_semicolon_in_regexp_interpolation + assert_empty Prism.parse('/#{; "["}/').errors + assert_empty Prism.parse(%q{/#{; '\x'}/}).errors + assert_equal [:regexp_parse_error], Prism.parse(%q{/#{'\x';}/}).errors.map(&:type) + end + # The content that a regular expression's parts join to takes the encoding # of its parts that are not ASCII, as joining strings does, so a string in # UTF-8 makes the content UTF-8 in a source in another encoding. diff --git a/test/prism/result/static_literal_flag_test.rb b/test/prism/result/static_literal_flag_test.rb index c308ee2548..45f88e989d 100644 --- a/test/prism/result/static_literal_flag_test.rb +++ b/test/prism/result/static_literal_flag_test.rb @@ -21,6 +21,15 @@ def test_interpolated_strings_with_unknown_values end end + def test_statement_separators_in_interpolation + assert_static_literal(false, '"#{; "x"}"') + assert_static_literal(false, ':"#{; "x"}"') + assert_static_literal(false, '/#{; "x"}/') + assert_static_literal(false, '/#{"#{; "x"}"}/') + assert_static_literal(true, '"#{"x";}"') + assert_static_literal(true, '"#{' + "\n" + '"x"}"') + end + private def assert_static_literal(expected, source) From 4045289231ae664fddb7e138205a16348da5bc87 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Tue, 24 Nov 2026 12:36:51 -0500 Subject: [PATCH 5/8] Track encoding when appending string parts --- src/prism.c | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/prism.c b/src/prism.c index 98a0cc7ef2..07309bd4c6 100644 --- a/src/prism.c +++ b/src/prism.c @@ -16912,17 +16912,29 @@ pm_string_concat_encoding(pm_parser_t *parser, const pm_node_t *part, const pm_e return false; } +typedef struct { + pm_node_list_t list; + const pm_encoding_t *encoding; + bool invalid_encoding; +} pm_string_parts_t; + +/* Check each join as its right operand is appended, in source order. */ +static PRISM_INLINE void +pm_string_parts_append(pm_parser_t *parser, pm_string_parts_t *parts, pm_node_t *part) { + if (parser->encoding_changed && !pm_string_concat_encoding(parser, part, &parts->encoding)) { + parts->invalid_encoding = true; + } + + pm_node_list_open_append(&parser->node_chunks, &parts->list, part); +} + /** * Parse a set of strings that could be concatenated together. */ static PRISM_INLINE pm_node_t * parse_strings(pm_parser_t *parser, pm_node_t *current, bool accepts_label, uint16_t depth) { PRISM_ASSERT(parser->current.type == PM_TOKEN_STRING_BEGIN); - bool concating = false; - bool valid_encoding = true; - - const pm_encoding_t *encoding = NULL; - pm_node_list_t container = { 0 }; + pm_string_parts_t container = { 0 }; while (match1(parser, PM_TOKEN_STRING_BEGIN)) { pm_node_t *node = NULL; @@ -17114,30 +17126,21 @@ parse_strings(pm_parser_t *parser, pm_node_t *current, bool accepts_label, uint1 // If we haven't already started the parts of our container for // concatenation, we'll do that now. - if (!concating) { + if (container.list.size == 0) { if (!PM_NODE_TYPE_P(current, PM_STRING_NODE) && !PM_NODE_TYPE_P(current, PM_INTERPOLATED_STRING_NODE)) { pm_parser_err_node(parser, current, PM_ERR_STRING_CONCATENATION); } - concating = true; - if (parser->encoding_changed) { - pm_string_concat_encoding(parser, current, &encoding); - } - - pm_node_list_open_append(&parser->node_chunks, &container, current); - } - - if (parser->encoding_changed && !pm_string_concat_encoding(parser, node, &encoding)) { - valid_encoding = false; + pm_string_parts_append(parser, &container, current); } - pm_node_list_open_append(&parser->node_chunks, &container, node); + pm_string_parts_append(parser, &container, node); } } - if (concating) { - pm_node_t *node = UP(pm_interpolated_string_node_create(parser, NULL, &container, NULL)); - if (!valid_encoding) { + if (container.list.size > 0) { + pm_node_t *node = UP(pm_interpolated_string_node_create(parser, NULL, &container.list, NULL)); + if (container.invalid_encoding) { pm_node_flag_unset( node, PM_NODE_FLAG_STATIC_LITERAL | PM_INTERPOLATED_STRING_NODE_FLAGS_STATIC_VALUE | From 5f90fff38627aad13b8a8a6329eb2bd626b809ba Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Tue, 24 Nov 2026 13:01:36 -0500 Subject: [PATCH 6/8] Check literal encodings inside interpolated strings --- src/prism.c | 94 ++++++++++----------- test/prism/encoding/string_encoding_test.rb | 31 +++++++ 2 files changed, 77 insertions(+), 48 deletions(-) diff --git a/src/prism.c b/src/prism.c index 07309bd4c6..81649282b2 100644 --- a/src/prism.c +++ b/src/prism.c @@ -16890,15 +16890,27 @@ parse_strings_empty_content(const uint8_t *location) { return (pm_token_t) { .type = PM_TOKEN_STRING_CONTENT, .start = location, .end = location }; } -/* Check one operand in a run of adjacent plain string literals. */ +/* Check one operand in a run of literal string parts. */ static bool pm_string_concat_encoding(pm_parser_t *parser, const pm_node_t *part, const pm_encoding_t **left) { - if (!PM_NODE_TYPE_P(part, PM_STRING_NODE)) { + const pm_node_t *value = part; + if (PM_NODE_TYPE_P(part, PM_EMBEDDED_STATEMENTS_NODE)) { + value = pm_statements_node_single_expression(((const pm_embedded_statements_node_t *) part)->statements); + + /* A plain begin/end around a literal also folds into the string. */ + while (value != NULL && PM_NODE_TYPE_P(value, PM_BEGIN_NODE)) { + const pm_begin_node_t *begin = (const pm_begin_node_t *) value; + if (begin->rescue_clause != NULL || begin->else_clause != NULL || begin->ensure_clause != NULL) break; + value = pm_statements_node_single_expression(begin->statements); + } + } + + if (value == NULL || !PM_NODE_TYPE_P(value, PM_STRING_NODE)) { *left = NULL; return true; } - const pm_string_node_t *string = (const pm_string_node_t *) part; + const pm_string_node_t *string = (const pm_string_node_t *) value; const pm_encoding_t *right = pm_string_node_encoding(string, parser->encoding); if (*left == right || pm_string_ascii_only_p(&string->unescaped) || pm_encoding_concat(left, right)) return true; @@ -16928,6 +16940,21 @@ pm_string_parts_append(pm_parser_t *parser, pm_string_parts_t *parts, pm_node_t pm_node_list_open_append(&parser->node_chunks, &parts->list, part); } +static pm_node_t * +pm_string_parts_to_string(pm_parser_t *parser, const pm_token_t *opening, pm_string_parts_t *parts, const pm_token_t *closing) { + pm_node_t *node = UP(pm_interpolated_string_node_create(parser, opening, &parts->list, closing)); + + if (parts->invalid_encoding) { + pm_node_flag_unset( + node, + PM_NODE_FLAG_STATIC_LITERAL | PM_INTERPOLATED_STRING_NODE_FLAGS_STATIC_VALUE | + PM_INTERPOLATED_STRING_NODE_FLAGS_FROZEN | PM_INTERPOLATED_STRING_NODE_FLAGS_MUTABLE + ); + } + + return node; +} + /** * Parse a set of strings that could be concatenated together. */ @@ -16938,6 +16965,7 @@ parse_strings(pm_parser_t *parser, pm_node_t *current, bool accepts_label, uint1 while (match1(parser, PM_TOKEN_STRING_BEGIN)) { pm_node_t *node = NULL; + pm_node_t *first_part = NULL; // Here we have found a string literal. We'll parse it and add it to // the list of strings. @@ -17056,52 +17084,31 @@ parse_strings(pm_parser_t *parser, pm_node_t *current, bool accepts_label, uint1 node = UP(pm_symbol_node_create_unescaped(parser, &opening, &content, &parser->previous, &unescaped, parse_symbol_encoding(parser, explicit_encoding, &content, &unescaped, true))); if (!label_allowed) pm_parser_err_node(parser, node, PM_ERR_UNEXPECTED_LABEL); } else { - // If we get here, then we have interpolation so we'll need - // to create a string or symbol node with interpolation. - pm_node_list_t parts = { 0 }; - pm_node_t *part = UP(pm_string_node_create_unescaped(parser, NULL, &parser->previous, NULL, &unescaped)); - pm_node_flag_set(part, parse_unescaped_encoding(parser, explicit_encoding)); - pm_node_list_open_append(&parser->node_chunks, &parts, part); - - while (!match3(parser, PM_TOKEN_STRING_END, PM_TOKEN_LABEL_END, PM_TOKEN_EOF)) { - if ((part = parse_string_part(parser, (uint16_t) (depth + 1))) != NULL) { - pm_node_list_open_append(&parser->node_chunks, &parts, part); - } - } - - if (accept1(parser, PM_TOKEN_LABEL_END)) { - node = UP(pm_interpolated_symbol_node_create(parser, &opening, &parts, &parser->previous)); - if (!label_allowed) pm_parser_err_node(parser, node, PM_ERR_UNEXPECTED_LABEL); - } else if (match1(parser, PM_TOKEN_EOF)) { - pm_parser_err_token(parser, &opening, PM_ERR_STRING_INTERPOLATED_TERM); - node = UP(pm_interpolated_string_node_create(parser, &opening, &parts, &parser->current)); - } else { - expect1(parser, PM_TOKEN_STRING_END, PM_ERR_STRING_INTERPOLATED_TERM); - node = UP(pm_interpolated_string_node_create(parser, &opening, &parts, &parser->previous)); - } + first_part = UP(pm_string_node_create_unescaped(parser, NULL, &parser->previous, NULL, &unescaped)); + pm_node_flag_set(first_part, parse_unescaped_encoding(parser, explicit_encoding)); } - } else { - // If we get here, then the first part of the string is not plain - // string content, in which case we need to parse the string as an - // interpolated string. - pm_node_list_t parts = { 0 }; - pm_node_t *part; + } + + if (node == NULL) { + /* Interpolated strings can start with literal text or with an interpolation. */ + pm_string_parts_t parts = { 0 }; + if (first_part != NULL) pm_string_parts_append(parser, &parts, first_part); while (!match3(parser, PM_TOKEN_STRING_END, PM_TOKEN_LABEL_END, PM_TOKEN_EOF)) { - if ((part = parse_string_part(parser, (uint16_t) (depth + 1))) != NULL) { - pm_node_list_open_append(&parser->node_chunks, &parts, part); - } + pm_node_t *part = parse_string_part(parser, (uint16_t) (depth + 1)); + if (part != NULL) pm_string_parts_append(parser, &parts, part); } if (accept1(parser, PM_TOKEN_LABEL_END)) { - node = UP(pm_interpolated_symbol_node_create(parser, &opening, &parts, &parser->previous)); + node = UP(pm_interpolated_symbol_node_create(parser, &opening, &parts.list, &parser->previous)); + if (parts.invalid_encoding) pm_node_flag_unset(node, PM_NODE_FLAG_STATIC_LITERAL); if (!label_allowed) pm_parser_err_node(parser, node, PM_ERR_UNEXPECTED_LABEL); } else if (match1(parser, PM_TOKEN_EOF)) { pm_parser_err_token(parser, &opening, PM_ERR_STRING_INTERPOLATED_TERM); - node = UP(pm_interpolated_string_node_create(parser, &opening, &parts, &parser->current)); + node = pm_string_parts_to_string(parser, &opening, &parts, &parser->current); } else { expect1(parser, PM_TOKEN_STRING_END, PM_ERR_STRING_INTERPOLATED_TERM); - node = UP(pm_interpolated_string_node_create(parser, &opening, &parts, &parser->previous)); + node = pm_string_parts_to_string(parser, &opening, &parts, &parser->previous); } } @@ -17139,16 +17146,7 @@ parse_strings(pm_parser_t *parser, pm_node_t *current, bool accepts_label, uint1 } if (container.list.size > 0) { - pm_node_t *node = UP(pm_interpolated_string_node_create(parser, NULL, &container.list, NULL)); - if (container.invalid_encoding) { - pm_node_flag_unset( - node, - PM_NODE_FLAG_STATIC_LITERAL | PM_INTERPOLATED_STRING_NODE_FLAGS_STATIC_VALUE | - PM_INTERPOLATED_STRING_NODE_FLAGS_FROZEN | PM_INTERPOLATED_STRING_NODE_FLAGS_MUTABLE - ); - } - - return node; + return pm_string_parts_to_string(parser, NULL, &container, NULL); } return current; diff --git a/test/prism/encoding/string_encoding_test.rb b/test/prism/encoding/string_encoding_test.rb index ef9ab57b17..f40abbb64f 100644 --- a/test/prism/encoding/string_encoding_test.rb +++ b/test/prism/encoding/string_encoding_test.rb @@ -96,6 +96,37 @@ def test_adjacent_literals_use_the_encoding_of_the_non_ascii_bytes end end + def test_incompatible_encodings_in_string_interpolation + [ + '"\u3042#{"\xD4"}"', + '"#{"\u3042"}#{"\xD4";}"', + '"\u3042#{begin "\xD4" end}"', + '/#{"\u3042#{"\xD4"}"}/', + '{"\u3042#{"\xD4"}": 1}' + ].each do |source| + errors = Prism.parse(source, encoding: "ASCII-8BIT").errors + assert_equal [:string_concatenation_encoding], errors.map(&:type), source + end + + errors = Prism.parse('"#{"\xD4"}\u3042"', encoding: "ASCII-8BIT").errors + assert_equal ["string literal encodings differ (ASCII-8BIT / UTF-8)"], errors.map(&:message) + assert_equal '\u3042', errors.first.location.slice + end + + def test_interpolation_encoding_errors_in_source_order + errors = Prism.parse('"\u3042#{"\xD4"}#{"\xD4" "\u3042"}"', encoding: "ASCII-8BIT").errors + assert_equal ["string literal encodings differ (UTF-8 / ASCII-8BIT)", "string literal encodings differ (ASCII-8BIT / UTF-8)"], errors.map(&:message) + end + + def test_dynamic_interpolation_separates_literal_encodings + ['"\u3042#{; "\xD4"}"', '"\u3042#{x}#{"\xD4"}"', '"\u3042#$x#{"\xD4"}"'].each do |source| + assert_predicate Prism.parse(source, encoding: "ASCII-8BIT"), :success?, source + end + + errors = Prism.parse('"#{x}#{"\u3042"}#{"\xD4"}"', encoding: "ASCII-8BIT").errors + assert_equal [:string_concatenation_encoding], errors.map(&:type) + end + # The part of a literal after an interpolation is in the source encoding, # whatever encoding the literals inside the interpolation fixed. def test_part_after_interpolation From 6ef96f1601d8181471cb68f0a0abcf660804bce1 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Tue, 24 Nov 2026 13:27:22 -0500 Subject: [PATCH 7/8] Check encoding where adjacent string literals meet --- src/prism.c | 35 ++++++++++++++++++--- test/prism/encoding/string_encoding_test.rb | 12 +++++++ 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/prism.c b/src/prism.c index 81649282b2..6ec7ef07b1 100644 --- a/src/prism.c +++ b/src/prism.c @@ -16932,14 +16932,25 @@ typedef struct { /* Check each join as its right operand is appended, in source order. */ static PRISM_INLINE void -pm_string_parts_append(pm_parser_t *parser, pm_string_parts_t *parts, pm_node_t *part) { - if (parser->encoding_changed && !pm_string_concat_encoding(parser, part, &parts->encoding)) { - parts->invalid_encoding = true; +pm_string_parts_append_operand(pm_parser_t *parser, pm_string_parts_t *parts, pm_node_t *part, const pm_node_t *first, const pm_node_t *last) { + if (parser->encoding_changed) { + if (first != NULL && !pm_string_concat_encoding(parser, first, &parts->encoding)) { + parts->invalid_encoding = true; + } else if (first == NULL || first != last) { + /* Interpolation separates the leading text from the trailing text. */ + parts->encoding = NULL; + if (last != NULL) pm_string_concat_encoding(parser, last, &parts->encoding); + } } pm_node_list_open_append(&parser->node_chunks, &parts->list, part); } +static PRISM_INLINE void +pm_string_parts_append(pm_parser_t *parser, pm_string_parts_t *parts, pm_node_t *part) { + pm_string_parts_append_operand(parser, parts, part, part, part); +} + static pm_node_t * pm_string_parts_to_string(pm_parser_t *parser, const pm_token_t *opening, pm_string_parts_t *parts, const pm_token_t *closing) { pm_node_t *node = UP(pm_interpolated_string_node_create(parser, opening, &parts->list, closing)); @@ -16962,6 +16973,7 @@ static PRISM_INLINE pm_node_t * parse_strings(pm_parser_t *parser, pm_node_t *current, bool accepts_label, uint16_t depth) { PRISM_ASSERT(parser->current.type == PM_TOKEN_STRING_BEGIN); pm_string_parts_t container = { 0 }; + const pm_node_t *current_tail = current; while (match1(parser, PM_TOKEN_STRING_BEGIN)) { pm_node_t *node = NULL; @@ -17089,6 +17101,7 @@ parse_strings(pm_parser_t *parser, pm_node_t *current, bool accepts_label, uint1 } } + const pm_node_t *last_part = node; if (node == NULL) { /* Interpolated strings can start with literal text or with an interpolation. */ pm_string_parts_t parts = { 0 }; @@ -17099,6 +17112,13 @@ parse_strings(pm_parser_t *parser, pm_node_t *current, bool accepts_label, uint1 if (part != NULL) pm_string_parts_append(parser, &parts, part); } + /* An incompatible pair discarded the leading text during recovery. */ + if (parts.invalid_encoding) first_part = NULL; + if (parts.encoding != NULL && parts.list.size > 0) { + const pm_node_t *last = pm_node_list_open_last(&parts.list); + if (PM_NODE_TYPE_P(last, PM_STRING_NODE)) last_part = last; + } + if (accept1(parser, PM_TOKEN_LABEL_END)) { node = UP(pm_interpolated_symbol_node_create(parser, &opening, &parts.list, &parser->previous)); if (parts.invalid_encoding) pm_node_flag_unset(node, PM_NODE_FLAG_STATIC_LITERAL); @@ -17110,6 +17130,8 @@ parse_strings(pm_parser_t *parser, pm_node_t *current, bool accepts_label, uint1 expect1(parser, PM_TOKEN_STRING_END, PM_ERR_STRING_INTERPOLATED_TERM); node = pm_string_parts_to_string(parser, &opening, &parts, &parser->previous); } + } else { + first_part = node; } if (current == NULL) { @@ -17123,12 +17145,15 @@ parse_strings(pm_parser_t *parser, pm_node_t *current, bool accepts_label, uint1 // If we don't already have a node, then it's fine and we can just // set the result to be the node we just parsed. current = node; + current_tail = last_part; } else { // Otherwise we need to check the type of the node we just parsed. // If it cannot be concatenated with the previous node, then we'll // need to add a syntax error. if (!PM_NODE_TYPE_P(node, PM_STRING_NODE) && !PM_NODE_TYPE_P(node, PM_INTERPOLATED_STRING_NODE)) { pm_parser_err_node(parser, node, PM_ERR_STRING_CONCATENATION); + first_part = NULL; + last_part = NULL; } // If we haven't already started the parts of our container for @@ -17138,10 +17163,10 @@ parse_strings(pm_parser_t *parser, pm_node_t *current, bool accepts_label, uint1 pm_parser_err_node(parser, current, PM_ERR_STRING_CONCATENATION); } - pm_string_parts_append(parser, &container, current); + pm_string_parts_append_operand(parser, &container, current, current_tail, current_tail); } - pm_string_parts_append(parser, &container, node); + pm_string_parts_append_operand(parser, &container, node, first_part, last_part); } } diff --git a/test/prism/encoding/string_encoding_test.rb b/test/prism/encoding/string_encoding_test.rb index f40abbb64f..8854b18930 100644 --- a/test/prism/encoding/string_encoding_test.rb +++ b/test/prism/encoding/string_encoding_test.rb @@ -96,6 +96,18 @@ def test_adjacent_literals_use_the_encoding_of_the_non_ascii_bytes end end + def test_encoding_at_adjacent_literal_text + source = 'if false; "#{x}\u3042" "\xD4#{x}"; end' + errors = Prism.parse(source, encoding: "ASCII-8BIT").errors + assert_equal [:string_concatenation_encoding], errors.map(&:type) + assert_equal :syntax, errors.first.level + assert_equal "string literal encodings differ (UTF-8 / ASCII-8BIT)", errors.first.message + + ['"\u3042#{x}" "\xD4"', '"\u3042" "#{x}\xD4"'].each do |source| + assert_predicate Prism.parse(source, encoding: "ASCII-8BIT"), :success?, source + end + end + def test_incompatible_encodings_in_string_interpolation [ '"\u3042#{"\xD4"}"', From d4fd5651220ed1e8f9155a7c578d7be13e66b21e Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Tue, 24 Nov 2026 13:36:50 -0500 Subject: [PATCH 8/8] Use leading semicolon for ripper translation --- lib/prism/translation/ripper.rb | 96 +++++++++------------------------ test/prism/ruby/ripper_test.rb | 14 +++++ 2 files changed, 40 insertions(+), 70 deletions(-) diff --git a/lib/prism/translation/ripper.rb b/lib/prism/translation/ripper.rb index 6c2dc4c04e..998d75ee35 100644 --- a/lib/prism/translation/ripper.rb +++ b/lib/prism/translation/ripper.rb @@ -1029,7 +1029,7 @@ def visit_begin_node(node) on_kw("begin") end - clauses = visit_begin_node_clauses(node.begin_keyword_loc, node, false) + clauses = visit_begin_node_clauses(node) if node.end_keyword_loc bounds(node.end_keyword_loc) @@ -1041,17 +1041,9 @@ def visit_begin_node(node) end # Visit the clauses of a begin node to form an on_bodystmt call. - private def visit_begin_node_clauses(location, node, allow_newline) - statements = - if node.statements.nil? - on_stmts_add(on_stmts_new, on_void_stmt) - else - body = node.statements.body - body = [nil, *body] if void_stmt?(location, node.statements.body[0].location, allow_newline) - - bounds(node.statements.location) - visit_statements_node_body(body) - end + private def visit_begin_node_clauses(node, class_name_loc = nil) + bounds(node.statements.location) if node.statements + statements = visit_statements_node_body(node.statements, class_name_loc) rescue_clause = visit(node.rescue_clause) else_clause = @@ -1059,17 +1051,8 @@ def visit_begin_node(node) bounds(else_clause_node.else_keyword_loc) on_kw("else") - else_statements = - if else_clause_node.statements.nil? - [nil] - else - body = else_clause_node.statements.body - body = [nil, *body] if void_stmt?(else_clause_node.else_keyword_loc, else_clause_node.statements.body[0].location, allow_newline) - body - end - bounds(else_clause_node.location) - visit_statements_node_body(else_statements) + visit_statements_node_body(else_clause_node.statements) end ensure_clause = visit(node.ensure_clause) @@ -1080,19 +1063,19 @@ def visit_begin_node(node) # Visit the body of a structure that can have either a set of statements # or statements wrapped in rescue/else/ensure. private def visit_body_node(location, node, allow_newline = false) + class_name_loc = location if allow_newline + case node when nil bounds(location) - on_bodystmt(visit_statements_node_body([nil]), nil, nil, nil) + on_bodystmt(visit_statements_node_body(nil), nil, nil, nil) when StatementsNode - body = [*node.body] - body = [nil, *body] if void_stmt?(location, body[0].location, allow_newline) - stmts = visit_statements_node_body(body) + stmts = visit_statements_node_body(node, class_name_loc) bounds(node.body.first.location) on_bodystmt(stmts, nil, nil, nil) when BeginNode - visit_begin_node_clauses(location, node, allow_newline) + visit_begin_node_clauses(node, class_name_loc) else raise end @@ -1134,9 +1117,7 @@ def visit_block_node(node) bounds(node.location) braces ? stmts : on_bodystmt(stmts, nil, nil, nil) when StatementsNode - stmts = node.body.body - stmts = [nil, *stmts] if void_stmt?(node.parameters&.location || node.opening_loc, node.body.location, false) - stmts = visit_statements_node_body(stmts) + stmts = visit_statements_node_body(node.body) bounds(node.body.location) braces ? stmts : on_bodystmt(stmts, nil, nil, nil) @@ -2048,16 +2029,7 @@ def visit_else_node(node) bounds(node.else_keyword_loc) on_kw("else") - statements = - if node.statements.nil? - [nil] - else - body = node.statements.body - body = [nil, *body] if void_stmt?(node.else_keyword_loc, node.statements.body[0].location, false) - body - end - - else_statements = visit_statements_node_body(statements) + else_statements = visit_statements_node_body(node.statements) bounds(node.end_keyword_loc) on_kw("end") @@ -2103,16 +2075,7 @@ def visit_ensure_node(node) bounds(node.ensure_keyword_loc) on_kw("ensure") - statements = - if node.statements.nil? - [nil] - else - body = node.statements.body - body = [nil, *body] if void_stmt?(node.ensure_keyword_loc, body[0].location, false) - body - end - - statements = visit_statements_node_body(statements) + statements = visit_statements_node_body(node.statements) bounds(node.location) on_ensure(statements) @@ -2887,9 +2850,7 @@ def visit_lambda_node(node) bounds(node.location) braces ? stmts : on_bodystmt(stmts, nil, nil, nil) when StatementsNode - stmts = node.body.body - stmts = [nil, *stmts] if void_stmt?(node.parameters&.location || node.opening_loc, node.body.location, false) - stmts = visit_statements_node_body(stmts) + stmts = visit_statements_node_body(node.body) bounds(node.body.location) braces ? stmts : on_bodystmt(stmts, nil, nil, nil) @@ -3381,9 +3342,7 @@ def visit_pre_execution_node(node) # The top-level program node. def visit_program_node(node) - body = node.statements.body - body = [nil] if body.empty? - statements = visit_statements_node_body(body) + statements = visit_statements_node_body(node.statements) bounds(node.location) on_program(statements) @@ -3647,17 +3606,20 @@ def visit_splat_node(node) # A list of statements. def visit_statements_node(node) bounds(node.location) - visit_statements_node_body(node.body) + visit_statements_node_body(node) end - # Visit the list of statements of a statements node. We support nil - # statements in the list. This would normally not be allowed by the - # structure of the prism parse tree, but we manually add them here so that - # we can mirror Ripper's void stmt. - private def visit_statements_node_body(body) - body.inject(on_stmts_new) do |stmts, stmt| - on_stmts_add(stmts, stmt.nil? ? on_void_stmt : visit(stmt)) + # Ripper emits one void_stmt for an empty list or a leading semicolon. + # A newline after a class or module name can also produce a void_stmt. + private def visit_statements_node_body(node, class_name_loc = nil) + stmts = on_stmts_new + return on_stmts_add(stmts, on_void_stmt) if node.nil? || node.body.empty? + + if node.leading_semicolon? || (class_name_loc && class_name_loc.end_line < node.start_line) + stmts = on_stmts_add(stmts, on_void_stmt) end + + node.body.inject(stmts) { |list, stmt| on_stmts_add(list, visit(stmt)) } end # "foo" @@ -4122,12 +4084,6 @@ def trailing_comma?(left, right) source.byteslice(left.end_offset...right.start_offset).include?(",") end - # Returns true if there is a semicolon between the two locations. - def void_stmt?(left, right, allow_newline) - pattern = allow_newline ? /[;\n]/ : /;/ - source.byteslice(left.end_offset...right.start_offset).match?(pattern) - end - # Visit the string content of a particular node. This method is used to # split into the various token types. def visit_token(token, allow_keywords = true) diff --git a/test/prism/ruby/ripper_test.rb b/test/prism/ruby/ripper_test.rb index a8c45a12e3..97dccbf81a 100644 --- a/test/prism/ruby/ripper_test.rb +++ b/test/prism/ruby/ripper_test.rb @@ -141,6 +141,20 @@ class RipperTest < TestCase define_method("#{fixture.test_name}_lex") { assert_ripper_lex(fixture.read) } end + def test_leading_void_stmt + [ + ";;x; y;", + "(;x)", + "(\nx;)", + '"#{;x}"', + "if x; y end", + "if x;; y end", + "foo { # ;\n x }", + "class C < D; x end", + "class C\n x; rescue; y; else\n z end" + ].each { |source| assert_ripper_sexp_raw(source) } + end + def test_lex_ignored_missing_heredoc_end ["", "-", "~"].each do |type| source = "<<#{type}FOO\n"