Skip to content

Commit 3793830

Browse files
committed
tools: Move gendoc.py to tools, and make it a little more generic.
1 parent 09bbe72 commit 3793830

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

stmhal/gendoc.py renamed to tools/gendoc.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ def error(self, msg):
6767
print('({}:{}) {}'.format(self.filename, self.cur_line, msg))
6868
raise Lexer.LexerError
6969

70+
class DocValidateError(Exception):
71+
pass
72+
7073
class DocItem:
7174
def __init__(self):
7275
self.doc = []
@@ -216,6 +219,10 @@ def process_method(self, lex, d):
216219
def process_constant(self, lex, d):
217220
self.cur_class.process_constant(lex, d)
218221

222+
def validate(self):
223+
if self.descr is None:
224+
raise DocValidateError('module {} referenced but never defined'.format(self.name))
225+
219226
def dump(self):
220227
s = []
221228
s.append('# module {}'.format(self.name))
@@ -262,15 +269,18 @@ def check_module(self, lex):
262269

263270
def process_module(self, lex, d):
264271
name = d['id']
265-
if name in self.modules:
272+
if name not in self.modules:
273+
self.modules[name] = DocModule(name, None)
274+
self.cur_module = self.modules[name]
275+
if self.cur_module.descr is not None:
266276
lex.error("multiple definition of module '{}'".format(name))
267-
self.cur_module = self.modules[name] = DocModule(name, d['descr'])
277+
self.cur_module.descr = d['descr']
268278
self.cur_module.add_doc(lex)
269279

270280
def process_moduleref(self, lex, d):
271281
name = d['id']
272282
if name not in self.modules:
273-
lex.error('module {} referenced before definition'.format(name))
283+
self.modules[name] = DocModule(name, None)
274284
self.cur_module = self.modules[name]
275285
lex.opt_break()
276286

@@ -294,6 +304,10 @@ def process_constant(self, lex, d):
294304
self.check_module(lex)
295305
self.cur_module.process_constant(lex, d)
296306

307+
def validate(self):
308+
for m in self.modules.values():
309+
m.validate()
310+
297311
def write(self, dir):
298312
for m in self.modules.values():
299313
mod_dir = os.path.join(dir, 'module', m.name)
@@ -339,17 +353,18 @@ def process_file(file, doc):
339353
def main():
340354
cmd_parser = argparse.ArgumentParser(description='Generate documentation for pyboard API from C files.')
341355
cmd_parser.add_argument('--outdir', metavar='<output dir>', default='gendoc-out', help='ouput directory')
342-
cmd_parser.add_argument('files', nargs='*', help='input files')
356+
cmd_parser.add_argument('files', nargs='+', help='input files')
343357
args = cmd_parser.parse_args()
344358

345-
if len(args.files) == 0:
346-
args.files = ['modpyb.c', 'accel.c', 'adc.c', 'dac.c', 'extint.c', 'i2c.c', 'led.c', 'pin.c', 'rng.c', 'servo.c', 'spi.c', 'uart.c', 'usrsw.c', 'timer.c', 'rtc.c']
347-
348359
doc = Doc()
349360
for file in args.files:
350361
print('processing', file)
351362
if not process_file(file, doc):
352363
return
364+
try:
365+
doc.validate()
366+
except DocValidateError as e:
367+
print(e)
353368
doc.write(args.outdir)
354369
print('written to', args.outdir)
355370

0 commit comments

Comments
 (0)