Commit ef12dc00 authored by aggsol's avatar aggsol

improved section parsing

parent c15b9fe3
......@@ -65,3 +65,8 @@ Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
[[section-7]]
== 7
This is section 7
[[section-8]]
== 8
This section is unreached
\ No newline at end of file
......@@ -46,7 +46,7 @@ private:
Style = 4,
ReferenceStart = 5,
ReferenceSection = 6,
ReferenceEnd = 7
Anchor = 8
};
};
......@@ -63,6 +63,12 @@ void Parser::parseLine(const std::string& line)
for(unsigned i=0; i<line.size() && state != State::Done; ++i)
{
char c = line[i];
char peek = 0;
if(i<line.size()-1)
{
peek = line[i+1];
}
switch(state)
{
case State::Start:
......@@ -84,11 +90,11 @@ void Parser::parseLine(const std::string& line)
}
break;
case State::ReferenceStart:
if(c == '<')
if(c == '<')
{
state = State::ReferenceSection;
}
else
else // stray '<' is allowed
{
value.clear();
state = State::Start;
......@@ -99,23 +105,17 @@ void Parser::parseLine(const std::string& line)
{
int section = std::stoi(value);
m_sections[m_currSection].m_children.insert(section);
state = State::ReferenceEnd;
if(peek != '>')
{
throw std::runtime_error("Expected '>' closing reference");
}
state = State::Start;
}
else if(std::isdigit(c))
{
value += c;
}
break;
case State::ReferenceEnd:
if(c == '>')
{
state = State::Start;
}
else
{
throw std::runtime_error("Expected '>'");
}
break;
case State::Style:
if(c == '\n' || c == ' ' || c == '\t' || c == '\r')
{
......@@ -165,9 +165,24 @@ void Parser::parseLine(const std::string& line)
}
else if(c == '[')
{
value.clear();
state = State::Anchor;
}
else
{
throw std::runtime_error("Invalid attribute. Missing '[' or '.'");
}
break;
case State::Anchor:
if(c == ']')
{
if(peek != ']')
{
throw std::runtime_error("Expected ']' closing anchor");
}
try
{
int section = std::stoi(line.substr(10));
int section = std::stoi(value);
// std::cout << "line:" << line << "\n"
// << "current section=" << m_currSection << " section=" << section << "\n";
if(section == m_currSection)
......@@ -191,9 +206,9 @@ void Parser::parseLine(const std::string& line)
throw std::runtime_error(std::string("Invalid section anchor: ") + ex.what());
}
}
else
else if(std::isdigit(c))
{
throw std::runtime_error("Invalid attribute. Missing '[' or '.'");
value += c;
}
break;
case State::CommentStart:
......@@ -226,7 +241,7 @@ int main(int argc, char* argv[])
// auto help = args.getOpt("h", "help");
auto input = args.getOpt<std::string>("i", "input", "<missing input file>");
auto dot = args.getOpt("d", "dot");
auto dot = args.getOpt("d", "dot-file");
std::ifstream file(input);
if(not file.is_open())
......@@ -270,7 +285,7 @@ int main(int argc, char* argv[])
throw std::runtime_error("Cannot write dot file");
}
dotfile << "digraph G {\n"
<< " node [style=filled fillcolor=white];";
<< " node [style=filled fillcolor=white];\n";
for(auto& s: parser.sections())
{
......
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