Allow args and members delimited by \n inside of (), {}, and [] #402

Closed
wants to merge 8 commits into
from

Conversation

Projects
None yet
8 participants

This allows for syntax such as (notice the missing commas):

some_hash = {
  :foo => 'bar'
  :bar => 'foo'
  :baz => {
    :qux => 'quux'
    :corge => 'grault'
  }
}

This does not break existing hashes. You are able to mix-and-match old and new syntax, e.g.,

some_hash = {
  :foo => 'bar'
  :bar => 'foo',
  :baz => {
    :foo => 'bar'
    :bar => 'foo',
    :qux => 'quux'
    :corge => 'grault',
  },
}

Additionally, this only affects hashes inside of curly braces ({ and }).

Update 2013-09-30

This now also applies to arrays inside of square brackets ([ and ]), e.g.,

some_array = [
  :foo
  :bar
  [
    'baz'
    :qux
  ]
  {
    :quux => 'grault'
    'garply' => [
      'waldo' => :fred
    ]
    :plugh => 'xyzzy'
  }
]

Update 2013-10-01

This now applies to method call arguments. This enables syntax such as

Hash[
  :foo, 'bar'
  :baz, 'qux'
]

(becomes { :foo => "bar", :baz => "qux" })

as well as

puts("this is line one"
     "this is line two"
     "this is line three")
Adam Dunson Allow hash values delimited by \n inside of {}
This allows for syntax such as:

some_hash = {
  :foo => 'bar'
  :bar => 'foo'
}
dbebf67

jlorich commented Sep 25, 2013

While this seems to be a mostly pointless change, and I'm sure there there are many issues it could bring up, I think the modified syntax goes well with ruby's overall feel.

I like the way it reduces keystrokes.

@jlorich This can help prevent typos resulting in syntax error, unexpected tSYMBEG, expecting '}', so I wouldn't necessarily say "pointless".

I like this implicit comma approach because it avoids a common issue while working on a piece of code, where you need to temporarily comment out a value.

some_hash = {
:this => 'that',
:foo => 'not bar'#'bar',
:those => 'the other'
}

it seems pretty useful, makes ruby more life coffee script, which is crazy cause I think coffees cript was inspired by ruby, at what point do the androids stop being robots and start being people ? if only I had a sheep.

👍
I totally ❤️ it...

Member

nobu commented Sep 26, 2013

You can reduce assocs_no_comma rule.
Please file a feature request on https://bugs.ruby-lang.org, hopefully with tests.

diff --git a/parse.y b/parse.y
index e203549..9e5a334 100644
--- a/parse.y
+++ b/parse.y
@@ -779,7 +779,7 @@ static void token_info_pop(struct parser_params*, const char *token);
 %type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
 %type <node> f_block_optarg f_block_opt
 %type <node> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs
-%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
+%type <node> assoc_list assocs assocs_no_comma assoc undef_list backref string_dvar for_var
 %type <node> block_param opt_block_param block_param_def f_opt
 %type <node> f_kwarg f_kw f_block_kwarg f_block_kw
 %type <node> bv_decls opt_bv_decl bvar
@@ -4893,7 +4893,7 @@ singleton : var_ref
        ;

 assoc_list : none
-       | assocs trailer
+       | assocs_no_comma trailer
            {
            /*%%%*/
            $$ = $1;
@@ -4920,6 +4920,23 @@ assocs       : assoc
            }
        ;

+assocs_no_comma : assoc
+           /*%c%*/
+           /*%c
+           {
+           $$ = rb_ary_new3(1, $1);
+           }
+           %*/
+       | assocs_no_comma assoc_separator assoc
+           {
+           /*%%%*/
+           $$ = list_concat($1, $3);
+           /*%
+           $$ = rb_ary_push($1, $3);
+           %*/
+           }
+       ;
+
 assoc      : arg_value tASSOC arg_value
            {
            /*%%%*/
@@ -4991,7 +5008,10 @@ rbracket : opt_nl ']'
        ;

 trailer        : /* none */
-       | '\n'
+       | assoc_separator
+       ;
+
+assoc_separator : '\n'
        | ','
        ;

Hi @nobu, thanks for the feedback! Good call on reducing the assocs_no_comma rules and adding a new assoc_seperator. I've submitted a feature request (https://bugs.ruby-lang.org/issues/8956), and I am looking into writing the tests now.

Adam Dunson added some commits Sep 27, 2013

Adam Dunson Add support for Ruby 1.9 style symbol hashes
Credit goes to nobu for this patch
(https://bugs.ruby-lang.org/issues/8956#note-2).
60e6bbc
Adam Dunson Allow array members delimited by \n inside []
This allows for syntax similar to the modified hash syntax, e.g. (rather
complex example):

    some_array = [
      :foo
      :bar
      [
        'baz'
        :qux
      ]
      {
        :quux => 'grault'
        'garply' => [
          'waldo' => :fred
        ]
        :plugh => 'xyzzy'
      }
    ]
e857c6f
Adam Dunson Rename assoc_seperator -> nl_or_comma
Now that I've added support for arrays, the name "assoc_seperator" is
somewhat misleading since this rule now also applies to aref_args.
55d0bc7
Adam Dunson Add support for method arguments
This enables syntax such as

    Hash[
      :foo, 'bar'
      :baz, 'qux'
    ]

(becomes `{ :foo => "bar", :baz => "qux" }`)

as well as

    puts("this is line one"
         "this is line two"
         "this is line three")
6d1878e
Adam Dunson Replace optional nl_or_comma with trailer b37ecac
Adam Dunson Tests for no-comma hashes, arrays, and method args 6e8e15b
Member

zzak commented Oct 15, 2013

Closing ticket to continue discussion in redmine Feature #8956

zzak closed this Oct 15, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment