build-a-toml-parser / lesson-06.md
Lesson 06 · String values

Basic strings

Strings are the most common TOML value. Today you parse a basic string, the double-quoted form, reading the characters between the quotes, and you fix comment handling so a # inside a string is kept, not cut.

The goal

Parse a double-quoted basic string into a string value.

Start here - the target
TO DO
Scenario: A double-quoted string
Giventhe pair name = "TOML"
WhenParse is called
Thenthe value for name is the string TOML
Andan empty string "" is the empty string, and a # inside a string is kept, so note = "a # b" is the string a # b
Background

A basic string is written in double quotes: "TOML". Parsing it means reading the characters between the opening and closing " and storing them as a KindString value. The empty string "" is perfectly valid and holds no characters. This is where parseValue becomes a real dispatch: it looks at the first character of the value and branches - a " starts a string, a digit or sign starts a number.

Strings also force a fix to comment handling. Earlier you cut everything after the first # on a line, which was safe when values were only integers. But "a # b" is a string whose content includes a #, and cutting there would corrupt it. The correct order is to read the value first - consuming the whole quoted string, # and all - and only then treat the remainder of the line (optional whitespace and an optional # comment) as trailing. From now on, comments are recognized around values, not by blindly slicing the raw line.

Make it work
// parseValue dispatches on the first character of the trimmed value:
// '"' -> read a basic string: consume chars until the closing '"'
// digit/sign -> the integer path from before
// read the value FIRST, then whatever follows (spaces + optional
// #comment) is trailing - so a '#' inside the quotes is not a comment
CheckpointDONE
Double-quoted basic strings parse, and a # inside them is safe. Commit and stop here.