From e0abee7f9f7c23fbeeeceb3345ed90ebcd78bbf7 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Mon, 13 Jun 2011 23:06:54 -0400 Subject: [PATCH] Expressions - Command line arguments are now handled a little differently. Each argument is subjected to further splitting, to break up expressions early in the process, prior to categorization. - Patterns are now treated as quoted string, where the quote character is /. --- src/Arguments.cpp | 59 ++++++++++++++++++++++++++++++++++++++-------- src/Arguments.h | 1 + src/DOM.cpp | 4 +++- src/Expression.cpp | 23 ++++++++++-------- 4 files changed, 66 insertions(+), 21 deletions(-) diff --git a/src/Arguments.cpp b/src/Arguments.cpp index 803d4f257..f765eacd7 100644 --- a/src/Arguments.cpp +++ b/src/Arguments.cpp @@ -148,7 +148,17 @@ Arguments::~Arguments () void Arguments::capture (int argc, const char** argv) { for (int i = 0; i < argc; ++i) - this->push_back (std::make_pair (argv[i], "")); + { + std::vector parts; + if (is_multipart (argv[i], parts)) + { + std::vector ::iterator part; + for (part = parts.begin (); part != parts.end (); ++part) + this->push_back (std::make_pair (*part, "")); + } + else + this->push_back (std::make_pair (argv[i], "")); + } categorize (); } @@ -157,7 +167,16 @@ void Arguments::capture (int argc, const char** argv) // Add a pair with a category of "". void Arguments::capture (const std::string& arg) { - this->push_back (std::make_pair (arg, "")); + std::vector parts; + if (is_multipart (arg, parts)) + { + std::vector ::iterator part; + for (part = parts.begin (); part != parts.end (); ++part) + this->push_back (std::make_pair (*part, "")); + } + else + this->push_back (std::make_pair (arg, "")); + categorize (); } @@ -617,6 +636,26 @@ bool Arguments::find_command (std::string& command) return false; } +//////////////////////////////////////////////////////////////////////////////// +bool Arguments::is_multipart ( + const std::string& input, + std::vector & parts) +{ + parts.clear (); + Nibbler n (input); + std::string part; + while (n.getQuoted ('"', part) || + n.getQuoted ('\'', part) || + n.getQuoted ('/', part) || + n.getUntilWS (part)) + { + n.skipWS (); + parts.push_back (part); + } + + return parts.size () > 1 ? true : false; +} + //////////////////////////////////////////////////////////////////////////////// bool Arguments::is_command ( const std::vector & keywords, @@ -758,12 +797,10 @@ bool Arguments::is_subst (const std::string& input) // // bool Arguments::is_pattern (const std::string& input) { - unsigned int length = input.length (); - - if (input[0] == '/' && - length > 2 && - input[length - 1] == '/' && - input.find ('/', 1) == length - 1) + Nibbler n (input); + std::string pattern; + if (input.length () > 2 && + n.getQuoted ('/', pattern, true)) return true; return false; @@ -974,7 +1011,8 @@ bool Arguments::extract_attr ( { // Both quoted and unquoted Att's are accepted. // Consider removing this for a stricter parse. - if (n.getQuoted ('"', value) || + if (n.getQuoted ('"', value) || + n.getQuoted ('\'', value) || n.getUntilEOS (value)) { return true; @@ -1031,7 +1069,8 @@ bool Arguments::extract_attmod ( { // Both quoted and unquoted Att's are accepted. // Consider removing this for a stricter parse. - if (n.getQuoted ('"', value) || + if (n.getQuoted ('"', value) || + n.getQuoted ('\'', value) || n.getUntilEOS (value)) { return true; diff --git a/src/Arguments.h b/src/Arguments.h index e0e042328..e7d1999c4 100644 --- a/src/Arguments.h +++ b/src/Arguments.h @@ -56,6 +56,7 @@ public: bool find_command (std::string&); + static bool is_multipart (const std::string&, std::vector &); static bool is_command (const std::vector &, std::string&); static bool is_attr (const std::string&); static bool is_attmod (const std::string&); diff --git a/src/DOM.cpp b/src/DOM.cpp index 0a6e3957f..b1e7b38e1 100644 --- a/src/DOM.cpp +++ b/src/DOM.cpp @@ -230,7 +230,9 @@ bool DOM::is_primitive (const std::string& input) // String? Nibbler n (input); - if (n.getQuoted ('"', s) && n.depleted ()) + if ((n.getQuoted ('"', s) || + n.getQuoted ('\'', s)) && + n.depleted ()) return true; // Number? diff --git a/src/Expression.cpp b/src/Expression.cpp index 4cadc10a8..e71468b70 100644 --- a/src/Expression.cpp +++ b/src/Expression.cpp @@ -45,26 +45,26 @@ Expression::Expression (Arguments& arguments) { _args.dump ("Expression::Expression"); - if (is_new_style () && context.config.getBoolean ("expressions")) - { + bool new_style = is_new_style () && context.config.getBoolean ("expressions"); + if (new_style) context.debug ("Filter --> new"); - expand_sequence (); - expand_tokens (); - postfix (); - } else - { context.debug ("Filter --> old"); - expand_sequence (); + + expand_sequence (); + + if (new_style) + { implicit_and (); expand_tag (); expand_pattern (); expand_attr (); expand_attmod (); expand_word (); - expand_tokens (); - postfix (); } + + expand_tokens (); + postfix (); } //////////////////////////////////////////////////////////////////////////////// @@ -226,6 +226,9 @@ void Expression::expand_tokens () n.getQuoted ('\'', s, true)) temp.push_back (std::make_pair (s, "string")); + else if (n.getQuoted ('/', s, true)) + temp.push_back (std::make_pair (s, "pattern")); + else if (n.getOneOf (operators, s)) temp.push_back (std::make_pair (s, "op"));