ford icon indicating copy to clipboard operation
ford copied to clipboard

Python API for determining properties of a Fortran docstring

Open gnikit opened this issue 3 years ago • 1 comments
trafficstars

I was wondering if ford has a Python API that one can use to determine if

  • a Fortran comment is a valid docstring
  • the docstring uses predocmark
  • the docstring uses docmark_alt
  • Other docstring info and ford settings

I suspect that internally it must have, but I was hoping to use said API to determine during parsing with fortls to what AST node should the docstring be attached. Currently the fortls documentation parsing is a bit flimsy.

gnikit avatar Apr 01 '22 18:04 gnikit

Not exactly, no. The way it works is to first convert the source into a "normalised" form where any predocmarks are put after the entity, the *_alt forms are converted to non-alt forms, all preceding whitespace is stripped. Then during parsing, we can simply collect all consecutive lines that begin with docmark.

The normalisation is done via https://github.com/Fortran-FOSS-Programmers/ford/blob/e9775f9958969d99960a994ff2628f1e7994ed99/ford/reader.py#L78 which is essentially an iterator over the source file.

This is currently tied to opening a file and iterating over its contents, but I think could be easily modified to iterate over a StringIO so that you could pass in a snippet of text and convert it to the normalised form. Then you would know that all lines beginning with docmark would be annotating the previous line.

Maybe that is sufficient?

You can use ford.parse_arguments({}, "ford_docs.md") to get a dict of Ford options (plus the parsed "ford_docs.md" file, and the markdown object)

ZedThree avatar Apr 04 '22 09:04 ZedThree