Skip to content

Commit ad9c1b1

Browse files
committed
feat(reference): Lexical Structure—Comments
1 parent c4d9b74 commit ad9c1b1

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

docs_config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ docs_groups:
8383
- reference/introduction
8484
- reference/lexical_structure:
8585
- reference/lexical_structure/whitespace
86-
# - reference/lexical_structure/comments
86+
- reference/lexical_structure/comments
8787
# - reference/lexical_structure/keywords
8888
# - reference/lexical_structure/identifiers
8989
# - reference/lexical_structure/literals
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
title: Comments
3+
---
4+
5+
Comments are completely ignored by the lexer. It's worth noting that comments act as token separators in the same way that whitespace does—`a/**/b` will produce two tokens, `a` and `b`.
6+
7+
Each comment type accepted by the lexer is described below.
8+
9+
## Documentation comments
10+
11+
```ebnf
12+
doc_comment = '/**' .* '*/' ;
13+
```
14+
15+
Documentation comments begin with `/**` and end with `*/`. Everything between the start and end characters is considered the the body of the comment.
16+
17+
### Examples
18+
19+
```grain
20+
/** single line documentation comment */
21+
```
22+
23+
```grain
24+
/** multiple
25+
line
26+
documentation
27+
comment
28+
*/
29+
```
30+
31+
## Block comments
32+
33+
```ebnf
34+
block_comment = '/*' .* '*/' ;
35+
```
36+
37+
Block comments begin with `/*` and end with `*/`. Everything between the start and end characters is considered the the body of the comment.
38+
39+
### Examples
40+
41+
```grain
42+
/* single line block comment */
43+
```
44+
45+
```grain
46+
/* multiple
47+
line
48+
documentation
49+
comment
50+
*/
51+
```
52+
53+
## Line comments
54+
55+
```ebnf
56+
line_comment = '//' .* NEWLINE ;
57+
```
58+
59+
Line comments begin with `//` and are terminated by a `NEWLINE`. Everything that appears after the `//` and before the end of the line is considered the body of the comment.
60+
61+
### Examples
62+
63+
```grain
64+
// single line line comment
65+
```
66+
67+
## Shebangs
68+
69+
```ebnf
70+
shebang = '#!' .* NEWLINE ;
71+
```
72+
73+
Shebangs, also known as hashbangs, begin with `#!` and are terminated by a `NEWLINE`. Everything that appears after the `#!` and before the end of the line is considered the body of the comment. The lexer allows shebangs to appear anywhere in the input, not just the first line.
74+
75+
### Examples
76+
77+
```grain
78+
#! /usr/bin/grain
79+
```

0 commit comments

Comments
 (0)