Barely working ansi color codes parser but I _really_ don't like this one. Too much code to just get it to process correctly which means it'll be brittle as hell later.
This commit is contained in:
parent
a44a9a04f9
commit
f32b39afe2
9 changed files with 621 additions and 152 deletions
99
ansi_parser.rl
Normal file
99
ansi_parser.rl
Normal file
|
@ -0,0 +1,99 @@
|
|||
#include <fmt/core.h>
|
||||
#include <string_view>
|
||||
#include <charconv>
|
||||
#include "dbc.hpp"
|
||||
|
||||
using namespace fmt;
|
||||
|
||||
%%{
|
||||
machine foo;
|
||||
|
||||
action tstart {
|
||||
start = fpc;
|
||||
}
|
||||
|
||||
action number {
|
||||
auto [ptr, ec] = std::from_chars(start, fpc, value);
|
||||
dbc::check(ec == std::errc(), "error in number parsing");
|
||||
println("NUM: {}", value);
|
||||
}
|
||||
|
||||
action color256 {
|
||||
println("COLOR256");
|
||||
}
|
||||
|
||||
action color24b {
|
||||
println("COLOR24B");
|
||||
}
|
||||
|
||||
action colorSingle {
|
||||
println("SINGLE");
|
||||
}
|
||||
|
||||
action colorBasic {
|
||||
println("BASIC");
|
||||
}
|
||||
|
||||
action bg {
|
||||
println("BG");
|
||||
}
|
||||
|
||||
action fg {
|
||||
println("FG");
|
||||
}
|
||||
|
||||
action any {
|
||||
println("ANY: {}:{}", int(fc), fc);
|
||||
}
|
||||
|
||||
action start {
|
||||
println("START");
|
||||
}
|
||||
|
||||
action end {
|
||||
println("END");
|
||||
}
|
||||
|
||||
start = 0x1B "[";
|
||||
fg = "38;" %fg;
|
||||
bg = "48;" %bg;
|
||||
num = digit+ >tstart %number;
|
||||
color256 = "5;" num ";" num;
|
||||
color24b = "2;" num ";" num ";" num;
|
||||
basic = num ";" num;
|
||||
single = num;
|
||||
|
||||
colorCode = (
|
||||
start %start
|
||||
(
|
||||
(fg|bg) color256 %color256 |
|
||||
(fg|bg) color24b %color24b |
|
||||
single %colorSingle |
|
||||
basic %colorBasic
|
||||
)**
|
||||
"m" %end
|
||||
);
|
||||
|
||||
other = (any+ @any -- 0x1B)*;
|
||||
|
||||
main := (other :>> colorCode)**;
|
||||
}%%
|
||||
|
||||
%% write data;
|
||||
|
||||
bool parse_ansi(std::string_view codes) {
|
||||
const char *start = NULL;
|
||||
int cs = 0;
|
||||
size_t value = 0;
|
||||
const char *p = codes.data();
|
||||
const char *pe = p + codes.size();
|
||||
const char *eof = pe;
|
||||
|
||||
|
||||
%% write init;
|
||||
%% write exec;
|
||||
|
||||
print("PROCESSED {} CHARS of {}", p - codes.data(), codes.size());
|
||||
|
||||
return true;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue