Expressions

- Improved categorization by using the Triple to represent the three
  (raw/inferred/category) values to aid in extraction.
This commit is contained in:
Paul Beckingham 2011-07-04 11:58:55 -04:00
parent 2d4ee13a46
commit 33bb6b6d85
5 changed files with 101 additions and 88 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 (Triple (*part, "?", ""));
this->push_back (Triple (*part, "", ""));
}
else
this->push_back (Triple (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 (Triple (*part, "?", ""));
this->push_back (Triple (*part, "", ""));
}
else
this->push_back (Triple (arg, "?", ""));
this->push_back (Triple (arg, "", ""));
categorize ();
}
@ -196,10 +196,10 @@ void Arguments::capture_first (const std::string& arg)
{
std::vector <std::string>::iterator part;
for (part = parts.begin (); part != parts.end (); ++part)
series.push_back (Triple (*part, "?", ""));
series.push_back (Triple (*part, "", ""));
}
else
series.push_back (Triple (arg, "?", ""));
series.push_back (Triple (arg, "", ""));
// Locate an appropriate place to insert the series. This would be
// immediately after the program and command arguments.
@ -238,7 +238,7 @@ void Arguments::append_stdin ()
if (arg == "--")
break;
this->push_back (Triple (arg, "?", ""));
this->push_back (Triple (arg, "", ""));
something_happened = true;
}
}
@ -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 (Triple (*e, "?", ""));
this->push_back (Triple (*e, "", ""));
categorize ();
}
@ -611,7 +611,7 @@ void Arguments::inject_defaults ()
else if (found_sequence)
{
context.header (STRING_ASSUME_INFO);
push_back (Triple ("information", "?", "command"));
push_back (Triple ("information", "", "command"));
}
}
}
@ -1357,8 +1357,7 @@ Arguments Arguments::extract_read_only_filter ()
arg->_third == "pattern" ||
arg->_third == "attr" ||
arg->_third == "attmod" ||
arg->_third == "id" ||
arg->_third == "uuid" ||
arg->_third == "seq" ||
arg->_third == "op" ||
arg->_third == "exp" ||
arg->_third == "word")
@ -1384,13 +1383,14 @@ Arguments Arguments::extract_read_only_filter ()
Arguments Arguments::extract_write_filter ()
{
Arguments filter;
bool before_command = true;
std::vector <Triple>::iterator arg;
for (arg = this->begin (); arg != this->end (); ++arg)
{
// Only use args prior to command.
if (arg->_third == "command")
break;
before_command = false;
// Excluded.
else if (arg->_third == "program" ||
@ -1400,27 +1400,34 @@ Arguments Arguments::extract_write_filter ()
;
}
// Included.
// Included regardless of position.
else if (arg->_third == "seq")
{
filter.push_back (*arg);
}
// Included if prior to command.
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 (arg->_first.find ("limit:") == std::string::npos)
filter.push_back (*arg);
if (before_command)
{
// "limit" is special - it is recognized but not included in filters.
if (arg->_first.find ("limit:") == std::string::npos)
filter.push_back (*arg);
}
}
// Error.
else
{
// substitution
throw std::string ("A substitutions '")
throw std::string ("A substitution '")
+ arg->_first
+ "' is not allowed in a read-only command filter.";
}
@ -1554,7 +1561,9 @@ void Arguments::dump (const std::string& label)
std::string category = (*this)[i]._third;
Color c;
if (color_map[category].nontrivial ())
if (color_map[expanded].nontrivial ())
c = color_map[expanded];
else if (color_map[category].nontrivial ())
c = color_map[category];
else
c = color_map["none"];

View file

@ -74,12 +74,11 @@ public:
}
public:
std::string _first;
std::string _second;
std::string _third;
std::string _first; // Represents token to be evaluated
std::string _second; // Represents progressive token type
std::string _third; // Represent original category
};
//class Arguments : public std::vector <std::pair <std::string, std::string> >
class Arguments : public std::vector <Triple>
{
public:

View file

@ -84,7 +84,7 @@ bool Expression::eval (Task& task)
std::vector <Triple>::const_iterator arg;
for (arg = _args.begin (); arg != _args.end (); ++arg)
{
if (arg->_third == "op")
if (arg->_second == "op")
{
// std::cout << "# operator " << arg->_first << "\n";
@ -377,7 +377,7 @@ bool Expression::eval (Task& task)
else
{
Variant operand;
create_variant (operand, arg->_first, arg->_third);
create_variant (operand, arg->_first, arg->_second);
value_stack.push_back (operand);
}
}
@ -515,7 +515,7 @@ void Expression::expand_sequence ()
}
// Now insert the new sequence expression.
temp.push_back (Triple (sequence.str (), "?", "exp"));
temp.push_back (Triple (sequence.str (), "exp", "seq"));
// Now copy everything after the last id/uuid.
bool found_id = false;
@ -556,7 +556,7 @@ void Expression::expand_tokens ()
std::vector <Triple>::iterator arg;
for (arg = _args.begin (); arg != _args.end (); ++arg)
{
if (arg->_third == "exp")
if (arg->_second == "exp")
{
// Nibble each arg token by token.
Nibbler n (arg->_first);
@ -565,35 +565,35 @@ void Expression::expand_tokens ()
{
if (n.getQuoted ('"', s, true) ||
n.getQuoted ('\'', s, true))
temp.push_back (Triple (s, "?", "string"));
temp.push_back (Triple (s, "string", arg->_third));
else if (n.getQuoted ('/', s, true))
temp.push_back (Triple (s, "?", "pattern"));
temp.push_back (Triple (s, "pattern", arg->_third));
else if (n.getOneOf (operators, s))
temp.push_back (Triple (s, "?", "op"));
temp.push_back (Triple (s, "op", arg->_third));
else if (n.getDOM (s))
temp.push_back (Triple (s, "?", "lvalue"));
temp.push_back (Triple (s, "lvalue", arg->_third));
else if (n.getNumber (d))
temp.push_back (Triple (format (d), "?", "number"));
temp.push_back (Triple (format (d), "number", arg->_third));
else if (n.getInt (i))
temp.push_back (Triple (format (i), "?", "int"));
temp.push_back (Triple (format (i), "int", arg->_third));
else if (n.getDateISO (t))
temp.push_back (Triple (Date (t).toISO (), "?", "date"));
temp.push_back (Triple (Date (t).toISO (), "date", arg->_third));
else if (n.getDate (date_format, t))
temp.push_back (Triple (Date (t).toString (date_format), "?", "date"));
temp.push_back (Triple (Date (t).toString (date_format), "date", arg->_third));
else
{
if (! n.getUntilWS (s))
n.getUntilEOS (s);
temp.push_back (Triple (s, "?", "?"));
temp.push_back (Triple (s, "?", arg->_third));
}
n.skipWS ();
@ -629,7 +629,7 @@ void Expression::implicit_and ()
if (previous != "op" &&
arg->_third != "op")
{
temp.push_back (Triple ("and", "?", "op"));
temp.push_back (Triple ("and", "op", "-"));
delta = true;
}
@ -663,9 +663,9 @@ void Expression::expand_tag ()
std::string value;
Arguments::extract_tag (arg->_first, type, value);
temp.push_back (Triple ("tags", "?", "lvalue"));
temp.push_back (Triple (type == '+' ? "~" : "!~", "?", "op"));
temp.push_back (Triple (value, "?", "string"));
temp.push_back (Triple ("tags", "lvalue", arg->_third));
temp.push_back (Triple (type == '+' ? "~" : "!~", "op", arg->_third));
temp.push_back (Triple (value, "string", arg->_third));
delta = true;
}
else
@ -695,9 +695,9 @@ void Expression::expand_pattern ()
std::string value;
Arguments::extract_pattern (arg->_first, value);
temp.push_back (Triple ("description", "?", "lvalue"));
temp.push_back (Triple ("~", "?", "op"));
temp.push_back (Triple (value, "?", "rx"));
temp.push_back (Triple ("description", "lvalue", arg->_third));
temp.push_back (Triple ("~", "op", arg->_third));
temp.push_back (Triple (value, "rx", arg->_third));
delta = true;
}
else
@ -734,9 +734,9 @@ void Expression::expand_attr ()
// are preserved.
value = "\"" + value + "\"";
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("=", "?", "op"));
temp.push_back (Triple (value, "?", "rvalue"));
temp.push_back (Triple (name, "lvalue", arg->_third));
temp.push_back (Triple ("=", "op", arg->_third));
temp.push_back (Triple (value, "rvalue", arg->_third));
delta = true;
}
else
@ -778,75 +778,75 @@ void Expression::expand_attmod ()
if (mod == "before" || mod == "under" || mod == "below")
{
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("<", "?", "op"));
temp.push_back (Triple (value, "?", "rvalue"));
temp.push_back (Triple (name, "lvalue", arg->_third));
temp.push_back (Triple ("<", "op", arg->_third));
temp.push_back (Triple (value, "rvalue", arg->_third));
}
else if (mod == "after" || mod == "over" || mod == "above")
{
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple (">", "?", "op"));
temp.push_back (Triple (value, "?", "rvalue"));
temp.push_back (Triple (name, "lvalue", arg->_third));
temp.push_back (Triple (">", "op", arg->_third));
temp.push_back (Triple (value, "rvalue", arg->_third));
}
else if (mod == "none")
{
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("==", "?", "op"));
temp.push_back (Triple ("\"\"", "?", "string"));
temp.push_back (Triple (name, "lvalue", arg->_third));
temp.push_back (Triple ("==", "op", arg->_third));
temp.push_back (Triple ("\"\"", "string", arg->_third));
}
else if (mod == "any")
{
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("!=", "?", "op"));
temp.push_back (Triple ("\"\"", "?", "string"));
temp.push_back (Triple (name, "lvalue", arg->_third));
temp.push_back (Triple ("!=", "op", arg->_third));
temp.push_back (Triple ("\"\"", "string", arg->_third));
}
else if (mod == "is" || mod == "equals")
{
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("=", "?", "op"));
temp.push_back (Triple (value, "?", "rvalue"));
temp.push_back (Triple (name, "lvalue", arg->_third));
temp.push_back (Triple ("=", "op", arg->_third));
temp.push_back (Triple (value, "rvalue", arg->_third));
}
else if (mod == "isnt" || mod == "not")
{
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("!=", "?", "op"));
temp.push_back (Triple (value, "?", "rvalue"));
temp.push_back (Triple (name, "lvalue", arg->_third));
temp.push_back (Triple ("!=", "op", arg->_third));
temp.push_back (Triple (value, "rvalue", arg->_third));
}
else if (mod == "has" || mod == "contains")
{
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("~", "?", "op"));
temp.push_back (Triple (value, "?", "rvalue"));
temp.push_back (Triple (name, "lvalue", arg->_third));
temp.push_back (Triple ("~", "op", arg->_third));
temp.push_back (Triple (value, "rvalue", arg->_third));
}
else if (mod == "hasnt")
{
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("!~", "?", "op"));
temp.push_back (Triple (value, "?", "rvalue"));
temp.push_back (Triple (name, "lvalue", arg->_third));
temp.push_back (Triple ("!~", "op", arg->_third));
temp.push_back (Triple (value, "rvalue", arg->_third));
}
else if (mod == "startswith" || mod == "left")
{
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("~", "?", "op"));
temp.push_back (Triple ("^" + raw_value, "?", "rx"));
temp.push_back (Triple (name, "lvalue", arg->_third));
temp.push_back (Triple ("~", "op", arg->_third));
temp.push_back (Triple ("^" + raw_value, "rx", arg->_third));
}
else if (mod == "endswith" || mod == "right")
{
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("~", "?", "op"));
temp.push_back (Triple (raw_value + "$", "?", "rx"));
temp.push_back (Triple (name, "lvalue", arg->_third));
temp.push_back (Triple ("~", "op", arg->_third));
temp.push_back (Triple (raw_value + "$", "rx", arg->_third));
}
else if (mod == "word")
{
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("~", "?", "op"));
temp.push_back (Triple ("\\b" + raw_value + "\\b", "?", "rx"));
temp.push_back (Triple (name, "lvalue", arg->_third));
temp.push_back (Triple ("~", "op", arg->_third));
temp.push_back (Triple ("\\b" + raw_value + "\\b", "rx", arg->_third));
}
else if (mod == "noword")
{
temp.push_back (Triple (name, "?", "lvalue"));
temp.push_back (Triple ("!~", "?", "op"));
temp.push_back (Triple ("\\b" + raw_value + "\\b", "?", "rx"));
temp.push_back (Triple (name, "lvalue", arg->_third));
temp.push_back (Triple ("!~", "op", arg->_third));
temp.push_back (Triple ("\\b" + raw_value + "\\b", "rx", arg->_third));
}
else
throw std::string ("Error: unrecognized attribute modifier '") + mod + "'.";
@ -877,9 +877,9 @@ void Expression::expand_word ()
{
if (arg->_third == "word")
{
temp.push_back (Triple ("description", "?", "lvalue"));
temp.push_back (Triple ("~", "?", "op"));
temp.push_back (Triple ("\"" + arg->_first + "\"", "?", "rvalue"));
temp.push_back (Triple ("description", "lvalue", arg->_third));
temp.push_back (Triple ("~", "op", arg->_third));
temp.push_back (Triple ("\"" + arg->_first + "\"", "rvalue", arg->_third));
delta = true;
}

View file

@ -153,7 +153,7 @@ void Hooks::initialize ()
}
}
else
context.debug ("Hooks::initialize - hook system off");
context.debug ("Hooks::initialize --> off");
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -264,7 +264,12 @@ void Command::filter (std::vector <Task>& input, std::vector <Task>& output)
{
Timer timer ("Command::filter");
Arguments f = context.args.extract_read_only_filter ();
Arguments f;
if (read_only ())
f = context.args.extract_read_only_filter ();
else
f = context.args.extract_write_filter ();
if (f.size ())
{
Expression e (f);