Accented variable names

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

Accented variable names

astient
Hi,

Could you please tell me if it possible to use accented variable names in Lua ? I'm using a the IDE named SciTE (adapted to french) to do some Lua code and i can use french accented character inside a string, but i always have an error when i try to use and accented variable name like "caché" :

-- don't work:
table ={caché="test"}

-- don't work:
table.caché = "test"

-- work (but not very handy):
table["caché"]="test"

the error message (given by lua.exe 1.5.1) is : '=' expected near 'é'

I precise that I have no problem to deal with accented string that are well recognized :

-- this work
table.cache="my string has accented character like the french é or à"

so my ide recognized french accented characters without problem since it is declared in a string (with quote). The problem is only when i want to named a variable with an accent, but i don't know if the problem comes from my IDE or from Lua.

So can Lua use variable name with accented characters ?

Thank you

Reply | Threaded
Open this post in threaded view
|

Re: Accented variable names

Tim Hill

On Aug 12, 2014, at 2:41 PM, [hidden email] wrote:

Hi,

Could you please tell me if it possible to use accented variable names in Lua ? I'm using a the IDE named SciTE (adapted to french) to do some Lua code and i can use french accented character inside a string, but i always have an error when i try to use and accented variable name like "caché” :

Lua only accepts basic “A” .. “Z” and “a” to “z” as letters allowed in identifiers.

—Tim


Reply | Threaded
Open this post in threaded view
|

Re: Accented variable names

astient
Hi again,

Thank you Tim for your very clear answer that will avoid me to loose more time on that problem.


De: "Tim Hill" <[hidden email]>
À: "Lua mailing list" <[hidden email]>
Envoyé: Mercredi 13 Août 2014 00:02:54
Objet: Re: Accented variable names


On Aug 12, 2014, at 2:41 PM, [hidden email] wrote:

Hi,

Could you please tell me if it possible to use accented variable names in Lua ? I'm using a the IDE named SciTE (adapted to french) to do some Lua code and i can use french accented character inside a string, but i always have an error when i try to use and accented variable name like "caché” :

Lua only accepts basic “A” .. “Z” and “a” to “z” as letters allowed in identifiers.

—Tim



Reply | Threaded
Open this post in threaded view
|

Re: Accented variable names

astient
In reply to this post by Tim Hill
Sorry, i forgot to ask you why in this case Lua accept this way to define identifiers with an accent:

table={}
table["caché"]="test"

isn't this way equivalent to: table.caché="test" ? However the first one is accepted (i can do a print of table["caché"]) but the second gives an error as you explain.


De: "Tim Hill" <[hidden email]>
À: "Lua mailing list" <[hidden email]>
Envoyé: Mercredi 13 Août 2014 00:02:54
Objet: Re: Accented variable names


On Aug 12, 2014, at 2:41 PM, [hidden email] wrote:

Hi,

Could you please tell me if it possible to use accented variable names in Lua ? I'm using a the IDE named SciTE (adapted to french) to do some Lua code and i can use french accented character inside a string, but i always have an error when i try to use and accented variable name like "caché” :

Lua only accepts basic “A” .. “Z” and “a” to “z” as letters allowed in identifiers.

—Tim



Reply | Threaded
Open this post in threaded view
|

Re: Accented variable names

Geoff Leyland

On 13/08/2014, at 10:34 am, [hidden email] wrote:

> Sorry, i forgot to ask you why in this case Lua accept this way to define identifiers with an accent:
>
> table={}
> table["caché"]="test"
>
> isn't this way equivalent to: table.caché="test" ? However the first one is accepted (i can do a print of table["caché"]) but the second gives an error as you explain.

If it helps understand, it’s legal to set 'table[“this has spaces”]', but 'table.this has spaces’ obviously won’t work.  Table keys can be pretty much anything (strings, numbers, tables, functions), but you when you’re using the ‘.’ notation you can only use keys that parse as identifiers.


Reply | Threaded
Open this post in threaded view
|

Re: Accented variable names

Coroutines
On Tue, Aug 12, 2014 at 3:38 PM, Geoff Leyland
<[hidden email]> wrote:

> If it helps understand, it’s legal to set 'table[“this has spaces”]', but 'table.this has spaces’ obviously won’t work.  Table keys can be pretty much anything (strings, numbers, tables, functions), but you when you’re using the ‘.’ notation you can only use keys that parse as identifiers.

What we need is Roberto to jump in explaining how to modify llex.c in
Lua's source so you can use 'é' and other extended ASCII in the table
shorthand form -- table.identifier is sugar of course :3

Reply | Threaded
Open this post in threaded view
|

Re: Accented variable names

Rena
In reply to this post by astient
On Tue, Aug 12, 2014 at 6:34 PM,  <[hidden email]> wrote:

> Sorry, i forgot to ask you why in this case Lua accept this way to define
> identifiers with an accent:
>
> table={}
> table["caché"]="test"
>
> isn't this way equivalent to: table.caché="test" ? However the first one is
> accepted (i can do a print of table["caché"]) but the second gives an error
> as you explain.
>

Really, it's the other way around: table.caché="test" is shorthand for
table["caché"]="test". In the latter syntax, caché is a string
literal, so it can have any characters inside. In the former, it's an
identifier, so it's limited to certain characters.

--
Sent from my Game Boy.

Reply | Threaded
Open this post in threaded view
|

Re: Accented variable names

Tim Hill
In reply to this post by astient

On Aug 12, 2014, at 3:34 PM, [hidden email] wrote:

Sorry, i forgot to ask you why in this case Lua accept this way to define identifiers with an accent:

table={}
table["caché"]="test"

isn't this way equivalent to: table.caché="test" ? However the first one is accepted (i can do a print of table["caché"]) but the second gives an error as you explain.


Any string (and any other value as well except nil) can be used as a table key so, as you note, table[“caché”] is perfectly valid. As a convenience, Lua provides syntactic sugar when the table key is a valid identifier, allowing you to use table.foo as shorthand for table[“foo”]. However, caché is NOT a valid identifier, and so of course you cannot use the shorthand syntax for table indexing in this case. This is also true for many other strings, for example table[“12”] is valid (and quite different from table[12] of course), but table.12 is not.

—Tim

Reply | Threaded
Open this post in threaded view
|

Re: Accented variable names

Elias Barrionovo

On Aug 12, 2014 7:47 PM, "Tim Hill" <[hidden email]> wrote:
>Any string (and any other value as well except nil) can be used as a table key

As well as nil, NaN ia also not allowed:
t =  {[0/0] = true}  -- raises an error

Reply | Threaded
Open this post in threaded view
|

Re: Accented variable names

astient
In reply to this post by Tim Hill

Thank you very much to all of you for your precise answers and nice examples, it is perfectly clear for me now.


De: "Tim Hill" <[hidden email]>
À: "Lua mailing list" <[hidden email]>
Envoyé: Mercredi 13 Août 2014 00:47:20
Objet: Re: Accented variable names


On Aug 12, 2014, at 3:34 PM, [hidden email] wrote:
Any string (and any other value as well except nil) can be used as a table key so, as you note, table[“caché”] is perfectly valid. As a convenience, Lua provides syntactic sugar when the table key is a valid identifier, allowing you to use table.foo as shorthand for table[“foo”]. However, caché is NOT a valid identifier, and so of course you cannot use the shorthand syntax for table indexing in this case. This is also true for many other strings, for example table[“12”] is valid (and quite different from table[12] of course), but table.12 is not.

—Tim


Reply | Threaded
Open this post in threaded view
|

Re: Accented variable names

Sean Conner
In reply to this post by Coroutines
It was thus said that the Great Coroutines once stated:

> On Tue, Aug 12, 2014 at 3:38 PM, Geoff Leyland
> <[hidden email]> wrote:
>
> > If it helps understand, it’s legal to set 'table[“this has spaces”]',
> > but 'table.this has spaces’ obviously won’t work.  Table keys can be
> > pretty much anything (strings, numbers, tables, functions), but you when
> > you’re using the ‘.’ notation you can only use keys that parse as
> > identifiers.
>
> What we need is Roberto to jump in explaining how to modify llex.c in
> Lua's source so you can use 'é' and other extended ASCII in the table
> shorthand form -- table.identifier is sugar of course :3

  In Lua 5.1, it uses isalpha() to check and therefore, it may or may not
allow other characters, depending upon the locale setting.  I was able to do
the following:

        export LANG=fr_FR
        xterm -fn -*-fixed-*-*-*-*-*-*-*-*-*-*-iso8859-1

and in the new xterm (done so because I usually run with a font based on
Unicode and not ISO-8859-1):

        lua
        Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
        > x = {}
        > x.système = 1
        > print(x.système)
        1
        >

  It failed when I tried LANG=fr_FR.UTF-8 (using a Unicode-based font),
probably because ISO-8859-1 is an 8-bit code only, whereas UTF-8 uses
multiple characters to represent non-ASCII based code-points (an educated
guess, since isalpha() is usually only given values between 0 and CHAR_MAX).

  Lua 5.2 uses a different method---it maintains its own character mapping
table in lctype.c, which would have to be modified appropriately depending
upon the locale and character set encoding you are using.

  The downside to this is that any Lua code written will not be generally
usable on other systems.

  -spc