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

main
Zed A. Shaw 3 weeks 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: reset:
powershell -executionpolicy bypass .\scripts\reset_build.ps1 powershell -executionpolicy bypass .\scripts\reset_build.ps1
rageldev:
make ragel
make test > test.txt
vim test.txt
ragel: ragel:
ragel -o .\scratchpad\testragel.cpp .\scratchpad\testragel.rl ragel -o .\scratchpad\testragel.cpp .\scratchpad\testragel.rl
ragel -o .\ansi_parser.cpp .\ansi_parser.rl ragel -o .\ansi_parser.cpp .\ansi_parser.rl

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

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