mirror of
https://github.com/falcosecurity/falco.git
synced 2025-06-27 07:07:23 +00:00
compiler.lua: consistently use 'ast' instead of 'node'
This commit is contained in:
parent
e725be968e
commit
a2ec9870de
@ -338,7 +338,7 @@ end
|
|||||||
definition uses another macro).
|
definition uses another macro).
|
||||||
|
|
||||||
--]]
|
--]]
|
||||||
function expand_macros(node, defs, changed)
|
function expand_macros(ast, defs, changed)
|
||||||
|
|
||||||
function copy(obj)
|
function copy(obj)
|
||||||
if type(obj) ~= 'table' then return obj end
|
if type(obj) ~= 'table' then return obj end
|
||||||
@ -347,110 +347,110 @@ function expand_macros(node, defs, changed)
|
|||||||
return res
|
return res
|
||||||
end
|
end
|
||||||
|
|
||||||
if (node.type == "Rule") then
|
if (ast.type == "Rule") then
|
||||||
macros = expand_macros(node.filter, defs, changed)
|
macros = expand_macros(ast.filter, defs, changed)
|
||||||
elseif node.type == "Filter" then
|
elseif ast.type == "Filter" then
|
||||||
if (node.value.type == "Macro") then
|
if (ast.value.type == "Macro") then
|
||||||
if (defs[node.value.value] == nil) then
|
if (defs[ast.value.value] == nil) then
|
||||||
error("Undefined macro '".. node.value.value .. "' used in filter.")
|
error("Undefined macro '".. ast.value.value .. "' used in filter.")
|
||||||
end
|
end
|
||||||
node.value = copy(defs[node.value.value])
|
ast.value = copy(defs[ast.value.value])
|
||||||
changed = true
|
changed = true
|
||||||
return changed
|
return changed
|
||||||
end
|
end
|
||||||
return expand_macros(node.value, defs, changed)
|
return expand_macros(ast.value, defs, changed)
|
||||||
|
|
||||||
elseif node.type == "BinaryBoolOp" then
|
elseif ast.type == "BinaryBoolOp" then
|
||||||
|
|
||||||
if (node.left.type == "Macro") then
|
if (ast.left.type == "Macro") then
|
||||||
if (defs[node.left.value] == nil) then
|
if (defs[ast.left.value] == nil) then
|
||||||
error("Undefined macro '".. node.left.value .. "' used in filter.")
|
error("Undefined macro '".. ast.left.value .. "' used in filter.")
|
||||||
end
|
end
|
||||||
node.left = copy(defs[node.left.value])
|
ast.left = copy(defs[ast.left.value])
|
||||||
changed = true
|
changed = true
|
||||||
end
|
end
|
||||||
|
|
||||||
if (node.right.type == "Macro") then
|
if (ast.right.type == "Macro") then
|
||||||
if (defs[node.right.value] == nil) then
|
if (defs[ast.right.value] == nil) then
|
||||||
error("Undefined macro ".. node.right.value .. "used in filter.")
|
error("Undefined macro ".. ast.right.value .. "used in filter.")
|
||||||
end
|
end
|
||||||
node.right = copy(defs[node.right.value])
|
ast.right = copy(defs[ast.right.value])
|
||||||
changed = true
|
changed = true
|
||||||
end
|
end
|
||||||
|
|
||||||
local changed_left = expand_macros(node.left, defs, false)
|
local changed_left = expand_macros(ast.left, defs, false)
|
||||||
local changed_right = expand_macros(node.right, defs, false)
|
local changed_right = expand_macros(ast.right, defs, false)
|
||||||
return changed or changed_left or changed_right
|
return changed or changed_left or changed_right
|
||||||
|
|
||||||
elseif node.type == "UnaryBoolOp" then
|
elseif ast.type == "UnaryBoolOp" then
|
||||||
if (node.argument.type == "Macro") then
|
if (ast.argument.type == "Macro") then
|
||||||
if (defs[node.argument.value] == nil) then
|
if (defs[ast.argument.value] == nil) then
|
||||||
error("Undefined macro ".. node.argument.value .. "used in filter.")
|
error("Undefined macro ".. ast.argument.value .. "used in filter.")
|
||||||
end
|
end
|
||||||
node.argument = copy(defs[node.argument.value])
|
ast.argument = copy(defs[ast.argument.value])
|
||||||
changed = true
|
changed = true
|
||||||
end
|
end
|
||||||
return expand_macros(node.argument, defs, changed)
|
return expand_macros(ast.argument, defs, changed)
|
||||||
end
|
end
|
||||||
return changed
|
return changed
|
||||||
end
|
end
|
||||||
|
|
||||||
function get_macros(node, set)
|
function get_macros(ast, set)
|
||||||
if (node.type == "Macro") then
|
if (ast.type == "Macro") then
|
||||||
set[node.value] = true
|
set[ast.value] = true
|
||||||
return set
|
return set
|
||||||
end
|
end
|
||||||
|
|
||||||
if node.type == "Filter" then
|
if ast.type == "Filter" then
|
||||||
return get_macros(node.value, set)
|
return get_macros(ast.value, set)
|
||||||
end
|
end
|
||||||
|
|
||||||
if node.type == "BinaryBoolOp" then
|
if ast.type == "BinaryBoolOp" then
|
||||||
local left = get_macros(node.left, {})
|
local left = get_macros(ast.left, {})
|
||||||
local right = get_macros(node.right, {})
|
local right = get_macros(ast.right, {})
|
||||||
|
|
||||||
for m, _ in pairs(left) do set[m] = true end
|
for m, _ in pairs(left) do set[m] = true end
|
||||||
for m, _ in pairs(right) do set[m] = true end
|
for m, _ in pairs(right) do set[m] = true end
|
||||||
|
|
||||||
return set
|
return set
|
||||||
end
|
end
|
||||||
if node.type == "UnaryBoolOp" then
|
if ast.type == "UnaryBoolOp" then
|
||||||
return get_macros(node.argument, set)
|
return get_macros(ast.argument, set)
|
||||||
end
|
end
|
||||||
return set
|
return set
|
||||||
end
|
end
|
||||||
|
|
||||||
function print_ast(node, level)
|
function print_ast(ast, level)
|
||||||
local t = node.type
|
local t = ast.type
|
||||||
level = level or 0
|
level = level or 0
|
||||||
local prefix = string.rep(" ", level*2)
|
local prefix = string.rep(" ", level*2)
|
||||||
level = level + 1
|
level = level + 1
|
||||||
|
|
||||||
if t == "Rule" then
|
if t == "Rule" then
|
||||||
print_ast(node.filter, level)
|
print_ast(ast.filter, level)
|
||||||
if (node.output) then
|
if (ast.output) then
|
||||||
print(prefix.."| "..node.output.value)
|
print(prefix.."| "..ast.output.value)
|
||||||
end
|
end
|
||||||
elseif t == "Filter" then
|
elseif t == "Filter" then
|
||||||
print_ast(node.value, level)
|
print_ast(ast.value, level)
|
||||||
|
|
||||||
elseif t == "BinaryBoolOp" or t == "BinaryRelOp" then
|
elseif t == "BinaryBoolOp" or t == "BinaryRelOp" then
|
||||||
print(prefix..node.operator)
|
print(prefix..ast.operator)
|
||||||
print_ast(node.left, level)
|
print_ast(ast.left, level)
|
||||||
print_ast(node.right, level)
|
print_ast(ast.right, level)
|
||||||
|
|
||||||
elseif t == "UnaryRelOp" or t == "UnaryBoolOp" then
|
elseif t == "UnaryRelOp" or t == "UnaryBoolOp" then
|
||||||
print (prefix..node.operator)
|
print (prefix..ast.operator)
|
||||||
print_ast(node.argument, level)
|
print_ast(ast.argument, level)
|
||||||
|
|
||||||
elseif t == "List" then
|
elseif t == "List" then
|
||||||
print(prefix.. "List: ")
|
print(prefix.. "List: ")
|
||||||
for i, v in ipairs(node.elements) do
|
for i, v in ipairs(ast.elements) do
|
||||||
print_ast(v, level)
|
print_ast(v, level)
|
||||||
end
|
end
|
||||||
|
|
||||||
elseif t == "FieldName" or t == "Number" or t == "String" or t == "BareString" or t == "Macro" then
|
elseif t == "FieldName" or t == "Number" or t == "String" or t == "BareString" or t == "Macro" then
|
||||||
print (prefix..t.." "..node.value)
|
print (prefix..t.." "..ast.value)
|
||||||
|
|
||||||
elseif t == "MacroDef" then
|
elseif t == "MacroDef" then
|
||||||
-- don't print for now
|
-- don't print for now
|
||||||
|
Loading…
Reference in New Issue
Block a user