Start of a ragel parser that can do the ansi code parsing for me.

This commit is contained in:
Zed A. Shaw 2024-10-31 02:53:38 -04:00
parent 35ef1e786d
commit 74310304bd
3 changed files with 215 additions and 67 deletions

View file

@ -1,26 +1,74 @@
#include <fmt/core.h>
#include <string>
#include <vector>
#include <string_view>
#include <charconv>
#include "dbc.hpp"
using namespace fmt;
%%{
machine foo;
test1 = 0x1B . "[" . [0-9]+ @{ println("NUM1"); } . ";" . [0-9]+ @{ println("NUM2"); } . "m" 0 @{ res = 1; };
test2 = 0x1B "A" [0-9]+ @{ println("NUM1"); } ";" [0-9]+ @{ println("NUM2"); } "m" 0 @{ res = 2; };
main := (test1 | test2);
action tstart {
start = fpc;
}
action number {
size_t value = 0;
auto [ptr, ec] = std::from_chars(start, fpc, value);
dbc::check(ec == std::errc(), "error in number parsing");
println("NUMBER {}", value);
}
start = 0x1B "[";
num = digit+ >tstart %number;
color256 = ";5;" (num ";"?)**;
color24b = ";2;" (num ";"?)**;
basic = num ";" num;
single = num;
main := |*
start => { println("START"); };
single => { println("single"); };
basic => { println("basic"); };
color256 => { println("256 color"); };
color24b => { println("true color"); };
"m" => { println("END"); };
*|;
}%%
%% write data;
int main() {
int cs, res = 0;
char *test = "\x1B[36;46m";
void parse_ansi(std::string_view &codes) {
const char *start = NULL;
int cs = 0;
size_t act = 0;
const char *p = codes.data();
const char *pe = p + codes.size();
const char *ts = p;
const char *te = pe;
const char *eof = pe;
char *p = test;
char *pe = p + strlen(p) + 1;
%% write init;
%% write exec;
}
int main() {
// possibly put alphtype unsigned int?
std::vector<std::string_view> tests = {
"\x1B[;5;78;98m",
"\x1B[;2;36;46;23m",
"\x1B[36;46m",
"\x1B[36m",
};
for(auto test : tests) {
println("--- PARSING");
parse_ansi(test);
}
fmt::println("result = {}", res);
return 0;
}