build-a-toml-parser / lesson-03.md
Lesson 03 · Keys, values, and a flat table

Comments and blank lines

Real config files are full of comments and spacing. Today you teach the reader to ignore a # comment, whether it is a whole line or trails a value, and to skip blank lines, so several pairs read cleanly into the table in order.

The goal

Ignore full-line and end-of-line # comments and parse several pairs in order.

Start here - the target
TO DO
Scenario: Comments and multiple pairs
Givena document with a full-line comment, a blank line, and two pairs a = 1 and b = 2 where the a line ends with a trailing comment
WhenParse is called
Thenthe table has exactly two entries, a with integer 1 then b with integer 2, in that order
Anda comment marker # inside nothing but code strips the rest of the line, so a = 1 # note yields the integer 1
Background

A # begins a comment that runs to the end of the line, and a blank line separates sections for readability. Neither carries data, so the reader drops both. A comment can stand on its own line or trail a value (port = 80 # the http port), and in every case everything from the # onward is discarded before the line is interpreted.

For now the whole rest of the line after a # can be cut away safely, because the only values you can parse are integers, which never contain a #. That changes once strings arrive - a # inside "a # b" is data, not a comment - and a later lesson sharpens the rule to respect that. Today the job is simpler: strip the comment, skip the line if nothing is left, and otherwise read the pair. Several pairs in a row land in the table in the order they appear, which the ordered entry list preserves.

Make it work
// for each raw line:
// strip a trailing comment: cut at the first '#' (safe now;
// strings that contain # arrive in a later lesson)
// trim surrounding whitespace
// if the line is now empty, skip it
// otherwise split on '=' as before
CheckpointDONE
Comments and blank lines are ignored and pairs keep their order. Commit and stop here.