feat(Visitors): implement type_mapping

This commit is contained in:
Aire-One 2023-05-20 15:14:02 +02:00
parent 02d82f6518
commit 37742f795d
6 changed files with 93 additions and 0 deletions

View File

@ -59,10 +59,21 @@ local function create_node(token: Node.Token, name: string): Node
end
local function iter_children(node: Node): function(): integer, Node
if node.children == nil then
return function(): integer, Node end
end
return ipairs(node.children)
end
local function in_order_visitor(node: Node, visitor: function(Node))
for _, child in iter_children(node) do
in_order_visitor(child, visitor)
end
visitor(node)
end
return {
create_node = create_node,
iter_children = iter_children,
in_order_visitor = in_order_visitor,
}

View File

@ -2,6 +2,7 @@ if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1" then
require("lldebugger").start()
end
local ast = require "ast"
local crawler = require "crawler"
local filesystem = require "filesystem"
local printer = require "printer"
@ -10,9 +11,11 @@ local logger = require "logger"
local Map = require "pl.Map"
local Module_Doc = require "entity.Module_Doc"
local Module_Info = require "entity.Module_Info"
local type Node = require "types.Node"
local property = require "property"
local scraper = require "scraper"
local stringx = require "pl.stringx"
local type_mapping = require "visitors.type_mapping"
local utils = require "utils"
local log = logger.log("main")
@ -150,6 +153,10 @@ local module_ast, other_nodes = scraper.module_doc.get_doc_from_page(
"awful.tag"
)
ast.in_order_visitor(module_ast, function(node: Node)
type_mapping.visit(node)
end)
log:info(logger.message_with_metadata("Finished", {
module_ast = module_ast,
other_nodes = other_nodes,

View File

@ -0,0 +1,7 @@
local type Node = require("types.Node")
local record Visitor
visit: function(Node)
end
return Visitor

View File

@ -0,0 +1,68 @@
local type Node = require("types.Node")
local type Visitor = require("types.Visitor")
-- Special types I don't want to deal with for now
local gears_shape_function = "function(cr: any, width: integer, height: integer)"
local type_map <const>: { string : string } = {
awesome = "awesome",
Awesome = "awesome",
bool = "boolean",
client = "Client",
["gears.shape"] = gears_shape_function,
["gears.surface"] = "Surface",
image = "Image",
int = "integer",
screen = "Screen",
shape = gears_shape_function,
surface = "Surface",
tag = "Tag",
["wibox.widget"] = "Widget",
widget = "Widget",
}
local function get_type(t: string): string
return type_map[t] or t
end
local function check_node(node: Node)
if not node.types then
return
end
for i, t in ipairs(node.types) do
node.types[i] = get_type(t)
end
end
local function check_function_parameters(node: Node)
if not node.parameters then
return
end
for _, parameter in ipairs(node.parameters) do
check_node(parameter)
end
end
local function check_function_returns(node: Node)
if not node.return_types then
return
end
for i, ret in ipairs(node.return_types) do
node.return_types[i] = get_type(ret)
end
end
local record Type_Mapping
visit: function(node: Node)
end
function Type_Mapping.visit(node: Node)
check_node(node)
check_function_parameters(node)
check_function_returns(node)
end
return Type_Mapping