Make Your Own Regex Engine!
summary
An actual regex engine gets built: a hand‑written parser (the longest part), a compiler (~60 lines) that emits a backtracking VM program, and a tiny VM implemented in about 21 lines that implements push/jump/char/match (full Ruby’s engine is ~2,000 lines). It shows capture via a caps array, char‑loop failure like aa?* causing infinite loops and the mitigation using push nil CHECK, and lookahead implemented by savepos/resetpos*. Ruby 3.3 later adds memoization optimizations.
AI-written summary · about
chapters (10)
- 0:00Introduction
- 2:14Why build a regex engine
- 4:02Transpiling regex to Ruby
- 10:28Backtracking VM basics
- 14:14Compiling regex to VM
- 17:38Implementing capture groups
- 19:51Handling char loops
- 24:20Lookahead in regex
- 27:33Next steps and optimizations
- 29:10Conclusion and Q&A
related talks
30m(Re)make Regexp in Ruby: Democratizing internals for the JIT
32mTime flies like an arrow; Fruit flies like a banana: Parsers for Great Good
46mGrammar a BNF like Ruby DSL Parsing
33mRuby the Hard Way: Writing Bytecode to Optimize Plain Ruby
37mAll About Regular Expressions
26mSlaying the Dragon
31mTime flies like an arrow, fruit flies like a banana
24mRuby Internals
