www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - In D, lexically, which are the chars that can follow $, exactly ?

reply Basile B. <b2.temp gmx.com> writes:
'$' is only valid in an indexExpression 
(https://dlang.org/spec/grammar.html#IndexExpression),
so it can only be followed by

- ' '
- ']'
-  operators , usually '-' but also '/', '+', '>>' etc

Is that right ?

I'd like to relax the lexical rule for C.E static macros which 
currently is

- "^\$\w*[a-zA-Z]$", so for example "$a1A" is valid and "$a1" is 
not.

But it looks like for example "$)" or "$}" wouldn't be ambiguous 
since it's not possible in D.
Mar 13 2016
parent reply Marc =?UTF-8?B?U2Now7x0eg==?= <schuetzm gmx.net> writes:
On Sunday, 13 March 2016 at 14:07:31 UTC, Basile B. wrote:
 '$' is only valid in an indexExpression 
 (https://dlang.org/spec/grammar.html#IndexExpression),
 so it can only be followed by

 - ' '
 - ']'
 -  operators , usually '-' but also '/', '+', '>>' etc

 Is that right ?

 I'd like to relax the lexical rule for C.E static macros which 
 currently is

 - "^\$\w*[a-zA-Z]$", so for example "$a1A" is valid and "$a1" 
 is not.

 But it looks like for example "$)" or "$}" wouldn't be 
 ambiguous since it's not possible in D.
I don't know what C.E is, but `$` is an expansion of `PrimaryExpression`, which means it can (syntactically) appear anywhere a `PrimaryExpression` is allowed. For example, this compiles: void main() { int[] a; a[0 .. ($)] = 0; }
Mar 13 2016
parent Basile B. <b2.temp gmx.com> writes:
On Sunday, 13 March 2016 at 14:55:36 UTC, Marc Schütz wrote:
 On Sunday, 13 March 2016 at 14:07:31 UTC, Basile B. wrote:
 '$' is only valid in an indexExpression 
 (https://dlang.org/spec/grammar.html#IndexExpression),
 so it can only be followed by

 - ' '
 - ']'
 -  operators , usually '-' but also '/', '+', '>>' etc

 Is that right ?

 I'd like to relax the lexical rule for C.E static macros which 
 currently is

 - "^\$\w*[a-zA-Z]$", so for example "$a1A" is valid and "$a1" 
 is not.

 But it looks like for example "$)" or "$}" wouldn't be 
 ambiguous since it's not possible in D.
I don't know what C.E is, but `$` is an expansion of `PrimaryExpression`, which means it can (syntactically) appear anywhere a `PrimaryExpression` is allowed. For example, this compiles: void main() { int[] a; a[0 .. ($)] = 0; }
Then the C.E macros syntax can't be enhanced. At least the current is not ambiguous. The thing is that a macro is substitued automatically/dynamically so the requirement is that nothing that's syntaxically valid in D must replacable.
Mar 13 2016