2020-02-19 16:12:39 +01:00
|
|
|
module doc
|
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
import os
|
2020-04-25 20:58:00 +02:00
|
|
|
import strings
|
2020-06-02 12:10:01 +02:00
|
|
|
import time
|
|
|
|
import v.ast
|
|
|
|
import v.fmt
|
|
|
|
import v.parser
|
2020-04-25 20:58:00 +02:00
|
|
|
import v.pref
|
2020-06-02 12:10:01 +02:00
|
|
|
import v.scanner
|
2020-04-25 20:58:00 +02:00
|
|
|
import v.table
|
2020-06-02 12:10:01 +02:00
|
|
|
import v.token
|
|
|
|
import v.util
|
2020-02-19 16:12:39 +01:00
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
pub struct Doc {
|
|
|
|
pub mut:
|
2020-06-02 16:19:55 +02:00
|
|
|
input_path string = ''
|
|
|
|
prefs &pref.Preferences = &pref.Preferences{}
|
|
|
|
table &table.Table = &table.Table{}
|
|
|
|
pub_only bool = true
|
|
|
|
head DocNode
|
|
|
|
with_comments bool = true
|
|
|
|
contents []DocNode
|
2020-06-02 12:10:01 +02:00
|
|
|
time_generated time.Time
|
2020-02-19 16:12:39 +01:00
|
|
|
}
|
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
pub struct DocPos {
|
|
|
|
pub:
|
|
|
|
line int
|
2020-06-02 16:19:55 +02:00
|
|
|
col int
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
2020-02-26 22:44:29 +01:00
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
pub struct DocNode {
|
|
|
|
pub mut:
|
2020-06-02 16:19:55 +02:00
|
|
|
name string
|
|
|
|
content string = ''
|
|
|
|
comment string
|
|
|
|
pos DocPos = DocPos{-1, -1}
|
|
|
|
file_path string = ''
|
2020-06-02 12:10:01 +02:00
|
|
|
parent_type string = ''
|
|
|
|
}
|
|
|
|
|
2020-06-08 08:22:10 +02:00
|
|
|
pub fn merge_comments(stmts []ast.Stmt) string {
|
|
|
|
mut res := []string{}
|
|
|
|
for s in stmts {
|
|
|
|
if s is ast.Comment {
|
|
|
|
c := s as ast.Comment
|
|
|
|
res << c.text.trim_left('|')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res.join('\n')
|
|
|
|
}
|
|
|
|
|
2020-06-06 17:47:16 +02:00
|
|
|
pub fn get_comment_block_right_before(stmts []ast.Stmt) string {
|
|
|
|
if stmts.len == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
mut comment := ''
|
2020-06-06 17:47:16 +02:00
|
|
|
mut last_comment_line_nr := 0
|
|
|
|
for i := stmts.len-1; i >= 0; i-- {
|
2020-06-02 12:10:01 +02:00
|
|
|
stmt := stmts[i]
|
2020-06-06 17:47:16 +02:00
|
|
|
if stmt !is ast.Comment {
|
2020-06-02 12:10:01 +02:00
|
|
|
panic('Not a comment')
|
2020-05-15 23:09:38 +02:00
|
|
|
}
|
2020-06-06 17:47:16 +02:00
|
|
|
cmt := stmt as ast.Comment
|
|
|
|
if last_comment_line_nr != 0 && cmt.pos.line_nr < last_comment_line_nr - 1 {
|
|
|
|
// skip comments that are not part of a continuous block,
|
|
|
|
// located right above the top level statement.
|
|
|
|
// break
|
|
|
|
}
|
2020-06-07 12:27:42 +02:00
|
|
|
mut cmt_content := cmt.text.trim_left('|')
|
|
|
|
if cmt_content.len == cmt.text.len || cmt.is_multi {
|
2020-06-06 17:47:16 +02:00
|
|
|
// ignore /* */ style comments for now
|
|
|
|
continue
|
2020-06-07 12:27:42 +02:00
|
|
|
// if cmt_content.len == 0 {
|
|
|
|
// continue
|
|
|
|
// }
|
|
|
|
// mut new_cmt_content := ''
|
|
|
|
// mut is_codeblock := false
|
|
|
|
// // println(cmt_content)
|
|
|
|
// lines := cmt_content.split_into_lines()
|
|
|
|
// for j, line in lines {
|
|
|
|
// trimmed := line.trim_space().trim_left(cmt_prefix)
|
|
|
|
// if trimmed.starts_with('- ') || (trimmed.len >= 2 && trimmed[0].is_digit() && trimmed[1] == `.`) || is_codeblock {
|
|
|
|
// new_cmt_content += line + '\n'
|
|
|
|
// } else if line.starts_with('```') {
|
|
|
|
// is_codeblock = !is_codeblock
|
|
|
|
// new_cmt_content += line + '\n'
|
|
|
|
// } else {
|
|
|
|
// new_cmt_content += trimmed + '\n'
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return new_cmt_content
|
2020-05-15 23:09:38 +02:00
|
|
|
}
|
2020-06-06 17:47:16 +02:00
|
|
|
//eprintln('cmt: $cmt')
|
|
|
|
cseparator := if cmt_content.starts_with('```') {'\n'} else {' '}
|
|
|
|
comment = cmt_content + cseparator + comment
|
|
|
|
last_comment_line_nr = cmt.pos.line_nr
|
2020-02-19 16:12:39 +01:00
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
return comment
|
2020-02-19 16:12:39 +01:00
|
|
|
}
|
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
fn convert_pos(file_path string, pos token.Position) DocPos {
|
2020-06-02 16:19:55 +02:00
|
|
|
source := util.read_file(file_path) or {
|
|
|
|
''
|
|
|
|
}
|
|
|
|
mut p := util.imax(0, util.imin(source.len - 1, pos.pos))
|
|
|
|
column := util.imax(0, pos.pos - p - 1)
|
|
|
|
return DocPos{
|
|
|
|
line: pos.line_nr + 1
|
|
|
|
col: util.imax(1, column + 1)
|
|
|
|
}
|
2020-02-26 22:44:29 +01:00
|
|
|
}
|
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
pub fn (d Doc) get_signature(stmt ast.Stmt) string {
|
|
|
|
mut f := fmt.Fmt{
|
|
|
|
out: strings.new_builder(1000)
|
|
|
|
out_imports: strings.new_builder(200)
|
|
|
|
table: d.table
|
2020-06-07 12:27:42 +02:00
|
|
|
cur_mod: d.head.name.split('.').last()
|
2020-06-02 12:10:01 +02:00
|
|
|
indent: 0
|
|
|
|
is_debug: false
|
|
|
|
}
|
|
|
|
match stmt {
|
|
|
|
ast.Module {
|
|
|
|
return 'module $it.name'
|
|
|
|
}
|
|
|
|
ast.FnDecl {
|
2020-06-07 12:27:42 +02:00
|
|
|
return it.str(d.table).replace(f.cur_mod + '.', '')
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
f.stmt(stmt)
|
|
|
|
return f.out.str().trim_space()
|
|
|
|
}
|
|
|
|
}
|
2020-02-26 22:44:29 +01:00
|
|
|
}
|
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
pub fn (d Doc) get_pos(stmt ast.Stmt) token.Position {
|
|
|
|
match stmt {
|
|
|
|
ast.FnDecl { return it.pos }
|
|
|
|
ast.StructDecl { return it.pos }
|
|
|
|
ast.EnumDecl { return it.pos }
|
|
|
|
ast.InterfaceDecl { return it.pos }
|
|
|
|
ast.ConstDecl { return it.pos }
|
|
|
|
else { return token.Position{} }
|
2020-02-19 16:12:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-07 12:27:42 +02:00
|
|
|
pub fn (d Doc) get_type_name(decl ast.TypeDecl) string {
|
|
|
|
match decl {
|
|
|
|
ast.SumTypeDecl { return it.name }
|
|
|
|
ast.FnTypeDecl { return it.name }
|
|
|
|
ast.AliasTypeDecl { return it.name }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
pub fn (d Doc) get_name(stmt ast.Stmt) string {
|
2020-06-07 12:27:42 +02:00
|
|
|
cur_mod := d.head.name.split('.').last()
|
2020-06-02 12:10:01 +02:00
|
|
|
match stmt {
|
|
|
|
ast.FnDecl { return it.name }
|
|
|
|
ast.StructDecl { return it.name }
|
|
|
|
ast.EnumDecl { return it.name }
|
|
|
|
ast.InterfaceDecl { return it.name }
|
2020-06-07 12:27:42 +02:00
|
|
|
ast.TypeDecl { return d.get_type_name(it).replace('&' + cur_mod + '.', '').replace(cur_mod + '.', '') }
|
2020-06-02 12:10:01 +02:00
|
|
|
ast.ConstDecl { return 'Constants' }
|
|
|
|
else { return '' }
|
2020-02-19 16:12:39 +01:00
|
|
|
}
|
2020-02-26 22:44:29 +01:00
|
|
|
}
|
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
pub fn new(input_path string) Doc {
|
|
|
|
return Doc{
|
2020-06-02 16:19:55 +02:00
|
|
|
input_path: os.real_path(input_path)
|
|
|
|
prefs: &pref.Preferences{}
|
|
|
|
table: table.new_table()
|
|
|
|
head: DocNode{}
|
|
|
|
contents: []DocNode{}
|
2020-06-02 12:10:01 +02:00
|
|
|
time_generated: time.now()
|
|
|
|
}
|
2020-02-26 22:44:29 +01:00
|
|
|
}
|
|
|
|
|
2020-06-08 10:28:46 +02:00
|
|
|
pub fn (nodes []DocNode) index_by_name(node_name string) ?int {
|
|
|
|
for i, node in nodes {
|
|
|
|
if node.name != node_name { continue }
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
return error('Node with the name "$node_name" was not found.')
|
|
|
|
}
|
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
pub fn (nodes []DocNode) find_children_of(parent_type string) []DocNode {
|
2020-06-02 16:19:55 +02:00
|
|
|
if parent_type.len == 0 {
|
|
|
|
return []DocNode{}
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
mut children := []DocNode{}
|
|
|
|
for node in nodes {
|
2020-06-02 16:19:55 +02:00
|
|
|
if node.parent_type != parent_type {
|
|
|
|
continue
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
children << node
|
|
|
|
}
|
|
|
|
return children
|
2020-02-19 16:12:39 +01:00
|
|
|
}
|
2020-03-10 19:49:04 +01:00
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
fn get_parent_mod(dir string) ?string {
|
2020-06-04 23:50:59 +02:00
|
|
|
$if windows {
|
2020-06-06 17:47:16 +02:00
|
|
|
// windows root path is C: or D:
|
2020-06-04 23:50:59 +02:00
|
|
|
if dir.len <= 2 { return error('root folder reached') }
|
|
|
|
} $else {
|
|
|
|
if dir.len == 0 { return error('root folder reached') }
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
base_dir := os.base_dir(dir)
|
|
|
|
if os.file_name(base_dir) in ['encoding', 'v'] && 'vlib' in base_dir {
|
|
|
|
return os.file_name(base_dir)
|
|
|
|
}
|
|
|
|
prefs := &pref.Preferences{}
|
2020-06-02 16:19:55 +02:00
|
|
|
files := os.ls(base_dir) or {
|
|
|
|
[]string{}
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
v_files := prefs.should_compile_filtered_files(base_dir, files)
|
|
|
|
if v_files.len == 0 {
|
2020-06-02 16:19:55 +02:00
|
|
|
parent_mod := get_parent_mod(base_dir) or {
|
|
|
|
''
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
if parent_mod.len > 0 {
|
|
|
|
return parent_mod + '.' + os.file_name(base_dir)
|
2020-03-10 19:49:04 +01:00
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
return error('No V files found.')
|
2020-03-10 19:49:04 +01:00
|
|
|
}
|
2020-06-06 17:47:16 +02:00
|
|
|
tbl := table.new_table()
|
|
|
|
scope := &ast.Scope{ parent: 0 }
|
|
|
|
file_ast := parser.parse_file(v_files[0], tbl, .skip_comments, prefs, scope)
|
2020-06-02 12:10:01 +02:00
|
|
|
if file_ast.mod.name == 'main' {
|
|
|
|
return ''
|
|
|
|
}
|
2020-06-02 16:19:55 +02:00
|
|
|
parent_mod := get_parent_mod(base_dir) or {
|
|
|
|
''
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
if parent_mod.len > 0 {
|
|
|
|
return parent_mod + '.' + file_ast.mod.name
|
|
|
|
}
|
|
|
|
return file_ast.mod.name
|
2020-03-10 19:49:04 +01:00
|
|
|
}
|
|
|
|
|
2020-06-02 12:10:01 +02:00
|
|
|
pub fn (mut d Doc) generate() ?bool {
|
|
|
|
// get all files
|
|
|
|
base_path := if os.is_dir(d.input_path) { d.input_path } else { os.real_path(os.base_dir(d.input_path)) }
|
2020-06-02 16:19:55 +02:00
|
|
|
project_files := os.ls(base_path) or {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
v_files := d.prefs.should_compile_filtered_files(base_path, project_files)
|
|
|
|
if v_files.len == 0 {
|
|
|
|
return error('vdoc: No valid V files were found.')
|
|
|
|
}
|
|
|
|
// parse files
|
|
|
|
mut file_asts := []ast.File{}
|
|
|
|
// TODO: remove later for vlib
|
2020-06-06 17:47:16 +02:00
|
|
|
comments_mode := if d.with_comments { scanner.CommentsMode.toplevel_comments } else { scanner.CommentsMode.skip_comments }
|
2020-06-02 12:10:01 +02:00
|
|
|
for file in v_files {
|
2020-06-02 16:19:55 +02:00
|
|
|
file_ast := parser.parse_file(file, d.table, comments_mode, d.prefs, &ast.Scope{
|
|
|
|
parent: 0
|
|
|
|
})
|
2020-06-02 12:10:01 +02:00
|
|
|
file_asts << file_ast
|
|
|
|
}
|
|
|
|
mut module_name := ''
|
|
|
|
mut parent_mod_name := ''
|
|
|
|
mut orig_mod_name := ''
|
2020-06-07 12:27:42 +02:00
|
|
|
mut const_idx := -1
|
2020-06-02 12:10:01 +02:00
|
|
|
for i, file_ast in file_asts {
|
|
|
|
if i == 0 {
|
2020-06-02 16:19:55 +02:00
|
|
|
parent_mod_name = get_parent_mod(base_path) or {
|
|
|
|
''
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
module_name = file_ast.mod.name
|
|
|
|
orig_mod_name = module_name
|
|
|
|
if module_name != 'main' && parent_mod_name.len > 0 {
|
|
|
|
module_name = parent_mod_name + '.' + module_name
|
|
|
|
}
|
|
|
|
d.head = DocNode{
|
2020-06-02 16:19:55 +02:00
|
|
|
name: module_name
|
|
|
|
content: 'module $module_name'
|
2020-06-02 12:10:01 +02:00
|
|
|
comment: ''
|
|
|
|
}
|
2020-06-05 09:59:26 +02:00
|
|
|
} else if file_ast.mod.name != orig_mod_name {
|
2020-03-10 19:49:04 +01:00
|
|
|
continue
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
stmts := file_ast.stmts
|
2020-06-08 08:22:10 +02:00
|
|
|
//
|
|
|
|
mut last_import_stmt_idx := 0
|
|
|
|
for sidx, stmt in stmts {
|
|
|
|
if stmt is ast.Import {
|
|
|
|
last_import_stmt_idx = sidx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mut prev_comments := []ast.Stmt{}
|
|
|
|
mut imports_section := true
|
|
|
|
for sidx, stmt in stmts {
|
2020-06-06 17:47:16 +02:00
|
|
|
//eprintln('stmt typeof: ' + typeof(stmt))
|
2020-06-02 16:19:55 +02:00
|
|
|
if stmt is ast.Comment {
|
2020-06-06 17:47:16 +02:00
|
|
|
prev_comments << stmt
|
2020-06-02 16:19:55 +02:00
|
|
|
continue
|
|
|
|
}
|
2020-06-06 17:47:16 +02:00
|
|
|
if stmt is ast.Module {
|
|
|
|
// the previous comments were probably a copyright/license one
|
|
|
|
module_comment := get_comment_block_right_before(prev_comments)
|
|
|
|
prev_comments = []
|
|
|
|
if module_comment == '' {
|
2020-06-02 12:10:01 +02:00
|
|
|
continue
|
|
|
|
}
|
2020-06-06 17:47:16 +02:00
|
|
|
if module_comment == d.head.comment {
|
|
|
|
continue
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
2020-06-06 17:47:16 +02:00
|
|
|
if d.head.comment != '' {
|
|
|
|
d.head.comment += '\n'
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
2020-06-06 17:47:16 +02:00
|
|
|
d.head.comment += module_comment
|
|
|
|
continue
|
|
|
|
}
|
2020-06-08 08:22:10 +02:00
|
|
|
if last_import_stmt_idx > 0 && sidx == last_import_stmt_idx {
|
|
|
|
// the accumulated comments were interspersed before/between the imports;
|
|
|
|
// just add them all to the module comment:
|
|
|
|
import_comments := merge_comments(prev_comments)
|
|
|
|
if d.head.comment != '' {
|
|
|
|
d.head.comment += '\n'
|
|
|
|
}
|
|
|
|
d.head.comment += import_comments
|
|
|
|
prev_comments = []
|
|
|
|
imports_section = false
|
|
|
|
}
|
|
|
|
if stmt is ast.Import {
|
|
|
|
continue
|
|
|
|
}
|
2020-06-06 17:47:16 +02:00
|
|
|
signature := d.get_signature(stmt)
|
|
|
|
pos := d.get_pos(stmt)
|
2020-06-07 12:27:42 +02:00
|
|
|
mut name := d.get_name(stmt)
|
|
|
|
if (!signature.starts_with('pub') && d.pub_only) || stmt is ast.GlobalDecl {
|
2020-06-06 17:47:16 +02:00
|
|
|
prev_comments = []
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if name.starts_with(orig_mod_name + '.') {
|
|
|
|
name = name.all_after(orig_mod_name + '.')
|
|
|
|
}
|
|
|
|
mut node := DocNode{
|
|
|
|
name: name
|
|
|
|
content: signature
|
|
|
|
comment: ''
|
|
|
|
pos: convert_pos(v_files[i], pos)
|
|
|
|
file_path: v_files[i]
|
|
|
|
}
|
|
|
|
if stmt is ast.FnDecl {
|
|
|
|
fnd := stmt as ast.FnDecl
|
|
|
|
if fnd.receiver.typ != 0 {
|
|
|
|
mut parent_type := d.table.get_type_name(fnd.receiver.typ)
|
|
|
|
if parent_type.starts_with(module_name + '.') {
|
|
|
|
parent_type = parent_type.all_after(module_name + '.')
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
2020-06-06 17:47:16 +02:00
|
|
|
node.parent_type = parent_type
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
2020-06-07 12:27:42 +02:00
|
|
|
}
|
|
|
|
if stmt is ast.ConstDecl {
|
|
|
|
if const_idx == -1 {
|
2020-06-08 08:22:10 +02:00
|
|
|
const_idx = sidx
|
2020-06-07 12:27:42 +02:00
|
|
|
} else {
|
|
|
|
node.parent_type = 'Constants'
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
}
|
2020-06-06 17:47:16 +02:00
|
|
|
if node.name.len == 0 && node.comment.len == 0 && node.content.len == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
d.contents << node
|
|
|
|
if d.with_comments && (prev_comments.len > 0) {
|
|
|
|
last_comment := d.contents[d.contents.len - 1].comment
|
|
|
|
cmt := last_comment + '\n' + get_comment_block_right_before(prev_comments)
|
|
|
|
d.contents[d.contents.len - 1].comment = cmt
|
2020-06-02 16:18:12 +02:00
|
|
|
}
|
2020-06-06 17:47:16 +02:00
|
|
|
prev_comments = []
|
2020-03-10 19:49:04 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
d.time_generated = time.now()
|
|
|
|
return true
|
2020-03-10 19:49:04 +01:00
|
|
|
}
|
2020-06-02 12:10:01 +02:00
|
|
|
|
2020-06-02 16:19:55 +02:00
|
|
|
pub fn generate(input_path string, pub_only, with_comments bool) ?Doc {
|
|
|
|
mut doc := new(input_path)
|
2020-06-02 12:10:01 +02:00
|
|
|
doc.pub_only = pub_only
|
|
|
|
doc.with_comments = with_comments
|
2020-06-08 00:47:04 +02:00
|
|
|
doc.generate() or {
|
2020-06-02 12:10:01 +02:00
|
|
|
return error(err)
|
|
|
|
}
|
|
|
|
return doc
|
2020-06-02 16:18:12 +02:00
|
|
|
}
|