Commit 7851ffa3 authored by aggsol's avatar aggsol 🙉

check for self reference of sections

parent 33d0d5a8
Pipeline #26881179 failed with stage
in 1 minute and 39 seconds
......@@ -76,7 +76,6 @@ namespace
}
break;
}
last = c;
}
......@@ -156,18 +155,10 @@ void Parser::parseLine(const std::string& line, std::map<int, Section>& sections
{
addLine = false;
state = State::CommentStart;
// if(m_lastLine == LineType::Anchor)
// {
// throw std::runtime_error("Missing section after anchor");
// }
m_lastLine = LineType::Other;
}
else if(c == '[')
{
// if(m_lastLine == LineType::Anchor)
// {
// throw std::runtime_error("Missing section after anchor");
// }
state = State::DataStart;
}
else if(c == '=')
......@@ -203,10 +194,6 @@ void Parser::parseLine(const std::string& line, std::map<int, Section>& sections
}
else
{
// if(m_lastLine == LineType::Anchor)
// {
// throw std::runtime_error("Missing section after anchor");
// }
m_lastLine = LineType::Other;
}
break;
......@@ -231,11 +218,10 @@ void Parser::parseLine(const std::string& line, std::map<int, Section>& sections
{
throw std::runtime_error("Expected '>' closing reference");
}
int section = -1;
try
{
int section = std::stoi(value);
sections[m_currSection].m_children.insert(section);
state = State::Start;
section = std::stoi(value);
}
catch(const std::exception& ex)
{
......@@ -243,6 +229,15 @@ void Parser::parseLine(const std::string& line, std::map<int, Section>& sections
msg << "Invalid section number. value=" << value;
throw std::runtime_error(msg.str());
}
assert(section >= 0);
if(section == m_currSection)
{
throw std::runtime_error("Cannot reference itself");
}
sections[m_currSection].m_children.insert(section);
state = State::Start;
}
else if(std::isdigit(c))
{
......@@ -258,11 +253,6 @@ void Parser::parseLine(const std::string& line, std::map<int, Section>& sections
}
break;
case State::PageBreak:
// NOP?
state = State::Done;
break;
case State::Style:
if(c == '\n' || c == ' ' || c == '\t' || c == '\r')
{
......@@ -299,10 +289,6 @@ void Parser::parseLine(const std::string& line, std::map<int, Section>& sections
try
{
m_currSection = std::stoi(line.substr(i));
// if(section != m_currSection)
// {
// throw std::runtime_error("Mismatch section title and section anchor");
// }
if(counter != 3 && m_currSection > 1)
{
std::cerr << "section=" << m_currSection << "\n";
......@@ -337,9 +323,6 @@ void Parser::parseLine(const std::string& line, std::map<int, Section>& sections
else if(c == '[')
{
throw std::runtime_error("Old style anchors are not supported anymore");
// value.clear();
// addLine = false;
// state = State::Anchor;
}
else
{
......@@ -347,42 +330,6 @@ void Parser::parseLine(const std::string& line, std::map<int, Section>& sections
}
break;
// case State::Anchor:
// if(c == ']')
// {
// if(peek != ']')
// {
// throw std::runtime_error("Expected ']' closing anchor");
// }
// try
// {
// int section = std::stoi(value);
// if(section == m_currSection)
// {
// throw std::runtime_error("Section already defined");
// }
// auto it = sections.find(section);
// if(it != sections.end())
// {
// throw std::runtime_error("Duplicate section");
// }
// sections[section].m_number = section;
// m_currSection = section;
// state = State::Done;
// }
// catch(const std::exception& ex)
// {
// throw std::runtime_error(std::string("Invalid section anchor: ") + ex.what());
// }
// m_lastLine = LineType::Anchor;
// }
// else if(std::isdigit(c))
// {
// value += c;
// }
// break;
case State::CommentStart:
if(c != '/')
{
......@@ -393,6 +340,7 @@ void Parser::parseLine(const std::string& line, std::map<int, Section>& sections
state = State::Done;
}
break;
default:
std::ostringstream msg;
msg << "Unhandled character '" << c << "' Parser State:" << static_cast<int>(state);
......@@ -415,7 +363,6 @@ void Parser::parseLine(const std::string& line, std::map<int, Section>& sections
{
sections.at(m_currSection).m_lines.push_back(line);
}
return;
}
......
......@@ -46,7 +46,6 @@ private:
enum class LineType
{
Other = 0,
// Anchor = 1,
Section = 2,
Image = 3
};
......@@ -61,9 +60,7 @@ private:
SectionTitle = 3,
Style = 4,
ReferenceStart = 5,
ReferenceSection = 6,
// Anchor = 8,
PageBreak = 9
ReferenceSection = 6
};
std::ifstream m_file;
......
......@@ -11,6 +11,7 @@ public:
ParserTest()
: tiny::Unit(__FILE__)
{
tiny::Unit::registerTest(&ParserTest::selfref, "selfref");
tiny::Unit::registerTest(&ParserTest::image, "image");
tiny::Unit::registerTest(&ParserTest::stylesection, "stylesection");
tiny::Unit::registerTest(&ParserTest::badheader, "badheader");
......@@ -18,6 +19,16 @@ public:
tiny::Unit::registerTest(&ParserTest::classes, "classes");
}
static void selfref()
{
bodhi::Parser parser("../test/selfref.adoc");
std::map<int, bodhi::Section> sections;
bool error = parser.parse(sections);
TINY_ASSERT_OK(error);
}
static void image()
{
bodhi::Parser parser("../test/image.adoc");
......
=== 1
<<_1>>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment