2011-06-18 11:45:45 +02:00
|
|
|
------------
|
|
|
|
-- Language-dependent parsing of code.
|
|
|
|
-- This encapsulates the different strategies needed for parsing C and Lua
|
|
|
|
-- source code.
|
|
|
|
|
2012-10-28 18:51:00 +01:00
|
|
|
local class = require 'pl.class'
|
|
|
|
local utils = require 'pl.utils'
|
2013-08-05 19:27:42 +02:00
|
|
|
local List = require 'pl.List'
|
2011-06-18 11:45:45 +02:00
|
|
|
local tools = require 'ldoc.tools'
|
|
|
|
local lexer = require 'ldoc.lexer'
|
2013-02-13 13:15:29 +01:00
|
|
|
local quit = utils.quit
|
2011-06-18 11:45:45 +02:00
|
|
|
local tnext = lexer.skipws
|
|
|
|
|
|
|
|
|
2013-02-13 13:15:29 +01:00
|
|
|
local Lang = class()
|
2011-06-18 11:45:45 +02:00
|
|
|
|
|
|
|
function Lang:trim_comment (s)
|
|
|
|
return s:gsub(self.line_comment,'')
|
|
|
|
end
|
|
|
|
|
|
|
|
function Lang:start_comment (v)
|
|
|
|
local line = v:match (self.start_comment_)
|
2012-12-31 16:02:40 +01:00
|
|
|
if line and self.end_comment_ and v:match (self.end_comment_) then
|
|
|
|
return nil
|
|
|
|
end
|
2011-06-18 11:45:45 +02:00
|
|
|
local block = v:match(self.block_comment)
|
|
|
|
return line or block, block
|
|
|
|
end
|
|
|
|
|
|
|
|
function Lang:empty_comment (v)
|
|
|
|
return v:match(self.empty_comment_)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Lang:grab_block_comment(v,tok)
|
|
|
|
v = v:gsub(self.block_comment,'')
|
2011-08-25 18:59:28 +02:00
|
|
|
return tools.grab_block_comment(v,tok,self.end_comment)
|
2011-06-18 11:45:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Lang:find_module(tok,t,v)
|
|
|
|
return '...',t,v
|
|
|
|
end
|
|
|
|
|
2011-07-29 15:53:45 +02:00
|
|
|
function Lang:item_follows(t,v)
|
2011-06-18 11:45:45 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function Lang:finalize()
|
|
|
|
self.empty_comment_ = self.start_comment_..'%s*$'
|
|
|
|
end
|
|
|
|
|
|
|
|
function Lang:search_for_token (tok,type,value,t,v)
|
|
|
|
while t and not (t == type and v == value) do
|
|
|
|
if t == 'comment' and self:start_comment(v) then return nil,t,v end
|
|
|
|
t,v = tnext(tok)
|
|
|
|
end
|
|
|
|
return t ~= nil,t,v
|
|
|
|
end
|
|
|
|
|
2011-07-29 15:53:45 +02:00
|
|
|
function Lang:parse_extra (tags,tok)
|
2011-06-18 11:45:45 +02:00
|
|
|
end
|
|
|
|
|
2011-12-06 18:19:09 +01:00
|
|
|
function Lang:is_module_modifier ()
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function Lang:parse_module_modifier (tags, tok)
|
|
|
|
return nil, "@usage or @exports deduction not implemented for this language"
|
2011-09-19 15:53:00 +02:00
|
|
|
end
|
|
|
|
|
2011-06-18 11:45:45 +02:00
|
|
|
|
2013-02-13 13:15:29 +01:00
|
|
|
local Lua = class(Lang)
|
2011-06-18 11:45:45 +02:00
|
|
|
|
|
|
|
function Lua:_init()
|
|
|
|
self.line_comment = '^%-%-+' -- used for stripping
|
|
|
|
self.start_comment_ = '^%-%-%-+' -- used for doc comment line start
|
2011-08-24 18:57:53 +02:00
|
|
|
self.block_comment = '^%-%-%[=*%[%-+' -- used for block doc comments
|
2013-09-26 15:34:53 +02:00
|
|
|
self.end_comment_ = '[^%-]%-%-+[^-]*\n$' ---- exclude --- this kind of comment ---
|
2013-08-07 11:05:55 +02:00
|
|
|
self.method_call = ':'
|
2011-06-18 11:45:45 +02:00
|
|
|
self:finalize()
|
|
|
|
end
|
|
|
|
|
|
|
|
function Lua.lexer(fname)
|
|
|
|
local f,e = io.open(fname)
|
|
|
|
if not f then quit(e) end
|
|
|
|
return lexer.lua(f,{}),f
|
|
|
|
end
|
|
|
|
|
2011-08-25 18:59:28 +02:00
|
|
|
function Lua:grab_block_comment(v,tok)
|
|
|
|
local equals = v:match('^%-%-%[(=*)%[')
|
2014-01-06 12:35:12 +01:00
|
|
|
if not equals then return v end
|
2011-08-25 18:59:28 +02:00
|
|
|
v = v:gsub(self.block_comment,'')
|
|
|
|
return tools.grab_block_comment(v,tok,'%]'..equals..'%]')
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2011-07-29 15:53:45 +02:00
|
|
|
function Lua:parse_module_call(tok,t,v)
|
|
|
|
t,v = tnext(tok)
|
|
|
|
if t == '(' then t,v = tnext(tok) end
|
|
|
|
if t == 'string' then -- explicit name, cool
|
|
|
|
return v,t,v
|
|
|
|
elseif t == '...' then -- we have to guess!
|
|
|
|
return '...',t,v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-18 11:45:45 +02:00
|
|
|
-- If a module name was not provided, then we look for an explicit module()
|
|
|
|
-- call. However, we should not try too hard; if we hit a doc comment then
|
|
|
|
-- we should go back and process it. Likewise, module(...) also means
|
|
|
|
-- that we must infer the module name.
|
|
|
|
function Lua:find_module(tok,t,v)
|
|
|
|
local res
|
|
|
|
res,t,v = self:search_for_token(tok,'iden','module',t,v)
|
|
|
|
if not res then return nil,t,v end
|
2011-07-29 15:53:45 +02:00
|
|
|
return self:parse_module_call(tok,t,v)
|
2011-06-18 11:45:45 +02:00
|
|
|
end
|
|
|
|
|
2011-07-29 15:53:45 +02:00
|
|
|
local function parse_lua_parameters (tags,tok)
|
|
|
|
tags.formal_args = tools.get_parameters(tok)
|
2012-12-29 11:07:26 +01:00
|
|
|
tags:add('class','function')
|
2011-06-18 11:45:45 +02:00
|
|
|
end
|
|
|
|
|
2011-07-29 15:53:45 +02:00
|
|
|
local function parse_lua_function_header (tags,tok)
|
2012-12-29 11:07:26 +01:00
|
|
|
if not tags.name then
|
|
|
|
tags:add('name',tools.get_fun_name(tok))
|
|
|
|
end
|
2012-11-08 18:09:59 +01:00
|
|
|
if not tags.name then return 'function has no name' end
|
2011-07-29 15:53:45 +02:00
|
|
|
parse_lua_parameters(tags,tok)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function parse_lua_table (tags,tok)
|
|
|
|
tags.formal_args = tools.get_parameters(tok,'}',function(s)
|
|
|
|
return s == ',' or s == ';'
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
--------------- function and variable inferrence -----------
|
|
|
|
-- After a doc comment, there may be a local followed by:
|
|
|
|
-- [1] (l)function: function NAME
|
|
|
|
-- [2] (l)function: NAME = function
|
|
|
|
-- [3] table: NAME = {
|
|
|
|
-- [4] field: NAME = <anything else> (this is a module-level field)
|
|
|
|
--
|
|
|
|
-- Depending on the case successfully detected, returns a function which
|
|
|
|
-- will be called later to fill in inferred item tags
|
|
|
|
function Lua:item_follows(t,v,tok)
|
2011-09-19 19:23:53 +02:00
|
|
|
local parser, case
|
2011-07-29 15:53:45 +02:00
|
|
|
local is_local = t == 'keyword' and v == 'local'
|
|
|
|
if is_local then t,v = tnext(tok) end
|
|
|
|
if t == 'keyword' and v == 'function' then -- case [1]
|
2011-09-19 19:23:53 +02:00
|
|
|
case = 1
|
2011-07-29 15:53:45 +02:00
|
|
|
parser = parse_lua_function_header
|
|
|
|
elseif t == 'iden' then
|
|
|
|
local name,t,v = tools.get_fun_name(tok,v)
|
2016-06-26 19:24:03 +02:00
|
|
|
if t ~= '=' then return nil,"not 'name = function,table or value'" end
|
2011-07-29 15:53:45 +02:00
|
|
|
t,v = tnext(tok)
|
|
|
|
if t == 'keyword' and v == 'function' then -- case [2]
|
|
|
|
tnext(tok) -- skip '('
|
2011-09-19 19:23:53 +02:00
|
|
|
case = 2
|
2011-07-29 15:53:45 +02:00
|
|
|
parser = function(tags,tok)
|
2012-12-29 11:07:26 +01:00
|
|
|
tags:add('name',name)
|
2011-07-29 15:53:45 +02:00
|
|
|
parse_lua_parameters(tags,tok)
|
|
|
|
end
|
|
|
|
elseif t == '{' then -- case [3]
|
2011-09-19 19:23:53 +02:00
|
|
|
case = 3
|
2011-07-29 15:53:45 +02:00
|
|
|
parser = function(tags,tok)
|
2012-12-29 11:07:26 +01:00
|
|
|
tags:add('class','table')
|
|
|
|
tags:add('name',name)
|
2011-07-29 15:53:45 +02:00
|
|
|
parse_lua_table (tags,tok)
|
|
|
|
end
|
|
|
|
else -- case [4]
|
2011-09-19 19:23:53 +02:00
|
|
|
case = 4
|
2011-07-29 15:53:45 +02:00
|
|
|
parser = function(tags)
|
2012-12-29 11:07:26 +01:00
|
|
|
tags:add('class','field')
|
|
|
|
tags:add('name',name)
|
2011-07-29 15:53:45 +02:00
|
|
|
end
|
|
|
|
end
|
2012-12-29 11:07:26 +01:00
|
|
|
elseif t == 'keyword' and v == 'return' then
|
|
|
|
t, v = tnext(tok)
|
|
|
|
if t == 'keyword' and v == 'function' then
|
|
|
|
-- return function(a, b, c)
|
|
|
|
tnext(tok) -- skip '('
|
|
|
|
case = 2
|
|
|
|
parser = parse_lua_parameters
|
|
|
|
elseif t == '{' then
|
|
|
|
-- return {...}
|
|
|
|
case = 5
|
|
|
|
parser = function(tags,tok)
|
|
|
|
tags:add('class','table')
|
|
|
|
parse_lua_table(tags,tok)
|
|
|
|
end
|
|
|
|
else
|
2016-06-26 19:24:03 +02:00
|
|
|
return nil,'not returning function or table'
|
2011-12-06 18:19:09 +01:00
|
|
|
end
|
2016-06-26 19:24:03 +02:00
|
|
|
else
|
|
|
|
return nil,"not 'name=value' or 'return value'"
|
2011-07-29 15:53:45 +02:00
|
|
|
end
|
2011-09-19 19:23:53 +02:00
|
|
|
return parser, is_local, case
|
2011-06-18 11:45:45 +02:00
|
|
|
end
|
|
|
|
|
2011-07-29 15:53:45 +02:00
|
|
|
|
2011-09-19 19:23:53 +02:00
|
|
|
-- we only call the function returned by the item_follows above if there
|
|
|
|
-- is not already a name and a type.
|
|
|
|
-- Otherwise, this is called. Currrently only tries to fill in the fields
|
|
|
|
-- of a table from a table definition as identified above
|
|
|
|
function Lua:parse_extra (tags,tok,case)
|
|
|
|
if tags.class == 'table' and not tags.field and case == 3 then
|
|
|
|
parse_lua_table(tags,tok)
|
2011-06-18 11:45:45 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-06 18:19:09 +01:00
|
|
|
-- For Lua, a --- @usage comment means that a long
|
|
|
|
-- string containing the usage follows, which we
|
|
|
|
-- use to update the module usage tag. Likewise, the @export
|
|
|
|
-- tag alone in a doc comment refers to the following returned
|
|
|
|
-- Lua table of functions
|
|
|
|
|
|
|
|
|
|
|
|
function Lua:is_module_modifier (tags)
|
|
|
|
return tags.summary == '' and (tags.usage or tags.export)
|
|
|
|
end
|
|
|
|
|
2012-12-22 15:29:42 +01:00
|
|
|
-- Allow for private name convention.
|
|
|
|
function Lua:is_private_var (name)
|
|
|
|
return name:match '^_' or name:match '_$'
|
|
|
|
end
|
|
|
|
|
2011-12-06 18:19:09 +01:00
|
|
|
function Lua:parse_module_modifier (tags, tok, F)
|
|
|
|
if tags.usage then
|
|
|
|
if tags.class ~= 'field' then return nil,"cannot deduce @usage" end
|
|
|
|
local t1= tnext(tok)
|
2011-12-09 14:43:37 +01:00
|
|
|
if t1 ~= '[' then return nil, t1..' '..': not a long string' end
|
2013-02-13 13:15:29 +01:00
|
|
|
local t, v = tools.grab_block_comment('',tok,'%]%]')
|
2011-12-06 18:19:09 +01:00
|
|
|
return true, v, 'usage'
|
|
|
|
elseif tags.export then
|
|
|
|
if tags.class ~= 'table' then return nil, "cannot deduce @export" end
|
|
|
|
for f in tags.formal_args:iter() do
|
2012-12-22 15:29:42 +01:00
|
|
|
if not self:is_private_var(f) then
|
|
|
|
F:export_item(f)
|
|
|
|
end
|
2011-12-06 18:19:09 +01:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2011-09-19 15:53:00 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2011-08-25 18:59:28 +02:00
|
|
|
-- note a difference here: we scan C/C++ code in full-text mode, not line by line.
|
2013-08-07 11:05:55 +02:00
|
|
|
-- This is because we can't detect multiline comments in line mode.
|
|
|
|
-- Note: this applies to C/C++ code used to generate _Lua_ documentation!
|
2011-06-18 11:45:45 +02:00
|
|
|
|
2013-02-13 13:15:29 +01:00
|
|
|
local CC = class(Lang)
|
2011-06-18 11:45:45 +02:00
|
|
|
|
|
|
|
function CC:_init()
|
|
|
|
self.line_comment = '^//+'
|
|
|
|
self.start_comment_ = '^///+'
|
|
|
|
self.block_comment = '^/%*%*+'
|
2013-08-07 11:05:55 +02:00
|
|
|
self.method_call = ':'
|
2011-06-18 11:45:45 +02:00
|
|
|
self:finalize()
|
|
|
|
end
|
|
|
|
|
|
|
|
function CC.lexer(f)
|
2013-02-13 13:15:29 +01:00
|
|
|
local err
|
2011-06-18 11:45:45 +02:00
|
|
|
f,err = utils.readfile(f)
|
|
|
|
if not f then quit(err) end
|
2014-10-25 12:01:48 +02:00
|
|
|
return lexer.cpp(f,{},nil,true)
|
2011-06-18 11:45:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function CC:grab_block_comment(v,tok)
|
2013-09-26 16:05:45 +02:00
|
|
|
v = v:gsub(self.block_comment,''):gsub('\n%s*%*','\n')
|
2011-06-18 11:45:45 +02:00
|
|
|
return 'comment',v:sub(1,-3)
|
|
|
|
end
|
|
|
|
|
2013-09-20 13:33:06 +02:00
|
|
|
--- here the argument name is always last, and the type is composed of any tokens before
|
|
|
|
function CC:extract_arg (tl,idx)
|
|
|
|
idx = idx or 1
|
|
|
|
local res = List()
|
|
|
|
for i = idx,#tl-1 do
|
|
|
|
res:append(tl[i][2])
|
|
|
|
end
|
|
|
|
local type = res:join ' '
|
|
|
|
return tl[#tl][2], type
|
|
|
|
end
|
|
|
|
|
|
|
|
function CC:item_follows (t,v,tok)
|
|
|
|
if not self.extra.C then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
if t == 'iden' or t == 'keyword' then --
|
|
|
|
if v == self.extra.export then -- this is not part of the return type!
|
|
|
|
t,v = tnext(tok)
|
|
|
|
end
|
2013-09-27 15:37:46 +02:00
|
|
|
-- types may have multiple tokens: example, const char *bonzo(...)
|
2014-01-06 12:35:12 +01:00
|
|
|
local return_type, name = v
|
2013-09-20 13:33:06 +02:00
|
|
|
t,v = tnext(tok)
|
2014-01-06 12:35:12 +01:00
|
|
|
name = v
|
2013-09-27 15:37:46 +02:00
|
|
|
t,v = tnext(tok)
|
2014-01-06 12:35:12 +01:00
|
|
|
while t ~= '(' do
|
2013-09-27 15:37:46 +02:00
|
|
|
return_type = return_type .. ' ' .. name
|
|
|
|
name = v
|
2013-09-20 13:33:06 +02:00
|
|
|
t,v = tnext(tok)
|
2013-09-27 15:37:46 +02:00
|
|
|
end
|
|
|
|
--print ('got',name,t,v,return_type)
|
|
|
|
return function(tags,tok)
|
|
|
|
if not tags.name then
|
|
|
|
tags:add('name',name)
|
2013-09-20 13:33:06 +02:00
|
|
|
end
|
2013-09-27 15:37:46 +02:00
|
|
|
tags:add('class','function')
|
|
|
|
if t == '(' then
|
|
|
|
tags.formal_args,t,v = tools.get_parameters(tok,')',',',self)
|
|
|
|
if return_type ~= 'void' then
|
|
|
|
tags.formal_args.return_type = return_type
|
2013-09-20 13:33:06 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2013-08-05 19:27:42 +02:00
|
|
|
local Moon = class(Lua)
|
|
|
|
|
|
|
|
function Moon:_init()
|
|
|
|
self.line_comment = '^%-%-+' -- used for stripping
|
|
|
|
self.start_comment_ = '^%s*%-%-%-+' -- used for doc comment line start
|
|
|
|
self.block_comment = '^%-%-%[=*%[%-+' -- used for block doc comments
|
|
|
|
self.end_comment_ = '[^%-]%-%-+\n$' ---- exclude --- this kind of comment ---
|
2016-06-27 11:48:35 +02:00
|
|
|
self.method_call = '\\'
|
2013-08-05 19:27:42 +02:00
|
|
|
self:finalize()
|
|
|
|
end
|
|
|
|
|
2016-06-27 11:48:35 +02:00
|
|
|
--- much like Lua, BUT auto-assign parameters start with @
|
|
|
|
function Moon:extract_arg (tl,idx)
|
|
|
|
idx = idx or 1
|
|
|
|
local auto_assign = tl[idx][1] == '@'
|
|
|
|
if auto_assign then idx = idx + 1 end
|
|
|
|
local res = tl[idx][2]
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
2013-08-05 19:27:42 +02:00
|
|
|
function Moon:item_follows (t,v,tok)
|
2013-08-07 09:53:39 +02:00
|
|
|
if t == '.' then -- enclosed in with statement
|
|
|
|
t,v = tnext(tok)
|
|
|
|
end
|
2013-08-05 19:27:42 +02:00
|
|
|
if t == 'iden' then
|
2013-08-07 09:53:39 +02:00
|
|
|
local name,t,v = tools.get_fun_name(tok,v,'')
|
2013-08-05 19:27:42 +02:00
|
|
|
if name == 'class' then
|
2013-08-07 09:53:39 +02:00
|
|
|
name,t,v = tools.get_fun_name(tok,v,'')
|
2013-08-05 19:27:42 +02:00
|
|
|
-- class!
|
|
|
|
return function(tags,tok)
|
|
|
|
tags:add('class','type')
|
|
|
|
tags:add('name',name)
|
|
|
|
end
|
|
|
|
elseif t == '=' or t == ':' then -- function/method
|
2016-06-27 11:48:35 +02:00
|
|
|
local fat = false
|
2013-08-05 19:27:42 +02:00
|
|
|
t,v = tnext(tok)
|
|
|
|
return function(tags,tok)
|
|
|
|
if not tags.name then
|
|
|
|
tags:add('name',name)
|
|
|
|
end
|
|
|
|
if t == '(' then
|
2016-06-27 11:48:35 +02:00
|
|
|
tags.formal_args,t,v = tools.get_parameters(tok,')',',',self)
|
2013-08-05 19:27:42 +02:00
|
|
|
else
|
|
|
|
tags.formal_args = List()
|
|
|
|
end
|
2016-06-27 11:48:35 +02:00
|
|
|
t,v = tnext(tok)
|
2013-08-05 19:27:42 +02:00
|
|
|
tags:add('class','function')
|
2016-06-27 11:48:35 +02:00
|
|
|
if t == '>' then
|
|
|
|
--~ tags.formal_args:insert(1,'self')
|
|
|
|
--~ tags.formal_args.comments = {self=''}
|
|
|
|
else
|
|
|
|
tags.static = true
|
2013-08-07 11:05:55 +02:00
|
|
|
end
|
2013-08-05 19:27:42 +02:00
|
|
|
end
|
|
|
|
else
|
2016-06-27 11:48:35 +02:00
|
|
|
return nil, "expecting '=' or ':'"
|
2013-08-05 19:27:42 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return { lua = Lua(), cc = CC(), moon = Moon() }
|