Use a lel:: namespace.

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

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

@ -1,92 +1,96 @@
#include "lel.hpp"
#include <fmt/core.h>
#include "dbc.hpp"
#include "lel_parser.cpp"
LELParser::LELParser(int x, int y, int width, int height) :
grid_x(x),
grid_y(y),
grid_w(width),
grid_h(height),
cur(0, 0)
{
}
namespace lel {
void LELParser::ltab() {
cur.row = rows;
}
Parser::Parser(int x, int y, int width, int height) :
grid_x(x),
grid_y(y),
grid_w(width),
grid_h(height),
cur(0, 0)
{
}
void LELParser::col() {
}
void Parser::ltab() {
cur.row = rows;
}
void LELParser::valign(char dir) {
cur.bottom = dir == '.';
}
void Parser::col() {
}
void LELParser::align(char dir) {
cur.right = dir == '>';
}
void Parser::valign(char dir) {
cur.bottom = dir == '.';
}
void LELParser::id(std::string name) {
if(name != "_") {
dbc::check(!cells.contains(name),
fmt::format("duplicate cell name {}", name));
cells.insert_or_assign(name, cur);
void Parser::align(char dir) {
cur.right = dir == '>';
}
cur = {cur.col + 1, cur.row};
}
void Parser::id(std::string name) {
if(name != "_") {
dbc::check(!cells.contains(name),
fmt::format("duplicate cell name {}", name));
cells.insert_or_assign(name, cur);
}
void LELParser::row() {
rows++;
columns = std::max(columns, cur.col);
cur.col = 0;
}
cur = {cur.col + 1, cur.row};
}
void LELParser::setwidth(int width) {
cur.max_w = width;
}
void Parser::row() {
rows++;
columns = std::max(columns, cur.col);
cur.col = 0;
}
void LELParser::setheight(int height) {
cur.max_h = height;
}
void Parser::setwidth(int width) {
cur.max_w = width;
}
void LELParser::expand() {
cur.expand = true;
}
void Parser::setheight(int height) {
cur.max_h = height;
}
void LELParser::finalize() {
dbc::check(columns > 0, "columns are 0");
dbc::check(rows > 0, "rows are 0");
int cell_width = grid_w / columns;
int cell_height = grid_h / rows;
dbc::check(cell_width > 0, "invalid cell width calc");
dbc::check(cell_height > 0, "invalid cell height calc");
for(auto& [name, cell] : cells) {
cell.x = grid_x + (cell.col * cell_width);
cell.y = grid_y + (cell.row * cell_height);
cell.max_w = cell.max_w == 0 ? cell_width : cell.max_w;
cell.max_h = cell.max_h == 0 ? cell_height : cell.max_h;
cell.w = cell.expand ? std::min(cell.max_w, grid_w) : std::min(cell_width, cell.max_w);
cell.h = cell.expand ? std::min(cell.max_h, grid_h) : std::min(cell_height, cell.max_h);
if(cell.right) {
cell.x += cell_width - cell.w;
}
void Parser::expand() {
cur.expand = true;
}
if(cell.bottom) {
cell.y += cell_height - cell.h;
void Parser::finalize() {
dbc::check(columns > 0, "columns are 0");
dbc::check(rows > 0, "rows are 0");
int cell_width = grid_w / columns;
int cell_height = grid_h / rows;
dbc::check(cell_width > 0, "invalid cell width calc");
dbc::check(cell_height > 0, "invalid cell height calc");
for(auto& [name, cell] : cells) {
cell.x = grid_x + (cell.col * cell_width);
cell.y = grid_y + (cell.row * cell_height);
cell.max_w = cell.max_w == 0 ? cell_width : cell.max_w;
cell.max_h = cell.max_h == 0 ? cell_height : cell.max_h;
cell.w = cell.expand ? std::min(cell.max_w, grid_w) : std::min(cell_width, cell.max_w);
cell.h = cell.expand ? std::min(cell.max_h, grid_h) : std::min(cell_height, cell.max_h);
if(cell.right) {
cell.x += cell_width - cell.w;
}
if(cell.bottom) {
cell.y += cell_height - cell.h;
}
dbc::check(cell.h > 0, fmt::format("invalid height cell {}", name));
dbc::check(cell.w > 0, fmt::format("invalid width cell {}", name));
}
dbc::check(cell.h > 0, fmt::format("invalid height cell {}", name));
dbc::check(cell.w > 0, fmt::format("invalid width cell {}", name));
}
}
void LELParser::reset() {
rows = 0;
columns = 0;
cur = {0, 0};
void Parser::reset() {
rows = 0;
columns = 0;
cur = {0, 0};
}
}

@ -1,24 +1,26 @@
#pragma once
#include <string>
#include <unordered_map>
#include <functional>
struct Cell {
int x = 0;
int y = 0;
int w = 0;
int h = 0;
int max_w = 0;
int max_h = 0;
int col = 0;
int row = 0;
bool right = false;
bool bottom = false;
bool expand = false;
namespace lel {
struct Cell {
int x = 0;
int y = 0;
int w = 0;
int h = 0;
int max_w = 0;
int max_h = 0;
int col = 0;
int row = 0;
bool right = false;
bool bottom = false;
bool expand = false;
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_y = 0;
int grid_w = 0;
@ -28,7 +30,7 @@ struct LELParser {
Cell cur;
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 ltab();
void align(char dir);
@ -41,4 +43,5 @@ struct LELParser {
void reset();
bool parse(std::string input);
void finalize();
};
};
}

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

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

@ -8,10 +8,7 @@
#include <vector>
TEST_CASE("test basic ops", "[lel]") {
LELParser parser(0, 0, 500, 500);
std::vector<std::string> labels{
"label_1", "label3", "text1", "people",
"label2", "message", "buttons"};
lel::Parser parser(0, 0, 500, 500);
bool good = parser.parse(
"[ 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("message").expand == false);
for(auto name : labels) {
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);
for(auto& [name, cell] : parser.cells) {
REQUIRE(cell.w > 0);
REQUIRE(cell.h > 0);
REQUIRE(cell.row < parser.rows);

Loading…
Cancel
Save