This seems to be the best way to do this, but I kepts a few other experiments in scratchpad.

main
Zed A. Shaw 2 months ago
parent 058ab23fa2
commit 2d550978b8
  1. 5
      Makefile
  2. 213
      ansi_parser.cpp
  3. 65
      ansi_parser.rl
  4. 82
      scratchpad/ansi_parser_normal.rl
  5. 97
      scratchpad/ansi_parser_scanner.rl

@ -3,6 +3,11 @@ all: build test
reset:
powershell -executionpolicy bypass .\scripts\reset_build.ps1
rageldev:
make ragel
make test > test.txt
vim test.txt
ragel:
ragel -o .\scratchpad\testragel.cpp .\scratchpad\testragel.rl
ragel -o .\ansi_parser.cpp .\ansi_parser.rl

@ -4,125 +4,106 @@
#include <string_view>
#include <charconv>
#include "dbc.hpp"
#include <SFML/Graphics.hpp>
using namespace fmt;
#line 74 ".\\ansi_parser.rl"
#line 62 ".\\ansi_parser.rl"
#line 11 ".\\ansi_parser.cpp"
#line 12 ".\\ansi_parser.cpp"
static const char _foo_actions[] = {
0, 1, 0, 1, 1, 1, 2, 1,
3, 1, 4, 1, 5, 1, 6, 1,
7, 1, 8, 1, 9, 2, 1, 10,
2, 1, 11
0, 1, 0, 1, 3, 1, 4, 1,
5, 1, 9, 1, 10, 2, 1, 6,
2, 1, 7, 2, 10, 5, 3, 1,
8, 2
};
static const char _foo_key_offsets[] = {
0, 0, 1, 2, 3, 4, 6, 7,
9, 12, 14, 17, 19, 20, 22, 25,
27, 28, 29, 31, 31, 35, 37
0, 0, 1, 2, 4, 6, 7, 8,
9, 11, 14, 16, 19, 21, 24, 25,
27, 28, 29, 30
};
static const char _foo_trans_keys[] = {
27, 91, 56, 59, 50, 53, 59, 48,
57, 59, 48, 57, 48, 57, 59, 48,
57, 48, 57, 59, 48, 57, 59, 48,
57, 48, 57, 56, 59, 50, 53, 27,
51, 52, 109, 48, 57, 48, 57, 0
27, 91, 51, 52, 56, 57, 59, 50,
59, 48, 57, 59, 48, 57, 48, 57,
59, 48, 57, 48, 57, 109, 48, 57,
109, 56, 57, 59, 50, 27, 27, 0
};
static const char _foo_single_lengths[] = {
0, 1, 1, 1, 1, 2, 1, 0,
1, 0, 1, 0, 1, 0, 1, 0,
1, 1, 2, 0, 4, 0, 0
0, 1, 1, 2, 2, 1, 1, 1,
0, 1, 0, 1, 0, 1, 1, 2,
1, 1, 1, 1
};
static const char _foo_range_lengths[] = {
0, 0, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 0, 1, 1, 1,
0, 0, 0, 0, 0, 1, 1
0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 0, 0,
0, 0, 0, 0
};
static const char _foo_index_offsets[] = {
0, 0, 2, 4, 6, 8, 11, 13,
15, 18, 20, 23, 25, 27, 29, 32,
34, 36, 38, 41, 42, 47, 49
0, 0, 2, 4, 7, 10, 12, 14,
16, 18, 21, 23, 26, 28, 31, 33,
36, 38, 40, 42
};
static const char _foo_trans_targs[] = {
19, 1, 20, 0, 4, 0, 5, 0,
6, 12, 0, 7, 0, 8, 0, 9,
8, 0, 10, 0, 11, 10, 0, 21,
0, 13, 0, 14, 0, 15, 14, 0,
22, 0, 17, 0, 18, 0, 6, 12,
0, 0, 2, 3, 16, 20, 0, 21,
20, 22, 20, 20, 20, 0
2, 1, 3, 0, 4, 15, 0, 5,
14, 0, 6, 0, 7, 0, 8, 0,
9, 0, 10, 9, 0, 11, 0, 12,
11, 0, 13, 0, 19, 13, 0, 19,
0, 16, 14, 0, 17, 0, 7, 0,
2, 1, 2, 1, 0
};
static const char _foo_trans_actions[] = {
11, 9, 17, 0, 0, 0, 0, 0,
7, 7, 0, 0, 0, 1, 0, 3,
0, 0, 1, 0, 3, 0, 0, 1,
0, 0, 0, 1, 0, 3, 0, 0,
1, 0, 0, 0, 0, 0, 5, 5,
0, 0, 0, 0, 0, 19, 0, 0,
24, 0, 21, 24, 21, 0
};
static const char _foo_to_state_actions[] = {
0, 13, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 13, 0, 0
};
static const char _foo_from_state_actions[] = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 15, 0, 0
0, 7, 0, 0, 9, 9, 0, 0,
0, 0, 0, 0, 5, 0, 0, 0,
1, 0, 13, 0, 0, 1, 0, 16,
0, 0, 1, 0, 22, 0, 0, 0,
0, 0, 0, 0, 0, 0, 3, 0,
0, 7, 11, 19, 0
};
static const char _foo_eof_trans[] = {
static const char _foo_eof_actions[] = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 52, 53
0, 0, 0, 11
};
static const int foo_start = 1;
static const int foo_first_final = 19;
static const int foo_start = 18;
static const int foo_first_final = 18;
static const int foo_error = 0;
static const int foo_en_ansi = 20;
static const int foo_en_main = 1;
static const int foo_en_main = 18;
#line 77 ".\\ansi_parser.rl"
#line 65 ".\\ansi_parser.rl"
bool parse_ansi(std::string_view codes) {
const char *start = NULL;
int cs = 0;
size_t value = 0;
uint8_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;
bool fgcolor = false;
sf::Color color;
#line 107 ".\\ansi_parser.cpp"
#line 91 ".\\ansi_parser.cpp"
{
cs = foo_start;
ts = 0;
te = 0;
act = 0;
}
#line 90 ".\\ansi_parser.rl"
#line 77 ".\\ansi_parser.rl"
#line 113 ".\\ansi_parser.cpp"
#line 94 ".\\ansi_parser.cpp"
{
int _klen;
unsigned int _trans;
@ -135,18 +116,6 @@ bool parse_ansi(std::string_view codes) {
if ( cs == 0 )
goto _out;
_resume:
_acts = _foo_actions + _foo_from_state_actions[cs];
_nacts = (unsigned int) *_acts++;
while ( _nacts-- > 0 ) {
switch ( *_acts++ ) {
case 7:
#line 1 "NONE"
{ts = p;}
break;
#line 132 ".\\ansi_parser.cpp"
}
}
_keys = _foo_trans_keys + _foo_key_offsets[cs];
_trans = _foo_index_offsets[cs];
@ -196,7 +165,6 @@ _resume:
}
_match:
_eof_trans:
cs = _foo_trans_targs[_trans];
if ( _foo_trans_actions[_trans] == 0 )
@ -209,13 +177,13 @@ _eof_trans:
switch ( *_acts++ )
{
case 0:
#line 11 ".\\ansi_parser.rl"
#line 12 ".\\ansi_parser.rl"
{
start = p;
}
break;
case 1:
#line 15 ".\\ansi_parser.rl"
#line 16 ".\\ansi_parser.rl"
{
auto [ptr, ec] = std::from_chars(start, p, value);
dbc::check(ec == std::errc(), "error in number parsing");
@ -223,66 +191,52 @@ _eof_trans:
}
break;
case 2:
#line 37 ".\\ansi_parser.rl"
#line 22 ".\\ansi_parser.rl"
{
println("BG");
println("color24b");
}
break;
case 3:
#line 41 ".\\ansi_parser.rl"
{
println("FG");
}
#line 26 ".\\ansi_parser.rl"
{ fgcolor = false; }
break;
case 4:
#line 45 ".\\ansi_parser.rl"
#line 27 ".\\ansi_parser.rl"
{ fgcolor = true; }
break;
case 5:
#line 29 ".\\ansi_parser.rl"
{
println("ANY: {}:{}", int((*p)), (*p));
}
break;
case 5:
#line 72 ".\\ansi_parser.rl"
{ p--; {cs = 20;goto _again;} }
case 6:
#line 33 ".\\ansi_parser.rl"
{ color.r = value; }
break;
case 7:
#line 34 ".\\ansi_parser.rl"
{ color.g = value; }
break;
case 8:
#line 49 ".\\ansi_parser.rl"
{te = p+1;{
println("START");
}}
#line 35 ".\\ansi_parser.rl"
{ color.b = value; }
break;
case 9:
#line 69 ".\\ansi_parser.rl"
{te = p+1;{ {cs = 1;goto _again;} }}
#line 37 ".\\ansi_parser.rl"
{ value = 0; }
break;
case 10:
#line 21 ".\\ansi_parser.rl"
{te = p;p--;{
println("COLOR256");
}}
break;
case 11:
#line 25 ".\\ansi_parser.rl"
{te = p;p--;{
println("COLOR24B");
}}
#line 39 ".\\ansi_parser.rl"
{
println("fg? {}, sf:Color: {},{},{},{}", fgcolor, color.r, color.g, color.b, color.a);
}
break;
#line 244 ".\\ansi_parser.cpp"
#line 211 ".\\ansi_parser.cpp"
}
}
_again:
_acts = _foo_actions + _foo_to_state_actions[cs];
_nacts = (unsigned int) *_acts++;
while ( _nacts-- > 0 ) {
switch ( *_acts++ ) {
case 6:
#line 1 "NONE"
{ts = 0;}
break;
#line 255 ".\\ansi_parser.cpp"
}
}
if ( cs == 0 )
goto _out;
if ( ++p != pe )
@ -290,18 +244,27 @@ _again:
_test_eof: {}
if ( p == eof )
{
if ( _foo_eof_trans[cs] > 0 ) {
_trans = _foo_eof_trans[cs] - 1;
goto _eof_trans;
const char *__acts = _foo_actions + _foo_eof_actions[cs];
unsigned int __nacts = (unsigned int) *__acts++;
while ( __nacts-- > 0 ) {
switch ( *__acts++ ) {
case 10:
#line 39 ".\\ansi_parser.rl"
{
println("fg? {}, sf:Color: {},{},{},{}", fgcolor, color.r, color.g, color.b, color.a);
}
break;
#line 231 ".\\ansi_parser.cpp"
}
}
}
_out: {}
}
#line 91 ".\\ansi_parser.rl"
#line 78 ".\\ansi_parser.rl"
print("PROCESSED {} CHARS of {}", p - codes.data(), codes.size());
print("PROCESSED {} CHARS of {}: {}", p - codes.data(), codes.size(), p);
return true;
}

@ -2,6 +2,7 @@
#include <string_view>
#include <charconv>
#include "dbc.hpp"
#include <SFML/Graphics.hpp>
using namespace fmt;
@ -18,59 +19,46 @@ using namespace fmt;
println("NUM: {}", value);
}
action color256 {
println("COLOR256");
}
action color24b {
println("COLOR24B");
}
action colorSingle {
println("SINGLE");
println("color24b");
}
action colorBasic {
println("BASIC");
}
action bg {
println("BG");
}
action fg {
println("FG");
}
action bg { fgcolor = false; }
action fg { fgcolor = true; }
action any {
println("ANY: {}:{}", int(fc), fc);
}
action start {
println("START");
}
action red { color.r = value; }
action blue { color.g = value; }
action green { color.b = value; }
action start { value = 0; }
action end {
println("END");
println("fg? {}, sf:Color: {},{},{},{}", fgcolor, color.r, color.g, color.b, color.a);
}
ESC = 0x1B;
start = ESC "[";
start = 0x1B "[";
fg = "38;" %fg;
bg = "48;" %bg;
reset = ("39" | "49");
num = digit+ >tstart %number;
color256 = "5;";
color24b = "2;";
num = (digit+) >tstart %number;
ansi := |*
start => start;
(fg|bg) color256 num ";" num => color256;
(fg|bg) color24b num ";" num ";" num => color24b;
"m" => { fgoto main; };
*|;
ansi = (
start %start
(
reset |
(fg|bg) color24b num %red ";" num %blue ";" num %green %color24b
) "m" %end
);
main := (any @any -- ESC)* ESC @{ fhold; fgoto ansi; };
other = (any+ @any -- 0x1B)*;
main := (other :> ansi)**;
}%%
%% write data;
@ -78,18 +66,17 @@ using namespace fmt;
bool parse_ansi(std::string_view codes) {
const char *start = NULL;
int cs = 0;
size_t value = 0;
uint8_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;
bool fgcolor = false;
sf::Color color;
%% write init;
%% write exec;
print("PROCESSED {} CHARS of {}", p - codes.data(), codes.size());
print("PROCESSED {} CHARS of {}: {}", p - codes.data(), codes.size(), p);
return true;
}

@ -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;
}

@ -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…
Cancel
Save