Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Support for %i{} and %I{} #2269

Closed
wants to merge 2 commits into from

4 participants

Piotr Niełacny Dirkjan Bussink Brian Shirai Mikael Henriksson
Piotr Niełacny

No description provided.

Dirkjan Bussink
Owner

Isn't this a 2.0 feature?

Piotr Niełacny

Isn't this a 2.0 feature?

Hm... yes. Should I only change guard on specs?

Dirkjan Bussink
Owner

Well, there are a few issues. First of all we need to get the parser for 2.0 in general. We don't want to be changing the 1.9 parser to add adhoc 2.0 features to it.

We also need a place in Rubinius to implement 2.0 features, like we have 1.8 and 1.9 stuff now as well in the kernel for example. The problem here however is that the 1.9 -> 2.0 difference is much smaller than 1.8 -> 1.9, so using the same strategy as 1.8 -> 1.9 could result in a lot of duplicate code.

The specs also need to be guarded with 2.0 guards then. They always need to pass if you run them with MRI 1.9.3, and also 2.0. Personally I always first write specs while running against MRI, before implementing stuff in Rubinius to make sure to always set them up correctly for the right MRI version.

Brian Shirai
Owner

I'll be working on the Ruby 2.0 parser in the 3.0 branch of https://github.com/rubinius/rubinius-melbourne.

Brian Shirai brixen closed this
Alexander Mankuta cheba referenced this pull request in rubinius/rubinius-melbourne
Closed

Symbol array literal (%i[] %I[]) syntax support #3

Mikael Henriksson

Any progress on this? It is holding us up

Brian Shirai
Owner

@mhenrixon I've been fixing up the parser and compiler specs so I can work on new syntax. Should be soon.

Mikael Henriksson
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 12, 2013
  1. Piotr Niełacny

    Add support for %i{} and %I{}

    LTe authored
  2. Piotr Niełacny

    Add specs for %i{} and %I{}

    LTe authored
This page is out of date. Refresh to see the latest.
Showing with 109 additions and 2 deletions.
  1. +96 −2 lib/ext/melbourne/grammar19.y
  2. +13 −0 spec/ruby/language/array_spec.rb
98 lib/ext/melbourne/grammar19.y
View
@@ -462,7 +462,7 @@ static int scan_hex(const char *start, size_t len, size_t *retlen);
%type <node> singleton strings string string1 xstring regexp
%type <node> string_contents xstring_contents regexp_contents string_content
-%type <node> words qwords word_list qword_list word
+%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
%type <node> literal numeric dsym cpath
%type <node> top_compstmt top_stmts top_stmt
%type <node> bodystmt compstmt stmts stmt expr arg primary command command_call method_call
@@ -510,7 +510,7 @@ static int scan_hex(const char *start, size_t len, size_t *retlen);
%token tSTAR /* * */
%token tAMPER /* & */
%token tLAMBDA /* -> */
-%token tSYMBEG tSTRING_BEG tXSTRING_BEG tREGEXP_BEG tWORDS_BEG tQWORDS_BEG
+%token tSYMBEG tSTRING_BEG tXSTRING_BEG tREGEXP_BEG tWORDS_BEG tQWORDS_BEG tSYMBOLS_BEG tQSYMBOLS_BEG
%token tSTRING_DBEG tSTRING_DVAR tSTRING_END tLAMBEG
/*
@@ -1649,6 +1649,8 @@ primary : literal
| regexp
| words
| qwords
+ | symbols
+ | qsymbols
| var_ref
| backref
| tFID
@@ -2541,6 +2543,45 @@ word : string_content
}
;
+symbols : tSYMBOLS_BEG ' ' tSTRING_END
+ {
+ /*%%%*/
+ $$ = NEW_ZARRAY();
+ /*%
+ $$ = dispatch0(symbols_new);
+ $$ = dispatch1(array, $$);
+ %*/
+ }
+ | tSYMBOLS_BEG symbol_list tSTRING_END
+ {
+ /*%%%*/
+ $$ = $2;
+ /*%
+ $$ = dispatch1(array, $2);
+ %*/
+ }
+ ;
+
+symbol_list : /* none */
+ {
+ /*%%%*/
+ $$ = 0;
+ /*%
+ $$ = dispatch0(symbols_new);
+ %*/
+ }
+ | symbol_list word ' '
+ {
+ /*%%%*/
+ $2 = evstr2dstr($2);
+ nd_set_type($2, NODE_DSYM);
+ $$ = list_append($1, $2);
+ /*%
+ $$ = dispatch2(symbols_add, $1, $2);
+ %*/
+ }
+ ;
+
qwords : tQWORDS_BEG ' ' tSTRING_END
{
$$ = NEW_ZARRAY();
@@ -2551,6 +2592,25 @@ qwords : tQWORDS_BEG ' ' tSTRING_END
}
;
+qsymbols : tQSYMBOLS_BEG ' ' tSTRING_END
+ {
+ /*%%%*/
+ $$ = NEW_ZARRAY();
+ /*%
+ $$ = dispatch0(qsymbols_new);
+ $$ = dispatch1(array, $$);
+ %*/
+ }
+ | tQSYMBOLS_BEG qsym_list tSTRING_END
+ {
+ /*%%%*/
+ $$ = $2;
+ /*%
+ $$ = dispatch1(array, $2);
+ %*/
+ }
+ ;
+
qword_list : /* none */
{
$$ = 0;
@@ -2561,6 +2621,28 @@ qword_list : /* none */
}
;
+qsym_list : /* none */
+ {
+ /*%%%*/
+ $$ = 0;
+ /*%
+ $$ = dispatch0(qsymbols_new);
+ %*/
+ }
+ | qsym_list tSTRING_CONTENT ' '
+ {
+ /*%%%*/
+ VALUE lit;
+ lit = $2->nd_lit;
+ $2->nd_lit = ID2SYM(parser_intern_str(lit));
+ nd_set_type($2, NODE_LIT);
+ $$ = list_append($1, $2);
+ /*%
+ $$ = dispatch2(qsymbols_add, $1, $2);
+ %*/
+ }
+ ;
+
string_contents : /* none */
{
$$ = 0;
@@ -5453,6 +5535,18 @@ retry:
pushback(c);
return tQWORDS_BEG;
+ case 'I':
+ lex_strterm = NEW_STRTERM(str_dword, term, paren);
+ do {c = nextc();} while (ISSPACE(c));
+ pushback(c);
+ return tSYMBOLS_BEG;
+
+ case 'i':
+ lex_strterm = NEW_STRTERM(str_sword, term, paren);
+ do {c = nextc();} while (ISSPACE(c));
+ pushback(c);
+ return tQSYMBOLS_BEG;
+
case 'x':
lex_strterm = NEW_STRTERM(str_xquote, term, paren);
return tXSTRING_BEG;
13 spec/ruby/language/array_spec.rb
View
@@ -54,6 +54,19 @@
%W(a\ b\tc).should == ["a ", "b\tc"]
%W(white\ \ \ \ \ space).should == ["white ", " ", " ", " space"]
end
+
+ ruby_version_is "2.0" do
+ it "%i() transforms unquoted barewords into an array of symbols" do
+ %i(a b c).should == [:a, :b, :c]
+ %i("foo).should == [:"\"foo"]
+ end
+
+ it "%I() transforms unquoted barewords into an array of symbols, supporting interpolation" do
+ x = 10
+ %I(a b#{x}).should == [:a, :b10]
+ %I("a#{x}).should == [:"\"a10"]
+ end
+ end
end
describe "The unpacking splat operator (*)" do
Something went wrong with that request. Please try again.