Support omission of string slice low/high indices, BIT-1097.

Omission of the low index defaults to 0:

    s = "12345"; s[:3] == "123"

Omission of the high index defaults to length of the string:

    s = "12345"; s[3:] == "45"
This commit is contained in:
Jon Siwek 2013-12-04 15:11:48 -06:00
parent 4014cdc277
commit 8515d3aa57
3 changed files with 56 additions and 4 deletions

View file

@ -55,7 +55,7 @@
%type <expr> opt_init
%type <val> TOK_CONSTANT
%type <re> pattern
%type <expr> expr init anonymous_function
%type <expr> expr opt_expr init anonymous_function
%type <event_expr> event
%type <stmt> stmt stmt_list func_body for_head
%type <type> type opt_type enum_body
@ -261,6 +261,13 @@ decl_list:
|
;
opt_expr:
expr
{ $$ = $1; }
|
{ $$ = 0; }
;
expr:
'(' expr ')'
{
@ -418,11 +425,13 @@ expr:
$$ = new IndexExpr($1, $3);
}
| expr '[' expr ':' expr ']'
| expr '[' opt_expr ':' opt_expr ']'
{
set_location(@1, @6);
ListExpr* le = new ListExpr($3);
le->Append($5);
Expr* low = $3 ? $3 : new ConstExpr(new Val(0, TYPE_COUNT));
Expr* high = $5 ? $5 : new SizeExpr($1);
ListExpr* le = new ListExpr(low);
le->Append(high);
$$ = new IndexExpr($1, le, true);
}

View file

@ -28,6 +28,41 @@ word[5] =
word[6] =
word[7] =
word[100] =
word[:-100] =
word[:-7] =
word[:-6] =
word[:-5] =
word[:-4] = H
word[:-3] = He
word[:-2] = Hel
word[:-1] = Help
word[:0] =
word[:1] = H
word[:2] = He
word[:3] = Hel
word[:4] = Help
word[:5] = HelpA
word[:6] = HelpA
word[:7] = HelpA
word[:100] = HelpA
word[-100:] = HelpA
word[-7:] = HelpA
word[-6:] = HelpA
word[-5:] = HelpA
word[-4:] = elpA
word[-3:] = lpA
word[-2:] = pA
word[-1:] = A
word[0:] = HelpA
word[1:] = elpA
word[2:] = lpA
word[3:] = pA
word[4:] = A
word[5:] =
word[6:] =
word[7:] =
word[100:] =
HelpA
A
1234

View file

@ -24,6 +24,14 @@ s = "012345";
for ( i in indices )
print fmt("word[%s] = %s", indices[i], word[indices[i]]);
for ( i in indices )
print fmt("word[:%s] = %s", indices[i], word[:indices[i]]);
for ( i in indices )
print fmt("word[%s:] = %s", indices[i], word[indices[i]:]);
print word[:];
print "";
print "A";