Expressions/Arguments

- Converted the arguments parsed from a pair (token/modified-category)
  to a triple (token/type/category)
This commit is contained in:
Paul Beckingham 2011-07-02 11:47:39 -04:00
parent e1aea08f81
commit 42becb9e41
6 changed files with 327 additions and 278 deletions

View file

@ -158,10 +158,10 @@ void Arguments::capture (int argc, const char** argv)
{
std::vector <std::string>::iterator part;
for (part = parts.begin (); part != parts.end (); ++part)
this->push_back (std::make_pair (*part, ""));
this->push_back (Triple (*part, "?", ""));
}
else
this->push_back (std::make_pair (argv[i], ""));
this->push_back (Triple (argv[i], "?", ""));
}
categorize ();
@ -176,10 +176,10 @@ void Arguments::capture (const std::string& arg)
{
std::vector <std::string>::iterator part;
for (part = parts.begin (); part != parts.end (); ++part)
this->push_back (std::make_pair (*part, ""));
this->push_back (Triple (*part, "?", ""));
}
else
this->push_back (std::make_pair (arg, ""));
this->push_back (Triple (arg, "?", ""));
categorize ();
}
@ -189,24 +189,24 @@ void Arguments::capture (const std::string& arg)
void Arguments::capture_first (const std::string& arg)
{
// Break the new argument into parts that comprise a series.
std::vector <std::pair <std::string, std::string> > series;
std::vector <Triple> series;
std::vector <std::string> parts;
if (is_multipart (arg, parts))
{
std::vector <std::string>::iterator part;
for (part = parts.begin (); part != parts.end (); ++part)
series.push_back (std::make_pair (*part, ""));
series.push_back (Triple (*part, "?", ""));
}
else
series.push_back (std::make_pair (arg, ""));
series.push_back (Triple (arg, "?", ""));
// Locate an appropriate place to insert the series. This would be
// immediately after the program and command arguments.
std::vector <std::pair <std::string, std::string> >::iterator position;
std::vector <Triple>::iterator position;
for (position = this->begin (); position != this->end (); ++position)
if (position->second != "program" &&
position->second != "command")
if (position->_third != "program" &&
position->_third != "command")
break;
this->insert (position, series.begin (), series.end ());
@ -238,7 +238,7 @@ void Arguments::append_stdin ()
if (arg == "--")
break;
this->push_back (std::make_pair (arg, ""));
this->push_back (Triple (arg, "?", ""));
something_happened = true;
}
}
@ -269,150 +269,150 @@ void Arguments::categorize ()
// Now categorize every argument.
std::string ignored;
std::vector <std::pair <std::string, std::string> >::iterator arg;
std::vector <Triple>::iterator arg;
for (arg = this->begin (); arg != this->end (); ++arg)
{
if (!terminated)
{
// Nothing after -- is to be interpreted in any way.
if (arg->first == "--")
if (arg->_first == "--")
{
terminated = true;
found_non_sequence = true;
if (found_sequence)
found_something_after_sequence = true;
arg->second = "terminator";
arg->_third = "terminator";
}
// program
else if (arg == this->begin ())
{
arg->second = "program"; // TODO Is this a problem for expressions that do not contain a program name?
arg->_third = "program"; // TODO Is this a problem for expressions that do not contain a program name?
}
// command
else if (!found_command &&
is_command (keywords, arg->first))
is_command (keywords, arg->_first))
{
found_command = true;
found_non_sequence = true;
if (found_sequence)
found_something_after_sequence = true;
arg->second = "command";
arg->_third = "command";
}
// rc:<file>
// Note: This doesn't break a sequence chain.
else if (arg->first.substr (0, 3) == "rc:")
else if (arg->_first.substr (0, 3) == "rc:")
{
arg->second = "rc";
arg->_third = "rc";
}
// rc.<name>:<value>
// Note: This doesn't break a sequence chain.
else if (arg->first.substr (0, 3) == "rc.")
else if (arg->_first.substr (0, 3) == "rc.")
{
arg->second = "override";
arg->_third = "override";
}
// <id>[-<id>][,...]
else if (is_id (arg->first))
else if (is_id (arg->_first))
{
if (!found_something_after_sequence)
{
found_sequence = true;
arg->second = "id";
arg->_third = "id";
}
else
{
arg->second = "word";
arg->_third = "word";
}
}
// <uuid>[,...]
else if (is_uuid (arg->first))
else if (is_uuid (arg->_first))
{
if (!found_something_after_sequence)
{
found_sequence = true;
arg->second = "uuid";
arg->_third = "uuid";
}
else
{
arg->second = "word";
arg->_third = "word";
}
}
// [+-]tag
else if (is_tag (arg->first))
else if (is_tag (arg->_first))
{
found_non_sequence = true;
if (found_sequence)
found_something_after_sequence = true;
arg->second = "tag";
arg->_third = "tag";
}
// <name>.<modifier>:<value>
else if (is_attmod (arg->first))
else if (is_attmod (arg->_first))
{
found_non_sequence = true;
if (found_sequence)
found_something_after_sequence = true;
arg->second = "attmod";
arg->_third = "attmod";
}
// <name>:<value>
else if (is_attr (arg->first))
else if (is_attr (arg->_first))
{
found_non_sequence = true;
if (found_sequence)
found_something_after_sequence = true;
arg->second = "attr";
arg->_third = "attr";
}
// /<from>/<to>/[g]
else if (is_subst (arg->first))
else if (is_subst (arg->_first))
{
found_non_sequence = true;
if (found_sequence)
found_something_after_sequence = true;
arg->second = "subst";
arg->_third = "subst";
}
// /pattern/
else if (enable_patterns && is_pattern (arg->first))
else if (enable_patterns && is_pattern (arg->_first))
{
found_non_sequence = true;
if (found_sequence)
found_something_after_sequence = true;
arg->second = "pattern";
arg->_third = "pattern";
}
// <operator>
else if (is_operator (arg->first))
else if (is_operator (arg->_first))
{
found_non_sequence = true;
if (found_sequence)
found_something_after_sequence = true;
arg->second = "op";
arg->_third = "op";
}
// <expression>
else if (enable_expressions && is_expression (arg->first))
else if (enable_expressions && is_expression (arg->_first))
{
found_non_sequence = true;
if (found_sequence)
found_something_after_sequence = true;
arg->second = "exp";
arg->_third = "exp";
}
// If the type is not known, it is treated as a generic word.
@ -422,7 +422,7 @@ void Arguments::categorize ()
if (found_sequence)
found_something_after_sequence = true;
arg->second = "word";
arg->_third = "word";
}
}
@ -433,7 +433,7 @@ void Arguments::categorize ()
if (found_sequence)
found_something_after_sequence = true;
arg->second = "word";
arg->_third = "word";
}
}
}
@ -444,12 +444,12 @@ void Arguments::rc_override (
File& rc)
{
// Is there an override for rc:<file>?
std::vector <std::pair <std::string, std::string> >::iterator arg;
std::vector <Triple>::iterator arg;
for (arg = this->begin (); arg != this->end (); ++arg)
{
if (arg->second == "rc")
if (arg->_third == "rc")
{
rc = File (arg->first.substr (3));
rc = File (arg->_first.substr (3));
home = rc;
std::string::size_type last_slash = rc.data.rfind ("/");
@ -474,15 +474,15 @@ void Arguments::get_data_location (std::string& data)
data = location;
// Are there any overrides for data.location?
std::vector <std::pair <std::string, std::string> >::iterator arg;
std::vector <Triple>::iterator arg;
for (arg = this->begin (); arg != this->end (); ++arg)
{
if (arg->second == "override")
if (arg->_third == "override")
{
if (arg->first.substr (0, 16) == "rc.data.location" &&
arg->first[16] == ':')
if (arg->_first.substr (0, 16) == "rc.data.location" &&
arg->_first[16] == ':')
{
data = arg->first.substr (17);
data = arg->_first.substr (17);
context.header ("Using alternate data.location " + data);
}
}
@ -497,14 +497,14 @@ void Arguments::get_data_location (std::string& data)
// leaving only the plain args.
void Arguments::apply_overrides ()
{
std::vector <std::pair <std::string, std::string> >::iterator arg;
std::vector <Triple>::iterator arg;
for (arg = this->begin (); arg != this->end (); ++arg)
{
if (arg->second == "override")
if (arg->_third == "override")
{
std::string name;
std::string value;
Nibbler n (arg->first);
Nibbler n (arg->_first);
if (n.getLiteral ("rc.") && // rc.
n.getUntil (':', name) && // xxx
n.skip (':')) // :
@ -515,7 +515,7 @@ void Arguments::apply_overrides ()
context.footnote ("Configuration override rc." + name + ":" + value);
}
else
context.footnote ("Problem with override: " + arg->first);
context.footnote ("Problem with override: " + arg->_first);
}
}
}
@ -528,22 +528,22 @@ void Arguments::resolve_aliases ()
std::vector <std::string> expanded;
bool something = false;
std::vector <std::pair <std::string, std::string> >::iterator arg;
std::vector <Triple>::iterator arg;
for (arg = this->begin (); arg != this->end (); ++arg)
{
std::map <std::string, std::string>::iterator match =
context.aliases.find (arg->first);
context.aliases.find (arg->_first);
if (match != context.aliases.end ())
{
context.debug (std::string ("Arguments::resolve_aliases '")
+ arg->first
+ arg->_first
+ "' --> '"
+ context.aliases[arg->first]
+ context.aliases[arg->_first]
+ "'");
std::vector <std::string> words;
splitq (words, context.aliases[arg->first], ' ');
splitq (words, context.aliases[arg->_first], ' ');
std::vector <std::string>::iterator word;
for (word = words.begin (); word != words.end (); ++word)
@ -552,7 +552,7 @@ void Arguments::resolve_aliases ()
something = true;
}
else
expanded.push_back (arg->first);
expanded.push_back (arg->_first);
}
// Only overwrite if something happened.
@ -561,7 +561,7 @@ void Arguments::resolve_aliases ()
this->clear ();
std::vector <std::string>::iterator e;
for (e = expanded.begin (); e != expanded.end (); ++e)
this->push_back (std::make_pair (*e, ""));
this->push_back (Triple (*e, "?", ""));
categorize ();
}
@ -574,18 +574,18 @@ void Arguments::inject_defaults ()
bool found_sequence = false;
bool found_other = false;
std::vector <std::pair <std::string, std::string> >::iterator arg;
std::vector <Triple>::iterator arg;
for (arg = this->begin (); arg != this->end (); ++arg)
{
if (arg->second == "command")
if (arg->_third == "command")
found_command = true;
else if (arg->second == "id")
else if (arg->_third == "id")
found_sequence = true;
else if (arg->second != "program" &&
arg->second != "override" &&
arg->second != "rc")
else if (arg->_third != "program" &&
arg->_third != "override" &&
arg->_third != "rc")
found_other = true;
}
@ -611,7 +611,7 @@ void Arguments::inject_defaults ()
else if (found_sequence)
{
context.header (STRING_ASSUME_INFO);
push_back (std::make_pair ("information", "command"));
push_back (Triple ("information", "?", "command"));
}
}
}
@ -620,9 +620,9 @@ void Arguments::inject_defaults ()
std::vector <std::string> Arguments::list ()
{
std::vector <std::string> all;
std::vector <std::pair <std::string, std::string> >::iterator i;
for (i = this->begin (); i != this->end (); ++i)
all.push_back (i->first);
std::vector <Triple>::iterator arg;
for (arg = this->begin (); arg != this->end (); ++arg)
all.push_back (arg->_first);
return all;
}
@ -642,13 +642,13 @@ std::string Arguments::combine ()
{
std::string combined;
std::vector <std::pair <std::string, std::string> >::iterator i;
for (i = this->begin (); i != this->end (); ++i)
std::vector <Triple>::iterator arg;
for (arg = this->begin (); arg != this->end (); ++arg)
{
if (i != this->begin ())
if (arg != this->begin ())
combined += " ";
combined += i->first;
combined += arg->_first;
}
return combined;
@ -657,12 +657,12 @@ std::string Arguments::combine ()
////////////////////////////////////////////////////////////////////////////////
bool Arguments::find_command (std::string& command)
{
std::vector <std::pair <std::string, std::string> >::iterator arg;
std::vector <Triple>::iterator arg;
for (arg = this->begin (); arg != this->end (); ++arg)
{
if (arg->second == "command")
if (arg->_third == "command")
{
command = arg->first;
command = arg->_first;
return true;
}
}
@ -673,10 +673,10 @@ bool Arguments::find_command (std::string& command)
////////////////////////////////////////////////////////////////////////////////
std::string Arguments::find_limit ()
{
std::vector <std::pair <std::string, std::string> >::reverse_iterator arg;
std::vector <Triple>::reverse_iterator arg;
for (arg = this->rbegin (); arg != this->rend (); ++arg)
if (arg->first.find ("limit:") != std::string::npos)
return arg->first.substr (6);
if (arg->_first.find ("limit:") != std::string::npos)
return arg->_first.substr (6);
return "";
}
@ -1222,8 +1222,8 @@ bool Arguments::extract_pattern (const std::string& input, std::string& pattern)
//
// The sequence is "1 2".
//
// The first number found in the command line is assumed to be a sequence. If
// there are two sequences, only the first is recognized, for example:
// The _first number found in the command line is assumed to be a sequence. If
// there are two sequences, only the _first is recognized, for example:
//
// 1,2 three 4,5
//
@ -1340,39 +1340,39 @@ Arguments Arguments::extract_read_only_filter ()
{
Arguments filter;
std::vector <std::pair <std::string, std::string> >::iterator i;
for (i = this->begin (); i != this->end (); ++i)
std::vector <Triple>::iterator arg;
for (arg = this->begin (); arg != this->end (); ++arg)
{
// Excluded.
if (i->second == "program" ||
i->second == "command" ||
i->second == "rc" ||
i->second == "override")
if (arg->_third == "program" ||
arg->_third == "command" ||
arg->_third == "rc" ||
arg->_third == "override")
{
;
}
// Included.
else if (i->second == "tag" ||
i->second == "pattern" ||
i->second == "attr" ||
i->second == "attmod" ||
i->second == "id" ||
i->second == "uuid" ||
i->second == "op" ||
i->second == "exp" ||
i->second == "word")
else if (arg->_third == "tag" ||
arg->_third == "pattern" ||
arg->_third == "attr" ||
arg->_third == "attmod" ||
arg->_third == "id" ||
arg->_third == "uuid" ||
arg->_third == "op" ||
arg->_third == "exp" ||
arg->_third == "word")
{
// "limit" is special - it is recognized but not included in filters.
if (i->first.find ("limit:") == std::string::npos)
filter.push_back (*i);
if (arg->_first.find ("limit:") == std::string::npos)
filter.push_back (*arg);
}
// Error.
else
{
// substitution
throw std::string ("A substitution '") + i->first + "' is not allowed "
throw std::string ("A substitution '") + arg->_first + "' is not allowed "
"in a read-only command filter.";
}
}
@ -1385,35 +1385,35 @@ Arguments Arguments::extract_write_filter ()
{
Arguments filter;
std::vector <std::pair <std::string, std::string> >::iterator i;
for (i = this->begin (); i != this->end (); ++i)
std::vector <Triple>::iterator arg;
for (arg = this->begin (); arg != this->end (); ++arg)
{
// Only use args prior to command.
if (i->second == "command")
if (arg->_third == "command")
break;
// Excluded.
else if (i->second == "program" ||
i->second == "rc" ||
i->second == "override")
else if (arg->_third == "program" ||
arg->_third == "rc" ||
arg->_third == "override")
{
;
}
// Included.
else if (i->second == "tag" ||
i->second == "pattern" ||
i->second == "attr" ||
i->second == "attmod" ||
i->second == "id" ||
i->second == "uuid" ||
i->second == "op" ||
i->second == "exp" ||
i->second == "word")
else if (arg->_third == "tag" ||
arg->_third == "pattern" ||
arg->_third == "attr" ||
arg->_third == "attmod" ||
arg->_third == "id" ||
arg->_third == "uuid" ||
arg->_third == "op" ||
arg->_third == "exp" ||
arg->_third == "word")
{
// "limit" is special - it is recognized but not included in filters.
if (i->first.find ("limit:") == std::string::npos)
filter.push_back (*i);
if (arg->_first.find ("limit:") == std::string::npos)
filter.push_back (*arg);
}
// Error.
@ -1421,7 +1421,7 @@ Arguments Arguments::extract_write_filter ()
{
// substitution
throw std::string ("A substitutions '")
+ i->first
+ arg->_first
+ "' is not allowed in a read-only command filter.";
}
}
@ -1435,11 +1435,11 @@ Arguments Arguments::extract_modifications ()
Arguments modifications;
bool seen_command = false;
std::vector <std::pair <std::string, std::string> >::iterator i;
for (i = this->begin (); i != this->end (); ++i)
std::vector <Triple>::iterator arg;
for (arg = this->begin (); arg != this->end (); ++arg)
{
// Only use args after command.
if (i->second == "command")
if (arg->_third == "command")
{
seen_command = true;
}
@ -1447,46 +1447,46 @@ Arguments Arguments::extract_modifications ()
else if (seen_command)
{
// Excluded.
if (i->second == "program" ||
i->second == "rc" ||
i->second == "override")
if (arg->_third == "program" ||
arg->_third == "rc" ||
arg->_third == "override")
{
}
// Included.
else if (i->second == "tag" ||
i->second == "attr" ||
i->second == "subst" ||
i->second == "op" ||
i->second == "word")
else if (arg->_third == "tag" ||
arg->_third == "attr" ||
arg->_third == "subst" ||
arg->_third == "op" ||
arg->_third == "word")
{
// "limit" is special - it is recognized but not included in filters.
if (i->first.find ("limit:") == std::string::npos)
modifications.push_back (*i);
if (arg->_first.find ("limit:") == std::string::npos)
modifications.push_back (*arg);
}
// Error.
else
{
if (i->second == "pattern")
if (arg->_third == "pattern")
throw std::string ("A pattern '")
+ i->first
+ arg->_first
+ "' is not allowed when modifiying a task.";
else if (i->second == "attmod")
else if (arg->_third == "attmod")
throw std::string ("An attribute modifier '")
+ i->first
+ arg->_first
+ "' is not allowed when modifiying a task.";
else if (i->second == "exp")
else if (arg->_third == "exp")
throw std::string ("An expression '")
+ i->first
+ arg->_first
+ "' is not allowed when modifiying a task.";
else if (i->second == "id")
else if (arg->_third == "id")
throw std::string ("A task id cannot be modified.");
else if (i->second == "uuid")
else if (arg->_third == "uuid")
throw std::string ("A task uuid cannot be modified.");
}
}
@ -1545,11 +1545,13 @@ void Arguments::dump (const std::string& label)
view.addRow ();
view.addRow ();
view.addRow ();
for (unsigned int i = 0; i < this->size (); ++i)
{
std::string arg = (*this)[i].first;
std::string category = (*this)[i].second;
std::string arg = (*this)[i]._first;
std::string expanded = (*this)[i]._second;
std::string category = (*this)[i]._third;
Color c;
if (color_map[category].nontrivial ())
@ -1558,7 +1560,8 @@ void Arguments::dump (const std::string& label)
c = color_map["none"];
view.set (0, i, arg, c);
view.set (1, i, category, c);
view.set (1, i, expanded, c);
view.set (2, i, category, c);
}
out << view.render ();

View file

@ -34,7 +34,53 @@
#define ARGUMENTS_SEQUENCE_MAX_RANGE 1000
class Arguments : public std::vector <std::pair <std::string, std::string> >
class Triple
{
public:
Triple (
const std::string& one,
const std::string& two,
const std::string& three)
: _first (one)
, _second (two)
, _third (three)
{
}
Triple (const Triple& other)
{
_first = other._first;
_second = other._second;
_third = other._third;
}
Triple& operator= (const Triple& other)
{
if (this != &other)
{
_first = other._first;
_second = other._second;
_third = other._third;
}
return *this;
}
bool operator== (const Triple& other) const
{
return _first == other._first &&
_second == other._second &&
_third == other._third;
}
public:
std::string _first;
std::string _second;
std::string _third;
};
//class Arguments : public std::vector <std::pair <std::string, std::string> >
class Arguments : public std::vector <Triple>
{
public:
Arguments ();

View file

@ -98,7 +98,7 @@ const std::string DOM::get (const std::string& name)
name.substr (0, 8) == "context.")
{
if (name == "context.program")
return /*_cache[name] =*/ context.args[0].first;
return /*_cache[name] =*/ context.args[0]._first;
else if (name == "context.args")
{

View file

@ -81,15 +81,15 @@ bool Expression::eval (Task& task)
// TODO Build an on-demand regex cache.
std::vector <std::pair <std::string, std::string> >::const_iterator arg;
std::vector <Triple>::const_iterator arg;
for (arg = _args.begin (); arg != _args.end (); ++arg)
{
if (arg->second == "op")
if (arg->_third == "op")
{
// std::cout << "# operator " << arg->first << "\n";
// std::cout << "# operator " << arg->_first << "\n";
// Handle the unary operator first.
if (arg->first == "!")
if (arg->_first == "!")
{
// Are there sufficient arguments?
if (value_stack.size () < 1)
@ -119,7 +119,7 @@ bool Expression::eval (Task& task)
// Are there sufficient arguments?
if (value_stack.size () < 2)
throw std::string ("Error: Insufficient operands for '") + arg->first + "' operator.";
throw std::string ("Error: Insufficient operands for '") + arg->_first + "' operator.";
// rvalue (string, rx, int, number, dom ...).
Variant right (value_stack.back ());
@ -144,7 +144,7 @@ bool Expression::eval (Task& task)
// std::cout << "# left raw=" << left._raw << " type=" << left._type << " value=" << left._string << "\n";
// Now the binary operators.
if (arg->first == "and")
if (arg->_first == "and")
{
// std::cout << "# " << left.dump () << " and " << right.dump () << "\n";
bool result = (left && right);
@ -155,7 +155,7 @@ bool Expression::eval (Task& task)
value_stack.push_back (left);
}
else if (arg->first == "xor")
else if (arg->_first == "xor")
{
// std::cout << "# " << left.dump () << " xor " << right.dump () << "\n";
bool left_bool = left.boolean ();
@ -168,7 +168,7 @@ bool Expression::eval (Task& task)
value_stack.push_back (left);
}
else if (arg->first == "or")
else if (arg->_first == "or")
{
// std::cout << "# " << left.dump () << " or " << right.dump () << "\n";
bool result = (left || right);
@ -179,7 +179,7 @@ bool Expression::eval (Task& task)
value_stack.push_back (left);
}
else if (arg->first == "<=")
else if (arg->_first == "<=")
{
// std::cout << "# " << left.dump () << " <= " << right.dump () << "\n";
bool result = false;
@ -202,7 +202,7 @@ bool Expression::eval (Task& task)
value_stack.push_back (left);
}
else if (arg->first == ">=")
else if (arg->_first == ">=")
{
// std::cout << "# " << left.dump () << " >= " << right.dump () << "\n";
bool result = false;
@ -225,7 +225,7 @@ bool Expression::eval (Task& task)
value_stack.push_back (left);
}
else if (arg->first == "!~")
else if (arg->_first == "!~")
{
// std::cout << "# " << left.dump () << " !~ " << right.dump () << "\n";
bool case_sensitive = context.config.getBoolean ("search.case.sensitive");
@ -246,7 +246,7 @@ bool Expression::eval (Task& task)
value_stack.push_back (left);
}
else if (arg->first == "!=")
else if (arg->_first == "!=")
{
// std::cout << "# " << left.dump () << " != " << right.dump () << "\n";
bool result = (left != right);
@ -257,7 +257,7 @@ bool Expression::eval (Task& task)
value_stack.push_back (left);
}
else if (arg->first == "=")
else if (arg->_first == "=")
{
// std::cout << "# " << left.dump () << " = " << right.dump () << "\n";
bool result = false;
@ -280,7 +280,7 @@ bool Expression::eval (Task& task)
value_stack.push_back (left);
}
else if (arg->first == ">")
else if (arg->_first == ">")
{
// std::cout << "# " << left.dump () << " > " << right.dump () << "\n";
bool result = false;
@ -302,7 +302,7 @@ bool Expression::eval (Task& task)
value_stack.push_back (left);
}
else if (arg->first == "~")
else if (arg->_first == "~")
{
// std::cout << "# " << left.dump () << " ~ " << right.dump () << "\n";
bool case_sensitive = context.config.getBoolean ("search.case.sensitive");
@ -323,31 +323,31 @@ bool Expression::eval (Task& task)
value_stack.push_back (left);
}
else if (arg->first == "*")
else if (arg->_first == "*")
{
left = left * right;
value_stack.push_back (left);
}
else if (arg->first == "/")
else if (arg->_first == "/")
{
left = left / right;
value_stack.push_back (left);
}
else if (arg->first == "+")
else if (arg->_first == "+")
{
left = left + right;
value_stack.push_back (left);
}
else if (arg->first == "-")
else if (arg->_first == "-")
{
left = left - right;
value_stack.push_back (left);
}
else if (arg->first == "<")
else if (arg->_first == "<")
{
// std::cout << "# " << left.dump () << " < " << right.dump () << "\n";
bool result = false;
@ -370,14 +370,14 @@ bool Expression::eval (Task& task)
}
else
throw std::string ("Unsupported operator '") + arg->first + "'.";
throw std::string ("Unsupported operator '") + arg->_first + "'.";
}
// It's not an operator, it's either and lvalue or some form of rvalue.
else
{
Variant operand;
create_variant (operand, arg->first, arg->second);
create_variant (operand, arg->_first, arg->_third);
value_stack.push_back (operand);
}
}
@ -464,14 +464,14 @@ void Expression::expand_sequence ()
// Extract all the components of a sequence.
std::vector <int> ids;
std::vector <std::string> uuids;
std::vector <std::pair <std::string, std::string> >::iterator arg;
std::vector <Triple>::iterator arg;
for (arg = _args.begin (); arg != _args.end (); ++arg)
{
if (arg->second == "id")
Arguments::extract_id (arg->first, ids);
if (arg->_third == "id")
Arguments::extract_id (arg->_first, ids);
else if (arg->second == "uuid")
Arguments::extract_uuid (arg->first, uuids);
else if (arg->_third == "uuid")
Arguments::extract_uuid (arg->_first, uuids);
}
// If there is no sequence, we're done.
@ -508,20 +508,20 @@ void Expression::expand_sequence ()
// Copy everything up to the first id/uuid.
for (arg = _args.begin (); arg != _args.end (); ++arg)
{
if (arg->second == "id" || arg->second == "uuid")
if (arg->_third == "id" || arg->_third == "uuid")
break;
temp.push_back (*arg);
}
// Now insert the new sequence expression.
temp.push_back (std::make_pair (sequence.str (), "exp"));
temp.push_back (Triple (sequence.str (), "?", "exp"));
// Now copy everything after the last id/uuid.
bool found_id = false;
for (arg = _args.begin (); arg != _args.end (); ++arg)
{
if (arg->second == "id" || arg->second == "uuid")
if (arg->_third == "id" || arg->_third == "uuid")
found_id = true;
else if (found_id)
@ -553,47 +553,47 @@ void Expression::expand_tokens ()
time_t t;
// Look at all args.
std::vector <std::pair <std::string, std::string> >::iterator arg;
std::vector <Triple>::iterator arg;
for (arg = _args.begin (); arg != _args.end (); ++arg)
{
if (arg->second == "exp")
if (arg->_third == "exp")
{
// Nibble each arg token by token.
Nibbler n (arg->first);
Nibbler n (arg->_first);
while (! n.depleted ())
{
if (n.getQuoted ('"', s, true) ||
n.getQuoted ('\'', s, true))
temp.push_back (std::make_pair (s, "string"));
temp.push_back (Triple (s, "?", "string"));
else if (n.getQuoted ('/', s, true))
temp.push_back (std::make_pair (s, "pattern"));
temp.push_back (Triple (s, "?", "pattern"));
else if (n.getOneOf (operators, s))
temp.push_back (std::make_pair (s, "op"));
temp.push_back (Triple (s, "?", "op"));
else if (n.getDOM (s))
temp.push_back (std::make_pair (s, "lvalue"));
temp.push_back (Triple (s, "?", "lvalue"));
else if (n.getNumber (d))
temp.push_back (std::make_pair (format (d), "number"));
temp.push_back (Triple (format (d), "?", "number"));
else if (n.getInt (i))
temp.push_back (std::make_pair (format (i), "int"));
temp.push_back (Triple (format (i), "?", "int"));
else if (n.getDateISO (t))
temp.push_back (std::make_pair (Date (t).toISO (), "date"));
temp.push_back (Triple (Date (t).toISO (), "?", "date"));
else if (n.getDate (date_format, t))
temp.push_back (std::make_pair (Date (t).toString (date_format), "date"));
temp.push_back (Triple (Date (t).toString (date_format), "?", "date"));
else
{
if (! n.getUntilWS (s))
n.getUntilEOS (s);
temp.push_back (std::make_pair (s, "?"));
temp.push_back (Triple (s, "?", "?"));
}
n.skipWS ();
@ -622,20 +622,20 @@ void Expression::implicit_and ()
bool delta = false;
std::string previous = "op";
std::vector <std::pair <std::string, std::string> >::iterator arg;
std::vector <Triple>::iterator arg;
for (arg = _args.begin (); arg != _args.end (); ++arg)
{
// Old-style filters need 'and' conjunctions.
if (previous != "op" &&
arg->second != "op")
arg->_third != "op")
{
temp.push_back (std::make_pair ("and", "op"));
temp.push_back (Triple ("and", "?", "op"));
delta = true;
}
// Now insert the adjacent non-operator.
temp.push_back (*arg);
previous = arg->second;
previous = arg->_third;
}
if (delta)
@ -654,18 +654,18 @@ void Expression::expand_tag ()
Arguments temp;
bool delta = false;
std::vector <std::pair <std::string, std::string> >::iterator arg;
std::vector <Triple>::iterator arg;
for (arg = _args.begin (); arg != _args.end (); ++arg)
{
if (arg->second == "tag")
if (arg->_third == "tag")
{
char type;
std::string value;
Arguments::extract_tag (arg->first, type, value);
Arguments::extract_tag (arg->_first, type, value);
temp.push_back (std::make_pair ("tags", "lvalue"));
temp.push_back (std::make_pair (type == '+' ? "~" : "!~", "op"));
temp.push_back (std::make_pair (value, "string"));
temp.push_back (Triple ("tags", "?", "lvalue"));
temp.push_back (Triple (type == '+' ? "~" : "!~", "?", "op"));
temp.push_back (Triple (value, "?", "string"));
delta = true;
}
else
@ -687,17 +687,17 @@ void Expression::expand_pattern ()
Arguments temp;
bool delta = false;
std::vector <std::pair <std::string, std::string> >::iterator arg;
std::vector <Triple>::iterator arg;
for (arg = _args.begin (); arg != _args.end (); ++arg)
{
if (arg->second == "pattern")
if (arg->_third == "pattern")
{
std::string value;
Arguments::extract_pattern (arg->first, value);
Arguments::extract_pattern (arg->_first, value);
temp.push_back (std::make_pair ("description", "lvalue"));
temp.push_back (std::make_pair ("~", "op"));
temp.push_back (std::make_pair (value, "rx"));
temp.push_back (Triple ("description", "?", "lvalue"));
temp.push_back (Triple ("~", "?", "op"));
temp.push_back (Triple (value, "?", "rx"));
delta = true;
}
else
@ -719,24 +719,24 @@ void Expression::expand_attr ()
Arguments temp;
bool delta = false;
std::vector <std::pair <std::string, std::string> >::iterator arg;
std::vector <Triple>::iterator arg;
for (arg = _args.begin (); arg != _args.end (); ++arg)
{
if (arg->second == "attr")
if (arg->_third == "attr")
{
// TODO Canonicalize 'name'.
std::string name;
std::string value;
Arguments::extract_attr (arg->first, name, value);
Arguments::extract_attr (arg->_first, name, value);
Arguments::is_attribute (name, name);
// Always quote the value, so that empty values, or values containing spaces
// are preserved.
value = "\"" + value + "\"";
temp.push_back (std::make_pair (name, "lvalue"));
temp.push_back (std::make_pair ("=", "op"));
temp.push_back (std::make_pair (value, "rvalue"));
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("=", "?", "op"));
temp.push_back (Triple (value, "?", "rvalue"));
delta = true;
}
else
@ -758,16 +758,16 @@ void Expression::expand_attmod ()
Arguments temp;
bool delta = false;
std::vector <std::pair <std::string, std::string> >::iterator arg;
std::vector <Triple>::iterator arg;
for (arg = _args.begin (); arg != _args.end (); ++arg)
{
if (arg->second == "attmod")
if (arg->_third == "attmod")
{
std::string name;
std::string mod;
std::string value;
std::string sense;
Arguments::extract_attmod (arg->first, name, mod, value, sense);
Arguments::extract_attmod (arg->_first, name, mod, value, sense);
Arguments::is_attribute (name, name);
Arguments::is_modifier (mod);
@ -778,75 +778,75 @@ void Expression::expand_attmod ()
if (mod == "before" || mod == "under" || mod == "below")
{
temp.push_back (std::make_pair (name, "lvalue"));
temp.push_back (std::make_pair ("<", "op"));
temp.push_back (std::make_pair (value, "rvalue"));
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("<", "?", "op"));
temp.push_back (Triple (value, "?", "rvalue"));
}
else if (mod == "after" || mod == "over" || mod == "above")
{
temp.push_back (std::make_pair (name, "lvalue"));
temp.push_back (std::make_pair (">", "op"));
temp.push_back (std::make_pair (value, "rvalue"));
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple (">", "?", "op"));
temp.push_back (Triple (value, "?", "rvalue"));
}
else if (mod == "none")
{
temp.push_back (std::make_pair (name, "lvalue"));
temp.push_back (std::make_pair ("==", "op"));
temp.push_back (std::make_pair ("\"\"", "string"));
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("==", "?", "op"));
temp.push_back (Triple ("\"\"", "?", "string"));
}
else if (mod == "any")
{
temp.push_back (std::make_pair (name, "lvalue"));
temp.push_back (std::make_pair ("!=", "op"));
temp.push_back (std::make_pair ("\"\"", "string"));
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("!=", "?", "op"));
temp.push_back (Triple ("\"\"", "?", "string"));
}
else if (mod == "is" || mod == "equals")
{
temp.push_back (std::make_pair (name, "lvalue"));
temp.push_back (std::make_pair ("=", "op"));
temp.push_back (std::make_pair (value, "rvalue"));
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("=", "?", "op"));
temp.push_back (Triple (value, "?", "rvalue"));
}
else if (mod == "isnt" || mod == "not")
{
temp.push_back (std::make_pair (name, "lvalue"));
temp.push_back (std::make_pair ("!=", "op"));
temp.push_back (std::make_pair (value, "rvalue"));
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("!=", "?", "op"));
temp.push_back (Triple (value, "?", "rvalue"));
}
else if (mod == "has" || mod == "contains")
{
temp.push_back (std::make_pair (name, "lvalue"));
temp.push_back (std::make_pair ("~", "op"));
temp.push_back (std::make_pair (value, "rvalue"));
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("~", "?", "op"));
temp.push_back (Triple (value, "?", "rvalue"));
}
else if (mod == "hasnt")
{
temp.push_back (std::make_pair (name, "lvalue"));
temp.push_back (std::make_pair ("!~", "op"));
temp.push_back (std::make_pair (value, "rvalue"));
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("!~", "?", "op"));
temp.push_back (Triple (value, "?", "rvalue"));
}
else if (mod == "startswith" || mod == "left")
{
temp.push_back (std::make_pair (name, "lvalue"));
temp.push_back (std::make_pair ("~", "op"));
temp.push_back (std::make_pair ("^" + raw_value, "rx"));
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("~", "?", "op"));
temp.push_back (Triple ("^" + raw_value, "?", "rx"));
}
else if (mod == "endswith" || mod == "right")
{
temp.push_back (std::make_pair (name, "lvalue"));
temp.push_back (std::make_pair ("~", "op"));
temp.push_back (std::make_pair (raw_value + "$", "rx"));
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("~", "?", "op"));
temp.push_back (Triple (raw_value + "$", "?", "rx"));
}
else if (mod == "word")
{
temp.push_back (std::make_pair (name, "lvalue"));
temp.push_back (std::make_pair ("~", "op"));
temp.push_back (std::make_pair ("\\b" + raw_value + "\\b", "rx"));
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("~", "?", "op"));
temp.push_back (Triple ("\\b" + raw_value + "\\b", "?", "rx"));
}
else if (mod == "noword")
{
temp.push_back (std::make_pair (name, "lvalue"));
temp.push_back (std::make_pair ("!~", "op"));
temp.push_back (std::make_pair ("\\b" + raw_value + "\\b", "rx"));
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("!~", "?", "op"));
temp.push_back (Triple ("\\b" + raw_value + "\\b", "?", "rx"));
}
else
throw std::string ("Error: unrecognized attribute modifier '") + mod + "'.";
@ -872,14 +872,14 @@ void Expression::expand_word ()
Arguments temp;
bool delta = false;
std::vector <std::pair <std::string, std::string> >::iterator arg;
std::vector <Triple>::iterator arg;
for (arg = _args.begin (); arg != _args.end (); ++arg)
{
if (arg->second == "word")
if (arg->_third == "word")
{
temp.push_back (std::make_pair ("description", "lvalue"));
temp.push_back (std::make_pair ("~", "op"));
temp.push_back (std::make_pair ("\"" + arg->first + "\"", "rvalue"));
temp.push_back (Triple ("description", "?", "lvalue"));
temp.push_back (Triple ("~", "?", "op"));
temp.push_back (Triple ("\"" + arg->_first + "\"", "?", "rvalue"));
delta = true;
}
@ -936,17 +936,17 @@ void Expression::postfix ()
int precedence;
char associativity;
std::vector <std::pair <std::string, std::string> >::iterator arg;
std::vector <Triple>::iterator arg;
for (arg = _args.begin (); arg != _args.end (); ++arg)
{
if (arg->first == "(")
if (arg->_first == "(")
{
op_stack.push_back (*arg);
}
else if (arg->first == ")")
else if (arg->_first == ")")
{
while (op_stack.size () > 0 &&
op_stack.back ().first != "(")
op_stack.back ()._first != "(")
{
temp.push_back (op_stack.back ());
op_stack.pop_back ();
@ -961,13 +961,13 @@ void Expression::postfix ()
throw std::string ("Mismatched parentheses in expression");
}
}
else if (Arguments::is_operator (arg->first, type, precedence, associativity))
else if (Arguments::is_operator (arg->_first, type, precedence, associativity))
{
char type2;
int precedence2;
char associativity2;
while (op_stack.size () > 0 &&
Arguments::is_operator (op_stack.back ().first, type2, precedence2, associativity2) &&
Arguments::is_operator (op_stack.back ()._first, type2, precedence2, associativity2) &&
((associativity == 'l' && precedence <= precedence2) ||
(associativity == 'r' && precedence < precedence2)))
{
@ -985,8 +985,8 @@ void Expression::postfix ()
while (op_stack.size () != 0)
{
if (op_stack.back ().first == "(" ||
op_stack.back ().first == ")")
if (op_stack.back ()._first == "(" ||
op_stack.back ()._first == ")")
throw std::string ("Mismatched parentheses in expression");
temp.push_back (op_stack.back ());
@ -1007,9 +1007,9 @@ void Expression::postfix ()
//
bool Expression::is_new_style ()
{
std::vector <std::pair <std::string, std::string> >::iterator arg;
std::vector <Triple>::iterator arg;
for (arg = _args.begin (); arg != _args.end (); ++arg)
if (Arguments::is_symbol_operator (arg->first))
if (Arguments::is_symbol_operator (arg->_first))
return true;
return false;

View file

@ -48,12 +48,12 @@ CmdExec::CmdExec ()
int CmdExec::execute (std::string& output)
{
std::string command_line;
std::vector <std::pair <std::string, std::string> >::iterator arg;
std::vector <Triple>::iterator arg;
for (arg = context.args.begin (); arg != context.args.end (); ++arg)
{
if (arg != context.args.begin () &&
arg->first != "execute")
command_line += arg->first;
arg->_first != "execute")
command_line += arg->_first;
}
return system (command_line.c_str ());

View file

@ -284,16 +284,16 @@ void Command::modify_task (Task& task, Arguments& arguments)
{
std::string description;
std::vector <std::pair <std::string, std::string> >::iterator arg;
std::vector <Triple>::iterator arg;
for (arg = arguments.begin (); arg != arguments.end (); ++arg)
{
// Attributes are essentially name:value pairs, and correspond directly
// to stored attributes.
if (arg->second == "attr")
if (arg->_third == "attr")
{
std::string name;
std::string value;
Arguments::extract_attr (arg->first, name, value);
Arguments::extract_attr (arg->_first, name, value);
// TODO All 'value's must be eval'd first.
@ -324,11 +324,11 @@ void Command::modify_task (Task& task, Arguments& arguments)
// Tags need special handling because they are essentially a vector stored
// in a single string, therefore Task::{add,remove}Tag must be called as
// appropriate.
else if (arg->second == "tag")
else if (arg->_third == "tag")
{
char type;
std::string value;
Arguments::extract_tag (arg->first, type, value);
Arguments::extract_tag (arg->_first, type, value);
if (type == '+')
task.addTag (value);
@ -337,29 +337,29 @@ void Command::modify_task (Task& task, Arguments& arguments)
}
// Words and operators are aggregated into a description.
else if (arg->second == "word" ||
arg->second == "op")
else if (arg->_third == "word" ||
arg->_third == "op")
{
if (description.length ())
description += " ";
description += arg->first;
description += arg->_first;
}
// Substitutions.
else if (arg->second == "subst")
else if (arg->_third == "subst")
{
std::string from;
std::string to;
bool global;
Arguments::extract_subst (arg->first, from, to, global);
Arguments::extract_subst (arg->_first, from, to, global);
task.substitute (from, to, global);
}
// Any additional argument types are indicative of a failure in
// Arguments::extract_modifications.
else
throw format (STRING_CMD_MOD_UNEXPECTED, arg->first);
throw format (STRING_CMD_MOD_UNEXPECTED, arg->_first);
}
// Only update description if one was specified.