diff --git a/src/awesomewm.d.tl/ast.tl b/src/awesomewm.d.tl/ast.tl index 0f8a608..b74390e 100644 --- a/src/awesomewm.d.tl/ast.tl +++ b/src/awesomewm.d.tl/ast.tl @@ -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, } diff --git a/src/awesomewm.d.tl/init.tl b/src/awesomewm.d.tl/init.tl index 5fa2966..f1bc8c2 100644 --- a/src/awesomewm.d.tl/init.tl +++ b/src/awesomewm.d.tl/init.tl @@ -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, diff --git a/src/awesomewm.d.tl/types/Dag.d.tl b/src/awesomewm.d.tl/types/Dag.tl similarity index 100% rename from src/awesomewm.d.tl/types/Dag.d.tl rename to src/awesomewm.d.tl/types/Dag.tl diff --git a/src/awesomewm.d.tl/types/Node.d.tl b/src/awesomewm.d.tl/types/Node.tl similarity index 100% rename from src/awesomewm.d.tl/types/Node.d.tl rename to src/awesomewm.d.tl/types/Node.tl diff --git a/src/awesomewm.d.tl/types/Visitor.tl b/src/awesomewm.d.tl/types/Visitor.tl new file mode 100644 index 0000000..9b2b26e --- /dev/null +++ b/src/awesomewm.d.tl/types/Visitor.tl @@ -0,0 +1,7 @@ +local type Node = require("types.Node") + +local record Visitor + visit: function(Node) +end + +return Visitor diff --git a/src/awesomewm.d.tl/visitors/type_mapping.tl b/src/awesomewm.d.tl/visitors/type_mapping.tl new file mode 100644 index 0000000..8699c9b --- /dev/null +++ b/src/awesomewm.d.tl/visitors/type_mapping.tl @@ -0,0 +1,66 @@ +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 : { 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 type Type_Coercion = Visitor + +function Type_Coercion.visit(node: Node) + check_node(node) + check_function_parameters(node) + check_function_returns(node) +end + +return Type_Coercion