This seems to be the best way to do this, but I kepts a few other experiments in scratchpad.
This commit is contained in:
parent
058ab23fa2
commit
2d550978b8
5 changed files with 299 additions and 165 deletions
82
scratchpad/ansi_parser_normal.rl
Normal file
82
scratchpad/ansi_parser_normal.rl
Normal file
|
@ -0,0 +1,82 @@
|
|||
#include <fmt/core.h>
|
||||
#include <string_view>
|
||||
#include <charconv>
|
||||
#include "dbc.hpp"
|
||||
#include <SFML/Graphics.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 color24b {
|
||||
println("color24b");
|
||||
}
|
||||
|
||||
action bg { fgcolor = false; }
|
||||
action fg { fgcolor = true; }
|
||||
|
||||
action any {
|
||||
println("ANY: {}:{}", int(fc), fc);
|
||||
}
|
||||
|
||||
action red { color.r = value; }
|
||||
action blue { color.g = value; }
|
||||
action green { color.b = value; }
|
||||
|
||||
action start { value = 0; }
|
||||
|
||||
action end {
|
||||
println("fg? {}, sf:Color: {},{},{},{}", fgcolor, color.r, color.g, color.b, color.a);
|
||||
}
|
||||
|
||||
start = 0x1B "[";
|
||||
fg = "38;" %fg;
|
||||
bg = "48;" %bg;
|
||||
reset = ("39" | "49");
|
||||
num = digit+ >tstart %number;
|
||||
color256 = "5;";
|
||||
color24b = "2;";
|
||||
|
||||
ansi = (
|
||||
start %start
|
||||
(
|
||||
reset |
|
||||
(fg|bg) color24b num %red ";" num %blue ";" num %green %color24b
|
||||
) "m" %end
|
||||
);
|
||||
|
||||
other = (any+ @any -- 0x1B)*;
|
||||
|
||||
main := (other :> ansi)**;
|
||||
}%%
|
||||
|
||||
%% write data;
|
||||
|
||||
bool parse_ansi(std::string_view codes) {
|
||||
const char *start = NULL;
|
||||
int cs = 0;
|
||||
uint8_t value = 0;
|
||||
const char *p = codes.data();
|
||||
const char *pe = p + codes.size();
|
||||
const char *eof = pe;
|
||||
bool fgcolor = false;
|
||||
sf::Color color;
|
||||
|
||||
%% write init;
|
||||
%% write exec;
|
||||
|
||||
print("PROCESSED {} CHARS of {}: {}", p - codes.data(), codes.size(), p);
|
||||
|
||||
return true;
|
||||
}
|
97
scratchpad/ansi_parser_scanner.rl
Normal file
97
scratchpad/ansi_parser_scanner.rl
Normal file
|
@ -0,0 +1,97 @@
|
|||
#include <fmt/core.h>
|
||||
#include <string_view>
|
||||
#include <charconv>
|
||||
#include "dbc.hpp"
|
||||
|
||||
using namespace fmt;
|
||||
|
||||
%%{
|
||||
machine ansi_parser;
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
ESC = 0x1B;
|
||||
start = ESC "[";
|
||||
fg = "38;" %fg;
|
||||
bg = "48;" %bg;
|
||||
color256 = "5;" %color256;
|
||||
color24b = "2;" %color24b;
|
||||
num = (digit+) >tstart %number;
|
||||
|
||||
ansi := |*
|
||||
start => start;
|
||||
(fg|bg) color256 num ";" num => { println("256 doit"); };
|
||||
(fg|bg) color24b num ";" num ";" num => { println("24b doit"); };
|
||||
num ";" num => colorBasic;
|
||||
num => colorSingle;
|
||||
"m" => { fgoto main; };
|
||||
*|;
|
||||
|
||||
main := (any @any -- ESC)* ESC @{ fhold; fgoto ansi; };
|
||||
|
||||
}%%
|
||||
|
||||
%% 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;
|
||||
const char *ts = p;
|
||||
const char *te = p;
|
||||
int act = 0;
|
||||
|
||||
%% 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