Statements and semicolons
Every simple statement ends with a ;: a variable declaration, an
expression, return, break and continue. So does every declaration that
does not end in a block: a module variable, a struct field, a type alias, an
import, a re-export and an extern function. A statement or declaration that
ends in a block (if, while, for, with, a function, a struct) needs
none.
Line breaks, spaces and comments mean nothing to the grammar. A statement
can span as many lines as it needs, breaking anywhere between tokens,
including before an operator or a ., and several statements can share a
line.
# Every simple statement ends with a `;`; line breaks mean nothing.
is_small(n: int): bool {
return n > 0 && n < 10;
}
main(): int {
var width = 3;
var height = 4; var depth = 5;
# A statement may span several lines, breaking before or after an
# operator.
var volume = width * height
* depth;
assert(volume == 60, "3 * 4 * 5");
var in_range = is_small(width)
&& is_small(height)
&& !is_small(volume);
assert(in_range, "3 and 4 are small; 60 is not");
var words = Array<String>();
words.append("line");
words.append("breaks");
var long_words = 0;
# A chain of calls may break before each `.`.
for (var word in words
.iterate()
.filter((w) => w.length > 4)) { # comments are fine anywhere
long_words += 1;
}
assert(long_words == 1, "one word is longer than four characters");
# So may a call's arguments.
assert(volume + words[0].length == 64,
"60 + 4");
return 0;
}
A missing ; is an error
Leaving out a ; is an error, reported right after the statement it should
end, with a fix that adds it. The language server offers the fix as a quick
fix:
main.pep:2:18: error: expected ';'
2 | var total = 1
| ^
| ;
Because line breaks do not end anything, a statement missing its ; would
otherwise run on into the next line whenever that line can continue it:
x = y
-z
reads as x = y - z; with its ; missing, not as two statements. The error
says where the ; belongs when the parse cannot go on.