diff --git a/src/awesomewm.d.tl/ast.tl b/src/awesomewm.d.tl/ast.tl index b74390e..6f5dae8 100644 --- a/src/awesomewm.d.tl/ast.tl +++ b/src/awesomewm.d.tl/ast.tl @@ -1,10 +1,12 @@ local type Node = require("types.Node") -local basic_nodes : { Node.Token : function(name: string): Node } = { - module = function(name: string): Node +local basic_nodes : { Node.Token : function(name: string, ...: any): Node } = { + module = function(name: string, module_path: string): Node return { token = "module", name = name, + module_path = module_path, + dependencies = {}, children = {}, } end, @@ -53,8 +55,8 @@ local basic_nodes : { Node.Token : function(name: string): Node } = { end, } -local function create_node(token: Node.Token, name: string): Node - local node = basic_nodes[token](name) +local function create_node(token: Node.Token, name: string, ...: any): Node + local node = basic_nodes[token](name, ...) return node end diff --git a/src/awesomewm.d.tl/dag.tl b/src/awesomewm.d.tl/dag.tl index b2ff5bf..d981263 100644 --- a/src/awesomewm.d.tl/dag.tl +++ b/src/awesomewm.d.tl/dag.tl @@ -1,27 +1,13 @@ local type Dag = require("types.Dag") -local type Node = require("types.Node") -local function init(): Dag +local function new(): Dag local dag : Dag = { - nodes_by_module_name = {}, + modules = {}, global_nodes = {}, } return dag end -local function insert(dag: Dag, module_name: string, node: Node) - if not dag.nodes_by_module_name[module_name] then - dag.nodes_by_module_name[module_name] = {} - end - table.insert(dag.nodes_by_module_name[module_name], node) -end - -local function insert_global(dag: Dag, node: Node) - table.insert(dag.global_nodes, node) -end - return { - init = init, - insert = insert, - insert_global = insert_global, + new = new, } diff --git a/src/awesomewm.d.tl/init.tl b/src/awesomewm.d.tl/init.tl index f1bc8c2..1d23308 100644 --- a/src/awesomewm.d.tl/init.tl +++ b/src/awesomewm.d.tl/init.tl @@ -11,6 +11,7 @@ local logger = require "logger" local Map = require "pl.Map" local Module_Doc = require "entity.Module_Doc" local Module_Info = require "entity.Module_Info" +local module_dependencies = require "visitors.module_dependencies" local type Node = require "types.Node" local property = require "property" local scraper = require "scraper" @@ -156,10 +157,22 @@ local module_ast, other_nodes = scraper.module_doc.get_doc_from_page( ast.in_order_visitor(module_ast, function(node: Node) type_mapping.visit(node) end) +ast.in_order_visitor(module_ast, function(node: Node) + module_dependencies.visit(node, module_ast, { + modules = { + Screen = { + name = "screen", + module_path = "awful.screen", + token = "module", + }, + } + }) +end) log:info(logger.message_with_metadata("Finished", { module_ast = module_ast, other_nodes = other_nodes, + deps = module_ast.dependencies, })) filesystem.file_writer.write( diff --git a/src/awesomewm.d.tl/scraper/module_doc.tl b/src/awesomewm.d.tl/scraper/module_doc.tl index 9ef4c64..ba5663c 100644 --- a/src/awesomewm.d.tl/scraper/module_doc.tl +++ b/src/awesomewm.d.tl/scraper/module_doc.tl @@ -245,7 +245,7 @@ local section_scrapers : { Section : function(html: string, record_name: local module = {} -function module.get_doc_from_page(html: string, module_name: string): Node, { Node } +function module.get_doc_from_page(html: string, module_path: string): Node, { Node } local html_nodes = scraper_utils.extract_nodes(html, { "h2.section-header", "dl.function", @@ -255,8 +255,8 @@ function module.get_doc_from_page(html: string, module_name: string): Node, { No error "The list aren't the same size!" end - local record_name = utils.capitalize((module_name:gsub(".*%.", ""))) - local module_root = ast.create_node("module", record_name) + local record_name = utils.capitalize((module_path:gsub(".*%.", ""))) + local module_root = ast.create_node("module", record_name, module_path) local other_nodes : { Node } = {} local module_signals_node = ast.create_node("enum", "Signal") @@ -268,7 +268,7 @@ function module.get_doc_from_page(html: string, module_name: string): Node, { No local dl_html = html_nodes:get("dl.function")[i]:outer_html() if section_scrapers[section_name] then - local module_nodes, global_nodes, signals_name = section_scrapers[section_name](dl_html, record_name, module_name) + local module_nodes, global_nodes, signals_name = section_scrapers[section_name](dl_html, record_name, module_path) for _, node in ipairs(module_nodes) do table.insert(module_root.children, node) end diff --git a/src/awesomewm.d.tl/types/Dag.tl b/src/awesomewm.d.tl/types/Dag.tl index 481ffa2..ad81e3e 100644 --- a/src/awesomewm.d.tl/types/Dag.tl +++ b/src/awesomewm.d.tl/types/Dag.tl @@ -1,7 +1,7 @@ local type Node = require("types.Node") local record Dag - nodes_by_module_name: { string : { Node } } + modules: { string : Node } -- module_name -> root_node (token = "module") global_nodes: { Node } end diff --git a/src/awesomewm.d.tl/types/Node.tl b/src/awesomewm.d.tl/types/Node.tl index b0440aa..fa841cc 100644 --- a/src/awesomewm.d.tl/types/Node.tl +++ b/src/awesomewm.d.tl/types/Node.tl @@ -20,6 +20,10 @@ local record Node -- for "function" and "metamethod" parameters: { Node } return_types: { string } + + -- for "module" + module_path: string + dependencies: { string : string } -- module_name -> module_path end return Node diff --git a/src/awesomewm.d.tl/types/Visitor.tl b/src/awesomewm.d.tl/types/Visitor.tl deleted file mode 100644 index 9b2b26e..0000000 --- a/src/awesomewm.d.tl/types/Visitor.tl +++ /dev/null @@ -1,7 +0,0 @@ -local type Node = require("types.Node") - -local record Visitor - visit: function(Node) -end - -return Visitor diff --git a/src/awesomewm.d.tl/utils.tl b/src/awesomewm.d.tl/utils.tl index 6c695bc..360dff5 100644 --- a/src/awesomewm.d.tl/utils.tl +++ b/src/awesomewm.d.tl/utils.tl @@ -76,10 +76,13 @@ function utils.do_or_fail(func: function(...: any): (T | nil, string), ... return res as T -- promote to T since pcall succeeded at this point end -function utils.spread(t: { T }, i: { T }) - for _, v in ipairs(i) do - table.insert(t, v) +function utils.spread(t: { T }, ...: { T }): { T } + for _, a in ipairs({ ... }) do + for _, v in ipairs(a) do + table.insert(t, v) + end end + return t end return utils diff --git a/src/awesomewm.d.tl/visitors/module_dependencies.tl b/src/awesomewm.d.tl/visitors/module_dependencies.tl new file mode 100644 index 0000000..977ef1b --- /dev/null +++ b/src/awesomewm.d.tl/visitors/module_dependencies.tl @@ -0,0 +1,35 @@ +local type Dag = require("types.Dag") +local type Node = require("types.Node") +local utils = require("utils") + +local spread = utils.spread + +local function get_all_types_in_node(node: Node): { string } + local parameters_types = {} + if node.parameters then + for _, v in ipairs(node.parameters) do + spread(parameters_types, v.types) + end + end + return spread( + {}, + node.types or {}, + node.return_types or {}, + parameters_types or {}) +end + +local record Module_Dependencies + visit: function(node: Node, mod: Node, d: Dag) +end + +function Module_Dependencies.visit(node: Node, mod: Node, d: Dag) + local all_types = get_all_types_in_node(node) + for _, v in ipairs(all_types) do + local dependency = d.modules[v] + if dependency then + mod.dependencies[dependency.name] = dependency.module_path + end + end +end + +return Module_Dependencies diff --git a/src/awesomewm.d.tl/visitors/type_mapping.tl b/src/awesomewm.d.tl/visitors/type_mapping.tl index 8699c9b..c54bf8a 100644 --- a/src/awesomewm.d.tl/visitors/type_mapping.tl +++ b/src/awesomewm.d.tl/visitors/type_mapping.tl @@ -1,5 +1,4 @@ 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)" @@ -55,7 +54,9 @@ local function check_function_returns(node: Node) end end -local type Type_Coercion = Visitor +local record Type_Coercion + visit: function(node: Node) +end function Type_Coercion.visit(node: Node) check_node(node)