Change substring index notation to use a colon (addresses #422).

String slice notation is written as `s[1:2]` instead of `s[1, 2]`
because the later is ambiguous with composite index types.
This commit is contained in:
Jon Siwek 2013-01-07 13:29:05 -06:00
parent e638f04301
commit 8b46bbb1c0
5 changed files with 46 additions and 24 deletions

View file

@ -2801,12 +2801,26 @@ bool AssignExpr::DoUnserialize(UnserialInfo* info)
return UNSERIALIZE(&is_init);
}
IndexExpr::IndexExpr(Expr* arg_op1, ListExpr* arg_op2)
IndexExpr::IndexExpr(Expr* arg_op1, ListExpr* arg_op2, bool is_string_slice)
: BinaryExpr(EXPR_INDEX, arg_op1, arg_op2)
{
if ( IsError() )
return;
if ( is_string_slice )
{
if ( ! IsString(op1->Type()->Tag()) )
ExprError("slice notation indexing can apply only to strings");
}
else if ( IsString(op1->Type()->Tag()) )
{
if ( arg_op2->Exprs().length() != 1 )
ExprError("invalid string index expression");
}
if ( IsError() )
return;
int match_type = op1->Type()->MatchesIndex(arg_op2);
if ( match_type == DOES_NOT_MATCH_INDEX )
SetError("not an index type");

View file

@ -646,7 +646,7 @@ protected:
class IndexExpr : public BinaryExpr {
public:
IndexExpr(Expr* op1, ListExpr* op2);
IndexExpr(Expr* op1, ListExpr* op2, bool is_string_slice = false);
int CanAdd() const;
int CanDel() const;

View file

@ -418,6 +418,14 @@ expr:
$$ = new IndexExpr($1, $3);
}
| expr '[' expr ':' expr ']'
{
set_location(@1, @6);
ListExpr* le = new ListExpr($3);
le->Append($5);
$$ = new IndexExpr($1, le, true);
}
| expr '$' TOK_ID
{
set_location(@1, @3);

View file

@ -11,16 +11,16 @@ event new_connection(c: connection)
{
local s = "0123456789";
print s[1];
print s[1,2];
print s[1,6];
print s[0,20];
print s[1:2];
print s[1:6];
print s[0:20];
print s[-2];
print s[-3,-1];
print s[-1,-10];
print s[-1,0];
print s[-1,5];
print s[20, 23];
print s[-20, 23];
print s[0,5][2];
print s[0,5][1,3][0];
print s[-3:1];
print s[-1:10];
print s[-1:0];
print s[-1:5];
print s[20:23];
print s[-20:23];
print s[0:5][2];
print s[0:5][1:3][0];
}

View file

@ -3,15 +3,15 @@
local s = "0123456789";
print s[1];
print s[1,2];
print s[1,6];
print s[0,20];
print s[1:2];
print s[1:6];
print s[0:20];
print s[-2];
print s[-3,-1];
print s[-1,-10];
print s[-1,0];
print s[-1,5];
print s[20, 23];
print s[-20, 23];
print s[0,5][2];
print s[0,5][1,3][0];
print s[-3:-1];
print s[-1:-10];
print s[-1:0];
print s[-1:5];
print s[20:23];
print s[-20:23];
print s[0:5][2];
print s[0:5][1:3][0];