build-a-regex-engine / lesson-14.md
Lesson 14 · Groups, classes & repetition

Predefined class shorthands

The escapes `\d`, `\w`, `\s` stand for the classes you reach for constantly - digits, word characters, whitespace. Each one desugars into a character class you already match.

The goal

Parse `\d`, `\w`, and `\s` into the character classes they abbreviate.

Start here - the target
TO DO
Scenario: Shorthand escapes expand into character classes
Giventhe pattern '\d'
WhenMatch is called against "a5b"
Thenit reports true, and '\d' against "abc" reports false
AndMatch for '\w' against "-x-" reports true
AndMatch for '\s' against "a b" reports true, and '\s' against "abc" reports false
Background

These three shorthands are everywhere in real patterns, and each is just a named character class: \d is [0-9], \w is [A-Za-z0-9_], and \s is the whitespace set (space, tab, newline, carriage return). Rather than invent new node kinds, you desugar them - the parser sees \d and emits the very Class node it would build for [0-9]. From the matcher’s point of view there is nothing new to do.

This is your first real taste of desugaring, a technique the rest of the project leans on hard: express a new surface feature entirely in terms of machinery that already exists. It keeps the matcher small even as the syntax grows. Tomorrow you handle the backslash’s other job - turning a metacharacter like . back into a plain literal - and it slots into the same “look at the byte after the backslash” branch.

Make it work
// In the parser, a backslash escape becomes a Class:
// \d -> [0-9]
// \w -> [A-Za-z0-9_]
// \s -> space, tab, newline, carriage return
// Build the same Class node you use for [ ... ].
CheckpointDONE
`\d`, `\w`, `\s` expand into their character classes. Commit and stop here.