From 751cb2cd6e12c3cefcf5c3d5e13aadb583982ae5 Mon Sep 17 00:00:00 2001 From: Jan Date: Thu, 11 Feb 2021 11:51:06 +0100 Subject: [PATCH] Add c preprocessor proxies to command file parser to support defines and include --- .../Parsing/Commands/CommandsFileReader.cpp | 43 +++++++++++++++++++ .../Parsing/Commands/CommandsFileReader.h | 3 ++ .../Parsing/Header/HeaderFileReader.cpp | 2 +- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/ZoneCodeGeneratorLib/Parsing/Commands/CommandsFileReader.cpp b/src/ZoneCodeGeneratorLib/Parsing/Commands/CommandsFileReader.cpp index 8f695ddc..22df4dc2 100644 --- a/src/ZoneCodeGeneratorLib/Parsing/Commands/CommandsFileReader.cpp +++ b/src/ZoneCodeGeneratorLib/Parsing/Commands/CommandsFileReader.cpp @@ -2,6 +2,12 @@ #include +#include "Parsing/ParsingException.h" +#include "Parsing/Impl/CommentRemovingStreamProxy.h" +#include "Parsing/Impl/DefinesStreamProxy.h" +#include "Parsing/Impl/IncludingStreamProxy.h" +#include "Parsing/Impl/ParserFilesystemStream.h" + CommandsFileReader::CommandsFileReader(const ZoneCodeGeneratorArguments* args, std::string filename) : m_args(args), m_filename(std::move(filename)) @@ -11,5 +17,42 @@ CommandsFileReader::CommandsFileReader(const ZoneCodeGeneratorArguments* args, s bool CommandsFileReader::ReadCommandsFile(IDataRepository* repository) { std::cout << "Reading commands file: " << m_filename << std::endl; + + ParserFilesystemStream stream(m_filename); + if (!stream.IsOpen()) + { + std::cout << "Could not open commands file" << std::endl; + return false; + } + + IParserLineStream* lineStream = &stream; + + CommentRemovingStreamProxy commentProxy(lineStream); + lineStream = &commentProxy; + + IncludingStreamProxy includeProxy(lineStream); + lineStream = &includeProxy; + + DefinesStreamProxy definesProxy(lineStream); + definesProxy.AddDefine(ZONE_CODE_GENERATOR_DEFINE_NAME, ZONE_CODE_GENERATOR_DEFINE_VALUE); + lineStream = &definesProxy; + + try + { + while (true) + { + auto line = lineStream->NextLine(); + + if (line.IsEof()) + break; + + std::cout << "Line " << line.m_filename << ":" << line.m_line_number << ": " << line.m_line << "\n"; + } + } + catch (const ParsingException& e) + { + std::cout << "Error: " << e.FullMessage() << std::endl; + } + return true; } diff --git a/src/ZoneCodeGeneratorLib/Parsing/Commands/CommandsFileReader.h b/src/ZoneCodeGeneratorLib/Parsing/Commands/CommandsFileReader.h index df6cc6dd..5b8ac207 100644 --- a/src/ZoneCodeGeneratorLib/Parsing/Commands/CommandsFileReader.h +++ b/src/ZoneCodeGeneratorLib/Parsing/Commands/CommandsFileReader.h @@ -7,6 +7,9 @@ class CommandsFileReader { + static constexpr const char* ZONE_CODE_GENERATOR_DEFINE_NAME = "__zonecodegenerator"; + static constexpr const char* ZONE_CODE_GENERATOR_DEFINE_VALUE = "1"; + const ZoneCodeGeneratorArguments* m_args; std::string m_filename; diff --git a/src/ZoneCodeGeneratorLib/Parsing/Header/HeaderFileReader.cpp b/src/ZoneCodeGeneratorLib/Parsing/Header/HeaderFileReader.cpp index 2820f004..ec7d70d9 100644 --- a/src/ZoneCodeGeneratorLib/Parsing/Header/HeaderFileReader.cpp +++ b/src/ZoneCodeGeneratorLib/Parsing/Header/HeaderFileReader.cpp @@ -47,7 +47,7 @@ bool HeaderFileReader::ReadHeaderFile(IDataRepository* repository) const if (line.IsEof()) break; - std::cout << "Line " << line.m_filename << ":" << line.m_line_number << ": " << line.m_line << std::endl; + std::cout << "Line " << line.m_filename << ":" << line.m_line_number << ": " << line.m_line << "\n"; } } catch (const ParsingException& e)