is this right??

Ary Borenszweig ary at esperanto.org.ar
Tue Jun 16 20:45:40 PDT 2009


BCS escribió:
> Hello BCS,
> 
>> void main()
>> {
>> int s;
>> mixin("s") = 5;
>> }
>> -----
>> Line 4: found '=' when expecting ';'
>> http://www.digitalmars.com/d/1.0/expression.html#MixinExpression
>>
> 
> (mixin("s")) = 5;
> 
> Given that the above works, it looks like the parser prematurely commits 
> to a mixin declaration when it finds "mixin (" at function scope.
> 
> Unless someone spots a flaw in this, I'll post a bug.

Yes, at that point the parser is parsing statements, so if it finds a 
"mixin(...)" it turns it into a statement.

I'm thinking about how to fix this. I think the parser should peek the 
next token after the closing parenthesis. If it's a semicolon, then 
treat it as a statement-mixin. Else, just invoke parseAssignExp and 
create a new ExpStatement.

I modified this in Descent and it seems to work. :-)

Here's the code (in Java, but the idea is simple to translate to C++):

// inside Parser::parseStatement
case TOKmixin: {
     Dsymbol d;
     t = peek(token);
     if (t.value == TOKlparen)
     {
         if (peekPastParen(t).value != TOKsemicolon) {
             Expression e = parseAssignExp();
             s = new ExpStatement(loc(), e);
         } else {
             // mixin(string)
             nextToken();
             check(TOKlparen);
             Expression e = parseAssignExp();
             check(TOKrparen);
             check(TOKsemicolon);
             s = newCompileStatement(loc(), e);
         }
         break;
     } else {
         d = parseMixin();
         s = newDeclarationStatement(loc(), d);
         break;
     }
}


More information about the Digitalmars-d-learn mailing list