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

master
Zed A. Shaw 1 week 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.setFramerateLimit(FRAME_LIMIT);
presenter.setVerticalSyncEnabled(VSYNC); 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); SlidesUI slides(data);
slides.init(); slides.init();

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

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

@ -5,19 +5,25 @@
#include <iostream> #include <iostream>
#include "parser.hpp" #include "parser.hpp"
#include "dbc.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}; std::ifstream in_file{md_file, std::ios::binary};
dbc::check(bool(in_file), fmt::format("failed to load {}", md_file)); 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() Parser::Parser()
{ {
deck = make_shared<SlideDeck>();
} }
void Parser::finalize() { 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); std::string load_file(const std::string& md_file);
struct Parser { struct Parser {
int last = 0;
bool error = false; bool error = false;
std::shared_ptr<SlideDeck> deck;
std::string title;
std::string content;
nlohmann::json config;
Parser(); Parser();
bool parse(const std::string& input); bool parse(const std::string& input);
void finalize(); void finalize();
std::shared_ptr<SlideDeck> results() { return deck; }
}; };

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

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

Loading…
Cancel
Save