| 1 | = Cobra Code Documentation = |
| 2 | |
| 3 | Cobra code can be documented internally with two constructs |
| 4 | * Comments |
| 5 | * Docstrings |
| 6 | |
| 7 | Comments explain or clarify code and intent.[[BR]] |
| 8 | Docstrings describe use of code. |
| 9 | |
| 10 | |
| 11 | |
| 12 | == Comments == |
| 13 | |
| 14 | Comments are used to provide some text to a person reading the code and are intended |
| 15 | to describe implementation; i.e clarify the code used or describe intent |
| 16 | or background to the code. |
| 17 | |
| 18 | Comment lines can be placed pretty much anywhere. |
| 19 | |
| 20 | There are two types of comments |
| 21 | * Single line |
| 22 | * Multiline |
| 23 | |
| 24 | === Grammar === |
| 25 | {{{ |
| 26 | |
| 27 | # ANY COMMENT TEXT |
| 28 | code... # TRAILING COMMENT TEXT |
| 29 | |
| 30 | |
| 31 | code... /# commented out (code) within line #/ ...code... |
| 32 | |
| 33 | code... |
| 34 | /# |
| 35 | multiline block of code or text commented out |
| 36 | # embedded line comment |
| 37 | code... # trailing comment |
| 38 | #/ |
| 39 | code... |
| 40 | }}} |
| 41 | |
| 42 | === Single line comments === |
| 43 | |
| 44 | Single line comments are introduced with a '''#''' character.[[br]] |
| 45 | The remainder of the line (upto the EOLn character) is ignored so far as |
| 46 | tokenization and code generation is concerned. |
| 47 | {{{ |
| 48 | numberType = if(.typeProvider, .typeProvider.numberType, DecimalType()) |
| 49 | # parse literal to same type as numberType |
| 50 | }}} |
| 51 | |
| 52 | A multiline block of comment text can be specified with multiple single |
| 53 | lines starting with '#'. [[br]] |
| 54 | Using multiple single comment lines this way also helps visually delineate the |
| 55 | descriptive block better. |
| 56 | |
| 57 | {{{ |
| 58 | def onWHITESPACE_LINE(tok as IToken) as IToken? |
| 59 | # Eat these. |
| 60 | # Don't muck with perceived indentation level as |
| 61 | # these kinds of lines are irrelevant. |
| 62 | #print '<> onWHITESPACE_LINE' |
| 63 | return nil |
| 64 | .... |
| 65 | # Note: The Tokenizer class handles it's input one line at a time, |
| 66 | # and retains the \n at the end of the line. This affects |
| 67 | # the regex design for the tokens below. |
| 68 | |
| 69 | }}} |
| 70 | |
| 71 | |
| 72 | The comment character can appear anywhere in a line so code may be |
| 73 | described with an inline trailing comment. |
| 74 | {{{ |
| 75 | var _didLineContinuation as bool # only meaningful after an EOL |
| 76 | }}} |
| 77 | |
| 78 | |
| 79 | === !MultiLine Comments === |
| 80 | |
| 81 | Multiline comments are a block of text delimited by '''/#''' and '''#/'''.[[BR]] |
| 82 | Everything from the first delimiter up to and including the first occurrence |
| 83 | of the second delimiter is ignored |
| 84 | (including any embedded single line or trailing comments. |
| 85 | |
| 86 | The most common use for this perhaps is to comment out a block of code. |
| 87 | {{{ |
| 88 | def startSource(fileName as String, source as String) as Tokenizer |
| 89 | /# |
| 90 | print '**********************************************' |
| 91 | print source |
| 92 | print '**********************************************' |
| 93 | #/ |
| 94 | _fileName = fileName |
| 95 | _stream = StringReader(source) |
| 96 | .afterStart |
| 97 | return this |
| 98 | }}} |
| 99 | |
| 100 | You can also comment (out) code within a line with inline '''/#... #/''' |
| 101 | {{{ |
| 102 | x = /# -1 * #/ z * y |
| 103 | # same as x = z * y |
| 104 | |
| 105 | params = .paramDecls(/#skipParen=#/true) |
| 106 | }}} |
| 107 | |
| 108 | |
| 109 | |
| 110 | === Special Comment Strings === |
| 111 | |
| 112 | Cobra generates warnings for things it detects can be done in a less verbose |
| 113 | or more ''snakelike'' manner. [[br]] |
| 114 | Mostly this is helpful, sometimes its irritating or unnecessary. |
| 115 | |
| 116 | You can suppress (all) warnings generated for a particular line with a |
| 117 | trailing comment starting with the tag '''.nowarnings.''' (note leading and trailing '.').[[br]] |
| 118 | Any subsequent text after the tag is ignored. |
| 119 | {{{ |
| 120 | # Normally this would warn with a suggestion to remove the trailing () |
| 121 | callMe() # .nowarnings. I like explicit () for this call |
| 122 | }}} |
| 123 | |
| 124 | |
| 125 | |
| 126 | === Notes === |
| 127 | |
| 128 | Its not uncommon to provide comments describing additional code or capability |
| 129 | to be implemented at a later date. [[br]] |
| 130 | Usually this uses a tag '''TODO:''' followed by the notation. |
| 131 | {{{ |
| 132 | # TODO: move to CobraCore |
| 133 | ... |
| 134 | # TODO: Add contract to enforce above statement |
| 135 | }}} |
| 136 | |
| 137 | |
| 138 | |
| 139 | ------------------------- |
| 140 | == !DocStrings == |
| 141 | |
| 142 | Docstrings can be placed at various specific points in cobracode and are intended |
| 143 | as documentation to describe the use of or as a summary of the docced item. |
| 144 | |
| 145 | Docstrings may be single line or multi line but in both cases are delimited start and end by triple ". [[BR]] |
| 146 | e.g |
| 147 | {{{ |
| 148 | """This is a single line docstring.""" |
| 149 | |
| 150 | """ |
| 151 | This is a multiline Docstring. |
| 152 | |
| 153 | Leading summary line, blank line and descriptive text |
| 154 | Leading and trailing triple-" delimiter each on its own line. |
| 155 | """ |
| 156 | }}} |
| 157 | Docstrings are placed after the item being documented indented as if they were executable code. |
| 158 | |
| 159 | Single line docstrings are intended for documenting simple really obvious cases.[[br]] |
| 160 | Multiline are for items that require a bit more description. |
| 161 | |
| 162 | For a good description of the form, format and intent of docstrings (wrt python) |
| 163 | please see the [http://www.python.org/dev/peps/pep-0257/ python PEP]. |
| 164 | |
| 165 | Docstrings can appear: |
| 166 | * At the start of a Module (File) |
| 167 | * Immediately after a '''namespace''' declaration |
| 168 | * Immediately after an Object declaration: |
| 169 | '''class''', '''interface''', '''struct''', '''mixin''', '''enum''', '''event''' |
| 170 | * Immediately after an extension declaration ('''extend''') |
| 171 | * after a '''var''' or '''const''' declaration |
| 172 | * after a property definition ('''pro''', '''get''', '''set''') |
| 173 | * after a method or cue (initializer) declaration ( '''def''' and '''cue''') |
| 174 | * after a '''sig''' declaration |
| 175 | |
| 176 | === Grammar === |
| 177 | {{{ |
| 178 | """ TEXT """ |
| 179 | |
| 180 | """ |
| 181 | TEXT LINE |
| 182 | SUBSEQUENT TEXT LINES ... |
| 183 | """ |
| 184 | }}} |
| 185 | === Examples === |
| 186 | |
| 187 | {{{ |
| 188 | """Example module showing docstring positions""" |
| 189 | |
| 190 | |
| 191 | class AClass |
| 192 | """ This is an example class containing lots of docstrings.""" |
| 193 | |
| 194 | var _foofahCount = 0 |
| 195 | """Used only with green wiglets""" |
| 196 | |
| 197 | get hasFoofahs as bool |
| 198 | """True if number of foofahs > 0""" |
| 199 | |
| 200 | pro wigletIsGreen as bool |
| 201 | """Triggers foofah count if true.""" |
| 202 | |
| 203 | cue init |
| 204 | """Std Initializer.""" |
| 205 | base.init |
| 206 | |
| 207 | def doNothing |
| 208 | """Nothing happens in this method. comeback later""" |
| 209 | pass |
| 210 | }}} |
| 211 | |
| 212 | |
| 213 | === Notes === |
| 214 | |
| 215 | The cobra compiler can be run to generate html files containing code entrypoints/signatures and docstrings for each listed file. |
| 216 | {{{ |
| 217 | cobra -doc standAlone.cobra |
| 218 | |
| 219 | cobra -doc main.cobra -files:files.cobra |
| 220 | }}} |
| 221 | |
| 222 | |
| 223 | == See Also == |
| 224 | |
| 225 | |
| 226 | [wiki:LanguageTopics Back to Language Topics] |