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

Exact repetition counts

`{n}` repeats an element an exact number of times. The trick is to expand it into pieces you already know how to match, so the matcher needs no new code.

The goal

Parse `x{n}` into n copies of the element.

Start here - the target
TO DO
Scenario: An exact count repeats an element n times
Giventhe pattern "^a{3}$"
WhenMatch is called against "aaa"
Thenit reports true, and "^a{3}$" against "aa" reports false
AndMatch for "^a{3}$" against "aaaa" reports false
AndMatch for "a{2}" against "xaaax" reports true
Background

{n} is a counted quantifier: a{3} matches exactly three as. Like the shorthand classes, it is best handled by desugaring at parse time - a{3} becomes Concat[a, a, a], three copies of the atom you just parsed. The matcher never learns a new trick; it just sees a longer concatenation. The anchored ^a{3}$ in the spec is there to make the count exact: without anchors, Match searches, so a{3} would find three as inside a longer run.

Parsing the suffix is the fresh part: after an atom, a { opens a count, you read the digits, and a } closes it. Keep the number handy - tomorrow you extend the same suffix parser to {n,m} and {n,}, where a comma introduces a range of allowed counts instead of a single fixed one.

Make it work
// Parse the {n} suffix after an atom, then desugar:
// x{3} -> Concat[x, x, x]
// Emit n copies of the atom you just parsed. No new matcher case.
CheckpointDONE
`{n}` repeats an element exactly n times by expanding into copies. Commit and stop here.