Skip to content

Releases: PSModule/Lua

v1.0.1

29 Apr 10:54
d532ebf

Choose a tag to compare

🩹 [Patch]: Reserved words in Lua input now detected with option to skip validation (#6)

ConvertFrom-Lua now validates that bare identifier keys and variable names are not Lua reserved words, throwing a clear error by default. A new -SkipValidation switch allows lenient import of out-of-spec data, emitting per-occurrence warnings instead. Enum string serialization no longer double-escapes backslashes.

  • Fixes #5

Changed: Reserved word validation on deserialization

ConvertFrom-Lua now rejects bare reserved words used as table keys or assignment variable names — matching the Lua 5.4 §3.1 grammar rules. Previously, invalid Lua like { end = 1 } or while = 42 was silently parsed.

# Default — throws a terminating error
ConvertFrom-Lua -InputObject '{ end = 1 }'
# Error: Reserved word 'end' cannot be used as a bare identifier key in a Lua table.
#        Use bracket notation: [\"end\"] = value.

Bracket-notation keys with reserved word strings remain fully supported:

ConvertFrom-Lua -InputObject '{ [\"end\"] = 1, [\"while\"] = 2 }' -AsHashtable
# Returns: @{ end = 1; while = 2 }

New: -SkipValidation switch for lenient import

When importing data that may not be spec-compliant, use -SkipValidation to suppress errors. Each reserved word occurrence produces its own warning, and parsing continues normally.

ConvertFrom-Lua -InputObject '{ end = 1, while = 2 }' -AsHashtable -SkipValidation
# WARNING: Reserved word 'end' used as a bare identifier key at position 2.
# WARNING: Reserved word 'while' used as a bare identifier key at position 11.
# Returns: @{ end = 1; while = 2 }

Fixed: Enum string escaping no longer double-escapes backslashes

ConvertTo-Lua -EnumsAsStrings previously produced \\\\ instead of \\ for backslashes in enum string representations due to an incorrect -replace pattern.

Technical Details

  • ConvertFrom-Lua.ps1: Added -SkipValidation switch parameter, threaded to ConvertFrom-LuaTable via -SkipValidation:$SkipValidation.
  • ConvertFrom-LuaTable.ps1: Added -SkipValidation parameter. Stored as $script:luaSkipValidation for use by recursive parser functions. Added $reservedWords array and validation after variable name extraction in the assignment parsing path — throws or warns based on skip flag.
  • Read-LuaTable.ps1: Added $reservedWords array and validation after bare identifier + = detection — throws or warns based on $script:luaSkipValidation.
  • ConvertTo-LuaTable.ps1: Fixed enum escaping: -replace '\\', '\\\\'-replace '\\', '\\'.
  • return keyword note: return is consumed as a leading keyword before assignment detection (for return { ... } patterns), so return = 42 triggers a different parse error rather than the reserved word check. Tests use while as the second assignment variable test word.
  • Tests: 8 new tests — 5 for default throw behavior (bare table keys ×2, bracket notation positive, assignment variables ×2), 3 for -SkipValidation (bare key warning, assignment warning, multiple warnings with count assertion)."

v1.0.0

16 Apr 06:42
e070ac5

Choose a tag to compare

🌟 [Release]: PowerShell-Lua data conversion now available via ConvertTo-Lua and ConvertFrom-Lua (#3)

PowerShell objects can now be serialized to Lua table constructor strings and deserialized back — enabling round-trip data conversion between PowerShell and Lua configuration files. This is useful for managing configuration data in Lua-based applications such as World of Warcraft addons (e.g., ElvUI).

  • Fixes #2

New: ConvertTo-Lua — serialize PowerShell objects to Lua

ConvertTo-Lua converts hashtables, ordered dictionaries, PSCustomObjects, arrays, and primitives into valid Lua table constructor strings per the Lua 5.4 grammar §3.4.9.

@{ name = "ElvUI"; version = "13.74"; enabled = $true } | ConvertTo-Lua
# Output:
# {
#     enabled = true,
#     name = "ElvUI",
#     version = "13.74"
# }

# Compressed output
@(1, 2, 3) | ConvertTo-Lua -Compress
# Output: {1,2,3}

Parameters:

Parameter Description
-InputObject Any PowerShell object (mandatory, accepts pipeline input)
-Depth Max recursion depth (default 2, range 0–100). Warns when exceeded
-Compress Omit whitespace and indentation
-EnumsAsStrings Serialize enum values as string names instead of numeric values
-AsArray Always wrap output in a Lua sequence table, even for a single value

Properties with $null values are omitted from output (per Lua's nil-means-absent semantics). Keys use bare identifiers when valid per Lua grammar; bracket-quote notation otherwise.

New: ConvertFrom-Lua — deserialize Lua table strings to PowerShell objects

ConvertFrom-Lua parses Lua table constructor strings into PSCustomObject by default, or ordered hashtable with -AsHashtable.

'{ server = "localhost", port = 8080 }' | ConvertFrom-Lua
# Output: PSCustomObject with .server and .port properties

# Round-trip fidelity
'{1, 2, 3}' | ConvertFrom-Lua -NoEnumerate | ConvertTo-Lua -Compress
# Output: {1,2,3}

Parameters:

Parameter Description
-InputObject A Lua table constructor string (mandatory, accepts pipeline input)
-AsHashtable Output [ordered] hashtable instead of PSCustomObject
-Depth Max nesting depth allowed in input (default 1024). Throws on violation
-NoEnumerate Output arrays as a single [object[]] without pipeline enumeration

Supported Lua data types: tables (sequence, key-value, mixed, nested, empty), strings (double-quoted, single-quoted, long strings with all escape sequences), numbers (integer, float, hex, scientific notation), booleans, and nil.

Lua comments (single-line -- and multi-line --[[ ]]) are correctly ignored during parsing.

Technical Details

  • Template files removed: All placeholder files from the PSModule template (Book.ps1, SecretWriter.ps1, Get-PSModuleTest.ps1, New-PSModuleTest.ps1, Set-PSModuleTest.ps1, Test-PSModuleTest.ps1, format/type XML files, data files, variables, etc.) replaced with actual module implementation.
  • Public functions: src/functions/public/Lua/ConvertTo-Lua.ps1 and src/functions/public/Lua/ConvertFrom-Lua.ps1 — thin wrappers handling pipeline input, parameter validation, and -AsArray/-NoEnumerate behavior.
  • Private functions: ConvertTo-LuaTable.ps1 (recursive serializer with depth tracking and 4-space indent), ConvertFrom-LuaTable.ps1 (recursive-descent parser with script-scoped state), Format-LuaKey.ps1 (Lua identifier validation against pattern [a-zA-Z_][a-zA-Z0-9_]* and reserved word list).
  • Parser: Pure PowerShell recursive-descent parser — no external DLL or .NET assembly dependency. Tracks nesting depth and throws when -Depth is exceeded.
  • Tests: 1,158 lines of Pester tests in tests/Lua.Tests.ps1 covering all Lua data types, comment handling, reserved words as keys, depth limits, parameter switches, round-trip conversion, and error cases. Test data files in tests/data/ (Lua/JSON pairs) for complex structure validation.
  • README: Updated with module description, installation instructions, and usage examples.
  • 45 files changed, 2,620 insertions, 648 deletions.