The counting rules
By the end of this chapter you can predict, for an ordinary source file, which of tokei's three columns each line will land in — including the lines designed to fool a simpler counter.
The rules are per language, but
most languages share the same few
ideas. The specimens are the lab's
block.c and nested.rs, both
counted by hand first (transcript
tokei-lab/transcripts/t02-traps.txt).
One line, one column
Every line is counted exactly once, as code, comment or blank, and code wins ties. This line of C —
printf("%s\n", text); // say it out loud
— holds code and a comment on the same line, and it is a code line. A counter that credited it to comments would understate the program by one line per trailing comment, which in a chatty codebase is a real number. The rule cuts the other way too: a comment sharing a line with code is invisible in the Comments column.
Blank means empty — unless it is inside something
A line with nothing on it (or only whitespace) is blank. The exception arrives in chapter 7: an empty line inside a multi-line string is part of the string, and counts as code. Blank is a property of the line's state, not just its contents.
Block comments are a state, not a marker
block.c opens with three lines
wrapped in /* ... */:
/* A block comment
that spans three
lines in total. */
All three count as comments, including the two that carry no comment marker at all — the counter is inside a comment from the first line to the third, and knows it. The whole file checks out at 10 lines: 6 code, 3 comments, 1 blank, exactly the hand count.
Strings are not comments
The same file contains the trap that catches counters which merely search for markers:
const char *text = "not a /* comment */ here";
A marker-searching counter sees
/*, opens a comment, and loses
the rest of the line — or the rest
of the file. Tokei's counter tracks
strings as a state of their own:
the /* is inside a string, starts
nothing, and the line is code. The
Rust specimen makes the same point
with //:
let text = "a // inside a string is not a comment";
Also code — and if you write
counters of your own, this is the
feature that separates them from
grep.
Nested comments nest, where the language allows
Rust permits block comments inside block comments. The specimen's knot —
/* The outer comment opens.
/* The inner comment nests inside it. */
The outer comment is still open here. */
— is three comment lines, because
the inner /* raises the depth to
two and the first */ only brings
it back to one. A counter without
depth would close the comment at
the end of the second line and
call the third line code. Tokei
tracks the depth; the file's Rust
row shows those three lines among
its comments. (The languages that
nest — Rust, Swift, OCaml and a
few others — get this because
their language definitions say so;
C's definition does not, and the
same text in C would end at the
first */. That, too, is a rule
speaking.)
Check: take the C file in
tokei-lab/fixtures/basics/, cover
the answer, and sort each of its
ten lines into a column by these
rules. If your sort matches
tokei --files — 6, 3, 1 — you
have this chapter.