Module:Template input parse: Difference between revisions

Ganaram inukshuk (talk | contribs)
Abandoned code that would return nil if the resulting tables were size zero
Ganaram inukshuk (talk | contribs)
m testing...?
 
(5 intermediate revisions by 2 users not shown)
Line 2: Line 2:


-- Helper module for various modules that require entry of multiple labels/values
-- Helper module for various modules that require entry of multiple labels/values
-- TODO:
-- Functions that return size-0 arrays should return nil instead.


-- Helper function
-- Helper function
Line 11: Line 8:
function p.parse_entries(unparsed, delimiter)
function p.parse_entries(unparsed, delimiter)
local parsed = {}
local parsed = {}
local delimiter = delimiter or ';'
local delimiter = delimiter or ";"
local expr = '([^' .. delimiter .. ']+)'
local expr = "([^" .. delimiter .. "]+)"
for entry in string.gmatch(unparsed, expr) do
for entry in string.gmatch(unparsed, expr) do
local trimmed = entry:gsub("^%s*(.-)%s*$", "%1")
local trimmed = entry:gsub("^%s*(.-)%s*$", "%1")
Line 26: Line 23:
function p.parse_numeric_entries(unparsed, delimiter)
function p.parse_numeric_entries(unparsed, delimiter)
local parsed = {}
local parsed = {}
local delimiter = delimiter or ';'
local delimiter = delimiter or ";"
local expr = '([^' .. delimiter .. ']+)'
local expr = "([^" .. delimiter .. "]+)"
for entry in string.gmatch(unparsed, expr) do
for entry in string.gmatch(unparsed, expr) do
local trimmed = entry:gsub("^%s*(.-)%s*$", "%1")
local trimmed = entry:gsub("^%s*(.-)%s*$", "%1")
Line 40: Line 37:
function p.parse_pair(unparsed, delimiter)
function p.parse_pair(unparsed, delimiter)
local parsed = {}
local parsed = {}
local delimiter = delimiter or ':'
local delimiter = delimiter or ":"
local expr = '([^' .. delimiter .. ']+)'
local expr = "([^" .. delimiter .. "]+)"
for entry in string.gmatch(unparsed, expr) do
for entry in string.gmatch(unparsed, expr) do
local trimmed = entry:gsub("^%s*(.-)%s*$", "%1")
local trimmed = entry:gsub("^%s*(.-)%s*$", "%1")
Line 55: Line 52:
-- Helper function
-- Helper function
-- Parses a pair of numeric values
-- Parses a pair of numeric values
function p.parse_numeric_pair(unparsed, delimiter)
-- Passing in TRUE for is_fuzzy allows singular values to be parsed as ratios
-- whose denominator is 1; default is FALSE
function p.parse_numeric_pair(unparsed, delimiter, is_fuzzy)
local parsed = {}
local parsed = {}
local delimiter = delimiter or ':'
local delimiter = delimiter or ":"
local expr = '([^' .. delimiter .. ']+)'
local is_fuzzy = is_fuzzy or false
local expr = "([^" .. delimiter .. "]+)"
for entry in string.gmatch(unparsed, expr) do
for entry in string.gmatch(unparsed, expr) do
local trimmed = entry:gsub("^%s*(.-)%s*$", "%1")
local trimmed = entry:gsub("^%s*(.-)%s*$", "%1")
Line 66: Line 66:
if #parsed == 2 then
if #parsed == 2 then
return parsed
return parsed
elseif #parsed == 1 and is_fuzzy then
return { parsed[1], 1 }
else
else
return nil
return nil
Line 73: Line 75:
-- Helper function
-- Helper function
-- Parses a list of numeric pairs
-- Parses a list of numeric pairs
-- Delimiter between each pair must be a semicolon
-- Delimiter between each entry can be anything; default is semicolon; this
-- allows for entry of subgroup entries
-- Delimiter between the pair's two values can be anything; default is slash
-- Delimiter between the pair's two values can be anything; default is slash
-- as the intended use is parsing ratios a/b, so input must be formatted as such
-- as the intended use is parsing ratios a/b, so input must be formatted as such
-- "a/b; c/d; e/f"
-- "a/b; c/d; e/f"
function p.parse_numeric_pairs(unparsed, delimiter)
-- Passing in TRUE for is_fuzzy allows singular values to be parsed as ratios
local delimiter = delimiter or "/"
-- whose denominator is 1; defualt is FALSE.
function p.parse_numeric_pairs(unparsed, delimiter1, delimiter2, is_fuzzy)
local delimiter1 = delimiter1 or ";"
local delimiter2 = delimiter2 or "/"
local is_fuzzy = is_fuzzy or false
-- Split the string of unparsed pairs
-- Split the string of unparsed pairs
local parsed = p.parse_entries(unparsed)
local parsed = p.parse_entries(unparsed, delimiter1)
-- Then tokenize the tokens into numeric pairs
-- Then tokenize the tokens into numeric pairs
local pairs_ = {}
local pairs_ = {}
for i = 1, #parsed do
for i = 1, #parsed do
local pair = p.parse_numeric_pair(parsed[i], delimiter)
local pair = p.parse_numeric_pair(parsed[i], delimiter2, is_fuzzy)
if pair ~= nil and #pair == 2 then
if pair ~= nil and #pair == 2 then
table.insert(pairs_, pair)
table.insert(pairs_, pair)
end
end
end
end
return pairs_
end
end


Line 107: Line 115:
end
end
end
end
return pairs_
end
function p.tester()
local parse_me = "15625/15552; 325/324, 625/624"
local parsed_me = p.parse_entries(parse_me, ";")
local parsed_me_2 = {}
for i = 1, #parsed_me do
local parsed = p.parse_numeric_pairs(parsed_me[i], ",", false)
table.insert(parsed_me_2, parsed)
end
return parsed_me_2
end
end


return p
return p