From 33bb6b6d8538919081faafc642645bcfaaf44d35 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Mon, 4 Jul 2011 11:58:55 -0400 Subject: [PATCH] Expressions - Improved categorization by using the Triple to represent the three (raw/inferred/category) values to aid in extraction. --- src/Arguments.cpp | 49 +++++++++------- src/Arguments.h | 7 +-- src/Expression.cpp | 124 +++++++++++++++++++-------------------- src/Hooks.cpp | 2 +- src/commands/Command.cpp | 7 ++- 5 files changed, 101 insertions(+), 88 deletions(-) diff --git a/src/Arguments.cpp b/src/Arguments.cpp index 9118a4d46..5494e1d0b 100644 --- a/src/Arguments.cpp +++ b/src/Arguments.cpp @@ -158,10 +158,10 @@ void Arguments::capture (int argc, const char** argv) { std::vector ::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 ::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 ::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 ::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 ::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"]; diff --git a/src/Arguments.h b/src/Arguments.h index 985394e6b..1730ec5ea 100644 --- a/src/Arguments.h +++ b/src/Arguments.h @@ -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 > class Arguments : public std::vector { public: diff --git a/src/Expression.cpp b/src/Expression.cpp index 2f3053727..8c820b41b 100644 --- a/src/Expression.cpp +++ b/src/Expression.cpp @@ -84,7 +84,7 @@ bool Expression::eval (Task& task) std::vector ::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 ::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; } diff --git a/src/Hooks.cpp b/src/Hooks.cpp index 8f8a49af7..0cd904f4c 100644 --- a/src/Hooks.cpp +++ b/src/Hooks.cpp @@ -153,7 +153,7 @@ void Hooks::initialize () } } else - context.debug ("Hooks::initialize - hook system off"); + context.debug ("Hooks::initialize --> off"); } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/commands/Command.cpp b/src/commands/Command.cpp index ad111e53c..6d4bc3174 100644 --- a/src/commands/Command.cpp +++ b/src/commands/Command.cpp @@ -264,7 +264,12 @@ void Command::filter (std::vector & input, std::vector & 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);