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

Literal strings

Sometimes you want the characters exactly as written, backslashes and all. Today you parse a literal string, the single-quoted form, which does no escaping at all.

The goal

Parse a single-quoted literal string with no escape processing.

Start here - the target
TO DO
Scenario: A single-quoted literal string
Giventhe pair path written with single quotes as C:\Users\name
WhenParse is called
Thenthe value for path is the exact text C:\Users\name, with every backslash kept
Andno escape is processed, so the literal string holding backslash-t is two characters, a backslash then a t, and a literal string cannot contain a single quote
Background

A literal string is written in single quotes and does the opposite of a basic string: no escaping happens at all. Every character between the single quotes is taken exactly as written, so 'C:\Users\name' is that path with its backslashes intact, and '\t' is the two characters backslash and t, not a tab. This is what makes literal strings the right tool for Windows paths and regular expressions, where backslashes are everywhere and escaping them would be noise.

The rule is simple precisely because there are no special cases inside: read from the opening single quote to the next single quote and keep everything between them. The trade-off is that a literal string cannot contain a single quote - there is no escape to smuggle one in - so the first ' you meet ends the string. That limitation is what the multiline literal form, a couple of lessons ahead, relaxes. Today, one flat rule: copy the characters through untouched.

Make it work
// parseValue: a leading '\'' starts a literal string
// read every character up to the next '\'' verbatim
// NO escape handling - a backslash is just a backslash
// the value is exactly the characters between the single quotes
CheckpointDONE
Single-quoted literal strings keep every character verbatim. Commit and stop here.