|
2 | 2 |
|
3 | 3 | > **<sup>Lexer</sup>**
|
4 | 4 | > LINE_COMMENT :
|
5 |
| -> `//` ~[\n\r]* |
| 5 | +> `//` (~[`/` `!`] | `//`) ~`\n`<sup>\*</sup> |
| 6 | +> | `//` |
6 | 7 | >
|
7 | 8 | > BLOCK_COMMENT :
|
8 |
| -> `/*` (BLOCK_COMMENT | .)* `*/` |
| 9 | +> `/*` (~[`*` `!`] | `**` | _BlockCommentOrDoc_) |
| 10 | +> (_BlockCommentOrDoc_ | ~`*/`)<sup>\*</sup> `*/` |
| 11 | +> | `/**/` |
| 12 | +> | `/***/` |
9 | 13 | >
|
10 |
| -> OUTER_DOC_LINE_COMMENT : |
11 |
| -> `//!` ~[\n\r]* |
| 14 | +> OUTER_LINE_DOC : |
| 15 | +> `//!` ~`\n`<sup>\*</sup> |
12 | 16 | >
|
13 |
| -> OUTER_DOC_BLOCK_COMMENT : |
14 |
| -> `/*!` (OUTER_DOC_BLOCK_COMMENT | .)* `*/` |
| 17 | +> OUTER_BLOCK_DOC : |
| 18 | +> `/*!` (_BlockCommentOrDoc_ | ~`*/`)<sup>\*</sup> `*/` |
15 | 19 | >
|
16 |
| -> INNER_DOC_LINE_COMMENT : |
17 |
| -> `///` ~[\n\r]* |
| 20 | +> INNER_DOC_LINE : |
| 21 | +> `///` (~`/` ~`\n`<sup>\*</sup>)<sup>?</sup> |
18 | 22 | >
|
19 |
| -> INNER_DOC_BLOCK_COMMENT : |
20 |
| -> `/**` (INNER_DOC_BLOCK_COMMENT | .)* `*/` |
| 23 | +> INNER_DOC_BLOCK : |
| 24 | +> `/**` (~`*` | _BlockCommentOrDoc_ ) |
| 25 | +> (_BlockCommentOrDoc_ | ~`*/`)<sup>\*</sup> `*/` |
| 26 | +> |
| 27 | +> _BlockCommentOrDoc_ : |
| 28 | +> BLOCK_COMMENT |
| 29 | +> | OUTER_BLOCK_DOC |
| 30 | +> | INNER_BLOCK_DOC |
| 31 | +
|
| 32 | +## Non-doc comments |
21 | 33 |
|
22 | 34 | Comments in Rust code follow the general C++ style of line (`//`) and
|
23 | 35 | block (`/* ... */`) comment forms. Nested block comments are supported.
|
24 | 36 |
|
25 |
| -Line comments beginning with exactly _three_ slashes (`///`), and block |
26 |
| -comments (`/** ... */`), are interpreted as a special syntax for `doc` |
27 |
| -[attributes]. That is, they are equivalent to writing |
| 37 | +Non-doc comments are interpreted as a form of whitespace. |
| 38 | + |
| 39 | +## Doc comments |
| 40 | + |
| 41 | +Line doc comments beginning with exactly _three_ slashes (`///`), and block |
| 42 | +doc comments (`/** ... */`), both inner doc comments, are interpreted as a |
| 43 | +special syntax for `doc` [attributes]. That is, they are equivalent to writing |
28 | 44 | `#[doc="..."]` around the body of the comment, i.e., `/// Foo` turns into
|
29 |
| -`#[doc="Foo"]`. |
| 45 | +`#[doc="Foo"]` and `/** Bar */` turns into `#[doc="Bar"]`. |
30 | 46 |
|
31 | 47 | Line comments beginning with `//!` and block comments `/*! ... */` are
|
32 | 48 | doc comments that apply to the parent of the comment, rather than the item
|
33 | 49 | that follows. That is, they are equivalent to writing `#![doc="..."]` around
|
34 | 50 | the body of the comment. `//!` comments are usually used to document
|
35 | 51 | modules that occupy a source file.
|
36 | 52 |
|
37 |
| -Non-doc comments are interpreted as a form of whitespace. |
| 53 | +## Examples |
| 54 | + |
| 55 | +```rust |
| 56 | +pub mod outer_module { |
| 57 | + |
| 58 | + //! - Inner line doc |
| 59 | + //!! - Still an inner line doc (but with a bang at the beginning) |
| 60 | + |
| 61 | + /*! - Inner block doc */ |
| 62 | + /*!! - Still an inner block doc (but with a bang at the beginning) */ |
| 63 | + |
| 64 | + // - Only a comment |
| 65 | + /// - Outer line doc (exactly 3 slashes) |
| 66 | + //// - Only a comment |
| 67 | + |
| 68 | + /* - Only a comment */ |
| 69 | + /** - Outer block doc (exactly) 2 asterisks */ |
| 70 | + /*** - Only a comment */ |
| 71 | + |
| 72 | + pub mod inner_module {} |
| 73 | + |
| 74 | + pub mod nested_comments { |
| 75 | + /* In Rust /* we can /* nest comments */ */ */ |
| 76 | + |
| 77 | + // All three types of block comments can contain or be nested inside |
| 78 | + // any other type: |
| 79 | + |
| 80 | + /* /* */ /** */ /*! */ */ |
| 81 | + /*! /* */ /** */ /*! */ */ |
| 82 | + /** /* */ /** */ /*! */ */ |
| 83 | + pub mod dummy_item {} |
| 84 | + } |
| 85 | + |
| 86 | + pub mod degenerate_cases { |
| 87 | + // empty inner line doc |
| 88 | + //! |
| 89 | + |
| 90 | + // empty inner block doc |
| 91 | + /*!*/ |
| 92 | + |
| 93 | + // empty line comment |
| 94 | + // |
| 95 | + |
| 96 | + // empty outer line doc |
| 97 | + /// |
| 98 | + |
| 99 | + // empty block comment |
| 100 | + /**/ |
| 101 | + |
| 102 | + pub mod dummy_item {} |
| 103 | + |
| 104 | + // empty 2-asterisk block isn't a doc block, it is a block comment |
| 105 | + /***/ |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + /* The next one isn't allowed because outer doc comments |
| 110 | + require an item that will receive the doc */ |
| 111 | + |
| 112 | + /// Where is my item? |
| 113 | +# mod dummy_item {} |
| 114 | +} |
| 115 | +``` |
38 | 116 |
|
39 | 117 | [attributes]: attributes.html
|
0 commit comments