Bundle LPeg (and ICU4Lua?)
Posted: Wed Aug 03, 2016 5:06 pm
I'm working on a GUI abstraction layer that uses a domain specific language; it's likely that I'll need to use LPeg for accurate (and maintainable) parsing of the DSL. Could LPeg be bundled with Factorio?
A rough example (WIP) of creating a dialog using the DSL:
The UI chunks are "compiled" only once (by LPeg), when the game starts, turning them in to a Lua table representation of the DSL. The 'compiled' tables are set up for fast runtime use. Each time the UI needs to be shown, it's a simple matter of passing the table to a function that renders the UI using LuaGui and LuaGuiElement.
Using this approach, it's possible to create 'custom' components, essentially re-usable UI patterns that mods can incorporate in to their own UI.
I also plan on adding DSLs for defining prototypes in a much simpler, more maintainable manner.
If it's not possible to get LPeg bundled, I can fall back to LulPeg which is written entirely in Lua, but obviously a bit slower than LPeg. Both libraries have identical API, I'm doing my initial dev in LulPeg. There are some weird errors in LulPeg, so if possible I'd really *really* prefer to use the proper LPeg library.
Note: The lpeg.locale features only work on single-byte chars, so if there is a need for UTF-8 support then ICU4Lua will also be needed - I can't find a Lua-only implementation of this so if UTF-8 is required it would definitely need bundling with Factorio (no workarounds available).
A rough example (WIP) of creating a dialog using the DSL:
Code: Select all
Design
'@dlgContent' [[
label #msg 'dlg-label'
]]
'@dlgButtons' [[
button #cancel 'dlg-button-cancel' .btn-cancel
button #confirm 'dlg-button-ok' .btn-confirm
]]
-- ↑ ↑ ↑ ↑
-- type name caption style
local dlg = Dialog 'Hello World' '@dlgContent' '@dlgButtons'
dlg.on('close', function(eventName,eventData) {
game.player.print eventData.button.caption -- localised caption of button clicked
}
dlg.show() -- defaults to local player (player 1)
Using this approach, it's possible to create 'custom' components, essentially re-usable UI patterns that mods can incorporate in to their own UI.
I also plan on adding DSLs for defining prototypes in a much simpler, more maintainable manner.
If it's not possible to get LPeg bundled, I can fall back to LulPeg which is written entirely in Lua, but obviously a bit slower than LPeg. Both libraries have identical API, I'm doing my initial dev in LulPeg. There are some weird errors in LulPeg, so if possible I'd really *really* prefer to use the proper LPeg library.
Code: Select all
local success, lpeg = pcall(require, "lpeg")
lpeg = success and lpeg or require "lulpeg":register(not _ENV and _G)