[switch]: implemented checks for missing [case], variable= and value=, Lua part

This commit is contained in:
Elvish_Hunter 2014-04-03 12:33:20 +02:00
parent 7e4beb8630
commit df3063ed70

View file

@ -380,23 +380,37 @@ wml_actions["while"] = function( cfg )
end
function wml_actions.switch(cfg)
local value = wesnoth.get_variable(cfg.variable)
-- check if variable= is missing
if not cfg.variable then
helper.wml_error "[switch] missing required variable= attribute"
end
local variable = wesnoth.get_variable(cfg.variable)
local found = false
-- check if the [case] sub-tag is missing, and raise error if so
if not helper.get_child( cfg, "case" ) then
helper.wml_error "[switch] missing required [case] tag"
end
-- Execute all the [case]s where the value matches.
for v in helper.child_range(cfg, "case") do
for case_child in helper.child_range(cfg, "case") do
-- warn if value= isn't present; it may be false, so check only for nil
if case_child.value == nil then
helper.wml_error "[case] missing required value= attribute"
end
local match = false
for w in string.gmatch(v.value, "[^%s,][^,]*") do
if w == tostring(value) then match = true ; break end
for w in string.gmatch(case_child.value, "[^%s,][^,]*") do
if w == tostring(variable) then match = true ; break end
end
if match then
handle_event_commands(v)
handle_event_commands(case_child)
found = true
end
end
-- Otherwise execute [else] statements.
if not found then
for v in helper.child_range(cfg, "else") do
handle_event_commands(v)
for else_child in helper.child_range(cfg, "else") do
handle_event_commands(else_child)
end
end
end