At sign ``@'' in key

classic Classic list List threaded Threaded
19 messages Options
Reply | Threaded
Open this post in threaded view
|

At sign ``@'' in key

Grizzly Bear
Hi,

I'd really want to have the at sign ``@'' in the key of a table, e.g.,
          tab["var@addr"] = 1.

But I haven't been able to do 
          tab.var@addr = 1.

The error message was 
          stdin:1: '=' expected near '@'

I'm using Lua 5.1.5. Any suggestions?

Thanks.
Wei
Reply | Threaded
Open this post in threaded view
|

Re: At sign ``@'' in key

Coda Highland
On Wed, Dec 19, 2012 at 5:26 PM, Grizzly Bear <[hidden email]> wrote:

> Hi,
>
> I'd really want to have the at sign ``@'' in the key of a table, e.g.,
>           tab["var@addr"] = 1.
>
> But I haven't been able to do
>           tab.var@addr = 1.
>
> The error message was
>           stdin:1: '=' expected near '@'
>
> I'm using Lua 5.1.5. Any suggestions?
>
> Thanks.
> Wei

"Doctor, it hurts when I do this."

"Then don't do that."

If you want to use keys that have characters that aren't valid in
identifiers, you have to use the [] syntax.

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: At sign ``@'' in key

Luiz Henrique de Figueiredo
In reply to this post by Grizzly Bear
> But I haven't been able to do
>           tab.var@addr = 1.
>
> I'm using Lua 5.1.5. Any suggestions?

Not for 5.1, but in 5.2 you can tell the lexer that @ is a letter by
editing lctype.c and rebuilding. Not that I recommend you to...

Reply | Threaded
Open this post in threaded view
|

Re: At sign ``@'' in key

Michal Kolodziejczyk-3
In reply to this post by Grizzly Bear
On 20.12.2012 02:26, Grizzly Bear wrote:

> Hi,
>
> I'd really want to have the at sign ``@'' in the key of a table, e.g.,
>           tab["var@addr"] = 1.
>
> But I haven't been able to do
>           tab.var@addr = 1.
>
> The error message was
>           stdin:1: '=' expected near '@'
>
> I'm using Lua 5.1.5. Any suggestions?

Put your key into var:
local key="var@addr"
tab[key]=1

or use:
tab[ [[var@addr]] ]=1

Works for me for both Lua 5.1 and 5.2.

Regards,
miko



Reply | Threaded
Open this post in threaded view
|

Re: At sign ``@'' in key

Wolfgang Pupp-2
Michal Kolodziejczyk wrote:
> <snip> or use:
> tab[ [[var@addr]] ]=1

This made me wonder- is "a[[[b]]] = c" actually a syntactically
correct Lua statement? (it was rejected in every implementation I
tried)

Looking at the Lua 5.1 EBNF, I'd have interpreted this as:
    var `=´ Name
with
    var <=> prefixexp `[´ exp `]´ <=> Name `[´ String `]´ <=> `a´ `[´
`[[b]]´ `]´

Now I'm confused... Is my understanding of the EBNF broken, or was it
merely an implementation choice to not allow this?

--Wolfgang

Reply | Threaded
Open this post in threaded view
|

Re: At sign ``@'' in key

Coda Highland
On Thu, Dec 20, 2012 at 11:05 AM, Wolfgang Pupp <[hidden email]> wrote:

> Michal Kolodziejczyk wrote:
>> <snip> or use:
>> tab[ [[var@addr]] ]=1
>
> This made me wonder- is "a[[[b]]] = c" actually a syntactically
> correct Lua statement? (it was rejected in every implementation I
> tried)
>
> Looking at the Lua 5.1 EBNF, I'd have interpreted this as:
>     var `=´ Name
> with
>     var <=> prefixexp `[´ exp `]´ <=> Name `[´ String `]´ <=> `a´ `[´
> `[[b]]´ `]´
>
> Now I'm confused... Is my understanding of the EBNF broken, or was it
> merely an implementation choice to not allow this?
>
> --Wolfgang
>

The spaces in [ [[b]] ] are necessary, otherwise the lexer reads the
first [[ as the string delimiter and tries to parse it as a function
call with a single string argument.

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: At sign ``@'' in key

Grizzly Bear
In reply to this post by Luiz Henrique de Figueiredo
Thank you both! Problem solved.

             tab.var@addr = 1

is terser than

             tab["var@addr"] = 1

saving 3 chars and I'm a miser. That's why I liked to be able to do it.

Wei
             
On Wed, Dec 19, 2012 at 6:26 PM, Luiz Henrique de Figueiredo <[hidden email]> wrote:
> But I haven't been able to do
>           tab.var@addr = 1.
>
> I'm using Lua 5.1.5. Any suggestions?

Not for 5.1, but in 5.2 you can tell the lexer that @ is a letter by
editing lctype.c and rebuilding. Not that I recommend you to...


Reply | Threaded
Open this post in threaded view
|

Re: At sign ``@'' in key

Rena
On Sat, Dec 22, 2012 at 1:40 PM, Grizzly Bear <[hidden email]> wrote:

> Thank you both! Problem solved.
>
>              tab.var@addr = 1
>
> is terser than
>
>              tab["var@addr"] = 1
>
> saving 3 chars and I'm a miser. That's why I liked to be able to do it.
>
> Wei
>
> On Wed, Dec 19, 2012 at 6:26 PM, Luiz Henrique de Figueiredo
> <[hidden email]> wrote:
>>
>> > But I haven't been able to do
>> >           tab.var@addr = 1.
>> >
>> > I'm using Lua 5.1.5. Any suggestions?
>>
>> Not for 5.1, but in 5.2 you can tell the lexer that @ is a letter by
>> editing lctype.c and rebuilding. Not that I recommend you to...
>>
>

I had pondered before the idea of allowing '@' and '$' in names, at
least at the beginning. That then led to the idea of functions named
only @() and $(), which function like prefixes in that they don't need
parentheses for a single argument, e.g.:

$ = tostring
x = {}
print($x) --> prints "table: 0x......"

The idea being mainly to avoid having to wrap things in tostring() and
tonumber() everywhere, which gets ugly fast.

--
Sent from my Game Boy.

Reply | Threaded
Open this post in threaded view
|

Re: At sign ``@'' in key

Coda Highland
On Sat, Dec 22, 2012 at 10:59 AM, Rena <[hidden email]> wrote:

> On Sat, Dec 22, 2012 at 1:40 PM, Grizzly Bear <[hidden email]> wrote:
>> Thank you both! Problem solved.
>>
>>              tab.var@addr = 1
>>
>> is terser than
>>
>>              tab["var@addr"] = 1
>>
>> saving 3 chars and I'm a miser. That's why I liked to be able to do it.
>>
>> Wei
>>
>> On Wed, Dec 19, 2012 at 6:26 PM, Luiz Henrique de Figueiredo
>> <[hidden email]> wrote:
>>>
>>> > But I haven't been able to do
>>> >           tab.var@addr = 1.
>>> >
>>> > I'm using Lua 5.1.5. Any suggestions?
>>>
>>> Not for 5.1, but in 5.2 you can tell the lexer that @ is a letter by
>>> editing lctype.c and rebuilding. Not that I recommend you to...
>>>
>>
>
> I had pondered before the idea of allowing '@' and '$' in names, at
> least at the beginning. That then led to the idea of functions named
> only @() and $(), which function like prefixes in that they don't need
> parentheses for a single argument, e.g.:
>
> $ = tostring
> x = {}
> print($x) --> prints "table: 0x......"
>
> The idea being mainly to avoid having to wrap things in tostring() and
> tonumber() everywhere, which gets ugly fast.
>
> --
> Sent from my Game Boy.
>

Personally I lean towards taking a tip from Scala, saying that any
character that isn't reserved for a known token is legal in
identifiers. Of course this means it becomes harder for the language
specification to ADD tokens...

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: At sign ``@'' in key

steve donovan
In reply to this post by Rena
On Sat, Dec 22, 2012 at 8:59 PM, Rena <[hidden email]> wrote:
> I had pondered before the idea of allowing '@' and '$' in names, at
> least at the beginning. That then led to the idea of functions named
> only @() and $(), which function like prefixes in that they don't need
> parentheses for a single argument, e.g.:

It's an entertaining idea to allow $ and @ in identifiers, but special
meanings are going to confuse things. (Imagine being responsible for
documenting this feature!).  When I see $var, I see a plain variable;
$(var) is something else. Years of the Unix shell allow for no other
meaning to take over.

If $ became a valid variable, then there would be a rush to be the
first library to colonize it (think of JQuery).  Obfuscated Lua
programs could become so much easier!

steve d.

Reply | Threaded
Open this post in threaded view
|

Re: At sign ``@'' in key

Florian Weimer
* steve donovan:

> If $ became a valid variable, then there would be a rush to be the
> first library to colonize it (think of JQuery).  Obfuscated Lua
> programs could become so much easier!

GCC allows $ in identifiers as a C extension (which cannot be switched
off).  Curiously, the same thing hasn't happened.

Reply | Threaded
Open this post in threaded view
|

Re: At sign ``@'' in key

Rena
On Sun, Dec 23, 2012 at 6:32 AM, Florian Weimer <[hidden email]> wrote:
> * steve donovan:
>
>> If $ became a valid variable, then there would be a rush to be the
>> first library to colonize it (think of JQuery).  Obfuscated Lua
>> programs could become so much easier!
>
> GCC allows $ in identifiers as a C extension (which cannot be switched
> off).  Curiously, the same thing hasn't happened.
>

I think that's because it's a compiler extension, and intended only
for internal use? IIRC it even generates a warning if you use it.

--
Sent from my Game Boy.

Reply | Threaded
Open this post in threaded view
|

Re: At sign ``@'' in key

Enrico Colombini
In reply to this post by steve donovan
On 23/12/2012 8.52, steve donovan wrote:
> If $ became a valid variable, then there would be a rush to be the
> first library to colonize it (think of JQuery).  Obfuscated Lua
> programs could become so much easier!

Perlua would be just around the corner :-)

--
   Enrico

Reply | Threaded
Open this post in threaded view
|

Re: At sign ``@'' in key

Michael Richter
Or$we$could$see$DEC$Lua$instead.


On 23 December 2012 21:17, Enrico Colombini <[hidden email]> wrote:
On 23/12/2012 8.52, steve donovan wrote:
If $ became a valid variable, then there would be a rush to be the
first library to colonize it (think of JQuery).  Obfuscated Lua
programs could become so much easier!

Perlua would be just around the corner :-)

--
  Enrico




--
"Perhaps people don't believe this, but throughout all of the discussions of entering China our focus has really been what's best for the Chinese people. It's not been about our revenue or profit or whatnot."
--Sergey Brin, demonstrating the emptiness of the "don't be evil" mantra.
Reply | Threaded
Open this post in threaded view
|

Re: At sign ``@'' in key

Florian Weimer
In reply to this post by Rena
* Rena:

>> GCC allows $ in identifiers as a C extension (which cannot be switched
>> off).  Curiously, the same thing hasn't happened.

> I think that's because it's a compiler extension, and intended only
> for internal use?

It's for VMS compatibility, where most system APIs have a $ in their
names.

> IIRC it even generates a warning if you use it.

Only for C++.


Reply | Threaded
Open this post in threaded view
|

Re: At sign ``@'' in key

Dirk Laurie-2
In reply to this post by Grizzly Bear
2012/12/20 Grizzly Bear <[hidden email]>:


> I'd really want to have the at sign ``@'' in the key of a table, e.g.,
>           tab["var@addr"] = 1.

You can do this.

1. Edit lctype.c so that all characters from 0x80 to 0xbf are digits
    (replace 0x00 by 0x16) and all characters from 0xc0 to 0xfd
    are letters (replace 0x00 by 0x15).  Rebuild Lua.  (This makes
    all multibyte UTF-8 sequences into valid alphabetic characters
    for Lua without opening the floodgates to arbitrary byte strings.)

2. Invent some way that you can easily enter Unicode uFF20 on
    your keyboard, e.g. by pressing Alt-@ (Alt-Shift-2 on my keyboard).

Here is a transcript of an actual session.

$ lua-utf8
Lua 5.2.1  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> tab = {}
> tab.var@addr = 1
> for k,v in pairs(tab) do print(k,v) end
var@addr 1

Reply | Threaded
Open this post in threaded view
|

Re: At sign ``@'' in key

Dave Dodge
In reply to this post by Florian Weimer
On Sun, Dec 23, 2012 at 03:33:43PM +0100, Florian Weimer wrote:
> >> GCC allows $ in identifiers as a C extension
>
> > I think that's because it's a compiler extension, and intended only
> > for internal use?
>
> It's for VMS compatibility, where most system APIs have a $ in their
> names.

Also some old Unix vendor C compilers and libraries.  For example if
you were writing GUI code for Apollo workstations in the 1980s nearly
every type name or function in the API was something like
gpr_$window_list_t or pad_$create_window().

I think even Apollo did it mostly for compatibility with their older
Pascal-based API, but because it was so common in the system libraries
it could trickle into new code as well.  Anyone who wanted to compile
C code originally written on Domain/OS definitely had to support it.

                                                  -Dave Dodge

Reply | Threaded
Open this post in threaded view
|

Re: At sign ``@'' in key

steve donovan
In reply to this post by Florian Weimer
On Sun, Dec 23, 2012 at 1:32 PM, Florian Weimer <[hidden email]> wrote:
> GCC allows $ in identifiers as a C extension (which cannot be switched
> off).  Curiously, the same thing hasn't happened.

Well, people tend to keep to the standard (unless they are wedded to
GCC like the Linux kernel); I'm reluctant to use cool GCC extensions
because they will remain in the GCC ghetto (unfortunately, that
includes the very good C99 standard because MS has no interest in it).

Plus, C's limitations prevent excessive cleverness (apart from
preprocessor abuse).

I think allowing UTF-8 in all identifiers (as Dirk indicates) to be a
good customization. Most scientific programmers would like to use
Greek letters ;)

steve d.

Reply | Threaded
Open this post in threaded view
|

Re: At sign ``@'' in key

Dirk Laurie-2
2012/12/24 steve donovan <[hidden email]>:

>
> I think allowing UTF-8 in all identifiers (as Dirk indicates) to be a
> good customization.

Actually, all my suggestion really ensures is that the first character
of a name is either a letter or the start of a UTF-8 sequence. After
that, it is horribly permissive.