Expressions Refactor - Clarified expansion rules

- Arg now has a _value member that reflects the value in play, rather
  than the input _raw value.  Only _value is used in eval.
- DOM expansion capabilities are now controlled by rc.dom.
This commit is contained in:
Paul Beckingham 2011-08-20 17:06:50 -04:00
parent c6229a6ca6
commit 215364958e
5 changed files with 168 additions and 166 deletions

View file

@ -1107,9 +1107,9 @@ const A3 A3::sequence (const A3& input) const
if (i)
sequenced.push_back (Arg ("or", "op"));
sequenced.push_back (Arg ("id", "string", "dom"));
sequenced.push_back (Arg ("id", "number", "dom"));
sequenced.push_back (Arg ("=", "op"));
sequenced.push_back (Arg (format(ids[i]), "num"));
sequenced.push_back (Arg (format(ids[i]), "number", "literal"));
}
for (unsigned int i = 0; i < uuids.size (); ++i)
@ -1119,7 +1119,7 @@ const A3 A3::sequence (const A3& input) const
sequenced.push_back (Arg ("uuid", "string", "dom"));
sequenced.push_back (Arg ("=", "op"));
sequenced.push_back (Arg (uuids[i], "num"));
sequenced.push_back (Arg (uuids[i], "string", "literal"));
}
sequenced.push_back (Arg (")", "op"));
@ -2029,9 +2029,11 @@ void A3::dump (const std::string& label)
view.addRow ();
view.addRow ();
view.addRow ();
view.addRow ();
for (unsigned int i = 0; i < this->size (); ++i)
{
std::string value = (*this)[i]._value;
std::string raw = (*this)[i]._raw;
std::string type = (*this)[i]._type;
std::string category = (*this)[i]._category;
@ -2042,9 +2044,10 @@ void A3::dump (const std::string& label)
else
c = color_map["none"];
view.set (0, i, raw, c);
view.set (1, i, type, c);
view.set (2, i, category, c);
view.set (0, i, value, c);
view.set (1, i, raw, c);
view.set (2, i, type, c);
view.set (3, i, category, c);
}
out << view.render ();

View file

@ -32,7 +32,8 @@ extern Context context;
////////////////////////////////////////////////////////////////////////////////
Arg::Arg ()
: _raw ("")
: _value ("")
, _raw ("")
, _type ("")
, _category ("")
{
@ -40,7 +41,8 @@ Arg::Arg ()
////////////////////////////////////////////////////////////////////////////////
Arg::Arg (const std::string& raw)
: _raw (raw)
: _value ("")
, _raw (raw)
, _type ("")
, _category ("")
{
@ -50,7 +52,8 @@ Arg::Arg (const std::string& raw)
Arg::Arg (
const std::string& raw,
const std::string& category)
: _raw (raw)
: _value ("")
, _raw (raw)
, _type ("")
, _category (category)
{
@ -61,7 +64,8 @@ Arg::Arg (
const std::string& raw,
const std::string& type,
const std::string& category)
: _raw (raw)
: _value ("")
, _raw (raw)
, _type (type)
, _category (category)
{
@ -70,6 +74,7 @@ Arg::Arg (
////////////////////////////////////////////////////////////////////////////////
Arg::Arg (const Arg& other)
{
_value = other._value;
_raw = other._raw;
_type = other._type;
_category = other._category;
@ -80,6 +85,7 @@ Arg& Arg::operator= (const Arg& other)
{
if (this != &other)
{
_value = other._value;
_raw = other._raw;
_type = other._type;
_category = other._category;
@ -91,7 +97,8 @@ Arg& Arg::operator= (const Arg& other)
////////////////////////////////////////////////////////////////////////////////
bool Arg::operator== (const Arg& other) const
{
return _raw == other._raw &&
return _value == other._value &&
_raw == other._raw &&
_type == other._type &&
_category == other._category;
}

View file

@ -44,6 +44,7 @@ public:
bool operator== (const Arg&) const;
public:
std::string _value; // Interpreted value
std::string _raw; // Raw input token, never modified
std::string _type; // Data type
std::string _category; // Categorized argument

View file

@ -36,7 +36,8 @@ extern Context context;
std::ostream& operator<< (std::ostream& out, const Arg& term)
{
out << term._raw << "|"
out << term._value << "|"
<< term._raw << "|"
<< term._type << "|"
<< term._category;
@ -47,11 +48,9 @@ std::ostream& operator<< (std::ostream& out, const Arg& term)
// Perform all the necessary steps prior to an eval call.
E9::E9 (const A3& args)
{
std::vector <Arg>::const_iterator arg;
for (arg = args.begin (); arg != args.end (); ++arg)
_terms.push_back (Arg (*arg));
_terms = args;
_dateformat = context.config.get ("dateformat");
_dom = context.config.getBoolean ("dom");
}
////////////////////////////////////////////////////////////////////////////////
@ -83,7 +82,7 @@ std::string E9::evalExpression (const Task& task)
std::vector <Arg> value_stack;
eval (task, value_stack);
return value_stack.back ()._raw;
return value_stack.back ()._value;
}
////////////////////////////////////////////////////////////////////////////////
@ -92,7 +91,7 @@ void E9::eval (const Task& task, std::vector <Arg>& value_stack)
// Case sensitivity is configurable.
bool case_sensitive = context.config.getBoolean ("search.case.sensitive");
std::vector <Arg>::iterator arg;
std::vector <Arg>::const_iterator arg;
for (arg = _terms.begin (); arg != _terms.end (); ++arg)
{
if (arg->_category == "op")
@ -108,16 +107,13 @@ void E9::eval (const Task& task, std::vector <Arg>& value_stack)
Arg right = value_stack.back ();
value_stack.pop_back ();
if (right._category == "dom")
{
if (context.config.getBoolean ("dom"))
right._raw = context.dom.get (right._raw, task);
right._category = "string";
}
operator_not (result, right);
}
/*
else if (arg->raw == "-")
{
}
*/
// Binary operators.
else
@ -128,25 +124,9 @@ void E9::eval (const Task& task, std::vector <Arg>& value_stack)
Arg right = value_stack.back ();
value_stack.pop_back ();
if (right._category == "dom")
{
if (context.config.getBoolean ("dom"))
right._raw = context.dom.get (right._raw, task);
right._category = "string";
}
Arg left = value_stack.back ();
value_stack.pop_back ();
if (left._category == "dom")
{
if (context.config.getBoolean ("dom"))
left._raw = context.dom.get (left._raw, task);
left._category = "string";
}
if (arg->_raw == "and") operator_and (result, left, right);
else if (arg->_raw == "or") operator_or (result, left, right);
else if (arg->_raw == "xor") operator_xor (result, left, right);
@ -173,16 +153,31 @@ void E9::eval (const Task& task, std::vector <Arg>& value_stack)
// Operand.
else
{
// Expand the value if possible.
if (arg->_category == "dom")
arg->_raw = context.dom.get (arg->_raw, task);
else if (arg->_category == "date")
arg->_raw = Date (arg->_raw, _dateformat).toEpochString ();
else if (arg->_category == "duration")
arg->_raw = (std::string)Duration (arg->_raw);
Arg operand (*arg);
//std::cout << "# E9::eval operand " << *arg << "\n";
value_stack.push_back (*arg);
// Expand the value if possible.
if (operand._category == "dom" && _dom)
{
operand._value = context.dom.get (operand._raw, task);
operand._category = "literal";
}
else if (operand._type == "date" &&
operand._category == "literal")
{
operand._value = Date (operand._raw, _dateformat).toEpochString ();
operand._category = "literal";
}
else if (operand._type == "duration" &&
operand._category == "literal")
{
operand._value = (std::string)Duration (operand._raw);
operand._category = "literal";
}
else
operand._value = operand._raw;
//std::cout << "# E9::eval operand " << operand << "\n";
value_stack.push_back (operand);
}
}
@ -200,17 +195,17 @@ bool E9::eval_match (Arg& left, Arg& right, bool case_sensitive)
right = coerce (right, "string");
// Create a cached entry, if it does not already exist.
if (_regexes.find (right._raw) == _regexes.end ())
_regexes[right._raw] = RX (right._raw, case_sensitive);
if (_regexes.find (right._value) == _regexes.end ())
_regexes[right._value] = RX (right._value, case_sensitive);
if (_regexes[right._raw].match (left._raw))
if (_regexes[right._value].match (left._value))
return true;
}
else
{
left = coerce (left, "string");
right = coerce (right, "string");
if (find (left._raw, right._raw, (bool) case_sensitive) != std::string::npos)
if (find (left._value, right._value, (bool) case_sensitive) != std::string::npos)
return true;
}
@ -221,63 +216,63 @@ bool E9::eval_match (Arg& left, Arg& right, bool case_sensitive)
void E9::operator_not (Arg& result, Arg& right)
{
// TODO This is not right.
result = Arg (right._raw, "bool", right._category);
result = Arg (right._value, "bool", right._category);
// std::cout << "# not " << right << " --> " << result << "\n";
// std::cout << "# <operator_not> " << right << " --> " << result << "\n";
}
////////////////////////////////////////////////////////////////////////////////
void E9::operator_and (Arg& result, Arg& left, Arg& right)
{
// Assume failure.
result._raw = "false";
result._value = "false";
result._category = "bool";
if (coerce (left, "bool")._raw == "true" &&
coerce (right, "bool")._raw == "true" )
if (coerce (left, "bool")._value == "true" &&
coerce (right, "bool")._value == "true" )
{
result._raw = "true";
result._value = "true";
result._category = "bool";
}
// std::cout << "# " << left << " and " << right << " --> " << result << "\n";
// std::cout << "# " << left << " <operator_and> " << right << " --> " << result << "\n";
}
////////////////////////////////////////////////////////////////////////////////
void E9::operator_or (Arg& result, Arg& left, Arg& right)
{
// Assume failure.
result._raw = "false";
result._value = "false";
result._category = "bool";
if (coerce (left, "bool")._raw == "true" ||
coerce (right, "bool")._raw == "true" )
if (coerce (left, "bool")._value == "true" ||
coerce (right, "bool")._value == "true" )
{
result._raw = "true";
result._value = "true";
result._category = "bool";
}
// std::cout << "# " << left << " or " << right << " --> " << result << "\n";
// std::cout << "# " << left << " <operator_or> " << right << " --> " << result << "\n";
}
////////////////////////////////////////////////////////////////////////////////
void E9::operator_xor (Arg& result, Arg& left, Arg& right)
{
// Assume failure.
result._raw = "false";
result._value = "false";
result._category = "bool";
bool bool_left = coerce (left, "bool")._raw == "true";
bool bool_right = coerce (right, "bool")._raw == "true";
bool bool_left = coerce (left, "bool")._value == "true";
bool bool_right = coerce (right, "bool")._value == "true";
if ((bool_left && !bool_right) ||
(!bool_left && bool_right))
{
result._raw = "true";
result._value = "true";
result._category = "bool";
}
// std::cout << "# " << left << " xor " << right << " --> " << result << "\n";
// std::cout << "# " << left << " <operator_xor> " << right << " --> " << result << "\n";
}
////////////////////////////////////////////////////////////////////////////////
@ -285,31 +280,31 @@ void E9::operator_lt (Arg& result, Arg& left, Arg& right)
{
if (left._raw == "priority")
{
if (left._raw != "H" && right._raw == "H") result._raw = "true";
else if (left._raw == "L" && right._raw == "M") result._raw = "true";
else if (left._raw == "" && right._raw != "") result._raw = "true";
else result._raw = "false";
if (left._value != "H" && right._value == "H") result._value = "true";
else if (left._value == "L" && right._value == "M") result._value = "true";
else if (left._value == "" && right._value != "") result._value = "true";
else result._value = "false";
}
else if (left._category == "date" ||
right._category == "date")
{
Date left_date (left._raw, _dateformat);
Date right_date (right._raw, _dateformat);
Date left_date (left._value, _dateformat);
Date right_date (right._value, _dateformat);
result._raw = result._raw = (left_date < right_date)
? "true"
: "false";
result._value = (left_date < right_date)
? "true"
: "false";
}
else
{
result._raw = result._raw = (left._raw < right._raw)
? "true"
: "false";
result._value = (left._value < right._value)
? "true"
: "false";
}
result._category = "bool";
// std::cout << "# " << left << " < " << right << " --> " << result << "\n";
// std::cout << "# " << left << " <operator_lt> " << right << " --> " << result << "\n";
}
////////////////////////////////////////////////////////////////////////////////
@ -317,32 +312,32 @@ void E9::operator_lte (Arg& result, Arg& left, Arg& right)
{
if (left._raw == "priority")
{
if (left._raw == right._raw ) result._raw = "true";
else if ( right._raw == "H") result._raw = "true";
else if (left._raw == "L" && right._raw == "M") result._raw = "true";
else if (left._raw == "" ) result._raw = "true";
else result._raw = "false";
if (left._value == right._value ) result._value = "true";
else if ( right._value == "H") result._value = "true";
else if (left._value == "L" && right._value == "M") result._value = "true";
else if (left._value == "" ) result._value = "true";
else result._value = "false";
}
else if (left._category == "date" ||
right._category == "date")
{
Date left_date (left._raw, _dateformat);
Date right_date (right._raw, _dateformat);
Date left_date (left._value, _dateformat);
Date right_date (right._value, _dateformat);
result._raw = result._raw = (left_date <= right_date)
? "true"
: "false";
result._value = (left_date <= right_date)
? "true"
: "false";
}
else
{
result._raw = result._raw = (left._raw <= right._raw)
? "true"
: "false";
result._value = (left._value <= right._value)
? "true"
: "false";
}
result._category = "bool";
// std::cout << "# " << left << " <= " << right << " --> " << result << "\n";
// std::cout << "# " << left << " <operator_lte> " << right << " --> " << result << "\n";
}
////////////////////////////////////////////////////////////////////////////////
@ -350,32 +345,32 @@ void E9::operator_gte (Arg& result, Arg& left, Arg& right)
{
if (left._raw == "priority")
{
if (left._raw == right._raw ) result._raw = "true";
else if (left._raw == "H" ) result._raw = "true";
else if (left._raw == "M" && right._raw == "L") result._raw = "true";
else if ( right._raw == "" ) result._raw = "true";
else result._raw = "false";
if (left._value == right._value ) result._value = "true";
else if (left._value == "H" ) result._value = "true";
else if (left._value == "M" && right._value == "L") result._value = "true";
else if ( right._value == "" ) result._value = "true";
else result._value = "false";
}
else if (left._category == "date" ||
right._category == "date")
{
Date left_date (left._raw, _dateformat);
Date right_date (right._raw, _dateformat);
Date left_date (left._value, _dateformat);
Date right_date (right._value, _dateformat);
result._raw = result._raw = (left_date >= right_date)
? "true"
: "false";
result._value = (left_date >= right_date)
? "true"
: "false";
}
else
{
result._raw = result._raw = (left._raw >= right._raw)
? "true"
: "false";
result._value = (left._value >= right._value)
? "true"
: "false";
}
result._category = "bool";
// std::cout << "# " << left << " >= " << right << " --> " << result << "\n";
// std::cout << "# " << left << " <operator_gte> " << right << " --> " << result << "\n";
}
////////////////////////////////////////////////////////////////////////////////
@ -383,31 +378,31 @@ void E9::operator_gt (Arg& result, Arg& left, Arg& right)
{
if (left._raw == "priority")
{
if (left._raw == "H" && right._raw != "H") result._raw = "true";
else if (left._raw == "M" && right._raw == "L") result._raw = "true";
else if (left._raw != "" && right._raw == "") result._raw = "true";
else result._raw = "false";
if (left._value == "H" && right._value != "H") result._value = "true";
else if (left._value == "M" && right._value == "L") result._value = "true";
else if (left._value != "" && right._value == "") result._value = "true";
else result._value = "false";
}
else if (left._category == "date" ||
right._category == "date")
{
Date left_date (left._raw, _dateformat);
Date right_date (right._raw, _dateformat);
Date left_date (left._value, _dateformat);
Date right_date (right._value, _dateformat);
result._raw = result._raw = (left_date > right_date)
? "true"
: "false";
result._value = result._value = (left_date > right_date)
? "true"
: "false";
}
else
{
result._raw = result._raw = (left._raw > right._raw)
? "true"
: "false";
result._value = (left._value > right._value)
? "true"
: "false";
}
result._category = "bool";
// std::cout << "# " << left << " > " << right << " --> " << result << "\n";
// std::cout << "# " << left << " <operator_gt> " << right << " --> " << result << "\n";
}
////////////////////////////////////////////////////////////////////////////////
@ -418,21 +413,14 @@ void E9::operator_inequal (
bool case_sensitive)
{
operator_equal (result, left, right, case_sensitive);
result._raw = result._raw == "false"
? "true"
: "false";
result._value = result._value == "false"
? "true"
: "false";
// std::cout << "# " << left << " != " << right << " --> " << result << "\n";
// std::cout << "# " << left << " <operator_inequal> " << right << " --> " << result << "\n";
}
////////////////////////////////////////////////////////////////////////////////
// if (right._string.length () <= left._string.length ())
// result = compare (right._string,
// left._string.substr (0, right._string.length ()),
// (bool) case_sensitive);
// }
// else
// result = (left == right);
void E9::operator_equal (
Arg& result,
Arg& left,
@ -440,19 +428,20 @@ void E9::operator_equal (
bool case_sensitive)
{
// Assume failure.
result._raw = "false";
result._value = "false";
result._category = "bool";
// 'project' and 'recur' attributes are matched leftmost.
if (left._raw == "project" || left._raw == "recur")
{
// std::cout << "# project/recur matching\n";
coerce (left, "string");
coerce (right, "string");
if (right._raw.length () <= left._raw.length () &&
compare (right._raw,
left._raw.substr (0, right._raw.length ()),
(bool) case_sensitive))
if (right._value.length () <= left._value.length () &&
compare (right._value,
left._value.substr (0, right._value.length ()),
case_sensitive))
{
result._raw = "true";
result._category = "bool";
@ -463,28 +452,31 @@ void E9::operator_equal (
else if (left._category == "date" ||
right._category == "date")
{
Date left_date (left._raw, _dateformat);
Date right_date (right._raw, _dateformat);
// std::cout << "# date matching\n";
Date left_date (left._value, _dateformat);
Date right_date (right._value, _dateformat);
result._raw = (left_date == right_date)
result._value = (left_date == right_date)
? "true"
: "false";
}
// Regular equality matching.
else
{
result._raw = left._raw == right._raw
// std::cout << "# generic matching\n";
result._value = compare (left._value, right._value, case_sensitive)
? "true"
: "false";
if (left._raw == right._raw)
if (left._value == right._value)
{
result._raw = "true";
result._value = "true";
result._category = "bool";
}
}
// std::cout << "# " << left << " = " << right << " --> " << result << "\n";
// std::cout << "# " << left << " <operator_equal> " << right << " --> " << result << "\n";
}
////////////////////////////////////////////////////////////////////////////////
@ -495,12 +487,11 @@ void E9::operator_match (
bool case_sensitive)
{
result._category = "bool";
result._value = eval_match (left, right, case_sensitive)
? "true"
: "false";
result._raw = eval_match (left, right, case_sensitive)
? "true"
: "false";
// std::cout << "# " << left << " ~ " << right << " --> " << result << "\n";
// std::cout << "# " << left << " <operator_match> " << right << " --> " << result << "\n";
}
////////////////////////////////////////////////////////////////////////////////
@ -521,36 +512,35 @@ void E9::operator_nomatch (
bool case_sensitive)
{
result._category = "bool";
result._value = eval_match (left, right, case_sensitive)
? "false"
: "true";
result._raw = eval_match (left, right, case_sensitive)
? "false"
: "true";
// std::cout << "# " << left << " !~ " << right << " --> " << result << "\n";
// std::cout << "# " << left << " <operator_nomatch> " << right << " --> " << result << "\n";
}
////////////////////////////////////////////////////////////////////////////////
void E9::operator_multiply (Arg& result, Arg& left, Arg& right)
{
// std::cout << "# " << left << " * " << right << " --> " << result << "\n";
// std::cout << "# " << left << " <operator_multiply> " << right << " --> " << result << "\n";
}
////////////////////////////////////////////////////////////////////////////////
void E9::operator_divide (Arg& result, Arg& left, Arg& right)
{
// std::cout << "# " << left << " / " << right << " --> " << result << "\n";
// std::cout << "# " << left << " <operator_divide> " << right << " --> " << result << "\n";
}
////////////////////////////////////////////////////////////////////////////////
void E9::operator_add (Arg& result, Arg& left, Arg& right)
{
// std::cout << "# " << left << " + " << right << " --> " << result << "\n";
// std::cout << "# " << left << " <operator_add> " << right << " --> " << result << "\n";
}
////////////////////////////////////////////////////////////////////////////////
void E9::operator_subtract (Arg& result, Arg& left, Arg& right)
{
// std::cout << "# " << left << " - " << right << " --> " << result << "\n";
// std::cout << "# " << left << " <operator_subtract> " << right << " --> " << result << "\n";
}
////////////////////////////////////////////////////////////////////////////////
@ -561,13 +551,13 @@ const Arg E9::coerce (const Arg& input, const std::string& type)
if (type == "bool")
{
result._category = "bool";
result._raw = get_bool (input) ? "true" : "false";
result._value = get_bool (input) ? "true" : "false";
}
if (type == "string")
{
// TODO Convert date?
result._raw = input._raw;
result._value = input._value;
result._category = "string";
}
@ -580,7 +570,7 @@ const Arg E9::coerce (const Arg& input, const std::string& type)
////////////////////////////////////////////////////////////////////////////////
bool E9::get_bool (const Arg& input)
{
std::string value = lowerCase (input._raw);
std::string value = lowerCase (input._value);
if (value == "true" ||
value == "t" ||
value == "1" ||

View file

@ -73,6 +73,7 @@ private:
std::vector <Arg> _terms;
std::map <std::string, RX> _regexes;
std::string _dateformat;
bool _dom;
};
#endif