This is based on the state machine described by https://vt100.net/emu/dec_ansi_parser and the public domain C implementation: https://github.com/haberman/vtparse