Started a new parser using ragel. It can parse the slides but without json. Need to re-read the ragel manual to get it fully working.
parent
94422a4155
commit
664de68d9d
@ -0,0 +1,62 @@ |
|||||||
|
#include "parser.hpp" |
||||||
|
#include "dbc.hpp" |
||||||
|
#include <filesystem> |
||||||
|
#include <fstream> |
||||||
|
#include <iostream> |
||||||
|
#include <nlohmann/json.hpp> |
||||||
|
#include <memory> |
||||||
|
#include <string_view> |
||||||
|
#include <ranges> |
||||||
|
|
||||||
|
using std::shared_ptr, std::string, nlohmann::json, std::string_view; |
||||||
|
namespace fs = std::filesystem; |
||||||
|
|
||||||
|
shared_ptr<SlideDeck> parse_slides(const string& md_file) { |
||||||
|
shared_ptr<SlideDeck> deck = std::make_shared<SlideDeck>(); |
||||||
|
|
||||||
|
dbc::check(fs::exists(md_file), "md file missing"); |
||||||
|
|
||||||
|
auto size = fs::file_size(md_file); |
||||||
|
string line(size, '\0'); |
||||||
|
bool started = false; |
||||||
|
json config; |
||||||
|
|
||||||
|
if(std::ifstream in_file{md_file, std::ios::binary}) { |
||||||
|
while(std::getline(in_file, line)) { |
||||||
|
if(line == "{") { |
||||||
|
string json_data; |
||||||
|
|
||||||
|
do { |
||||||
|
json_data += line; |
||||||
|
} while (std::getline(in_file, line) && line != "}"); |
||||||
|
|
||||||
|
json_data += "}"; |
||||||
|
|
||||||
|
config = json::parse(json_data); |
||||||
|
std::cout << "JSON: " << config << '\n'; |
||||||
|
} else if(line == "===") { |
||||||
|
deck->config = config; |
||||||
|
fmt::println("START"); |
||||||
|
started = true; |
||||||
|
} else if(line == "---") { |
||||||
|
fmt::println("START SLIDE"); |
||||||
|
} else { |
||||||
|
if(started) { |
||||||
|
string title = line; |
||||||
|
string content; |
||||||
|
|
||||||
|
while(std::getline(in_file, line) && line != "---") { |
||||||
|
content += line + "\n"; |
||||||
|
} |
||||||
|
|
||||||
|
deck->slides.emplace_back(title, content, config); |
||||||
|
config = json::parse("{}"); |
||||||
|
} else { |
||||||
|
fmt::println("JUNK: {}", line); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return deck; |
||||||
|
} |
@ -0,0 +1,222 @@ |
|||||||
|
|
||||||
|
#line 1 "md_parser.rl" |
||||||
|
#include <fmt/core.h> |
||||||
|
#include <iostream> |
||||||
|
|
||||||
|
|
||||||
|
#line 38 "md_parser.rl" |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#line 7 "md_parser.cpp" |
||||||
|
static const char _Parser_actions[] = { |
||||||
|
0, 1, 0, 1, 3, 1, 4, 2,
|
||||||
|
0, 1, 2, 0, 2, 2, 1, 0,
|
||||||
|
2, 2, 0, 2, 3, 5 |
||||||
|
}; |
||||||
|
|
||||||
|
static const char _Parser_key_offsets[] = { |
||||||
|
0, 0, 1, 2, 3, 4, 10, 11,
|
||||||
|
18, 22, 29, 31, 33, 34 |
||||||
|
}; |
||||||
|
|
||||||
|
static const char _Parser_trans_keys[] = { |
||||||
|
61, 61, 61, 10, 10, 32, 35, 42,
|
||||||
|
9, 13, 10, 10, 32, 35, 42, 45,
|
||||||
|
9, 13, 10, 32, 9, 13, 10, 32,
|
||||||
|
35, 42, 45, 9, 13, 10, 45, 10,
|
||||||
|
45, 10, 10, 32, 35, 42, 45, 9,
|
||||||
|
13, 0 |
||||||
|
}; |
||||||
|
|
||||||
|
static const char _Parser_single_lengths[] = { |
||||||
|
0, 1, 1, 1, 1, 4, 1, 5,
|
||||||
|
2, 5, 2, 2, 1, 5 |
||||||
|
}; |
||||||
|
|
||||||
|
static const char _Parser_range_lengths[] = { |
||||||
|
0, 0, 0, 0, 0, 1, 0, 1,
|
||||||
|
1, 1, 0, 0, 0, 1 |
||||||
|
}; |
||||||
|
|
||||||
|
static const char _Parser_index_offsets[] = { |
||||||
|
0, 0, 2, 4, 6, 8, 14, 16,
|
||||||
|
23, 27, 34, 37, 40, 42 |
||||||
|
}; |
||||||
|
|
||||||
|
static const char _Parser_indicies[] = { |
||||||
|
0, 1, 2, 1, 3, 1, 4, 1,
|
||||||
|
7, 6, 8, 9, 6, 5, 11, 10,
|
||||||
|
7, 6, 8, 9, 12, 6, 5, 13,
|
||||||
|
6, 6, 5, 7, 6, 14, 15, 12,
|
||||||
|
6, 5, 11, 16, 10, 11, 17, 10,
|
||||||
|
18, 10, 7, 6, 8, 9, 12, 6,
|
||||||
|
5, 0 |
||||||
|
}; |
||||||
|
|
||||||
|
static const char _Parser_trans_targs[] = { |
||||||
|
2, 0, 3, 4, 5, 6, 8, 9,
|
||||||
|
8, 8, 6, 7, 10, 9, 8, 8,
|
||||||
|
11, 12, 13 |
||||||
|
}; |
||||||
|
|
||||||
|
static const char _Parser_trans_actions[] = { |
||||||
|
0, 0, 0, 0, 5, 1, 1, 0,
|
||||||
|
13, 16, 0, 3, 1, 3, 7, 10,
|
||||||
|
0, 0, 19 |
||||||
|
}; |
||||||
|
|
||||||
|
static const int Parser_start = 1; |
||||||
|
static const int Parser_first_final = 13; |
||||||
|
static const int Parser_error = 0; |
||||||
|
|
||||||
|
static const int Parser_en_main = 1; |
||||||
|
|
||||||
|
|
||||||
|
#line 41 "md_parser.rl" |
||||||
|
|
||||||
|
|
||||||
|
bool Parser::parse(const std::string& input) { |
||||||
|
int cs = 0; |
||||||
|
const char *start = nullptr; |
||||||
|
const char *begin = input.data(); |
||||||
|
const char *p = input.data(); |
||||||
|
const char *pe = p + input.size(); |
||||||
|
const char *eof = p + input.size(); (void)eof; |
||||||
|
std::string tk; |
||||||
|
|
||||||
|
|
||||||
|
#line 81 "md_parser.cpp" |
||||||
|
{ |
||||||
|
cs = Parser_start; |
||||||
|
} |
||||||
|
|
||||||
|
#line 53 "md_parser.rl" |
||||||
|
|
||||||
|
#line 84 "md_parser.cpp" |
||||||
|
{ |
||||||
|
int _klen; |
||||||
|
unsigned int _trans; |
||||||
|
const char *_acts; |
||||||
|
unsigned int _nacts; |
||||||
|
const char *_keys; |
||||||
|
|
||||||
|
if ( p == pe ) |
||||||
|
goto _test_eof; |
||||||
|
if ( cs == 0 ) |
||||||
|
goto _out; |
||||||
|
_resume: |
||||||
|
_keys = _Parser_trans_keys + _Parser_key_offsets[cs]; |
||||||
|
_trans = _Parser_index_offsets[cs]; |
||||||
|
|
||||||
|
_klen = _Parser_single_lengths[cs]; |
||||||
|
if ( _klen > 0 ) { |
||||||
|
const char *_lower = _keys; |
||||||
|
const char *_mid; |
||||||
|
const char *_upper = _keys + _klen - 1; |
||||||
|
while (1) { |
||||||
|
if ( _upper < _lower ) |
||||||
|
break; |
||||||
|
|
||||||
|
_mid = _lower + ((_upper-_lower) >> 1); |
||||||
|
if ( (*p) < *_mid ) |
||||||
|
_upper = _mid - 1; |
||||||
|
else if ( (*p) > *_mid ) |
||||||
|
_lower = _mid + 1; |
||||||
|
else { |
||||||
|
_trans += (unsigned int)(_mid - _keys); |
||||||
|
goto _match; |
||||||
|
} |
||||||
|
} |
||||||
|
_keys += _klen; |
||||||
|
_trans += _klen; |
||||||
|
} |
||||||
|
|
||||||
|
_klen = _Parser_range_lengths[cs]; |
||||||
|
if ( _klen > 0 ) { |
||||||
|
const char *_lower = _keys; |
||||||
|
const char *_mid; |
||||||
|
const char *_upper = _keys + (_klen<<1) - 2; |
||||||
|
while (1) { |
||||||
|
if ( _upper < _lower ) |
||||||
|
break; |
||||||
|
|
||||||
|
_mid = _lower + (((_upper-_lower) >> 1) & ~1); |
||||||
|
if ( (*p) < _mid[0] ) |
||||||
|
_upper = _mid - 2; |
||||||
|
else if ( (*p) > _mid[1] ) |
||||||
|
_lower = _mid + 2; |
||||||
|
else { |
||||||
|
_trans += (unsigned int)((_mid - _keys)>>1); |
||||||
|
goto _match; |
||||||
|
} |
||||||
|
} |
||||||
|
_trans += _klen; |
||||||
|
} |
||||||
|
|
||||||
|
_match: |
||||||
|
_trans = _Parser_indicies[_trans]; |
||||||
|
cs = _Parser_trans_targs[_trans]; |
||||||
|
|
||||||
|
if ( _Parser_trans_actions[_trans] == 0 ) |
||||||
|
goto _again; |
||||||
|
|
||||||
|
_acts = _Parser_actions + _Parser_trans_actions[_trans]; |
||||||
|
_nacts = (unsigned int) *_acts++; |
||||||
|
while ( _nacts-- > 0 ) |
||||||
|
{ |
||||||
|
switch ( *_acts++ ) |
||||||
|
{ |
||||||
|
case 0: |
||||||
|
#line 12 "md_parser.rl" |
||||||
|
{ start = p; } |
||||||
|
break; |
||||||
|
case 1: |
||||||
|
#line 13 "md_parser.rl" |
||||||
|
{ fmt::println("TITLE"); } |
||||||
|
break; |
||||||
|
case 2: |
||||||
|
#line 14 "md_parser.rl" |
||||||
|
{ fmt::println("ENUM"); } |
||||||
|
break; |
||||||
|
case 3: |
||||||
|
#line 15 "md_parser.rl" |
||||||
|
{ |
||||||
|
tk = input.substr(start - begin, p - start); |
||||||
|
std::cout << "CONTENT:" << std::quoted(tk) << '\n'; |
||||||
|
} |
||||||
|
break; |
||||||
|
case 4: |
||||||
|
#line 19 "md_parser.rl" |
||||||
|
{ fmt::println("----- START"); } |
||||||
|
break; |
||||||
|
case 5: |
||||||
|
#line 20 "md_parser.rl" |
||||||
|
{ fmt::println("END"); } |
||||||
|
break; |
||||||
|
#line 178 "md_parser.cpp" |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
_again: |
||||||
|
if ( cs == 0 ) |
||||||
|
goto _out; |
||||||
|
if ( ++p != pe ) |
||||||
|
goto _resume; |
||||||
|
_test_eof: {} |
||||||
|
_out: {} |
||||||
|
} |
||||||
|
|
||||||
|
#line 54 "md_parser.rl" |
||||||
|
|
||||||
|
bool good = pe - p == 0; |
||||||
|
|
||||||
|
if(good) { |
||||||
|
finalize(); |
||||||
|
} else { |
||||||
|
error = true; |
||||||
|
std::cout << "!!!!!!!!!!!!!!!!!!!!!! error at:"; |
||||||
|
std::cout << p; |
||||||
|
} |
||||||
|
return good; |
||||||
|
} |
@ -0,0 +1,65 @@ |
|||||||
|
#include <fmt/core.h> |
||||||
|
#include <iostream> |
||||||
|
|
||||||
|
%%{ |
||||||
|
machine Parser; |
||||||
|
alphtype char; |
||||||
|
|
||||||
|
action token { |
||||||
|
tk = input.substr(start - begin, fpc - start); |
||||||
|
std::cout << "TOKEN: " << std::quoted(tk) << '\n'; |
||||||
|
} |
||||||
|
action mark { start = fpc; } |
||||||
|
action title { fmt::println("TITLE"); } |
||||||
|
action enum { fmt::println("ENUM"); } |
||||||
|
action content { |
||||||
|
tk = input.substr(start - begin, fpc - start); |
||||||
|
std::cout << "CONTENT:" << std::quoted(tk) << '\n'; |
||||||
|
} |
||||||
|
action start { fmt::println("----- START"); } |
||||||
|
action end { fmt::println("END"); } |
||||||
|
action plain { fmt::println("PLAIN"); } |
||||||
|
|
||||||
|
eol = "\n"; |
||||||
|
start = "===" eol $start; |
||||||
|
end = "---" eol $end; |
||||||
|
pound = "#" $title; |
||||||
|
asterisk = "*" $enum; |
||||||
|
content = (any+ -- (eol|end)) >mark %content; |
||||||
|
|
||||||
|
title = pound space* content eol; |
||||||
|
enum = asterisk space* content eol; |
||||||
|
line = space* content eol; |
||||||
|
blank = space* eol; |
||||||
|
|
||||||
|
slide = (title | enum | line | blank)+ end; |
||||||
|
|
||||||
|
main := start (slide)+; |
||||||
|
}%% |
||||||
|
|
||||||
|
%% write data; |
||||||
|
|
||||||
|
|
||||||
|
bool Parser::parse(const std::string& input) { |
||||||
|
int cs = 0; |
||||||
|
const char *start = nullptr; |
||||||
|
const char *begin = input.data(); |
||||||
|
const char *p = input.data(); |
||||||
|
const char *pe = p + input.size(); |
||||||
|
const char *eof = p + input.size(); (void)eof; |
||||||
|
std::string tk; |
||||||
|
|
||||||
|
%% write init; |
||||||
|
%% write exec; |
||||||
|
|
||||||
|
bool good = pe - p == 0; |
||||||
|
|
||||||
|
if(good) { |
||||||
|
finalize(); |
||||||
|
} else { |
||||||
|
error = true; |
||||||
|
std::cout << "!!!!!!!!!!!!!!!!!!!!!! error at:"; |
||||||
|
std::cout << p; |
||||||
|
} |
||||||
|
return good; |
||||||
|
} |
@ -1,60 +1,24 @@ |
|||||||
#include "parser.hpp" |
#include <fmt/core.h> |
||||||
#include "dbc.hpp" |
#include <numeric> |
||||||
#include <filesystem> |
#include <cassert> |
||||||
#include <fstream> |
#include <fstream> |
||||||
#include <iostream> |
#include <iostream> |
||||||
#include <nlohmann/json.hpp> |
#include "parser.hpp" |
||||||
#include <memory> |
#include "dbc.hpp" |
||||||
|
|
||||||
using std::shared_ptr, std::string, nlohmann::json; |
|
||||||
namespace fs = std::filesystem; |
|
||||||
|
|
||||||
shared_ptr<SlideDeck> parse_slides(const string& md_file) { |
|
||||||
shared_ptr<SlideDeck> deck = std::make_shared<SlideDeck>(); |
|
||||||
|
|
||||||
dbc::check(fs::exists(md_file), "md file missing"); |
|
||||||
|
|
||||||
auto size = fs::file_size(md_file); |
|
||||||
string line(size, '\0'); |
|
||||||
bool started = false; |
|
||||||
json config; |
|
||||||
|
|
||||||
if(std::ifstream in_file{md_file, std::ios::binary}) { |
|
||||||
while(std::getline(in_file, line)) { |
|
||||||
if(line == "{") { |
|
||||||
string json_data; |
|
||||||
|
|
||||||
do { |
#include "./md_parser.cpp" |
||||||
json_data += line; |
|
||||||
} while (std::getline(in_file, line) && line != "}"); |
|
||||||
|
|
||||||
json_data += "}"; |
std::string load_file(const std::string& md_file) { |
||||||
|
std::ifstream in_file{md_file, std::ios::binary}; |
||||||
|
dbc::check(bool(in_file), fmt::format("failed to load {}", md_file)); |
||||||
|
|
||||||
config = json::parse(json_data); |
return std::string(std::istreambuf_iterator<char>(in_file), std::istreambuf_iterator<char>()); |
||||||
std::cout << "JSON: " << config << '\n'; |
} |
||||||
} else if(line == "===") { |
|
||||||
deck->config = config; |
|
||||||
fmt::println("START"); |
|
||||||
started = true; |
|
||||||
} else if(line == "---") { |
|
||||||
fmt::println("START SLIDE"); |
|
||||||
} else { |
|
||||||
if(started) { |
|
||||||
string title = line; |
|
||||||
string content; |
|
||||||
|
|
||||||
while(std::getline(in_file, line) && line != "---") { |
Parser::Parser() |
||||||
content += line + "\n"; |
{ |
||||||
} |
|
||||||
|
|
||||||
deck->slides.emplace_back(title, content, config); |
} |
||||||
config = json::parse("{}"); |
|
||||||
} else { |
|
||||||
fmt::println("JUNK: {}", line); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
return deck; |
void Parser::finalize() { |
||||||
} |
} |
||||||
|
@ -1,33 +1,27 @@ |
|||||||
{ |
|
||||||
"title": "1: A Good First Program", |
|
||||||
"summary": "If you skipped Exercise 0, then you are not doing this book right." |
|
||||||
} |
|
||||||
=== |
=== |
||||||
1: A Good First Program |
# 1: A Good First Program |
||||||
|
|
||||||
* The key is this |
* The key is this |
||||||
* That's what we want |
* That's what we want |
||||||
--- |
--- |
||||||
What You Should See |
# What You Should See |
||||||
|
|
||||||
Yes, but with more feeling. |
Yes, but with more feeling. |
||||||
--- |
--- |
||||||
{ |
# Study Drills |
||||||
"bg_color": [255, 0, 0, 255] |
|
||||||
} |
|
||||||
Study Drills |
|
||||||
|
|
||||||
Besos would be proud. |
Besos would be proud. |
||||||
--- |
--- |
||||||
Common Student Questions |
# Common Student Questions |
||||||
|
|
||||||
- This again. |
* This again. |
||||||
- That too. |
* That too. |
||||||
--- |
--- |
||||||
The Blue Plus |
# The Blue Plus |
||||||
|
|
||||||
_You must find it_. |
_You must find it_. |
||||||
--- |
--- |
||||||
The End |
# The End |
||||||
|
|
||||||
# See You Soon! |
See You Soon! |
||||||
|
--- |
||||||
|
@ -0,0 +1,34 @@ |
|||||||
|
{ |
||||||
|
"title": "1: A Good First Program", |
||||||
|
"summary": "If you skipped Exercise 0, then you are not doing this book right." |
||||||
|
} |
||||||
|
=== |
||||||
|
# 1: A Good First Program |
||||||
|
|
||||||
|
* The key is this |
||||||
|
* That's what we want |
||||||
|
--- |
||||||
|
# What You Should See |
||||||
|
|
||||||
|
Yes, but with more feeling. |
||||||
|
--- |
||||||
|
{ |
||||||
|
"bg_color": [255, 0, 0, 255] |
||||||
|
} |
||||||
|
# Study Drills |
||||||
|
|
||||||
|
Besos would be proud. |
||||||
|
--- |
||||||
|
# Common Student Questions |
||||||
|
|
||||||
|
- This again. |
||||||
|
- That too. |
||||||
|
--- |
||||||
|
# The Blue Plus |
||||||
|
|
||||||
|
_You must find it_. |
||||||
|
--- |
||||||
|
# The End |
||||||
|
|
||||||
|
See You Soon! |
||||||
|
--- |
@ -0,0 +1,19 @@ |
|||||||
|
#include <catch2/catch_test_macros.hpp> |
||||||
|
#include "guecs/sfml/backend.hpp" |
||||||
|
#include <fmt/core.h> |
||||||
|
#include <string> |
||||||
|
#include "parser.hpp" |
||||||
|
|
||||||
|
TEST_CASE("parse a simple example", "[parsing]") { |
||||||
|
sfml::Backend backend; |
||||||
|
guecs::init(&backend); |
||||||
|
|
||||||
|
auto data = parse_slides("./sample/01-a-good-first-program.md"); |
||||||
|
REQUIRE(data->slides.size() > 0); |
||||||
|
|
||||||
|
auto contents = load_file("./sample/01-a-good-first-program.md"); |
||||||
|
Parser rp; |
||||||
|
rp.parse(contents); |
||||||
|
|
||||||
|
REQUIRE(!rp.error); |
||||||
|
} |
Loading…
Reference in new issue