Skip to content

Commit b272259

Browse files
committed
Adjusted comments' grammar to account for special cases
1 parent 586e630 commit b272259

File tree

1 file changed

+93
-15
lines changed

1 file changed

+93
-15
lines changed

src/comments.md

Lines changed: 93 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,116 @@
22

33
> **<sup>Lexer</sup>**
44
> LINE_COMMENT :
5-
> &nbsp;&nbsp; `//` ~[\n\r]*
5+
> &nbsp;&nbsp; &nbsp;&nbsp; `//` (~[`/` `!`] | `//`) ~`\n`<sup>\*</sup>
6+
> &nbsp;&nbsp; | `//`
67
>
78
> BLOCK_COMMENT :
8-
> &nbsp;&nbsp; `/*` (BLOCK_COMMENT | .)* `*/`
9+
> &nbsp;&nbsp; &nbsp;&nbsp; `/*` (~[`*` `!`] | `**` | _BlockCommentOrDoc_)
10+
> (_BlockCommentOrDoc_ | ~`*/`)<sup>\*</sup> `*/`
11+
> &nbsp;&nbsp; | `/**/`
12+
> &nbsp;&nbsp; | `/***/`
913
>
10-
> OUTER_DOC_LINE_COMMENT :
11-
> &nbsp;&nbsp; `//!` ~[\n\r]*
14+
> OUTER_LINE_DOC :
15+
> &nbsp;&nbsp; `//!` ~`\n`<sup>\*</sup>
1216
>
13-
> OUTER_DOC_BLOCK_COMMENT :
14-
> &nbsp;&nbsp; `/*!` (OUTER_DOC_BLOCK_COMMENT | .)* `*/`
17+
> OUTER_BLOCK_DOC :
18+
> &nbsp;&nbsp; `/*!` (_BlockCommentOrDoc_ | ~`*/`)<sup>\*</sup> `*/`
1519
>
16-
> INNER_DOC_LINE_COMMENT :
17-
> &nbsp;&nbsp; `///` ~[\n\r]*
20+
> INNER_DOC_LINE :
21+
> &nbsp;&nbsp; `///` (~`/` ~`\n`<sup>\*</sup>)<sup>?</sup>
1822
>
19-
> INNER_DOC_BLOCK_COMMENT :
20-
> &nbsp;&nbsp; `/**` (INNER_DOC_BLOCK_COMMENT | .)* `*/`
23+
> INNER_DOC_BLOCK :
24+
> &nbsp;&nbsp; `/**` (~`*` | _BlockCommentOrDoc_ )
25+
> (_BlockCommentOrDoc_ | ~`*/`)<sup>\*</sup> `*/`
26+
>
27+
> _BlockCommentOrDoc_ :
28+
> &nbsp;&nbsp; &nbsp;&nbsp; BLOCK_COMMENT
29+
> &nbsp;&nbsp; | OUTER_BLOCK_DOC
30+
> &nbsp;&nbsp; | INNER_BLOCK_DOC
31+
32+
## Non-doc comments
2133

2234
Comments in Rust code follow the general C++ style of line (`//`) and
2335
block (`/* ... */`) comment forms. Nested block comments are supported.
2436

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
2844
`#[doc="..."]` around the body of the comment, i.e., `/// Foo` turns into
29-
`#[doc="Foo"]`.
45+
`#[doc="Foo"]` and `/** Bar */` turns into `#[doc="Bar"]`.
3046

3147
Line comments beginning with `//!` and block comments `/*! ... */` are
3248
doc comments that apply to the parent of the comment, rather than the item
3349
that follows. That is, they are equivalent to writing `#![doc="..."]` around
3450
the body of the comment. `//!` comments are usually used to document
3551
modules that occupy a source file.
3652

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+
```
38116

39117
[attributes]: attributes.html

0 commit comments

Comments
 (0)