· Overview · D for Win32 · Win32 DLLs in D · C .h to D Modules · FAQ · Style Guide · Example: wc · Future · D Change Log · Tech Tips · Glossary · Acknowledgements Tools · DMD D Compiler · GDC D Compiler · Linker · Profiler Community · News Digest · News · Forum · Announcements · Learn · D links Archives · digitalmars.D · digitalmars.D.dtl · digitalmars.D.announce · digitalmars.D.learn · digitalmars.D.bugs · D.gnu · Old D |
Embedded DocumentationThe D programming language enables embedding both contracts and test code along side the actual code, which helps to keep them all consistent with each other. One thing lacking is the documentation, as ordinary comments are usually unsuitable for automated extraction and formatting into manual pages. Embedding the user documentation into the source code has important advantages, such as not having to write the documentation twice, and the likelihood of the documentation staying consistent with the code.Some existing approaches to this are:
SpecificationThe specification for the form of embedded documentation comments only specifies how information is to be presented to the compiler. It is implementation-defined how that information is used and the form of the final presentation. Whether the final presentation form is an HTML web page, a man page, a PDF file, etc. is not specified as part of the D Programming Language.
Phases of ProcessingEmbedded documentation comments are processed in a series of phases:
LexicalEmbedded documentation comments are one of the following forms:
/// This is a one line documentation comment. /** So is this. */ /++ And this. +/ /** This is a brief documentation comment. */ /** * The leading * on this line is not part of the documentation comment. */ /********************************* The extra *'s immediately following the /** are not part of the documentation comment. */ /++ This is a brief documentation comment. +/ /++ + The leading + on this line is not part of the documentation comment. +/ /+++++++++++++++++++++++++++++++++ The extra +'s immediately following the /++ are not part of the documentation comment. +/The extra *'s and +'s on the comment opening and left margin are ignored and are not part of the embedded documentation. Comments not following one of those forms are not documentation comments. ParsingEach documentation comment is associated with a declaration. If the documentation comment is on a line by itself or with only whitespace to the left, it refers to the next declaration. Multiple documentation comments applying to the same declaration are concatenated. Documentation comments not associated with a declaration are ignored. Documentation comments preceding the ModuleDeclaration apply to the entire module. If the documentation comment appears on the same line to the right of a declaration, it applies to that.If a documentation comment for a declaration consists only of the identifier ditto then the documentation comment for the previous declaration at the same declaration scope is applied to this declaration as well. If there is no documentation comment for a declaration, that declaration may not appear in the output. To ensure it does appear in the output, put an empty declaration comment for it. int a; /// documentation for a; b has no documentation int b; /** documentation for c and d */ /** more documentation for c and d */ int c; /** ditto */ int d; /** documentation for e and f */ int e; int f; /// ditto /** documentation for g */ int g; /// more documentation for g /// documentation for C and D class C { int x; /// documentation for C.x /** documentation for C.y and C.z */ int y; int z; /// ditto } /// ditto class D { } SectionsThe document comment is a series of Sections. A Section is a name that is the first non-blank character on a line immediately followed by a ':'. This name forms the section name. The section name is not case sensitive.
SummaryThe first section is the Summary, and does not have a section name. It is first paragraph, up to a blank line or a section name. While the summary can be any length, try to keep it to one line. The Summary section is optional.SynopsisThe next unnamed section is the Synopsis. It consists of all the paragraphs following the Summary until a section name is encountered or the end of the comment.While the Synopsis section is optional, there cannot be a Synopsis without a Summary section. /*********************************** * Brief summary of what * myfunc does, forming the summary section. * * First paragraph of synopsis description. * * Second paragraph of * synopsis description. */ void myfunc() { }Named sections follow the Summary and Synopsis unnamed sections. Standard SectionsFor consistency and predictability, there are several standard sections. None of these are required to be present.
Special SectionsSome sections have specialized meanings and syntax.
MacrosThe documentation comment processor includes a simple macro text preprocessor. There are predefined macros, and macros defined by the Macros: section. When a appears in section text it is replaced with NAME's corresponding replacment text. The replacement text is then recursively scanned for more macros. If a macro is recursively encountered, only one level of recursion is performed. Macro invocations that cut across replacement text boundaries are not expanded. If NAME is not a macro name, it is assumed to be a file name, and the contents of the file with that name become the replacement text. (By convention, non-filename macro names are in all upper case, and filenames are not.) If the file does not exist, the replacement text has no characters in it. If a is desired to exist in the output without being macro expanded, add an extra $ as in: $(NAME). The extra $ will be elided.Predefined Macros
HighlightingEmbedded CommentsThe documentation comments can themselves be commented using the <!-- comment text --> syntax. These comments do not nest.Embedded CodeD code can be embedded using lines with at least three -'s in them to delinate the code section:/++++++++++++++++++++++++ + Our function. + Example: + -------------------------- + #include <stdio.h> + + void foo() + { + printf("foo!\n"); /* print the string */ + } + -------------------------- +/Note that documentation comment uses the /++ ... +/ form so that /* ... */ can be used inside the code section. Embedded HTMLHTML can be embedded into the documentation comments, and it will be passed through to the HTML output unchanged. However, since it is not necessarily true that HTML will be the desired output format of the embedded documentation comment extractor, it is best to avoid using it where practical./** Example of embedded HTML: * <ol> * <li> <a href="www.digitalmars.com">Digital Mars</a> * <li> <a href="www.classicempire.com">Empire</a> * </ol> */ EmphasisIdentifiers in documentation comments that are function parameters or are names that are in scope at the associated declaration are emphasized in the output. This emphasis can take the form of italics, boldface, a hyperlink, etc. How it is emphasized depends on what it is - a function parameter, type, D keyword, etc. To prevent unintended emphasis of an identifier, it can be preceded by an underscore (_). The underscore will be stripped from the output.Character EntitiesBecause the characters <, > and & have special meaning to the documentation processor, to avoid confusion it can be best to replace them with their corresponding character entities:
|
Add feedback and comments regarding this page.