mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-07-07 20:06:36 +02:00
Parser
- ::findIdSequence now safely removes nodes out of the iterator loop.
This commit is contained in:
parent
4f1b63ff02
commit
f898953a2e
1 changed files with 147 additions and 134 deletions
281
src/Parser.cpp
281
src/Parser.cpp
|
@ -1273,159 +1273,172 @@ void Parser::findAttributeModifier ()
|
|||
void Parser::findIdSequence ()
|
||||
{
|
||||
context.debug ("Parser::findIdSequence");
|
||||
bool action = false;
|
||||
bool action = true;
|
||||
|
||||
std::vector <Tree*> nodes;
|
||||
collect (nodes);
|
||||
std::vector <Tree*>::iterator i;
|
||||
for (i = nodes.begin (); i != nodes.end (); ++i)
|
||||
do
|
||||
{
|
||||
// Container for min/max ID ranges.
|
||||
std::vector <std::pair <int, int> > ranges;
|
||||
action = false;
|
||||
|
||||
// Split the ID list into elements.
|
||||
std::string raw = (*i)->attribute ("raw");
|
||||
std::vector <std::string> elements;
|
||||
split (elements, raw, ',');
|
||||
|
||||
bool not_an_id = false;
|
||||
std::vector <std::string>::iterator e;
|
||||
for (e = elements.begin (); e != elements.end (); ++e)
|
||||
std::vector <Tree*> nodes;
|
||||
collect (nodes, collectAll);
|
||||
std::vector <Tree*>::iterator i;
|
||||
for (i = nodes.begin (); i != nodes.end (); ++i)
|
||||
{
|
||||
// Split the ID range into min/max.
|
||||
std::vector <std::string> terms;
|
||||
split (terms, *e, '-');
|
||||
std::string raw = (*i)->attribute ("raw");
|
||||
|
||||
if (terms.size () == 1)
|
||||
if (raw == "--")
|
||||
break;
|
||||
|
||||
if (! (*i)->hasTag ("?"))
|
||||
continue;
|
||||
|
||||
// Container for min/max ID ranges.
|
||||
std::vector <std::pair <int, int> > ranges;
|
||||
|
||||
// Split the ID list into elements.
|
||||
std::vector <std::string> elements;
|
||||
split (elements, raw, ',');
|
||||
|
||||
bool not_an_id = false;
|
||||
std::vector <std::string>::iterator e;
|
||||
for (e = elements.begin (); e != elements.end (); ++e)
|
||||
{
|
||||
if (! digitsOnly (terms[0]))
|
||||
// Split the ID range into min/max.
|
||||
std::vector <std::string> terms;
|
||||
split (terms, *e, '-');
|
||||
|
||||
if (terms.size () == 1)
|
||||
{
|
||||
not_an_id = true;
|
||||
break;
|
||||
if (! digitsOnly (terms[0]))
|
||||
{
|
||||
not_an_id = true;
|
||||
break;
|
||||
}
|
||||
|
||||
Nibbler n (terms[0]);
|
||||
int id;
|
||||
if (n.getUnsignedInt (id) &&
|
||||
n.depleted ())
|
||||
{
|
||||
ranges.push_back (std::pair <int, int> (id, id));
|
||||
}
|
||||
else
|
||||
{
|
||||
not_an_id = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (terms.size () == 2)
|
||||
{
|
||||
if (! digitsOnly (terms[0]) ||
|
||||
! digitsOnly (terms[1]))
|
||||
{
|
||||
not_an_id = true;
|
||||
break;
|
||||
}
|
||||
|
||||
Nibbler n_min (terms[0]);
|
||||
Nibbler n_max (terms[1]);
|
||||
int id_min;
|
||||
int id_max;
|
||||
if (n_min.getUnsignedInt (id_min) &&
|
||||
n_min.depleted () &&
|
||||
n_max.getUnsignedInt (id_max) &&
|
||||
n_max.depleted ())
|
||||
{
|
||||
if (id_min > id_max)
|
||||
throw std::string (STRING_PARSER_RANGE_INVERTED);
|
||||
|
||||
ranges.push_back (std::pair <int, int> (id_min, id_max));
|
||||
}
|
||||
else
|
||||
{
|
||||
not_an_id = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (not_an_id)
|
||||
continue;
|
||||
|
||||
// Now convert the ranges into an infix expression.
|
||||
(*i)->unTag ("?");
|
||||
(*i)->removeAllBranches ();
|
||||
(*i)->tag ("ID");
|
||||
|
||||
Tree* branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", "(");
|
||||
branch->tag ("OP");
|
||||
|
||||
std::vector <std::pair <int, int> >::iterator r;
|
||||
for (r = ranges.begin (); r != ranges.end (); ++r)
|
||||
{
|
||||
if (r != ranges.begin ())
|
||||
{
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", "or");
|
||||
branch->tag ("OP");
|
||||
}
|
||||
|
||||
Nibbler n (terms[0]);
|
||||
int id;
|
||||
if (n.getUnsignedInt (id) &&
|
||||
n.depleted ())
|
||||
if (r->first == r->second)
|
||||
{
|
||||
ranges.push_back (std::pair <int, int> (id, id));
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", "id");
|
||||
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", "==");
|
||||
branch->tag ("OP");
|
||||
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", r->first);
|
||||
}
|
||||
else
|
||||
{
|
||||
not_an_id = true;
|
||||
break;
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", "(");
|
||||
branch->tag ("OP");
|
||||
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", "id");
|
||||
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", ">=");
|
||||
branch->tag ("OP");
|
||||
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", r->first);
|
||||
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", "and");
|
||||
branch->tag ("OP");
|
||||
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", "id");
|
||||
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", "<=");
|
||||
branch->tag ("OP");
|
||||
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", r->second);
|
||||
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", ")");
|
||||
branch->tag ("OP");
|
||||
}
|
||||
}
|
||||
else if (terms.size () == 2)
|
||||
{
|
||||
if (! digitsOnly (terms[0]) ||
|
||||
! digitsOnly (terms[1]))
|
||||
{
|
||||
not_an_id = true;
|
||||
break;
|
||||
}
|
||||
|
||||
Nibbler n_min (terms[0]);
|
||||
Nibbler n_max (terms[1]);
|
||||
int id_min;
|
||||
int id_max;
|
||||
if (n_min.getUnsignedInt (id_min) &&
|
||||
n_min.depleted () &&
|
||||
n_max.getUnsignedInt (id_max) &&
|
||||
n_max.depleted ())
|
||||
{
|
||||
if (id_min > id_max)
|
||||
throw std::string (STRING_PARSER_RANGE_INVERTED);
|
||||
|
||||
ranges.push_back (std::pair <int, int> (id_min, id_max));
|
||||
}
|
||||
else
|
||||
{
|
||||
not_an_id = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", ")");
|
||||
branch->tag ("OP");
|
||||
action = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (not_an_id)
|
||||
continue;
|
||||
|
||||
// Now convert the ranges into an infix expression.
|
||||
(*i)->unTag ("?");
|
||||
(*i)->removeAllBranches ();
|
||||
(*i)->tag ("ID");
|
||||
|
||||
Tree* branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", "(");
|
||||
branch->tag ("OP");
|
||||
|
||||
std::vector <std::pair <int, int> >::iterator r;
|
||||
for (r = ranges.begin (); r != ranges.end (); ++r)
|
||||
{
|
||||
if (r != ranges.begin ())
|
||||
{
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", "or");
|
||||
branch->tag ("OP");
|
||||
}
|
||||
|
||||
if (r->first == r->second)
|
||||
{
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", "id");
|
||||
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", "==");
|
||||
branch->tag ("OP");
|
||||
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", r->first);
|
||||
}
|
||||
else
|
||||
{
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", "(");
|
||||
branch->tag ("OP");
|
||||
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", "id");
|
||||
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", ">=");
|
||||
branch->tag ("OP");
|
||||
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", r->first);
|
||||
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", "and");
|
||||
branch->tag ("OP");
|
||||
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", "id");
|
||||
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", "<=");
|
||||
branch->tag ("OP");
|
||||
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", r->second);
|
||||
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", ")");
|
||||
branch->tag ("OP");
|
||||
}
|
||||
}
|
||||
|
||||
branch = (*i)->addBranch (new Tree ("argSeq"));
|
||||
branch->attribute ("raw", ")");
|
||||
branch->tag ("OP");
|
||||
action = true;
|
||||
}
|
||||
while (action);
|
||||
|
||||
if (action)
|
||||
context.debug (_tree->dump ());
|
||||
context.debug (_tree->dump ());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue