Skip to content

Commit 62661b9

Browse files
Add reserved word checks for Lua variable names and table keys in conversion functions
1 parent e070ac5 commit 62661b9

4 files changed

Lines changed: 48 additions & 1 deletion

File tree

src/functions/private/ConvertFrom-LuaTable.ps1

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@
8181
}
8282

8383
if ($assignmentDetected) {
84+
# Lua 5.4 reserved words per §3.1
85+
$reservedWords = @(
86+
'and', 'break', 'do', 'else', 'elseif', 'end',
87+
'false', 'for', 'function', 'goto', 'if', 'in',
88+
'local', 'nil', 'not', 'or', 'repeat', 'return',
89+
'then', 'true', 'until', 'while'
90+
)
91+
8492
# Parse one or more assignment statements into an ordered dictionary
8593
$assignments = [ordered]@{}
8694
while ($script:luaPos -lt $script:luaString.Length) {
@@ -100,6 +108,11 @@
100108
}
101109
$varName = $script:luaString.Substring($identStart, $script:luaPos - $identStart)
102110

111+
# Lua grammar: variable names cannot be reserved words (§3.1)
112+
if ($varName -in $reservedWords) {
113+
throw "Reserved word '$varName' cannot be used as a variable name at position $identStart."
114+
}
115+
103116
Skip-LuaWhitespace
104117

105118
# Expect '='

src/functions/private/ConvertTo-LuaTable.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
# Enum handling
5959
if ($InputObject -is [enum]) {
6060
if ($EnumsAsStrings) {
61-
$escaped = $InputObject.ToString() -replace '\\', '\\\\' -replace '"', '\"'
61+
$escaped = $InputObject.ToString() -replace '\\', '\\' -replace '"', '\"'
6262
return "`"$escaped`""
6363
}
6464
$underlyingType = [System.Enum]::GetUnderlyingType($InputObject.GetType())

src/functions/private/Read-LuaTable.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313
begin {}
1414

1515
process {
16+
# Lua 5.4 reserved words per §3.1
17+
$reservedWords = @(
18+
'and', 'break', 'do', 'else', 'elseif', 'end',
19+
'false', 'for', 'function', 'goto', 'if', 'in',
20+
'local', 'nil', 'not', 'or', 'repeat', 'return',
21+
'then', 'true', 'until', 'while'
22+
)
23+
1624
$script:luaCurrentDepth++
1725
if ($script:luaCurrentDepth -gt $script:luaMaxDepth) {
1826
throw "Maximum nesting depth ($($script:luaMaxDepth)) exceeded."
@@ -78,6 +86,10 @@
7886

7987
if ($script:luaPos -lt $script:luaString.Length -and
8088
$script:luaString[$script:luaPos] -eq '=') {
89+
# Lua grammar: Name cannot be a reserved word (§3.1)
90+
if ($ident -in $reservedWords) {
91+
throw "Reserved word '$ident' cannot be used as a bare identifier key in a Lua table. Use bracket notation: [\"$ident\"] = value."
92+
}
8193
# Key = value pair
8294
$script:luaPos++ # skip =
8395
Skip-LuaWhitespace

tests/Lua.Tests.ps1

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,28 @@ B = { val = 2 }
507507
It 'Throws on assignment with missing value' {
508508
{ ConvertFrom-Lua -InputObject 'A = ' } | Should -Throw '*Unexpected end of input*'
509509
}
510+
511+
It 'Throws on reserved word as bare table key' {
512+
{ ConvertFrom-Lua -InputObject '{ end = 1 }' } | Should -Throw '*Reserved word*'
513+
}
514+
515+
It 'Throws on reserved word as bare table key (while)' {
516+
{ ConvertFrom-Lua -InputObject '{ while = "loop" }' } | Should -Throw '*Reserved word*'
517+
}
518+
519+
It 'Allows reserved words in bracket notation' {
520+
$result = ConvertFrom-Lua -InputObject '{ ["end"] = 1, ["while"] = 2 }' -AsHashtable
521+
$result['end'] | Should -Be 1
522+
$result['while'] | Should -Be 2
523+
}
524+
525+
It 'Throws on reserved word as assignment variable name' {
526+
{ ConvertFrom-Lua -InputObject 'end = 1' } | Should -Throw '*Reserved word*'
527+
}
528+
529+
It 'Throws on reserved word as assignment variable name (return as assignment)' {
530+
{ ConvertFrom-Lua -InputObject 'return = 42' } | Should -Throw '*Reserved word*'
531+
}
510532
}
511533

512534
Context 'Pipeline input' {

0 commit comments

Comments
 (0)