Skip to content

Commit 01c86b8

Browse files
ppolesiukforell
andauthored
Specification of comments (#4)
Co-authored-by: Patrycja Balik <root@pbalik.com>
1 parent d1c7924 commit 01c86b8

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

src/ref/lexer.md

+85
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,95 @@ by the lexer, but they separate tokens, e.g., identifiers or literals.
99

1010
## Comments
1111

12+
Fram supports two kinds of comments: single-line and block comments. Comments
13+
are treated similarly to whitespace: they are ignored by the lexer, but they
14+
always separate tokens.
15+
16+
Single-line comments start with the `#` character and end with a new line or
17+
the end of file.
18+
19+
Block comments are introduced by the sequence `{#` followed by any, possibly
20+
empty, sequence *name* of valid identifier characters, operators, and
21+
`#` characters. Such a block comment is terminated by the first occurrence of
22+
*name* that is immediately followed by `#}`. More precisely, it can be
23+
described by the following grammar.
24+
```bnf
25+
block-comment-name ::= { ident-char | op-char | "#" }
26+
block-comment-start ::= "{#" block-comment-name
27+
block-comment-end ::= block-comment-name "#}"
28+
```
29+
Non-terminal symbols `ident-char` and `op-char` are defined later in this
30+
chapter. At the comment opening, the longest consecutive sequence described by
31+
`block-comment-name` is taken as the comment name. This name should be a suffix
32+
of the name provided at comment closing. Comments using the same name cannot
33+
be nested. This is not an issue in practice, since the programmer can always
34+
choose a unique name to accomplish nesting.
35+
36+
By convention, single-line comments starting with `##` and block comments
37+
with a name starting with `#` are used as documentation comments, and can be
38+
recognized by some external tools.
39+
40+
### Examples
41+
42+
The following code contains some valid comments.
43+
````fram
44+
# This is a single-line comment.
45+
{# This is a block comment. #}
46+
{# Block comments
47+
may span multiple lines.
48+
#}
49+
let id x = x # A single-line comment may appear at the end of a line.
50+
51+
let n {# A block comment may span a part of a single line. #} = 42
52+
{#aaa
53+
Comments cannot be nested,
54+
{# but the programmer may choose the comment delimiters. #}
55+
aaa#}
56+
57+
{#!a! Comment names may contain operators. !a!#}
58+
59+
{#abc
60+
This comment is ended by `abc` immediately followed by `#}`,
61+
even if the closing sequence is preceded by other characters.
62+
zzabc#}
63+
64+
let {#
65+
# This is not a single-line comment,
66+
# because comments are not nested.
67+
# This comment can be ended #} here = 13
68+
69+
## This is a documentation comment.
70+
let foo x = x
71+
72+
{## This is another documentation comment. ##}
73+
let bar = foo
74+
75+
{###
76+
Documentation comments can contain some code
77+
```
78+
{## with another documentation comment (with a different name). #}}
79+
let some_code = 42
80+
```
81+
###}
82+
let baz = bar
83+
````
84+
1285
## Literals
1386

87+
```bnf
88+
digit ::= "0"..."9"
89+
lower-char ::= "a"..."z" | "_"
90+
upper-char ::= "A"..."Z"
91+
ident-char ::= lower-char | upper-char | digit | "'"
92+
```
93+
1494
## Keywords
1595

1696
## Identifiers
1797

1898
## Operators
99+
100+
```bnf
101+
op-char ::= "<" | ">" | "&" | "$" | "?" | "!" | "@" | "^" | "+" | "-"
102+
| "~" | "*" | "%" | ";" | "," | "=" | "|" | ":" | "." | "/"
103+
```

0 commit comments

Comments
 (0)