Use a lel:: namespace.

Zed A. Shaw 3 weeks ago
parent cebf61a794
commit 1620a5420f
  1. 2
      combat_ui.hpp
  2. 28
      lel.cpp
  3. 7
      lel.hpp
  4. 90
      lel_parser.cpp
  5. 8
      lel_parser.rl
  6. 12
      tests/lel.cpp

@ -8,7 +8,7 @@
namespace gui { namespace gui {
class CombatUI { class CombatUI {
public: public:
LELParser $layout; lel::Parser $layout;
GameLevel $level; GameLevel $level;
std::unordered_map<std::string, sf::RectangleShape> $shapes; std::unordered_map<std::string, sf::RectangleShape> $shapes;
CombatUI(GameLevel level); CombatUI(GameLevel level);

@ -1,9 +1,12 @@
#include "lel.hpp" #include "lel.hpp"
#include <fmt/core.h> #include <fmt/core.h>
#include "dbc.hpp" #include "dbc.hpp"
#include "lel_parser.cpp" #include "lel_parser.cpp"
LELParser::LELParser(int x, int y, int width, int height) : namespace lel {
Parser::Parser(int x, int y, int width, int height) :
grid_x(x), grid_x(x),
grid_y(y), grid_y(y),
grid_w(width), grid_w(width),
@ -12,22 +15,22 @@ LELParser::LELParser(int x, int y, int width, int height) :
{ {
} }
void LELParser::ltab() { void Parser::ltab() {
cur.row = rows; cur.row = rows;
} }
void LELParser::col() { void Parser::col() {
} }
void LELParser::valign(char dir) { void Parser::valign(char dir) {
cur.bottom = dir == '.'; cur.bottom = dir == '.';
} }
void LELParser::align(char dir) { void Parser::align(char dir) {
cur.right = dir == '>'; cur.right = dir == '>';
} }
void LELParser::id(std::string name) { void Parser::id(std::string name) {
if(name != "_") { if(name != "_") {
dbc::check(!cells.contains(name), dbc::check(!cells.contains(name),
fmt::format("duplicate cell name {}", name)); fmt::format("duplicate cell name {}", name));
@ -37,25 +40,25 @@ void LELParser::id(std::string name) {
cur = {cur.col + 1, cur.row}; cur = {cur.col + 1, cur.row};
} }
void LELParser::row() { void Parser::row() {
rows++; rows++;
columns = std::max(columns, cur.col); columns = std::max(columns, cur.col);
cur.col = 0; cur.col = 0;
} }
void LELParser::setwidth(int width) { void Parser::setwidth(int width) {
cur.max_w = width; cur.max_w = width;
} }
void LELParser::setheight(int height) { void Parser::setheight(int height) {
cur.max_h = height; cur.max_h = height;
} }
void LELParser::expand() { void Parser::expand() {
cur.expand = true; cur.expand = true;
} }
void LELParser::finalize() { void Parser::finalize() {
dbc::check(columns > 0, "columns are 0"); dbc::check(columns > 0, "columns are 0");
dbc::check(rows > 0, "rows are 0"); dbc::check(rows > 0, "rows are 0");
int cell_width = grid_w / columns; int cell_width = grid_w / columns;
@ -85,8 +88,9 @@ void LELParser::finalize() {
} }
} }
void LELParser::reset() { void Parser::reset() {
rows = 0; rows = 0;
columns = 0; columns = 0;
cur = {0, 0}; cur = {0, 0};
} }
}

@ -1,7 +1,9 @@
#pragma once #pragma once
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <functional>
namespace lel {
struct Cell { struct Cell {
int x = 0; int x = 0;
int y = 0; int y = 0;
@ -18,7 +20,7 @@ struct Cell {
Cell(int col, int row) : col(col), row(row) {} Cell(int col, int row) : col(col), row(row) {}
}; };
struct LELParser { struct Parser {
int grid_x = 0; int grid_x = 0;
int grid_y = 0; int grid_y = 0;
int grid_w = 0; int grid_w = 0;
@ -28,7 +30,7 @@ struct LELParser {
Cell cur; Cell cur;
std::unordered_map<std::string, Cell> cells; std::unordered_map<std::string, Cell> cells;
LELParser(int x, int y, int width, int height); Parser(int x, int y, int width, int height);
void col(); void col();
void ltab(); void ltab();
void align(char dir); void align(char dir);
@ -42,3 +44,4 @@ struct LELParser {
bool parse(std::string input); bool parse(std::string input);
void finalize(); void finalize();
}; };
}

@ -4,25 +4,27 @@
#include <fmt/core.h> #include <fmt/core.h>
#include <iostream> #include <iostream>
namespace lel {
#line 34 "lel_parser.rl"
#line 36 "lel_parser.rl"
#line 8 "lel_parser.cpp"
static const char _LELParser_actions[] = { #line 10 "lel_parser.cpp"
static const char _Parser_actions[] = {
0, 1, 1, 1, 2, 1, 3, 1, 0, 1, 1, 1, 2, 1, 3, 1,
4, 1, 5, 1, 6, 1, 9, 1, 4, 1, 5, 1, 6, 1, 9, 1,
10, 1, 11, 2, 0, 7, 2, 0, 10, 1, 11, 2, 0, 7, 2, 0,
8, 2, 4, 1, 2, 4, 5 8, 2, 4, 1, 2, 4, 5
}; };
static const char _LELParser_key_offsets[] = { static const char _Parser_key_offsets[] = {
0, 0, 4, 18, 20, 24, 35, 47, 0, 0, 4, 18, 20, 24, 35, 47,
52, 54, 57 52, 54, 57
}; };
static const char _LELParser_trans_keys[] = { static const char _Parser_trans_keys[] = {
32, 91, 9, 13, 32, 40, 42, 46, 32, 91, 9, 13, 32, 40, 42, 46,
60, 62, 94, 95, 9, 13, 65, 90, 60, 62, 94, 95, 9, 13, 65, 90,
97, 122, 48, 57, 41, 44, 48, 57, 97, 122, 48, 57, 41, 44, 48, 57,
@ -33,22 +35,22 @@ static const char _LELParser_trans_keys[] = {
57, 32, 91, 9, 13, 0 57, 32, 91, 9, 13, 0
}; };
static const char _LELParser_single_lengths[] = { static const char _Parser_single_lengths[] = {
0, 2, 8, 0, 2, 7, 4, 3, 0, 2, 8, 0, 2, 7, 4, 3,
0, 1, 2 0, 1, 2
}; };
static const char _LELParser_range_lengths[] = { static const char _Parser_range_lengths[] = {
0, 1, 3, 1, 1, 2, 4, 1, 0, 1, 3, 1, 1, 2, 4, 1,
1, 1, 1 1, 1, 1
}; };
static const char _LELParser_index_offsets[] = { static const char _Parser_index_offsets[] = {
0, 0, 4, 16, 18, 22, 32, 41, 0, 0, 4, 16, 18, 22, 32, 41,
46, 48, 51 46, 48, 51
}; };
static const char _LELParser_indicies[] = { static const char _Parser_indicies[] = {
0, 2, 0, 1, 3, 4, 5, 6, 0, 2, 0, 1, 3, 4, 5, 6,
7, 7, 6, 8, 3, 8, 8, 1, 7, 7, 6, 8, 3, 8, 8, 1,
9, 1, 10, 11, 12, 1, 4, 5, 9, 1, 10, 11, 12, 1, 4, 5,
@ -58,28 +60,28 @@ static const char _LELParser_indicies[] = {
21, 22, 1, 23, 2, 23, 1, 0 21, 22, 1, 23, 2, 23, 1, 0
}; };
static const char _LELParser_trans_targs[] = { static const char _Parser_trans_targs[] = {
1, 0, 2, 2, 3, 5, 5, 5, 1, 0, 2, 2, 3, 5, 5, 5,
6, 4, 5, 8, 4, 7, 6, 10, 6, 4, 5, 8, 4, 7, 6, 10,
2, 7, 10, 2, 9, 5, 9, 10 2, 7, 10, 2, 9, 5, 9, 10
}; };
static const char _LELParser_trans_actions[] = { static const char _Parser_trans_actions[] = {
0, 0, 3, 0, 0, 13, 5, 11, 0, 0, 3, 0, 0, 13, 5, 11,
17, 15, 19, 19, 0, 7, 0, 28, 17, 15, 19, 19, 0, 7, 0, 28,
25, 0, 9, 1, 15, 22, 0, 0 25, 0, 9, 1, 15, 22, 0, 0
}; };
static const int LELParser_start = 1; static const int Parser_start = 1;
static const int LELParser_first_final = 10; static const int Parser_first_final = 10;
static const int LELParser_error = 0; static const int Parser_error = 0;
static const int LELParser_en_main = 1; static const int Parser_en_main = 1;
#line 37 "lel_parser.rl" #line 39 "lel_parser.rl"
bool LELParser::parse(std::string input) { bool Parser::parse(std::string input) {
reset(); reset();
int cs = 0; int cs = 0;
const char *start = nullptr; const char *start = nullptr;
@ -89,14 +91,14 @@ bool LELParser::parse(std::string input) {
std::string tk; std::string tk;
#line 84 "lel_parser.cpp" #line 86 "lel_parser.cpp"
{ {
cs = LELParser_start; cs = Parser_start;
} }
#line 48 "lel_parser.rl" #line 50 "lel_parser.rl"
#line 87 "lel_parser.cpp" #line 89 "lel_parser.cpp"
{ {
int _klen; int _klen;
unsigned int _trans; unsigned int _trans;
@ -109,10 +111,10 @@ bool LELParser::parse(std::string input) {
if ( cs == 0 ) if ( cs == 0 )
goto _out; goto _out;
_resume: _resume:
_keys = _LELParser_trans_keys + _LELParser_key_offsets[cs]; _keys = _Parser_trans_keys + _Parser_key_offsets[cs];
_trans = _LELParser_index_offsets[cs]; _trans = _Parser_index_offsets[cs];
_klen = _LELParser_single_lengths[cs]; _klen = _Parser_single_lengths[cs];
if ( _klen > 0 ) { if ( _klen > 0 ) {
const char *_lower = _keys; const char *_lower = _keys;
const char *_mid; const char *_mid;
@ -135,7 +137,7 @@ _resume:
_trans += _klen; _trans += _klen;
} }
_klen = _LELParser_range_lengths[cs]; _klen = _Parser_range_lengths[cs];
if ( _klen > 0 ) { if ( _klen > 0 ) {
const char *_lower = _keys; const char *_lower = _keys;
const char *_mid; const char *_mid;
@ -158,67 +160,67 @@ _resume:
} }
_match: _match:
_trans = _LELParser_indicies[_trans]; _trans = _Parser_indicies[_trans];
cs = _LELParser_trans_targs[_trans]; cs = _Parser_trans_targs[_trans];
if ( _LELParser_trans_actions[_trans] == 0 ) if ( _Parser_trans_actions[_trans] == 0 )
goto _again; goto _again;
_acts = _LELParser_actions + _LELParser_trans_actions[_trans]; _acts = _Parser_actions + _Parser_trans_actions[_trans];
_nacts = (unsigned int) *_acts++; _nacts = (unsigned int) *_acts++;
while ( _nacts-- > 0 ) while ( _nacts-- > 0 )
{ {
switch ( *_acts++ ) switch ( *_acts++ )
{ {
case 0: case 0:
#line 9 "lel_parser.rl" #line 11 "lel_parser.rl"
{tk = input.substr(start - begin, p - start); } {tk = input.substr(start - begin, p - start); }
break; break;
case 1: case 1:
#line 11 "lel_parser.rl" #line 13 "lel_parser.rl"
{ col(); } { col(); }
break; break;
case 2: case 2:
#line 12 "lel_parser.rl" #line 14 "lel_parser.rl"
{ ltab(); } { ltab(); }
break; break;
case 3: case 3:
#line 13 "lel_parser.rl" #line 15 "lel_parser.rl"
{ valign((*p)); } { valign((*p)); }
break; break;
case 4: case 4:
#line 14 "lel_parser.rl" #line 16 "lel_parser.rl"
{ id(input.substr(start - begin, p - start)); } { id(input.substr(start - begin, p - start)); }
break; break;
case 5: case 5:
#line 15 "lel_parser.rl" #line 17 "lel_parser.rl"
{ row(); } { row(); }
break; break;
case 6: case 6:
#line 16 "lel_parser.rl" #line 18 "lel_parser.rl"
{ align((*p)); } { align((*p)); }
break; break;
case 7: case 7:
#line 17 "lel_parser.rl" #line 19 "lel_parser.rl"
{ setwidth(std::stoi(tk)); } { setwidth(std::stoi(tk)); }
break; break;
case 8: case 8:
#line 18 "lel_parser.rl" #line 20 "lel_parser.rl"
{ setheight(std::stoi(tk)); } { setheight(std::stoi(tk)); }
break; break;
case 9: case 9:
#line 19 "lel_parser.rl" #line 21 "lel_parser.rl"
{ expand(); } { expand(); }
break; break;
case 10: case 10:
#line 27 "lel_parser.rl" #line 29 "lel_parser.rl"
{ start = p; } { start = p; }
break; break;
case 11: case 11:
#line 30 "lel_parser.rl" #line 32 "lel_parser.rl"
{start = p;} {start = p;}
break; break;
#line 196 "lel_parser.cpp" #line 198 "lel_parser.cpp"
} }
} }
@ -231,7 +233,7 @@ _again:
_out: {} _out: {}
} }
#line 49 "lel_parser.rl" #line 51 "lel_parser.rl"
bool good = pe - p == 0; bool good = pe - p == 0;
if(good) { if(good) {
@ -242,3 +244,5 @@ _again:
} }
return good; return good;
} }
}

@ -2,8 +2,10 @@
#include <fmt/core.h> #include <fmt/core.h>
#include <iostream> #include <iostream>
namespace lel {
%%{ %%{
machine LELParser; machine Parser;
alphtype char; alphtype char;
action token {tk = input.substr(start - begin, fpc - start); } action token {tk = input.substr(start - begin, fpc - start); }
@ -35,7 +37,7 @@
%% write data; %% write data;
bool LELParser::parse(std::string input) { bool Parser::parse(std::string input) {
reset(); reset();
int cs = 0; int cs = 0;
const char *start = nullptr; const char *start = nullptr;
@ -56,3 +58,5 @@ bool LELParser::parse(std::string input) {
} }
return good; return good;
} }
}

@ -8,10 +8,7 @@
#include <vector> #include <vector>
TEST_CASE("test basic ops", "[lel]") { TEST_CASE("test basic ops", "[lel]") {
LELParser parser(0, 0, 500, 500); lel::Parser parser(0, 0, 500, 500);
std::vector<std::string> labels{
"label_1", "label3", "text1", "people",
"label2", "message", "buttons"};
bool good = parser.parse( bool good = parser.parse(
"[ label_1 | label3 | test1]" "[ label_1 | label3 | test1]"
@ -31,12 +28,7 @@ TEST_CASE("test basic ops", "[lel]") {
REQUIRE(parser.cells.at("people").expand == false); REQUIRE(parser.cells.at("people").expand == false);
REQUIRE(parser.cells.at("message").expand == false); REQUIRE(parser.cells.at("message").expand == false);
for(auto name : labels) { for(auto& [name, cell] : parser.cells) {
auto& cell = parser.cells.at(name);
fmt::println("name={}; col/row={},{}; x/y={},{} w/h={},{}; right={}, bottom={}, expand={}",
name, cell.col, cell.row, cell.x, cell.y, cell.w, cell.h, cell.right, cell.bottom, cell.expand);
REQUIRE(cell.w > 0); REQUIRE(cell.w > 0);
REQUIRE(cell.h > 0); REQUIRE(cell.h > 0);
REQUIRE(cell.row < parser.rows); REQUIRE(cell.row < parser.rows);

Loading…
Cancel
Save