Add Header Lexer for ZCG cpp

This commit is contained in:
Jan
2021-02-12 00:01:41 +01:00
parent 8b8f1d4f2a
commit 87b7921c73
20 changed files with 1386 additions and 59 deletions

View File

@ -2,7 +2,7 @@
#include <iostream>
#include "Impl/HeaderLexer.h"
#include "Parsing/ParsingException.h"
#include "Parsing/Impl/CommentRemovingStreamProxy.h"
#include "Parsing/Impl/DefinesStreamProxy.h"
@ -11,43 +11,92 @@
HeaderFileReader::HeaderFileReader(const ZoneCodeGeneratorArguments* args, std::string filename)
: m_args(args),
m_filename(std::move(filename))
m_filename(std::move(filename)),
m_stream(nullptr)
{
}
bool HeaderFileReader::ReadHeaderFile(IDataRepository* repository) const
bool HeaderFileReader::OpenBaseStream()
{
std::cout << "Reading header file: " << m_filename << std::endl;
ParserFilesystemStream stream(m_filename);
if (!stream.IsOpen())
auto stream = std::make_unique<ParserFilesystemStream>(m_filename);
if (!stream->IsOpen())
{
std::cout << "Could not open header file" << std::endl;
return false;
}
IParserLineStream* lineStream = &stream;
m_stream = stream.get();
m_open_streams.emplace_back(std::move(stream));
return true;
}
CommentRemovingStreamProxy commentProxy(lineStream);
lineStream = &commentProxy;
void HeaderFileReader::SetupStreamProxies()
{
auto commentProxy = std::make_unique<CommentRemovingStreamProxy>(m_stream);
auto includeProxy = std::make_unique<IncludingStreamProxy>(commentProxy.get());
auto definesProxy = std::make_unique<DefinesStreamProxy>(includeProxy.get());
definesProxy->AddDefine(ZONE_CODE_GENERATOR_DEFINE_NAME, ZONE_CODE_GENERATOR_DEFINE_VALUE);
IncludingStreamProxy includeProxy(lineStream);
lineStream = &includeProxy;
m_stream = definesProxy.get();
DefinesStreamProxy definesProxy(lineStream);
definesProxy.AddDefine(ZONE_CODE_GENERATOR_DEFINE_NAME, ZONE_CODE_GENERATOR_DEFINE_VALUE);
lineStream = &definesProxy;
m_open_streams.emplace_back(std::move(commentProxy));
m_open_streams.emplace_back(std::move(includeProxy));
m_open_streams.emplace_back(std::move(definesProxy));
}
bool HeaderFileReader::ReadHeaderFile(IDataRepository* repository)
{
std::cout << "Reading header file: " << m_filename << std::endl;
if (!OpenBaseStream())
return false;
SetupStreamProxies();
auto lexer = std::make_unique<HeaderLexer>(m_stream);
try
{
while (true)
/*while (true)
{
auto line = lineStream->NextLine();
auto line = m_stream->NextLine();
if (line.IsEof())
break;
std::cout << "Line " << line.m_filename << ":" << line.m_line_number << ": " << line.m_line << "\n";
std::cout << "Line " << line.m_filename.get() << ":" << line.m_line_number << ": " << line.m_line << "\n";
}*/
auto eof = false;
while (!eof)
{
const auto& token = lexer->GetToken(0);
switch (token.m_type)
{
case HeaderParserValueType::END_OF_FILE:
case HeaderParserValueType::INVALID:
eof = true;
break;
case HeaderParserValueType::CHARACTER:
std::cout << "Token " << token.CharacterValue() << "\n";
break;
case HeaderParserValueType::IDENTIFIER:
std::cout << "Token IDENTIFIER \"" << token.IdentifierValue() << "\"\n";
break;
case HeaderParserValueType::STRING:
std::cout << "Token STRING \"" << token.StringValue() << "\"\n";
break;
default:
std::cout << "Token UNKNOWN\n";
break;
}
lexer->PopTokens(1);
}
}
catch (const ParsingException& e)