Skip to content

Commit dca2439

Browse files
Add SkipValidation parameter to allow warnings for reserved words in Lua parsing
1 parent 62661b9 commit dca2439

4 files changed

Lines changed: 53 additions & 7 deletions

File tree

src/functions/private/ConvertFrom-LuaTable.ps1

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222

2323
# Maximum allowed nesting depth.
2424
[Parameter()]
25-
[int] $MaxDepth = 1024
25+
[int] $MaxDepth = 1024,
26+
27+
# Skip strict Lua grammar validation. Warnings are emitted instead of terminating errors.
28+
[Parameter()]
29+
[switch] $SkipValidation
2630
)
2731

2832
begin {}
@@ -33,6 +37,7 @@
3337
$script:luaAsPSCustomObject = $AsPSCustomObject.IsPresent
3438
$script:luaMaxDepth = $MaxDepth
3539
$script:luaCurrentDepth = 0
40+
$script:luaSkipValidation = $SkipValidation.IsPresent
3641

3742
Skip-LuaWhitespace
3843

@@ -110,7 +115,11 @@
110115

111116
# Lua grammar: variable names cannot be reserved words (§3.1)
112117
if ($varName -in $reservedWords) {
113-
throw "Reserved word '$varName' cannot be used as a variable name at position $identStart."
118+
if ($script:luaSkipValidation) {
119+
Write-Warning "Reserved word '$varName' used as a variable name at position $identStart."
120+
} else {
121+
throw "Reserved word '$varName' cannot be used as a variable name at position $identStart."
122+
}
114123
}
115124

116125
Skip-LuaWhitespace

src/functions/private/Read-LuaTable.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@
8888
$script:luaString[$script:luaPos] -eq '=') {
8989
# Lua grammar: Name cannot be a reserved word (§3.1)
9090
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."
91+
if ($script:luaSkipValidation) {
92+
Write-Warning "Reserved word '$ident' used as a bare identifier key at position $identStart."
93+
} else {
94+
throw "Reserved word '$ident' cannot be used as a bare identifier key in a Lua table. Use bracket notation: [`"$ident`"] = value."
95+
}
9296
}
9397
# Key = value pair
9498
$script:luaPos++ # skip =

src/functions/public/Lua/ConvertFrom-Lua.ps1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,17 @@
7777

7878
# Output arrays as a single object instead of enumerating elements through the pipeline.
7979
[Parameter()]
80-
[switch] $NoEnumerate
80+
[switch] $NoEnumerate,
81+
82+
# Skip strict Lua grammar validation (e.g., reserved words as bare keys). Warnings are emitted instead of errors.
83+
[Parameter()]
84+
[switch] $SkipValidation
8185
)
8286

8387
begin {}
8488

8589
process {
86-
$result = ConvertFrom-LuaTable -InputString $InputObject -AsPSCustomObject:(-not $AsHashtable) -MaxDepth $Depth
90+
$result = ConvertFrom-LuaTable -InputString $InputObject -AsPSCustomObject:(-not $AsHashtable) -MaxDepth $Depth -SkipValidation:$SkipValidation
8791
if ($NoEnumerate -and $result -is [System.Array]) {
8892
Write-Output -InputObject $result -NoEnumerate
8993
} else {

tests/Lua.Tests.ps1

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,37 @@ B = { val = 2 }
526526
{ ConvertFrom-Lua -InputObject 'end = 1' } | Should -Throw '*Reserved word*'
527527
}
528528

529-
It 'Throws on reserved word as assignment variable name (return as assignment)' {
530-
{ ConvertFrom-Lua -InputObject 'return = 42' } | Should -Throw '*Reserved word*'
529+
It 'Throws on reserved word as assignment variable name (while as assignment)' {
530+
{ ConvertFrom-Lua -InputObject 'while = 42' } | Should -Throw '*Reserved word*'
531+
}
532+
533+
It 'SkipValidation allows reserved word as bare table key with warning' {
534+
$result = ConvertFrom-Lua -InputObject '{ end = 1 }' -AsHashtable -SkipValidation 3>&1
535+
$warnings = $result | Where-Object { $_ -is [System.Management.Automation.WarningRecord] }
536+
$output = $result | Where-Object { $_ -isnot [System.Management.Automation.WarningRecord] }
537+
$warnings.Message | Should -BeLike '*Reserved word*end*'
538+
$output['end'] | Should -Be 1
539+
}
540+
541+
It 'SkipValidation allows reserved word as assignment variable name with warning' {
542+
$result = ConvertFrom-Lua -InputObject 'end = 1' -AsHashtable -SkipValidation 3>&1
543+
$warnings = $result | Where-Object { $_ -is [System.Management.Automation.WarningRecord] }
544+
$output = $result | Where-Object { $_ -isnot [System.Management.Automation.WarningRecord] }
545+
$warnings.Message | Should -BeLike '*Reserved word*end*'
546+
$output['end'] | Should -Be 1
547+
}
548+
549+
It 'SkipValidation emits a warning for each reserved word occurrence' {
550+
$result = ConvertFrom-Lua -InputObject '{ end = 1, while = 2, for = 3 }' -AsHashtable -SkipValidation 3>&1
551+
$warnings = @($result | Where-Object { $_ -is [System.Management.Automation.WarningRecord] })
552+
$output = $result | Where-Object { $_ -isnot [System.Management.Automation.WarningRecord] }
553+
$warnings.Count | Should -Be 3
554+
$warnings[0].Message | Should -BeLike '*end*'
555+
$warnings[1].Message | Should -BeLike '*while*'
556+
$warnings[2].Message | Should -BeLike '*for*'
557+
$output['end'] | Should -Be 1
558+
$output['while'] | Should -Be 2
559+
$output['for'] | Should -Be 3
531560
}
532561
}
533562

0 commit comments

Comments
 (0)