Commit eeb67160 authored by aggsol's avatar aggsol 🙉

improved header parsing, updated test cases

parent ec1c8fbb
......@@ -47,7 +47,7 @@ bool Parser::parse(std::map<int, Section>& sections)
}
catch(const std::exception& ex)
{
std::cerr << "Error : Line:" << m_lineCounter << " :" << ex.what() << "\n";
std::cerr << "Error in line " << m_lineCounter << " : " << ex.what() << "\n";
return true;
}
m_lineCounter++;
......@@ -69,14 +69,15 @@ void Parser::parseLine(const std::string& line, std::map<int, Section>& sections
bool addLine = true;
State state = State::Start;
std::string value;
unsigned counter = 0;
for(unsigned i=0; i<line.size() && state != State::Done; ++i)
{
char c = line[i];
const char c = line.at(i);
char peek = 0;
if(i<line.size()-1)
{
peek = line[i+1];
peek = line.at(i+1);
}
switch(state)
......@@ -101,6 +102,7 @@ void Parser::parseLine(const std::string& line, std::map<int, Section>& sections
}
else if(c == '=')
{
counter = 1; // already parsed 1
state = State::SectionTitle;
m_lastLine = LineType::Section;
}
......@@ -117,6 +119,7 @@ void Parser::parseLine(const std::string& line, std::map<int, Section>& sections
m_lastLine = LineType::Other;
}
break;
case State::ReferenceStart:
if(c == '<')
{
......@@ -128,6 +131,7 @@ void Parser::parseLine(const std::string& line, std::map<int, Section>& sections
state = State::Start;
}
break;
case State::ReferenceSection:
if(c == '>')
{
......@@ -148,6 +152,7 @@ void Parser::parseLine(const std::string& line, std::map<int, Section>& sections
throw std::runtime_error("Invalid whitespace in section reference");
}
break;
case State::Style:
if(c == '\n' || c == ' ' || c == '\t' || c == '\r')
{
......@@ -172,10 +177,18 @@ void Parser::parseLine(const std::string& line, std::map<int, Section>& sections
addLine = false;
if(c == '=')
{
// NOP
counter++;
if(counter > 3)
{
throw std::runtime_error("Level 2 header expected: ===");
}
}
else if(c == ' ')
{
if(counter != 3)
{
throw std::runtime_error("Level 2 header expected: ===");
}
try
{
int section = std::stoi(line.substr(i));
......@@ -195,9 +208,9 @@ void Parser::parseLine(const std::string& line, std::map<int, Section>& sections
}
}
}
else if(m_currSection != 0)
else
{
throw std::runtime_error("Expected section number");
throw std::runtime_error("Invalid section header");
}
break;
case State::DataStart:
......@@ -271,6 +284,11 @@ void Parser::parseLine(const std::string& line, std::map<int, Section>& sections
}
}
if(state == State::SectionTitle)
{
throw std::runtime_error("Invalid section header");
}
if(state == State::ReferenceSection)
{
throw std::runtime_error("Invalid section reference");
......
......@@ -10,10 +10,21 @@ public:
ParserTest()
: tiny::Unit(__FILE__)
{
tiny::Unit::registerTest(&ParserTest::badheader, "badheader");
tiny::Unit::registerTest(&ParserTest::badref, "badref");
tiny::Unit::registerTest(&ParserTest::classes, "classes");
}
static void badheader()
{
bodhi::Parser parser("../test/badheading.adoc");
std::map<int, bodhi::Section> sections;
bool error = parser.parse(sections);
TINY_ASSERT_OK(error);
}
static void badref()
{
bodhi::Parser parser("../test/badreference.adoc");
......
[[section-1]]
=== 1
blabliblubb
Turn to <<section-2>>
[[section-2]]
===
blablibubb kj asd aw p3o0rlkfsd
\ No newline at end of file
[[section-1]]
== 1
=== 1
[.fixed]
AAA
Turn to <<section-2>>
[[section-2]]
== 2
=== 2
BBB
Turn to <<section-3>>
[[section-3]]
== 3
=== 3
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
[.value-2.dist-3.progress-4]
*The End*
......
// This comment is lost
[[section-1]]
== 1
=== 1
[.fixed]
AAA
Turn to <<section-2>>
[[section-2]]
== 2
=== 2
BBB
Turn to <<section-3>>
[[section-3]]
== 3
=== 3
CCC
Turn to <<section-4>>
[[section-4]]
== 4
=== 4
DDD
Turn to <<section-5>>
[[section-5]]
== 5
=== 5
[.combat.skill-7.stamina-6]
EEE
Fight the Troll
......@@ -32,26 +32,26 @@ Troll [.Skill]#Skill#:7 [.Stamina]#Stamina#:6
Turn to <<section-6>>
[[section-6]]
== 6
=== 6
FFF
Turn to <<section-7>>
[[section-7]]
== 7
=== 7
GGG
Turn to <<section-8>>
// the next sections are not separated by a newline
[[section-8]]
== 8
=== 8
[.fixed]
HHH
Turn to <<section-9>>
[[section-9]]
== 9
=== 9
III
Turn to <<section-10>>
[[section-10]]
== 10
=== 10
JJJ
This is the end
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