diff --git a/ext/ripper/eventids2.c b/ext/ripper/eventids2.c index 8b0d9c3..7ef8cc2 100644 --- a/ext/ripper/eventids2.c +++ b/ext/ripper/eventids2.c @@ -38,6 +38,7 @@ static ID ripper_id_tstring_content; static ID ripper_id_tstring_end; static ID ripper_id_words_beg; static ID ripper_id_qwords_beg; +static ID ripper_id_qsymbols_beg; static ID ripper_id_words_sep; static ID ripper_id_regexp_beg; static ID ripper_id_regexp_end; @@ -91,6 +92,7 @@ ripper_init_eventids2(VALUE self) ripper_id_tstring_end = rb_intern_const("on_tstring_end"); ripper_id_words_beg = rb_intern_const("on_words_beg"); ripper_id_qwords_beg = rb_intern_const("on_qwords_beg"); + ripper_id_qsymbols_beg = rb_intern_const("on_qsymbols_beg"); ripper_id_words_sep = rb_intern_const("on_words_sep"); ripper_id_regexp_beg = rb_intern_const("on_regexp_beg"); ripper_id_regexp_end = rb_intern_const("on_regexp_end"); @@ -230,6 +232,7 @@ static const struct token_assoc { {tOROP, &ripper_id_op}, {tPOW, &ripper_id_op}, {tQWORDS_BEG, &ripper_id_qwords_beg}, + {tQSYMBOLS_BEG, &ripper_id_qsymbols_beg}, {tREGEXP_BEG, &ripper_id_regexp_beg}, {tREGEXP_END, &ripper_id_regexp_end}, {tRPAREN, &ripper_id_rparen}, diff --git a/parse.y b/parse.y index eb5f7fa..f588698 100644 --- a/parse.y +++ b/parse.y @@ -683,7 +683,7 @@ static void token_info_pop(struct parser_params*, const char *token); %type singleton strings string string1 xstring regexp %type string_contents xstring_contents regexp_contents string_content -%type words qwords word_list qword_list word +%type words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word %type literal numeric dsym cpath %type top_compstmt top_stmts top_stmt %type bodystmt compstmt stmts stmt expr arg primary command command_call method_call @@ -734,7 +734,7 @@ static void token_info_pop(struct parser_params*, const char *token); %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 /* @@ -2619,6 +2619,8 @@ primary : literal | regexp | words | qwords + | symbols + | qsymbols | var_ref | backref | tFID @@ -4025,6 +4027,45 @@ word : string_content } ; +symbols : tSYMBOLS_BEG ' ' tSTRING_END + { + /*%%%*/ + $$ = NEW_ZARRAY(); + /*% + $$ = dispatch0(qsymbols_new); + $$ = dispatch1(array, $$); + %*/ + } + | tSYMBOLS_BEG symbol_list tSTRING_END + { + /*%%%*/ + $$ = $2; + /*% + $$ = dispatch1(array, $2); + %*/ + } + ; + +symbol_list : /* none */ + { + /*%%%*/ + $$ = 0; + /*% + $$ = dispatch0(words_new); + %*/ + } + | symbol_list word ' ' + { + /*%%%*/ + $2 = evstr2dstr($2); + nd_set_type($2, NODE_DSYM); + $$ = list_append($1, $2); + /*% + $$ = dispatch2(words_add, $1, $2); + %*/ + } + ; + qwords : tQWORDS_BEG ' ' tSTRING_END { /*%%%*/ @@ -4044,6 +4085,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 */ { /*%%%*/ @@ -4062,6 +4122,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(rb_intern_str(lit)); + nd_set_type($2, NODE_LIT); + $$ = list_append($1, $2); + /*% + $$ = dispatch2(qsymbols_add, $1, $2); + %*/ + } + ; + string_contents : /* none */ { /*%%%*/ @@ -7599,6 +7681,18 @@ parser_yylex(struct parser_params *parser) 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; diff --git a/test/ripper/test_parser_events.rb b/test/ripper/test_parser_events.rb index e6968e1..7187c7c 100644 --- a/test/ripper/test_parser_events.rb +++ b/test/ripper/test_parser_events.rb @@ -749,12 +749,36 @@ class TestRipper::ParserEvents < Test::Unit::TestCase assert_equal true, thru_qwords_add end + def test_qsymbols_add + thru_qsymbols_add = false + parse('%i[a]', :on_qsymbols_add) {thru_qsymbols_add = true} + assert_equal true, thru_qsymbols_add + end + + def test_symbols_add + thru_symbols_add = false + parse('%I[a]', :on_symbols_add) {thru_symbols_add = true} + assert_equal true, thru_symbols_add + end + def test_qwords_new thru_qwords_new = false parse('%w[]', :on_qwords_new) {thru_qwords_new = true} assert_equal true, thru_qwords_new end + def test_qsymbols_new + thru_qsymbols_new = false + parse('%i[]', :on_qsymbols_new) {thru_qsymbols_new = true} + assert_equal true, thru_qsymbols_new + end + + def test_symbols_new + thru_symbols_new = false + parse('%I[]', :on_symbols_new) {thru_symbols_new = true} + assert_equal true, thru_symbols_new + end + def test_redo thru_redo = false parse('redo', :on_redo) {thru_redo = true} diff --git a/test/ripper/test_scanner_events.rb b/test/ripper/test_scanner_events.rb index 7d5bb3c..6b4f551 100644 --- a/test/ripper/test_scanner_events.rb +++ b/test/ripper/test_scanner_events.rb @@ -607,6 +607,17 @@ class TestRipper::ScannerEvents < Test::Unit::TestCase scan('qwords_beg', '%w( w w w )') end + def test_qsymbols_beg + assert_equal [], + scan('qsymbols_beg', '') + assert_equal ['%i('], + scan('qsymbols_beg', '%i()') + assert_equal ['%i('], + scan('qsymbols_beg', '%i(w w w)') + assert_equal ['%i( '], + scan('qsymbols_beg', '%i( w w w )') + end + # FIXME: Close paren must not present (`words_end' scanner event?). def test_words_sep assert_equal [], diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index 6452476..4882d80 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -12,6 +12,17 @@ class TestArray < Test::Unit::TestCase $VERBOSE = @verbose end + def test_percent_i + assert_equal([:foo, :bar], %i[foo bar]) + assert_equal([:"\"foo"], %i["foo]) + end + + def test_percent_I + x = 10 + assert_equal([:foo, :b10], %I[foo b#{x}]) + assert_equal([:"\"foo10"], %I["foo#{x}]) + end + def test_0_literal assert_equal([1, 2, 3, 4], [1, 2] + [3, 4]) assert_equal([1, 2, 1, 2], [1, 2] * 2)