Parser now works on the same slides but no per-slide metadata yet.

master
Zed A. Shaw 7 days ago
parent 0691632f05
commit a9fa98b644
  1. 5
      main.cpp
  2. 67
      md_parser.cpp
  3. 45
      md_parser.rl
  4. 14
      parser.cpp
  5. 7
      parser.hpp
  6. 1
      sample/01-a-good-first-program.md
  7. 3
      tests/parsing.cpp

@ -26,7 +26,10 @@ int main(int argc, char *argv[]) {
presenter.setFramerateLimit(FRAME_LIMIT);
presenter.setVerticalSyncEnabled(VSYNC);
auto data = parse_slides(argv[1]);
auto contents = load_file(argv[1]);
Parser rp;
rp.parse(contents);
auto data = rp.results();
SlidesUI slides(data);
slides.init();

@ -3,12 +3,18 @@
#include <fmt/core.h>
#include <iostream>
enum {
PLAIN=0,
TITLE=1,
ENUM=2
};
#line 45 "md_parser.rl"
#line 66 "md_parser.rl"
#line 7 "md_parser.cpp"
#line 13 "md_parser.cpp"
static const char _Parser_actions[] = {
0, 1, 0, 1, 1, 1, 2, 1,
3, 1, 4, 1, 5, 1, 6, 1,
@ -97,7 +103,7 @@ static const int Parser_error = 0;
static const int Parser_en_main = 1;
#line 48 "md_parser.rl"
#line 69 "md_parser.rl"
bool Parser::parse(const std::string& input) {
@ -110,14 +116,14 @@ bool Parser::parse(const std::string& input) {
std::string tk;
#line 105 "md_parser.cpp"
#line 111 "md_parser.cpp"
{
cs = Parser_start;
}
#line 60 "md_parser.rl"
#line 81 "md_parser.rl"
#line 108 "md_parser.cpp"
#line 114 "md_parser.cpp"
{
int _klen;
unsigned int _trans;
@ -192,44 +198,59 @@ _match:
switch ( *_acts++ )
{
case 0:
#line 12 "md_parser.rl"
#line 14 "md_parser.rl"
{ start = p; }
break;
case 1:
#line 13 "md_parser.rl"
{ fmt::println("TITLE"); }
#line 15 "md_parser.rl"
{ last = TITLE; }
break;
case 2:
#line 14 "md_parser.rl"
{ fmt::println("ENUM"); }
#line 17 "md_parser.rl"
{ last = ENUM; }
break;
case 3:
#line 15 "md_parser.rl"
#line 19 "md_parser.rl"
{
tk = input.substr(start - begin, p - start);
std::cout << "CONTENT:" << std::quoted(tk) << '\n';
if(last == TITLE) {
title = input.substr(start - begin, p - start);
} else if(last == ENUM) {
content += "* " + input.substr(start - begin, p - start);
content += "\n";
} else {
content += input.substr(start - begin, p - start);
content += "\n";
}
last = PLAIN;
}
break;
case 4:
#line 19 "md_parser.rl"
#line 32 "md_parser.rl"
{
tk = input.substr(start - begin, p - start);
std::cout << "META:" << std::quoted(tk) << '\n';
deck->config = json::parse(tk);
std::cout << "META:" << deck->config << '\n';
}
break;
case 5:
#line 23 "md_parser.rl"
#line 38 "md_parser.rl"
{ fmt::println("----- START"); }
break;
case 6:
#line 25 "md_parser.rl"
{ fmt::println(">> START SLIDE"); }
#line 40 "md_parser.rl"
{
title.clear();
content.clear();
}
break;
case 7:
#line 26 "md_parser.rl"
{ fmt::println("<< END SLIDE"); }
#line 45 "md_parser.rl"
{
deck->slides.emplace_back(title, content, config);
}
break;
#line 211 "md_parser.cpp"
#line 232 "md_parser.cpp"
}
}
@ -242,7 +263,7 @@ _again:
_out: {}
}
#line 61 "md_parser.rl"
#line 82 "md_parser.rl"
bool good = pe - p == 0;

@ -1,29 +1,50 @@
#include <fmt/core.h>
#include <iostream>
enum {
PLAIN=0,
TITLE=1,
ENUM=2
};
%%{
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 title { last = TITLE; }
action enum { last = ENUM; }
action content {
tk = input.substr(start - begin, fpc - start);
std::cout << "CONTENT:" << std::quoted(tk) << '\n';
if(last == TITLE) {
title = input.substr(start - begin, fpc - start);
} else if(last == ENUM) {
content += "* " + input.substr(start - begin, fpc - start);
content += "\n";
} else {
content += input.substr(start - begin, fpc - start);
content += "\n";
}
last = PLAIN;
}
action meta {
tk = input.substr(start - begin, fpc - start);
std::cout << "META:" << std::quoted(tk) << '\n';
deck->config = json::parse(tk);
std::cout << "META:" << deck->config << '\n';
}
action start { fmt::println("----- START"); }
action plain { fmt::println("PLAIN"); }
action start_slide { fmt::println(">> START SLIDE"); }
action end_slide { fmt::println("<< END SLIDE"); }
action start_slide {
title.clear();
content.clear();
}
action end_slide {
deck->slides.emplace_back(title, content, config);
}
eol = '\n';
start = '===' %start;

@ -5,19 +5,25 @@
#include <iostream>
#include "parser.hpp"
#include "dbc.hpp"
#include <nlohmann/json.hpp>
#include <memory>
#include <string>
using std::string, nlohmann::json, std::shared_ptr, std::make_shared;
#include "./md_parser.cpp"
std::string load_file(const std::string& md_file) {
string load_file(const string& md_file) {
std::ifstream in_file{md_file, std::ios::binary};
dbc::check(bool(in_file), fmt::format("failed to load {}", md_file));
return std::string(std::istreambuf_iterator<char>(in_file), std::istreambuf_iterator<char>());
return string(std::istreambuf_iterator<char>(in_file), std::istreambuf_iterator<char>());
}
#include "./md_parser.cpp"
Parser::Parser()
{
deck = make_shared<SlideDeck>();
}
void Parser::finalize() {

@ -8,9 +8,16 @@ std::shared_ptr<SlideDeck> parse_slides(const std::string& md_file);
std::string load_file(const std::string& md_file);
struct Parser {
int last = 0;
bool error = false;
std::shared_ptr<SlideDeck> deck;
std::string title;
std::string content;
nlohmann::json config;
Parser();
bool parse(const std::string& input);
void finalize();
std::shared_ptr<SlideDeck> results() { return deck; }
};

@ -8,7 +8,6 @@
* The key is this
* That's what we want
---
{"bg_color": [255, 0, 0, 255]}
# What You Should See
Yes, but with more feeling.

@ -16,4 +16,7 @@ TEST_CASE("parse a simple example", "[parsing]") {
rp.parse(contents);
REQUIRE(!rp.error);
auto deck = rp.results();
REQUIRE(deck->slides.size() > 0);
}

Loading…
Cancel
Save