Skip to content

Commit 6d29d00

Browse files
authored
Merge pull request #85 from brauliobz/grammar_pull_request
Added grammar of comments
2 parents 25f5ef1 + cd80d1f commit 6d29d00

File tree

1 file changed

+110
-5
lines changed

1 file changed

+110
-5
lines changed

src/comments.md

Lines changed: 110 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,125 @@
11
# Comments
22

3+
> **<sup>Lexer</sup>**
4+
> LINE_COMMENT :
5+
> &nbsp;&nbsp; &nbsp;&nbsp; `//` (~[`/` `!`] | `//`) ~`\n`<sup>\*</sup>
6+
> &nbsp;&nbsp; | `//`
7+
>
8+
> BLOCK_COMMENT :
9+
> &nbsp;&nbsp; &nbsp;&nbsp; `/*` (~[`*` `!`] | `**` | _BlockCommentOrDoc_)
10+
> (_BlockCommentOrDoc_ | ~`*/`)<sup>\*</sup> `*/`
11+
> &nbsp;&nbsp; | `/**/`
12+
> &nbsp;&nbsp; | `/***/`
13+
>
14+
> OUTER_LINE_DOC :
15+
> &nbsp;&nbsp; `//!` ~[`\n` _IsolatedCR_]<sup>\*</sup>
16+
>
17+
> OUTER_BLOCK_DOC :
18+
> &nbsp;&nbsp; `/*!` ( _BlockCommentOrDoc_ | ~[`*/` _IsolatedCR_] )<sup>\*</sup> `*/`
19+
>
20+
> INNER_LINE_DOC :
21+
> &nbsp;&nbsp; `///` (~`/` ~[`\n` _IsolatedCR_]<sup>\*</sup>)<sup>?</sup>
22+
>
23+
> INNER_BLOCK_DOC :
24+
> &nbsp;&nbsp; `/**` (~`*` | _BlockCommentOrDoc_ )
25+
> (_BlockCommentOrDoc_ | ~[`*/` _IsolatedCR_])<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+
> _IsolatedCR_ :
33+
> &nbsp;&nbsp; _A `\r` not followed by a `\n`_
34+
35+
## Non-doc comments
36+
337
Comments in Rust code follow the general C++ style of line (`//`) and
438
block (`/* ... */`) comment forms. Nested block comments are supported.
539

6-
Line comments beginning with exactly _three_ slashes (`///`), and block
7-
comments (`/** ... */`), are interpreted as a special syntax for `doc`
8-
[attributes]. That is, they are equivalent to writing
40+
Non-doc comments are interpreted as a form of whitespace.
41+
42+
## Doc comments
43+
44+
Line doc comments beginning with exactly _three_ slashes (`///`), and block
45+
doc comments (`/** ... */`), both inner doc comments, are interpreted as a
46+
special syntax for `doc` [attributes]. That is, they are equivalent to writing
947
`#[doc="..."]` around the body of the comment, i.e., `/// Foo` turns into
10-
`#[doc="Foo"]`.
48+
`#[doc="Foo"]` and `/** Bar */` turns into `#[doc="Bar"]`.
1149

1250
Line comments beginning with `//!` and block comments `/*! ... */` are
1351
doc comments that apply to the parent of the comment, rather than the item
1452
that follows. That is, they are equivalent to writing `#![doc="..."]` around
1553
the body of the comment. `//!` comments are usually used to document
1654
modules that occupy a source file.
1755

18-
Non-doc comments are interpreted as a form of whitespace.
56+
Isolated CRs (`\r`), i.e. not followed by LF (`\n`), are not allowed in doc
57+
comments.
58+
59+
## Examples
60+
61+
```rust
62+
//! A doc comment that applies to the implicit anonymous module of this crate
63+
64+
pub mod outer_module {
65+
66+
//! - Inner line doc
67+
//!! - Still an inner line doc (but with a bang at the beginning)
68+
69+
/*! - Inner block doc */
70+
/*!! - Still an inner block doc (but with a bang at the beginning) */
71+
72+
// - Only a comment
73+
/// - Outer line doc (exactly 3 slashes)
74+
//// - Only a comment
75+
76+
/* - Only a comment */
77+
/** - Outer block doc (exactly) 2 asterisks */
78+
/*** - Only a comment */
79+
80+
pub mod inner_module {}
81+
82+
pub mod nested_comments {
83+
/* In Rust /* we can /* nest comments */ */ */
84+
85+
// All three types of block comments can contain or be nested inside
86+
// any other type:
87+
88+
/* /* */ /** */ /*! */ */
89+
/*! /* */ /** */ /*! */ */
90+
/** /* */ /** */ /*! */ */
91+
pub mod dummy_item {}
92+
}
93+
94+
pub mod degenerate_cases {
95+
// empty inner line doc
96+
//!
97+
98+
// empty inner block doc
99+
/*!*/
100+
101+
// empty line comment
102+
//
103+
104+
// empty outer line doc
105+
///
106+
107+
// empty block comment
108+
/**/
109+
110+
pub mod dummy_item {}
111+
112+
// empty 2-asterisk block isn't a doc block, it is a block comment
113+
/***/
114+
115+
}
116+
117+
/* The next one isn't allowed because outer doc comments
118+
require an item that will receive the doc */
119+
120+
/// Where is my item?
121+
# mod boo {}
122+
}
123+
```
19124

20125
[attributes]: attributes.html

0 commit comments

Comments
 (0)