Ansi.ParserSourceStreaming escape sequence parser.
Streaming ANSI escape sequence parser.
Converts byte streams into high-level tokens representing ANSI escape sequences, SGR attributes, and plain text. The parser handles partial input: incomplete escape sequences and UTF-8 multi-byte characters at chunk boundaries are buffered until the next feed call.
Parsing never raises on malformed input. Unrecognized sequences become Unknown controls and invalid UTF-8 is replaced with U+FFFD. Maximum sequence lengths (max_escape_length for CSI, max_osc_length for OSC) prevent unbounded buffering.
type control = | CUU of intCursor Up by n lines.
| CUD of intCursor Down by n lines.
| CUF of intCursor Forward by n columns.
| CUB of intCursor Backward by n columns.
| CNL of intCursor Next Line (down n, column 1).
| CPL of intCursor Previous Line (up n, column 1).
| CHA of intCursor Horizontal Absolute to column n.
| VPA of intVertical Position Absolute to row n.
| CUP of int * intCursor Position to (row, col).
| ED of intErase in Display.
*)| EL of intErase in Line.
*)| IL of intInsert n blank Lines.
| DL of intDelete n Lines.
| DCH of intDelete n Characters.
| ICH of intInsert n blank Characters.
| OSC of int * stringGeneric OSC with code and payload.
*)| Hyperlink of ((string * string) list * string) optionOSC 8 hyperlink. None closes the current hyperlink. Some (params, url) opens one, where params are key=value pairs from the parameter string.
| ResetRIS — reset to initial state (ESC c).
| DECSCSave cursor position (ESC 7).
| DECRCRestore cursor position (ESC 8).
| Unknown of stringUnrecognized sequence, preserved as raw bytes.
*)The type for control sequences other than SGR.
type sgr_attr = [ | `Reset| `Bold| `Dim| `Italic| `Underline| `Double_underline| `Blink| `Inverse| `Hidden| `Strikethrough| `Overline| `Framed| `Encircled| `No_bold| `No_dim| `No_italic| `No_underline| `No_blink| `No_inverse| `No_strikethrough| `No_overline| `No_framed| `No_encircled| `Fg of Color.t| `Bg of Color.t ]The type for SGR (Select Graphic Rendition) attributes. Represents individual style changes from a single SGR sequence. A sequence like ESC [ 1 ; 31 m produces [`Bold; `Fg Red].
The type for parsed tokens.
The type for parsers. Mutable and not thread-safe.
reset p clears internal buffers and returns p to the default state. Discards buffered partial sequences.
feed p buf ~off ~len f processes len bytes from buf starting at off, calling f for each complete token. Incomplete sequences are buffered until the next call.
parse s parses a complete string into tokens. Creates a temporary parser. Not suitable for streaming; use feed.
has_pending p is true iff p has buffered data (incomplete escape sequences or pending UTF-8 bytes).
pending p is a copy of raw input bytes not yet consumed. Escape sequence bodies being accumulated (CSI parameters, OSC payloads) are stored in separate internal buffers and are not included. Use has_pending to avoid allocation when only checking for pending data.