Made the token matching narrower

This commit is contained in:
Yessiest 2022-05-08 23:28:19 +04:00
parent af00e0c9d9
commit a492d03ecc
2 changed files with 38 additions and 17 deletions

View File

@ -72,16 +72,16 @@ cron._compare_tables = function(d1,d2)
end end
-- Token types, in (regex, type, preprocessor) format -- Token types, in (regex, type, preprocessor) format
local token_types = { local token_types = {
{"@(%w+)", "directive", function(text) {"^@(%w+)$", "directive", function(text)
return text return text
end}, end},
{"(%d%d)%.(%d%d)%.(%d%d%d?%d?)","date",function(d,m,y) {"^(%d%d)%.(%d%d)%.(%d%d%d?%d?)$","date",function(d,m,y)
return cron._date(d,m,y) return cron._date(d,m,y)
end}, end},
{"(%d%d):(%d%d)","time",function(h,m) {"^(%d%d):(%d%d)$","time",function(h,m)
return cron._time(h,m) return cron._time(h,m)
end}, end},
{"(%d*,.*)", "any_list",function(text) {"^(%d+,[%d,]+)$", "any_list",function(text)
return function(num) return function(num)
local status = false local status = false
text:gsub("%d*",function(number) text:gsub("%d*",function(number)
@ -92,27 +92,27 @@ local token_types = {
return status return status
end end
end}, end},
{"%*/(%d+)", "any_modulo", function(text) {"^%*/(%d+)$", "any_modulo", function(text)
return function(num) return function(num)
return (num % tonumber(text) == 0) return (num % tonumber(text) == 0)
end end
end}, end},
{"%*", "any", function() {"^%*$", "any", function()
return function() return function()
return true return true
end end
end}, end},
{"%d+", "number", function(text) {"^%d+$", "number", function(text)
return function(num) return function(num)
return num == tonumber(text) return num == tonumber(text)
end end
end}, end},
{"^%s*$","spacer", function(text) return text end}, {"^%s*$","spacer", function(text) return text end},
{"%S+","command", function(text) return text end} {"^%S+$","command", function(text) return text end}
} }
-- Valid argument matching predicates for directives -- Valid argument matching predicates for directives
local predtypes = { local predtypes = {
{"([<>])(=?)(%d*)","comparison",function(lm,eq,number) {"^([<>])(=?)(%d+)$","comparison",function(lm,eq,number)
local number = tonumber(number) local number = tonumber(number)
return function(input) return function(input)
local input = tonumber(input) local input = tonumber(input)
@ -122,42 +122,42 @@ local predtypes = {
((lm == "<") and number > input) ((lm == "<") and number > input)
end end
end}, end},
{"/([^/]*)/","regex",function(regex) {"^/([^/]*)/$","regex",function(regex)
return function(input) return function(input)
return (tostring(input):match(regex) ~= nil) return (tostring(input):match(regex) ~= nil)
end end
end}, end},
{"\"([^\"]*)\"","string",function(str) {"^\"([^\"]*)\"$","string",function(str)
return function(input) return function(input)
return str==tostring(input) return str==tostring(input)
end end
end}, end},
{"'([^']*)'","string",function(str) {"^'([^']*)'$","string",function(str)
return function(input) return function(input)
return str==tostring(input) return str==tostring(input)
end end
end}, end},
{"%d+","number",function(number) {"^%d+$","number",function(number)
return function(input) return function(input)
return number == tostring(input) return number == tostring(input)
end end
end}, end},
{"%*","any",function() {"^%*$","any",function()
return function() return function()
return true return true
end end
end}, end},
{":","delimiter",function() {"^:$","delimiter",function()
return function() return function()
error("Delimiter is not a predicate!") error("Delimiter is not a predicate!")
end end
end}, end},
{"%s+","spacer",function() {"^%s+$","spacer",function()
return function() return function()
error("Spacer is not a predicate!") error("Spacer is not a predicate!")
end end
end}, end},
{"%S+","command", function(text) {"^%S+$","command", function(text)
return function() return function()
return text return text
end end

21
libraries/crontests.lua Normal file
View File

@ -0,0 +1,21 @@
cron = require("cron"); b,err = cron.parse_line("@directive /test/ >6 : s"); print(b,err); s,comm = b({"test",7}); print(s,comm)
-- Testing basic functionality
b,err = cron.parse_line("@directive /test/ >6 : s"); print(b,err); print(b({"test",5}));
b,err = cron.parse_line("@directive \"test\" >6 : s"); print(b,err); print(b({"nottest",7}));
b,err = cron.parse_line("*/2 14 */2 * * test"); print(b,err); print(b(os.date("*t")));
b,err = cron.parse_line("08.05.22 15:30 test"); print(b,err); print(b(os.date("*t")));
b,err = cron.parse_line("12:00 08.05.22 test"); print(b,err); print(b(os.date("*t")));
-- Testing command forming
b,err = cron.parse_line("08.05.22 15:30 test 2 the mega test"); print(b,err); print(b(os.date("*t")));
b,err = cron.parse_line("12:00 08.05.22 test supreme"); print(b,err); print(b(os.date("*t")));
b,err = cron.parse_line("*/2 14 */2 * * test of course yet another"); print(b,err); print(b(os.date("*t")));
-- Testing parsing limits
b,err = cron.parse_line("* * * test"); print(b,err);
b,err = cron.parse_line("* * * * * * test"); print(b,err); --actually valid - and should be!
b,err = cron.parse_line("@directive /test/ >6 no delimiter error"); print(b,err);
b,err = cron.parse_line("08.05.22 shit"); print(b,err);
b,err = cron.parse_line("10:20 shit"); print(b,err);
-- Testing parsing dates and time
b,err = cron.parse_line("51.02.2022 shit"); print(b,err);
b,err = cron.parse_line("25:69 shit"); print(b,err);