18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 17:04:52 +01:00
Files
maszyna/tests/unit/test_parser_tape.cpp
T
maj00r cfbe515db8 feat(parser): a tape of what the scenery parser gave its reader, and a parser replaying it
Step 2c of docs/world-format-eu07.md (#516, section 7): the logic layer of the cache holds the node definitions as
tokens read back through today's parsers. cParser::startRecording() makes the outermost parser write down every
token as it comes out to the reader (includes expanded, template parameters put in, injected text included),
with what the reader may ask about it (Name, Line, LineMain, TokenBegin/End, InLayerFile), and the side effects
the includes have on the way: the node groups of *.inc templates opened and closed, and the terrain files the
binary terrain stands in for. A parser made from the tape gives the same reader the same answers and repeats
the side effects at the same places, with no file opened: injectString() does nothing there, as the tape holds
the injected tokens already. The reads the parser makes for itself (the name and parameters of an include) are
not recorded.

With a tape the fast path that takes model entries straight from the text is off (remainingText() is empty),
so every token goes through the tape. The loader takes the case-kept header of a node from the new
headerText(4), its first four words: the same words as before, recorded and replayed as a text entry. Without
a tape nothing changes.

parser_tape::write/read keep a tape as bytes, the state of a token as differences from the one before in
variable-length integers.

Tests: a reader asking the way the loader does (several tokens, case-kept ones, a vector, a look ahead, the
header) gets the same transcript from the files with and without recording and from the tape through bytes,
with templates, parameters, nested includes, injected text and traction loaded or left out; the node groups
are on the tape, a skipped terrain file is counted on replay as on the first reading, and a cut or foreign
tape doesn't read.
2026-12-02 22:02:58 +01:00

169 lines
6.3 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "scratch.h"
#include "utilities/parser.h"
#include "utilities/parsertape.h"
#include "utilities/Globals.h"
#include <algorithm>
#include <format>
#include <sstream>
#include <string>
#include <vector>
// the tape of a scenery parser (utilities/parsertape.h): what the outermost parser gave its reader, replayed to the same reader with
// the same answers and without the files (docs/world-format-eu07.md, section 7)
namespace {
// a reader that asks the parser the way the scenery loader does: several tokens at once, single ones in their case, a vector, a
// look ahead, and the header text; it writes down every answer, and the state of the parser after it
std::vector<std::string> read_all(cParser &Input) {
std::vector<std::string> transcript;
auto const state{[&Input, &transcript](std::string const &What) {
transcript.push_back(std::format("{} | {} {} {} {} {} {}", What, Input.Name(), Input.Line(), Input.LineMain(), Input.TokenBegin(), Input.TokenEnd(), Input.InLayerFile()));
}};
while (true) {
auto const header{Input.headerText(4)};
auto const keyword{Input.getToken<std::string>()};
if (keyword.empty())
break;
state(std::format("keyword {} [{}]", keyword, header));
if (keyword == "node") {
Input.getTokens(4);
std::string name;
std::string type;
double range_max{0.0};
double range_min{0.0};
Input >> range_max >> range_min >> name >> type;
state(std::format("node {} {} {} {}", range_max, range_min, name, type));
auto const position{Input.getToken<glm::vec3>()};
state(std::format("at {} {} {}", position.x, position.y, position.z));
auto const texture{Input.getToken<std::string>(false)};
state("texture " + texture);
Input.getTokens(1);
state("peek " + Input.peek());
Input.getTokens(1);
std::string end;
Input >> end;
state("end " + end);
}
}
return transcript;
}
} // namespace
TEST_CASE("a parser replaying its tape gives the reader the same tokens and the same state, with no file opened") {
test::scratch_folder const folder{"parser_tape"};
folder.write("main.scn",
"// the main file\n"
"node 1000 0 Signal_A model 1.5 2.0 -3.25 Textures/Sem.DDS 7 endmodel\n"
"include tpl/pole.inc Pole_1 12.5 endtrack end\n"
"include tr/wire.scm end\n"
"node 500 0 none track 0 0 0 Rail none endtrack\n");
folder.write("tpl/pole.inc",
"node 300 0 (p1) model (p2) 0 0 Pole.dds 1 (p3)\n"
"include tpl/inner.inc end\n");
folder.write("tpl/inner.inc", "node 100 0 Inner model 0 0 0 none 2 endmodel\n");
folder.write("tr/wire.scm", "node 900 0 Wire traction 0 0 0 none 3 endtraction\n");
auto const record_and_replay{[&folder](bool const Loadtraction) {
parser_tape::tape recording;
std::vector<std::string> recorded;
{
cParser input{"main.scn", cParser::buffer_FILE, folder.path(), Loadtraction};
input.injectString("node 1 0 Injected model 0 0 0 none 4 endmodel");
input.startRecording(recording);
recorded = read_all(input);
}
// the same reading from the files, without a tape
std::vector<std::string> plain;
{
cParser input{"main.scn", cParser::buffer_FILE, folder.path(), Loadtraction};
input.injectString("node 1 0 Injected model 0 0 0 none 4 endmodel");
plain = read_all(input);
}
CHECK(recorded == plain);
// and from the tape, through bytes, with the files gone
std::stringstream bytes;
parser_tape::write(bytes, recording);
parser_tape::tape back;
REQUIRE(parser_tape::read(bytes, back));
CHECK(back == recording);
std::vector<std::string> replayed;
{
cParser input{back};
// the loader injects its text again; the tape holds it already
input.injectString("node 1 0 Injected model 0 0 0 none 4 endmodel");
replayed = read_all(input);
}
CHECK(replayed == recorded);
return std::pair{recorded, recording};
}};
auto const [with_traction, tape]{record_and_replay(true)};
CHECK(with_traction.size() > 30);
// the parameters of the template are in the tokens, the nested include expanded
auto const joined{[](std::vector<std::string> const &Lines) {
std::string all;
for (auto const &line : Lines)
all += line + "\n";
return all;
}};
CHECK(joined(with_traction).find("node 300 0 pole_1 model") != std::string::npos);
CHECK(joined(with_traction).find("node 100 0 inner model") != std::string::npos);
CHECK(joined(with_traction).find("wire traction") != std::string::npos);
// the two templates opened and closed their node groups, and the tape holds that
auto const count{[&tape](parser_tape::entry_kind const Kind) { return std::ranges::count(tape.entries, Kind, &parser_tape::entry::kind); }};
CHECK(count(parser_tape::entry_kind::group_create) == 2);
CHECK(count(parser_tape::entry_kind::group_close) == 2);
// with the traction left out, its include is skipped in the tape as in the files
auto const without_traction{record_and_replay(false).first};
CHECK(joined(without_traction).find("wire traction") == std::string::npos);
SUBCASE("a terrain file the binary terrain stands in for is counted on replay as on the first reading") {
folder.write("terrain.scn", "include x_ter.scm end\nnode 1 0 After model 0 0 0 none 1 endmodel\n");
auto const previous_state{Global.file_binary_terrain_state};
Global.file_binary_terrain_state = true;
auto const before{Global.file_binary_terrain_skipped};
parser_tape::tape terrain;
{
cParser input{"terrain.scn", cParser::buffer_FILE, folder.path()};
input.startRecording(terrain);
read_all(input);
}
CHECK(Global.file_binary_terrain_skipped == before + 1);
{
cParser input{terrain};
read_all(input);
}
CHECK(Global.file_binary_terrain_skipped == before + 2);
Global.file_binary_terrain_state = previous_state;
}
}
TEST_CASE("a broken tape doesn't read") {
parser_tape::tape tape;
tape.files = {"a.scn"};
parser_tape::entry token;
token.text = "node";
token.line = 3;
token.begin = 10;
token.end = 14;
tape.entries.push_back(token);
std::stringstream bytes;
parser_tape::write(bytes, tape);
auto const whole{bytes.str()};
for (std::size_t length = 0; length < whole.size(); ++length) {
std::stringstream cut{whole.substr(0, length)};
parser_tape::tape back;
CHECK_FALSE(parser_tape::read(cut, back));
}
auto wrong{whole};
wrong[0] = 9; // another version
std::stringstream other{wrong};
parser_tape::tape back;
CHECK_FALSE(parser_tape::read(other, back));
}