From ee14cefcb5be898b37195aa9d01d20e32c06dfae Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Wed, 25 Nov 2026 05:24:38 +0100 Subject: [PATCH] fix(parser): an include at the very end of a text leaves the end of the data to the included file (#492) A file which ends right after the end of an include directive, with no line break after it, had the parser report its end as soon as the directive was read: eof() looked only at the text being read, not at the included file it still had to go through. A reader which stops at the end of the data, like deserialize_map() (the sound overrides), then read only the first line of the included file. eof() now also waits for the included file to end. The tokens themselves don't change, and a file with a line break after the end of its last include reports the end as before. The .fiz files of the wagons which broke the change of #100 (pkp/yb_v1, the directive at the very end without a line break) are read through on staging already; a test now keeps them that way. --- tests/unit/test_parser.cpp | 64 ++++++++++++++++++++++++++++++++++++++ utilities/parser.h | 6 ++-- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_parser.cpp b/tests/unit/test_parser.cpp index 2584218d0..ee82aca9a 100644 --- a/tests/unit/test_parser.cpp +++ b/tests/unit/test_parser.cpp @@ -696,6 +696,70 @@ TEST_CASE("parser: read line by line, a line starting with include is the direct CHECK(rest(word, false, "\n\r") == "one|two"); } +namespace { + +// the lines of a file, read the way the vehicle's .fiz is (TMoverParameters::LoadFIZ_Parse): line by line while the +// parser is ok +std::string fiz_lines(cParser &Parser) { + std::string joined; + while (Parser.ok()) { + auto const line{Parser.getToken(false, "\n\r")}; + if (line.empty()) { + continue; + } + if (false == joined.empty()) { + joined += '|'; + } + joined += line; + } + return joined; +} + +} // namespace + +TEST_CASE("parser: a file which ends right after the end of an include is read through, the included file too") { + // the shape of the .fiz files of the wagons (pkp/yb_v1/b84_pl_1.fiz): the directive, the file and its parameters + // each on a line of their own, and no line break after the end. the change of MaSzyna-EU07/maszyna#100 broke on these + test::scratch_folder const folder{"parser_include_at_end"}; + folder.write("yb.fiz.inc", "Mass=(p1)\r\nVmax=(p2)\r\nLength=20\r\n"); + folder.write("ended.fiz", "include\r\nyb.fiz.inc\r\n160\r\n80\r\nend"); + folder.write("newline.fiz", "include\r\nyb.fiz.inc\r\n160\r\n80\r\nend\r\n"); + + cParser ended("ended.fiz", cParser::buffer_FILE, folder.path()); + CHECK(fiz_lines(ended) == "Mass=160|Vmax=80|Length=20"); + cParser newline("newline.fiz", cParser::buffer_FILE, folder.path()); + CHECK(fiz_lines(newline) == "Mass=160|Vmax=80|Length=20"); + + SUBCASE("the end of the data shows once the included file is read through, not when its directive is") { + folder.write("pair.inc", "x y"); + cParser parser("include pair.inc end", cParser::buffer_TEXT, folder.path()); + CHECK(parser.getToken() == "x"); + CHECK_FALSE(parser.eof()); + CHECK(parser.getToken() == "y"); + CHECK(parser.eof()); + CHECK(parser.ok()); + CHECK(parser.getToken().empty()); + CHECK_FALSE(parser.ok()); + // the same as for a text with the included tokens in place of the directive + cParser same("x y", cParser::buffer_TEXT); + CHECK(same.getToken() == "x"); + CHECK_FALSE(same.eof()); + CHECK(same.getToken() == "y"); + CHECK(same.eof()); + } + SUBCASE("a reader which stops at the end of the data reads the whole included file") { + folder.write("map.inc", "alpha One\nbeta Two"); + for (auto const *const text : {"include map.inc end", "include map.inc end\n"}) { + CAPTURE(text); + cParser parser(text, cParser::buffer_TEXT, folder.path()); + std::map map; + deserialize_map(map, parser); + CHECK(map["alpha"] == "One"); + CHECK(map["beta"] == "Two"); + } + } +} + TEST_CASE("parser: an include can name several files to pick one from at random") { test::scratch_folder const folder{"parser_random_include"}; folder.write("only.inc", "chosen"); diff --git a/utilities/parser.h b/utilities/parser.h index 19fc34f18..fa4c2c597 100644 --- a/utilities/parser.h +++ b/utilities/parser.h @@ -62,10 +62,12 @@ class cParser //: public std::stringstream std::string out; readToken(out); return out == Value; }; + // the end of the data: of this text, and of the file included last in it, which an include directive at the very + // end of the text leaves to be read inline bool - eof() { - return mEofBit; }; + eof() const { + return mEofBit && ( mIncludeParser == nullptr || mIncludeParser->eof() ); }; inline bool ok() {