mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-14 00:38:15 -05:00
Extract commonly used Parser code to new Parser component
This commit is contained in:
79
src/Parser/Parsing/Impl/CommentRemovingStreamProxy.cpp
Normal file
79
src/Parser/Parsing/Impl/CommentRemovingStreamProxy.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "CommentRemovingStreamProxy.h"
|
||||
|
||||
CommentRemovingStreamProxy::CommentRemovingStreamProxy(IParserLineStream* stream)
|
||||
: m_stream(stream),
|
||||
m_inside_multi_line_comment(false),
|
||||
m_next_line_is_comment(false)
|
||||
{
|
||||
}
|
||||
|
||||
ParserLine CommentRemovingStreamProxy::NextLine()
|
||||
{
|
||||
auto line = m_stream->NextLine();
|
||||
|
||||
if (m_next_line_is_comment)
|
||||
{
|
||||
m_next_line_is_comment = !line.m_line.empty() && line.m_line[line.m_line.size() - 1] == '\\';
|
||||
return ParserLine(line.m_filename, line.m_line_number, std::string());
|
||||
}
|
||||
|
||||
unsigned multiLineCommentStart = 0;
|
||||
for (auto i = 0u; i < line.m_line.size(); i++)
|
||||
{
|
||||
const auto c = line.m_line[i];
|
||||
|
||||
if (m_inside_multi_line_comment)
|
||||
{
|
||||
if (c == '*' && i + 1 < line.m_line.size() && line.m_line[i + 1] == '/')
|
||||
{
|
||||
line.m_line.erase(multiLineCommentStart, i + 2 - multiLineCommentStart);
|
||||
multiLineCommentStart = 0;
|
||||
m_inside_multi_line_comment = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(c == '/' && i + 1 < line.m_line.size())
|
||||
{
|
||||
const auto c1 = line.m_line[i + 1];
|
||||
|
||||
if (c1 == '*')
|
||||
{
|
||||
multiLineCommentStart = i;
|
||||
m_inside_multi_line_comment = true;
|
||||
}
|
||||
else if(c1 == '/')
|
||||
{
|
||||
m_next_line_is_comment = line.m_line[line.m_line.size() - 1] == '\\';
|
||||
line.m_line.erase(i);
|
||||
return line;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(m_inside_multi_line_comment)
|
||||
line.m_line.erase(multiLineCommentStart);
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
bool CommentRemovingStreamProxy::IncludeFile(const std::string& filename)
|
||||
{
|
||||
return m_stream->IncludeFile(filename);
|
||||
}
|
||||
|
||||
void CommentRemovingStreamProxy::PopCurrentFile()
|
||||
{
|
||||
m_stream->PopCurrentFile();
|
||||
}
|
||||
|
||||
bool CommentRemovingStreamProxy::IsOpen() const
|
||||
{
|
||||
return m_stream->IsOpen();
|
||||
}
|
||||
|
||||
bool CommentRemovingStreamProxy::Eof() const
|
||||
{
|
||||
return m_stream->Eof();
|
||||
}
|
Reference in New Issue
Block a user