build-a-sql-database / lesson-11.md
Lesson 11 · Tokenizing SQL

String literals

Text values in SQL are wrapped in single quotes. Today you teach the tokenizer to read a quoted string into a token holding just the text between the quotes.

The goal

Recognize a single-quoted run of characters as one string token, holding the text without the quotes.

Start here - the target
TO DO
Scenario: Tokenizing single-quoted string literals
Giventhe input 'alice'
Whenit is tokenized
Thenthe single token is String("alice")
Andthe input '' produces String("") - an empty string
Andan unterminated quote reports an error
Background

In SQL, text literals are wrapped in single quotes: 'alice'. The tokenizer sees the opening quote, then consumes every character up to the closing quote, and the token’s text is what lies between them - the quotes are syntax, not data. The empty string '' is a real, common value and must tokenize to a string token whose text is empty.

One boundary matters: a quote that is never closed - 'alice - is a syntax error, not a token. Reaching the end of the input while still inside a string should report a clear “unterminated string” rather than silently swallowing the rest of the query. Doubled-quote escaping ('it''s') is a refinement you can add later; a straight run to the closing quote is enough today.

Make it work
// saw a single quote: consume chars until the closing quote
// the token text is everything strictly between the quotes
// hitting end-of-input before the closing quote is an error
CheckpointDONE
The tokenizer reads single-quoted string literals, including the empty string. Commit and stop here.