From 733548b80bc5a892dfee0bf9bf8e0f107aaa7466 Mon Sep 17 00:00:00 2001 From: Henri DF Date: Sun, 28 Feb 2016 14:24:54 -0800 Subject: [PATCH 1/2] Grammar: fix commas in lists commas were (unintentionally) optional in lists. so a list like (a b c) was a valid three-element list. --- userspace/digwatch/lua/compiler.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/userspace/digwatch/lua/compiler.lua b/userspace/digwatch/lua/compiler.lua index a976fbe7..84a6a69f 100644 --- a/userspace/digwatch/lua/compiler.lua +++ b/userspace/digwatch/lua/compiler.lua @@ -99,7 +99,7 @@ end local function list (pat, sep) - return Ct(pat^0 * (sep * pat^0)^0) / function(elements) return {type = "List", elements=elements} end + return Ct(pat^-1 * (sep * pat^0)^0) / function(elements) return {type = "List", elements=elements} end end --http://lua-users.org/wiki/StringTrim From 6569f0372dd0a004e71b34ed1dd100e4317d7fb7 Mon Sep 17 00:00:00 2001 From: Henri DF Date: Sun, 28 Feb 2016 14:28:00 -0800 Subject: [PATCH 2/2] Grammar: support function-call syntax in outputs This change adds syntax support for function call outputs. For example: ... | syslog(evt, WARN) Regular outputs are still allowed and parsed in the same way. --- userspace/digwatch/lua/compiler.lua | 21 ++++++++++++++++++--- userspace/digwatch/lua/smoke.sh | 18 +++++++++++++----- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/userspace/digwatch/lua/compiler.lua b/userspace/digwatch/lua/compiler.lua index 84a6a69f..aadd33bc 100644 --- a/userspace/digwatch/lua/compiler.lua +++ b/userspace/digwatch/lua/compiler.lua @@ -167,6 +167,10 @@ local function outputformat (format) return {type = "OutputFormat", value = format} end +local function functioncall (name, args) + return {type = "FunctionCall", name = name, arguments = args} +end + local function rule(filter, output) if not output then output = outputformat("") @@ -182,7 +186,7 @@ local G = { -- Grammar Comment = P"#" * P(1)^0; - Rule = V"Filter" / filter * ((V"Skip" * V"Pipe" * V"Skip" * (P(1)^0 / outputformat) )^-1 ); + Rule = V"Filter" / filter * ((V"Skip" * V"Pipe" * V"Skip" * V"Output")^-1 ); Filter = V"OrExpression"; OrExpression = @@ -212,6 +216,9 @@ local G = { MacroDef = (C(V"Macro") * V"Skip" * V"Colon" * (V"Filter")); + FuncArgs = symb("(") * list(V"Value", symb(",")) * symb(")"); + Output = ((V"Name" * V"FuncArgs") / functioncall) + P(1)^0 / outputformat; + -- Terminals Value = terminal "Number" + terminal "String" + terminal "BareString"; @@ -448,8 +455,17 @@ function print_ast(ast, level) if t == "Rule" then print_ast(ast.filter, level) if (ast.output) then - print(prefix.."| "..ast.output.value) + print(prefix.."| ") + print_ast(ast.output) end + elseif t == "OutputFormat" then + print(ast.value) + + elseif t == "FunctionCall" then + print(ast.name .. "(" ) + print_ast(ast.arguments) + print(")") + elseif t == "Filter" then print_ast(ast.value, level) @@ -463,7 +479,6 @@ function print_ast(ast, level) print_ast(ast.argument, level) elseif t == "List" then - print(prefix.. "List: ") for i, v in ipairs(ast.elements) do print_ast(v, level) end diff --git a/userspace/digwatch/lua/smoke.sh b/userspace/digwatch/lua/smoke.sh index 8ba7b357..06802fe4 100755 --- a/userspace/digwatch/lua/smoke.sh +++ b/userspace/digwatch/lua/smoke.sh @@ -57,8 +57,6 @@ good "evt.dir=> and fd.name=*.log" good "evt.dir=> and fd.name=/var/log/httpd.log" good "a.g in (1, 'a', b.c)" good "a.b = a.a" -good "a.b = a.a |" -good "a.b = a.a | %evt.type %fd.num blabla" good "evt.arg[0] contains /bin" bad "evt.arg[a] contains /bin" @@ -67,9 +65,9 @@ bad "evt.arg[] contains /bin" bad "a.g in ()" bad "a.b = b = 1" bad "(a.b = 1" -bad " | %a.b" -# Macros + +# Macros good "a: a.b exists" good "a: b and c" good "a: b" @@ -77,9 +75,19 @@ good "a : b" good "a : evt.dir=>" good "inbound: (syscall.type=listen and evt.dir='>') or (syscall.type=accept and evt.dir='<')" bad "a:" -bad "a : b | bla" bad "b and d" +# Outputs +good "a.b = a.a |" +good "a.b = a.a | %evt.type %fd.num blabla" +good "a.b = a.a | blabla (%evt.type %fd.num)" +good "a.b = a.a | f(evt)" +good "a.b = a.a | f()" +bad "a.b = a.a | f() evt" + +bad "a : b | bla" +bad " | %a.b" + echo echo "All tests passed."