(c) Support for params with unlimited amount of pointers
I created a new option in tree called : 'first_recursive' that will find the first node matching the node_type recursively. Closes #10
This commit is contained in:
@@ -1,40 +1,3 @@
|
|||||||
-- hello.c:
|
|
||||||
--
|
|
||||||
-- #include <stdio.h>
|
|
||||||
--
|
|
||||||
-- int main(int argc, char *argv[])
|
|
||||||
-- {
|
|
||||||
-- printf("Hello world!\n");
|
|
||||||
-- return 0;
|
|
||||||
-- }
|
|
||||||
--
|
|
||||||
-- AST
|
|
||||||
--
|
|
||||||
-- preproc_include [0, 0] - [2, 0]
|
|
||||||
-- path: system_lib_string [0, 9] - [0, 18]
|
|
||||||
-- function_definition [2, 0] - [6, 1]
|
|
||||||
-- type: primitive_type [2, 0] - [2, 3]
|
|
||||||
-- declarator: function_declarator [2, 4] - [2, 32]
|
|
||||||
-- declarator: identifier [2, 4] - [2, 8]
|
|
||||||
-- parameters: parameter_list [2, 8] - [2, 32]
|
|
||||||
-- parameter_declaration [2, 9] - [2, 17]
|
|
||||||
-- type: primitive_type [2, 9] - [2, 12]
|
|
||||||
-- declarator: identifier [2, 13] - [2, 17]
|
|
||||||
-- parameter_declaration [2, 19] - [2, 31]
|
|
||||||
-- type: primitive_type [2, 19] - [2, 23]
|
|
||||||
-- declarator: pointer_declarator [2, 24] - [2, 31]
|
|
||||||
-- declarator: array_declarator [2, 25] - [2, 31]
|
|
||||||
-- declarator: identifier [2, 25] - [2, 29]
|
|
||||||
-- body: compound_statement [3, 0] - [6, 1]
|
|
||||||
-- expression_statement [4, 4] - [4, 29]
|
|
||||||
-- call_expression [4, 4] - [4, 28]
|
|
||||||
-- function: identifier [4, 4] - [4, 10]
|
|
||||||
-- arguments: argument_list [4, 10] - [4, 28]
|
|
||||||
-- string_literal [4, 11] - [4, 27]
|
|
||||||
-- escape_sequence [4, 24] - [4, 26]
|
|
||||||
-- return_statement [5, 4] - [5, 13]
|
|
||||||
-- number_literal [5, 11] - [5, 12]
|
|
||||||
|
|
||||||
local c_function_extractor = function(node)
|
local c_function_extractor = function(node)
|
||||||
local tree = {
|
local tree = {
|
||||||
{
|
{
|
||||||
@@ -49,35 +12,7 @@ local c_function_extractor = function(node)
|
|||||||
retrieve = "all",
|
retrieve = "all",
|
||||||
node_type = "parameter_declaration",
|
node_type = "parameter_declaration",
|
||||||
subtree = {
|
subtree = {
|
||||||
{
|
{ retrieve = "first_recursive", node_type = "identifier", extract = true}
|
||||||
retrieve = "all",
|
|
||||||
node_type = "identifier",
|
|
||||||
extract = true,
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
retrieve = "all",
|
|
||||||
node_type = "pointer_declarator",
|
|
||||||
subtree = {
|
|
||||||
{
|
|
||||||
retrieve = "all",
|
|
||||||
node_type = "identifier",
|
|
||||||
extract = true,
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
retrieve = "all",
|
|
||||||
node_type = "array_declarator",
|
|
||||||
subtree = {
|
|
||||||
{
|
|
||||||
retrieve = "all",
|
|
||||||
node_type = "identifier",
|
|
||||||
extract = true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -25,6 +25,26 @@ neogen.utilities.nodes = {
|
|||||||
return results
|
return results
|
||||||
end,
|
end,
|
||||||
|
|
||||||
|
--- Find all nested childs from `parent` that match `node_name`. Returns a table of found nodes
|
||||||
|
--- @param parent userdata
|
||||||
|
--- @param node_name string
|
||||||
|
--- @param result table
|
||||||
|
--- @return table
|
||||||
|
recursive_find = function(self, parent, node_name, result)
|
||||||
|
local results = result or {}
|
||||||
|
|
||||||
|
for child in parent:iter_children() do
|
||||||
|
if child:named() and child:type() == node_name then
|
||||||
|
table.insert(results, child)
|
||||||
|
else
|
||||||
|
local found = self:recursive_find(child, node_name, results)
|
||||||
|
vim.tbl_deep_extend("keep", results, found)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return results
|
||||||
|
end,
|
||||||
|
|
||||||
--- Get all required nodes from tree
|
--- Get all required nodes from tree
|
||||||
--- @param parent userdata the parent node
|
--- @param parent userdata the parent node
|
||||||
--- @param tree table a nested table : { retrieve = "all|first", node_type = node_name, subtree = tree }
|
--- @param tree table a nested table : { retrieve = "all|first", node_type = node_name, subtree = tree }
|
||||||
@@ -52,6 +72,10 @@ neogen.utilities.nodes = {
|
|||||||
matched = { matched[1] }
|
matched = { matched[1] }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if subtree.retrieve == "first_recursive" then
|
||||||
|
matched = self:recursive_find(parent, subtree.node_type)
|
||||||
|
end
|
||||||
|
|
||||||
for _, child in pairs(matched) do
|
for _, child in pairs(matched) do
|
||||||
if subtree.extract == true then
|
if subtree.extract == true then
|
||||||
local name = subtree.node_type or "_"
|
local name = subtree.node_type or "_"
|
||||||
|
|||||||
Reference in New Issue
Block a user