Raku Land

Terminal::ANSIParser

zef:japhb

Actions Status

NAME

Terminal::ANSIParser - ANSI/VT stream parser

SYNOPSIS

use Terminal::ANSIParser;

my @parsed;
my &parse-byte := make-ansi-parser(emit-item => { @parsed.push: $_ });
parse-byte($_) for $input-buffer.list;

DESCRIPTION

Terminal::ANSIParser is a general parser for ANSI/VT escape codes, as defined by the related specs ANSI X3.64, ECMA-48, ISO/IEC 6429, and of course the actual physical DEC VT terminals generally considered the standard for escape code behavior.

The basic make-ansi-parser() routine builds and returns a byte-by-byte table-based binary parsing state machine, based on (and extended from) the error-recovering state machine built from observed DEC VT behavior described at https://vt100.net/emu/dec_ansi_parser.

Each time the parser determines that it has parsed enough bytes, it emits a token representing the parsed data, which can take one of the following forms:

A few Sequence subclasses exist for separate cases:

Likewise, String has its own subclasses:

End of Input

End of input can be signaled by parsing an undefined "byte"; any partial sequence in progress will be flushed as Incomplete, and the undefined marker will be emitted as well, so that downstream consumers are also notified that input is complete.

AUTHOR

Geoffrey Broadwell gjb@sonic.net

COPYRIGHT AND LICENSE

Copyright 2021 Geoffrey Broadwell

This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.