D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5953 - Too many trailing commas are accepted
Summary: Too many trailing commas are accepted
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 minor
Assignee: No Owner
URL:
Keywords: accepts-invalid, patch
Depends on:
Blocks:
 
Reported: 2011-05-07 16:28 UTC by bearophile_hugs
Modified: 2011-08-31 00:36 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2011-05-07 16:28:09 UTC
Accepting an extra trailing comma is acceptable, but this looks excessive (this compiles and runs with no errors, DMD 2.053 beta):


void main() {
    auto a = [,,,,,,,,,,,,,,,,,,,,,,,,,,,,,];
    assert(a.length == 0);
}


As in Python I suggest to accept only one extra trailing comma:

>>> a = [1]
>>> a = [1,]
>>> a = [1,,]
  File "<stdin>", line 1
    a = [1,,]
           ^
SyntaxError: invalid syntax
Comment 1 kennytm 2011-05-07 23:51:21 UTC
Please don't use Python to say a D syntax is invalid. Use the D spec or TDPL.

--------

In the D spec an array literal follows the rule:

ArrayLiteral:
	'[' ArgumentList ']'

ArgumentList:
	AssignExpression
        AssignExpression ','
	AssignExpression ',' ArgumentList

and an AssignExpression cannot be empty, so '[1,,]' should be rejected.

(BTW this rule doesn't handle '[]' at all.)
Comment 2 bearophile_hugs 2011-05-08 03:39:56 UTC
(In reply to comment #1)
> Please don't use Python to say a D syntax is invalid. Use the D spec or TDPL.

I feel free to show what's the design of other languages, if they are a good example.


> and an AssignExpression cannot be empty, so '[1,,]' should be rejected.

OK. So it's not a design problem, just an implementation issue.
Comment 3 kennytm 2011-05-08 04:40:20 UTC
(In reply to comment #2)
> (In reply to comment #1)
> > Please don't use Python to say a D syntax is invalid. Use the D spec or TDPL.
> 
> I feel free to show what's the design of other languages, if they are a good
> example.
> 

Yes, but with the 'accept-invalid' tag I think that you want to say [,,,,,,,,,,,,,,,,,,] is invalid because it is invalid in Python. Sorry for misinterpreting.

> 
> > and an AssignExpression cannot be empty, so '[1,,]' should be rejected.
> 
> OK. So it's not a design problem, just an implementation issue.
Comment 4 Kenji Hara 2011-08-27 22:45:11 UTC
Reduce test case.
StructInitializer has same problem as ArrayInitializer.

void main()
{
    // parsing ArrayInitializer
    auto a1 = [];	// valid
    auto a2 = [,];	// invalid, but compiles
    auto a3 = [,,,];	// invalid, but compiles

    // parsing StructInitializer
    struct S{}
    S s1 = {};    // valid
    S s2 = {,};   // invalid, but compiles
    S s3 = {,,,}; // invalid, but compiles
}

And, maybe, D1 also has same problem.