Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ir] Generate yaml documentation for statement classes (#2192)
- Loading branch information
1 parent
42b8ed3
commit dee35f4
Showing
2 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import taichi as ti | ||
import os | ||
import yaml | ||
|
||
|
||
def extract_doc(doc_filename=None): | ||
repo_dir = ti.get_repo_directory() | ||
statements_fn = os.path.join(repo_dir, 'taichi/ir/statements.h') | ||
with open(statements_fn, 'r') as f: | ||
statements = f.readlines() | ||
|
||
class_doc = {} | ||
|
||
for i in range(len(statements)): | ||
line = statements[i] | ||
start_pos = line.find('/**') | ||
if start_pos == -1: | ||
continue | ||
current_doc = line[start_pos + 3:].strip() | ||
doc_ends_at_line = 0 | ||
for j in range(i + 1, len(statements)): | ||
next_line = statements[j] | ||
end_pos = next_line.find('*/') | ||
if end_pos != -1: | ||
doc_ends_at_line = j | ||
break | ||
next_line = next_line.strip() | ||
if next_line.startswith('*'): | ||
next_line = next_line[1:].strip() | ||
if next_line == '': # an empty line | ||
current_doc += '\n' | ||
else: | ||
current_doc += ' ' + next_line | ||
current_doc = current_doc.strip() | ||
|
||
line = statements[doc_ends_at_line + 1] | ||
start_pos = line.find('class') | ||
if start_pos == -1: | ||
print('We only support doc for classes now. ' | ||
f'The following doc at line {i}-{doc_ends_at_line} ' | ||
'cannot be recognized:\n' | ||
f'{current_doc}') | ||
continue | ||
class_name = line[start_pos + 5:].strip().split()[0] | ||
class_doc[class_name] = current_doc | ||
|
||
if doc_filename is None: | ||
doc_filename = 'ir_design_doc.yml' | ||
with open(doc_filename, 'w') as f: | ||
yaml.dump(class_doc, f, Dumper=yaml.SafeDumper) | ||
|
||
|
||
def yml_to_md(yml_filename=None, md_filename=None): | ||
if yml_filename is None: | ||
yml_filename = 'ir_design_doc.yml' | ||
if md_filename is None: | ||
md_filename = 'ir_design_doc.md' | ||
with open(yml_filename, 'r') as f: | ||
doc_yml = yaml.load(f, Loader=yaml.SafeLoader) | ||
doc_md = '' | ||
for (class_name, class_doc) in doc_yml.items(): | ||
doc_md += f'### {class_name}\n' | ||
doc_md += class_doc | ||
doc_md += '\n\n' | ||
with open(md_filename, 'w') as f: | ||
f.write(doc_md) | ||
|
||
|
||
if __name__ == '__main__': | ||
extract_doc() | ||
yml_to_md() | ||
print('Done!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters