This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
documenter.ts
259 lines (239 loc) · 11.7 KB
/
documenter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2022
// Carlos Alberto Ruiz Naranjo [[email protected]]
// Ismael Perez Rojo [[email protected] ]
//
// This file is part of colibri2
//
// Colibri is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Colibri is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with colibri2. If not, see <https://www.gnu.org/licenses/>.
import * as fs from 'fs';
import * as path_lib from 'path';
import * as section_creator from './section_creator';
import * as common_documenter from "./common";
import * as common_hdl from "../parser/common";
import { HDL_LANG } from "../common/general";
import * as parser_lib from "../parser/factory";
import * as css_const_style from "./css";
import { t_documenter_options } from "../config/auxiliar_config";
type result_type = {
document: string;
error: boolean;
};
export class Documenter extends section_creator.Creator {
private init_parser = false;
private vhdl_parser: any;
private verilog_parser: any;
constructor() {
super();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Save
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
async save_document(code: string, lang: HDL_LANG, configuration: t_documenter_options,
input_path: string, output_path: string, output_type: common_documenter.doc_output_type): Promise<boolean> {
if (output_type === common_documenter.doc_output_type.SVG) {
return this.save_svg(code, lang, configuration, output_path);
}
else {
const output_dir = path_lib.dirname(output_path);
const result = await this.get_document(code, lang, configuration, true, input_path, output_dir, false,
output_type);
if (result.error === false) {
fs.writeFileSync(output_path, result.document);
}
return result.error;
}
}
async save_svg(code: string, lang: HDL_LANG, configuration: t_documenter_options,
path: string): Promise<boolean> {
const hdl_element = await this.get_code_tree(code, lang, configuration);
if (hdl_element === undefined) {
return false;
}
// Diagram
const svg_diagram_str = await this.get_diagram_svg_from_code_tree(hdl_element);
await fs.writeFileSync(path, svg_diagram_str);
//FSM
const fsm_list = this.get_fsm_svg(code, lang, configuration);
await this.save_fsm(fsm_list, hdl_element, path);
// await this.save_wavedrom(code_tree, path);
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Get document
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
async get_document(code: string, lang: HDL_LANG, configuration: t_documenter_options,
save: boolean, input_path: string, output_svg_dir: string, extra_top_space: boolean,
output_type: common_documenter.doc_output_type): Promise<result_type> {
// const svg_dir_path = path_lib.dirname(output_svg_dir);
// const filename_svg = path_lib.basename(input_path, path_lib.extname(input_path));
const hdl_element: common_hdl.Hdl_element = await this.get_code_tree(code, lang, configuration);
if (hdl_element === undefined) {
const result: result_type = {
document: '',
error: true
};
return result;
}
////////////////////////////////////////////////////////////////////////
// HTML preparation
////////////////////////////////////////////////////////////////////////
let html_style = '';
if (save) {
html_style = `<div id="teroshdl" class='templateTerosHDL'>\n`;
html_style = css_const_style.html_style_save + html_style;
}
else {
// eslint-disable-next-line max-len
html_style = `<div id="teroshdl" class='templateTerosHDL' style="overflow-y:auto;height:100%;width:100%">\n`;
html_style = '';
html_style = css_const_style.html_style_preview + html_style;
}
let html = html_style;
if (extra_top_space) {
html += "<br><br>\n";
}
////////////////////////////////////////////////////////////////////////
// Document preparation
////////////////////////////////////////////////////////////////////////
let document = "";
if (output_type === common_documenter.doc_output_type.HTML) {
document = html;
}
////////////////////////////////////////////////////////////////////////
// Title section
////////////////////////////////////////////////////////////////////////
document += this.get_title_section(hdl_element, configuration, output_type);
////////////////////////////////////////////////////////////////////////
// Input path section
////////////////////////////////////////////////////////////////////////
document += this.get_input_section(input_path, configuration, output_type);
////////////////////////////////////////////////////////////////////////
// Info section
////////////////////////////////////////////////////////////////////////
document += this.get_info_section(hdl_element, configuration, output_type);
////////////////////////////////////////////////////////////////////////
// Custom section begin
////////////////////////////////////////////////////////////////////////
document += this.get_custom_section('custom_section_begin', hdl_element, input_path, output_type);
////////////////////////////////////////////////////////////////////////
// Diagram
////////////////////////////////////////////////////////////////////////
document += this.get_diagram_section(hdl_element, configuration, output_svg_dir, output_type);
////////////////////////////////////////////////////////////////////////
// Description
////////////////////////////////////////////////////////////////////////
document += this.get_description_section(hdl_element, configuration, output_svg_dir, output_type);
////////////////////////////////////////////////////////////////////////
// Interface section
////////////////////////////////////////////////////////////////////////
document += this.get_interface_section(hdl_element, configuration, output_type);
////////////////////////////////////////////////////////////////////////
// Generic and port
////////////////////////////////////////////////////////////////////////
document += this.get_in_out_section(hdl_element, configuration, output_type);
////////////////////////////////////////////////////////////////////////
// Signal and constant
////////////////////////////////////////////////////////////////////////
document += this.get_signal_constant_section(hdl_element, configuration, output_type);
////////////////////////////////////////////////////////////////////////
// Function
////////////////////////////////////////////////////////////////////////
document += this.get_function_section(hdl_element, configuration, output_type);
////////////////////////////////////////////////////////////////////////
// Processes
////////////////////////////////////////////////////////////////////////
document += this.get_process_section(hdl_element, configuration, output_type);
////////////////////////////////////////////////////////////////////////
// Instantiation
////////////////////////////////////////////////////////////////////////
document += this.get_instantiation_section(hdl_element, configuration, output_type);
////////////////////////////////////////////////////////////////////////
// State machine
////////////////////////////////////////////////////////////////////////
const fsm_list = await this.get_fsm_svg(code, lang, configuration);
document += this.get_fsm_section(fsm_list, hdl_element, configuration, output_svg_dir, output_type);
////////////////////////////////////////////////////////////////////////
// Custom section end
////////////////////////////////////////////////////////////////////////
document += this.get_custom_section('custom_section_end', hdl_element, input_path, output_type);
////////////////////////////////////////////////////////////////////////
// Interface section
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
// End
////////////////////////////////////////////////////////////////////////
if (output_type === common_documenter.doc_output_type.HTML) {
document += `
<br><br><br><br><br><br>
</article class="markdown-body">
</body>
`;
}
const result: result_type = {
document: document,
error: false
};
return result;
}
////////////////////////////////////////////////////////////////////////////
// Parsers
////////////////////////////////////////////////////////////////////////////
private async get_code_tree(code: string, lang: HDL_LANG, configuration: t_documenter_options) {
const parser = await this.get_parser(lang);
let symbol = configuration.verilog_symbol;
if (lang === HDL_LANG.VHDL) {
symbol = configuration.vhdl_symbol;
}
const code_tree = await parser.get_all(code, symbol);
return code_tree;
}
private async init() {
await this.create_parser(HDL_LANG.VERILOG);
await this.create_parser(HDL_LANG.VHDL);
this.init_parser = true;
}
private async get_parser(lang: HDL_LANG) {
if (this.init_parser === false) {
await this.init();
}
if (lang === HDL_LANG.VHDL) {
return this.vhdl_parser;
}
else {
return this.verilog_parser;
}
}
private async create_parser(lang: HDL_LANG) {
const parser_factory = new parser_lib.Factory();
if (lang === HDL_LANG.VHDL) {
this.vhdl_parser = await parser_factory.get_parser(lang);
}
else {
this.verilog_parser = await parser_factory.get_parser(lang);
}
}
public async get_fsm(code: string, lang: HDL_LANG, configuration: t_documenter_options) {
let symbol = configuration.verilog_symbol;
if (lang === HDL_LANG.VHDL) {
symbol = configuration.vhdl_symbol;
}
const parser = await this.get_parser(lang);
const fsm_list = await parser.get_svg_sm(code, symbol);
return fsm_list;
}
private async get_fsm_svg(code: string, lang: HDL_LANG, configuration: t_documenter_options) {
const fsm_list = await this.get_fsm(code, lang, configuration);
return fsm_list.svg;
}
}