Commit 99b5d43a authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

Merge pull request #1031 from FUKUZAWA-Tadashi/iI

implement %I %i literal
parents 5217d889 cdf8114e
...@@ -68,13 +68,15 @@ struct mrb_parser_message { ...@@ -68,13 +68,15 @@ struct mrb_parser_message {
enum mrb_string_type { enum mrb_string_type {
str_not_parsing = (0), str_not_parsing = (0),
str_squote = (STR_FUNC_PARSING), str_squote = (STR_FUNC_PARSING),
str_dquote = (STR_FUNC_PARSING|STR_FUNC_EXPAND), str_dquote = (STR_FUNC_PARSING|STR_FUNC_EXPAND),
str_regexp = (STR_FUNC_PARSING|STR_FUNC_REGEXP|STR_FUNC_EXPAND), str_regexp = (STR_FUNC_PARSING|STR_FUNC_REGEXP|STR_FUNC_EXPAND),
str_sword = (STR_FUNC_PARSING|STR_FUNC_WORD|STR_FUNC_ARRAY), str_sword = (STR_FUNC_PARSING|STR_FUNC_WORD|STR_FUNC_ARRAY),
str_dword = (STR_FUNC_PARSING|STR_FUNC_WORD|STR_FUNC_ARRAY|STR_FUNC_EXPAND), str_dword = (STR_FUNC_PARSING|STR_FUNC_WORD|STR_FUNC_ARRAY|STR_FUNC_EXPAND),
str_ssym = (STR_FUNC_PARSING|STR_FUNC_SYMBOL), str_ssym = (STR_FUNC_PARSING|STR_FUNC_SYMBOL),
str_heredoc = (STR_FUNC_PARSING|STR_FUNC_HEREDOC), str_ssymbols = (STR_FUNC_PARSING|STR_FUNC_SYMBOL|STR_FUNC_ARRAY),
str_dsymbols = (STR_FUNC_PARSING|STR_FUNC_SYMBOL|STR_FUNC_ARRAY|STR_FUNC_EXPAND),
str_heredoc = (STR_FUNC_PARSING|STR_FUNC_HEREDOC),
}; };
/* heredoc structure */ /* heredoc structure */
......
...@@ -2027,6 +2027,10 @@ codegen(codegen_scope *s, node *tree, int val) ...@@ -2027,6 +2027,10 @@ codegen(codegen_scope *s, node *tree, int val)
gen_literal_array(s, tree, FALSE, val); gen_literal_array(s, tree, FALSE, val);
break; break;
case NODE_SYMBOLS:
gen_literal_array(s, tree, TRUE, val);
break;
case NODE_REGX: case NODE_REGX:
if (val) { if (val) {
char *p1 = (char*)tree->car; char *p1 = (char*)tree->car;
......
...@@ -108,6 +108,7 @@ enum node_type { ...@@ -108,6 +108,7 @@ enum node_type {
NODE_HEREDOC, NODE_HEREDOC,
NODE_LITERAL_DELIM, NODE_LITERAL_DELIM,
NODE_WORDS, NODE_WORDS,
NODE_SYMBOLS,
NODE_LAST NODE_LAST
}; };
......
...@@ -763,6 +763,13 @@ new_words(parser_state *p, node *a) ...@@ -763,6 +763,13 @@ new_words(parser_state *p, node *a)
return cons((node*)NODE_WORDS, a); return cons((node*)NODE_WORDS, a);
} }
// (:symbols . a)
static node*
new_symbols(parser_state *p, node *a)
{
return cons((node*)NODE_SYMBOLS, a);
}
// xxx ----------------------------- // xxx -----------------------------
// (:call a op) // (:call a op)
...@@ -991,7 +998,7 @@ heredoc_end(parser_state *p) ...@@ -991,7 +998,7 @@ heredoc_end(parser_state *p)
%type <nd> mlhs mlhs_list mlhs_post mlhs_basic mlhs_item mlhs_node mlhs_inner %type <nd> mlhs mlhs_list mlhs_post mlhs_basic mlhs_item mlhs_node mlhs_inner
%type <id> fsym sym basic_symbol operation operation2 operation3 %type <id> fsym sym basic_symbol operation operation2 operation3
%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg %type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg
%type <nd> heredoc words %type <nd> heredoc words symbols
%token tUPLUS /* unary+ */ %token tUPLUS /* unary+ */
%token tUMINUS /* unary- */ %token tUMINUS /* unary- */
...@@ -1020,7 +1027,7 @@ heredoc_end(parser_state *p) ...@@ -1020,7 +1027,7 @@ heredoc_end(parser_state *p)
%token tSTAR /* * */ %token tSTAR /* * */
%token tAMPER /* & */ %token tAMPER /* & */
%token tLAMBDA /* -> */ %token tLAMBDA /* -> */
%token tSYMBEG tREGEXP_BEG tWORDS_BEG %token tSYMBEG tREGEXP_BEG tWORDS_BEG tSYMBOLS_BEG
%token tSTRING_BEG tSTRING_DVAR tLAMBEG %token tSTRING_BEG tSTRING_DVAR tLAMBEG
%token <nd> tHEREDOC_BEG /* <<, <<- */ %token <nd> tHEREDOC_BEG /* <<, <<- */
%token tHEREDOC_END tLITERAL_DELIM %token tHEREDOC_END tLITERAL_DELIM
...@@ -2535,6 +2542,7 @@ opt_ensure : keyword_ensure compstmt ...@@ -2535,6 +2542,7 @@ opt_ensure : keyword_ensure compstmt
literal : numeric literal : numeric
| symbol | symbol
| words | words
| symbols
; ;
string : tCHAR string : tCHAR
...@@ -2653,6 +2661,16 @@ sym : fname ...@@ -2653,6 +2661,16 @@ sym : fname
} }
; ;
symbols : tSYMBOLS_BEG tSTRING
{
$$ = new_symbols(p, list1($2));
}
| tSYMBOLS_BEG string_rep tSTRING
{
$$ = new_symbols(p, push($2, $3));
}
;
numeric : tINTEGER numeric : tINTEGER
| tFLOAT | tFLOAT
| tUMINUS_NUM tINTEGER %prec tLOWEST | tUMINUS_NUM tINTEGER %prec tLOWEST
...@@ -4625,6 +4643,14 @@ parser_yylex(parser_state *p) ...@@ -4625,6 +4643,14 @@ parser_yylex(parser_state *p)
p->lex_strterm = new_strterm(p, str_ssym, term, paren); p->lex_strterm = new_strterm(p, str_ssym, term, paren);
return tSYMBEG; return tSYMBEG;
case 'I':
p->lex_strterm = new_strterm(p, str_dsymbols, term, paren);
return tSYMBOLS_BEG;
case 'i':
p->lex_strterm = new_strterm(p, str_ssymbols, term, paren);
return tSYMBOLS_BEG;
default: default:
yyerror(p, "unknown type of %string"); yyerror(p, "unknown type of %string");
return 0; return 0;
......
...@@ -186,8 +186,54 @@ d ...@@ -186,8 +186,54 @@ d
test1 and test2 test1 and test2
end end
assert('Literals Array of symbols') do
a = %I{abc#{1+2}def \}g}
b = %I(abc #{2+3} def \(g)
c = %I[#{3+4}]
d = %I< #{4+5} >
e = %I//
f = %I[[ab cd][ef]]
g = %I{
ab
#{-1}1
2#{2}
}
test1 = (a == [:'abc3def', :'}g'] and
b == [:'abc', :'5', :'def', :'(g'] and
c == [:'7'] and
d == [:'9'] and
e == [] and
f == [:'[ab', :'cd][ef]'] and
g == [:'ab', :'-11', :'22']
)
a = %i{abc#{1+2}def \}g}
b = %i(abc #{2+3} def \(g)
c = %i[#{3+4}]
d = %i< #{4+5} >
e = %i//
f = %i[[ab cd][ef]]
g = %i{
ab
#{-1}1
2#{2}
}
test2 = (a == [:'abc#{1+2}def', :'}g'] and
b == [:'abc', :'#{2+3}', :'def', :'(g'] and
c == [:'#{3+4}'] and
d == [:'#{4+5}'] and
e == [] and
f == [:'[ab', :'cd][ef]'] and
g == [:'ab', :'#{-1}1', :'2#{2}']
)
test1 and test2
end
assert('Literals Symbol', '8.7.6.6') do assert('Literals Symbol', '8.7.6.6') do
/* do not compile error */ # do not compile error
:$asd :$asd
:@asd :@asd
:@@asd :@@asd
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment