From 7ee866b2aa886e80b74030a174f4c0462a78accb Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 21 Mar 2023 13:36:43 -0700 Subject: [PATCH] Add Oxford Comma support Ruby has regular commas: ```ruby [a, b, c] ``` Ruby has trailing commas: ```ruby [ a, b, c, ] ``` But I think both of these are hard to read compared to the Oxford comma. We should introduce the Oxford comma so that code is easier to read: For example: ```ruby def foo a, b, and c [a, b, and c] end p foo(1, 2, and 3) ``` As an added bonus, this feature also makes specifying musical beats quite natural, for example: ``` [1, and 2, and 3, and 4] ``` Just to make sure that everyone is pleased, you are allowed to mix the Oxford comma with trailing commas like this: ``` [ 1, and 2, and 3, and 4, ] ``` --- parse.y | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/parse.y b/parse.y index 4ef5b88407..090006520c 100644 --- a/parse.y +++ b/parse.y @@ -3149,6 +3149,13 @@ args : arg_value /*% %*/ /*% ripper: args_add!($1, $3) %*/ } + | args ',' keyword_and arg_value + { + /*%%%*/ + $$ = last_arg_append(p, $1, $4, &@$); + /*% %*/ + /*% ripper: args_add!($1, $4) %*/ + } | args ',' tSTAR arg_value { /*%%%*/ @@ -5693,6 +5700,16 @@ f_arg : f_arg_item /*% %*/ /*% ripper: rb_ary_push($1, get_value($3)) %*/ } + | f_arg ',' keyword_and f_arg_item + { + /*%%%*/ + $$ = $1; + $$->nd_plen++; + $$->nd_next = block_append(p, $$->nd_next, $4->nd_next); + rb_discard_node(p, $4); + /*% %*/ + /*% ripper: rb_ary_push($1, get_value($4)) %*/ + } ; -- 2.37.1