From 816b07e8684e5afbcd77b9280c781e8ef86d01bf Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Fri, 19 Aug 2011 22:42:19 -0400 Subject: [PATCH] Expressions - Refactor - The A3::Arg object is very similar to the E9::Term object, so the two are being merged. First step is to separate A3::Arg into it's own space, then add a _type member. - Added more valid stop characters as terminators for various arg types. - Removed redundant E9 special handling for dates, which is already built in to the Date object. --- src/A3.cpp | 36 ++++++++++------ src/A3.h | 46 +-------------------- src/Arg.cpp | 100 +++++++++++++++++++++++++++++++++++++++++++++ src/Arg.h | 54 ++++++++++++++++++++++++ src/CMakeLists.txt | 1 + src/E9.cpp | 65 +++++------------------------ 6 files changed, 190 insertions(+), 112 deletions(-) create mode 100644 src/Arg.cpp create mode 100644 src/Arg.h diff --git a/src/A3.cpp b/src/A3.cpp index 7c3ec21c3..3f0b715db 100644 --- a/src/A3.cpp +++ b/src/A3.cpp @@ -137,7 +137,7 @@ A3::~A3 () void A3::capture (int argc, const char** argv) { for (int i = 0; i < argc; ++i) - this->push_back (Arg (argv[i], "")); + this->push_back (Arg (argv[i])); } //////////////////////////////////////////////////////////////////////////////// @@ -145,7 +145,7 @@ void A3::capture (int argc, const char** argv) void A3::capture (const std::string& arg) { std::vector parts; - this->push_back (Arg (arg, "")); + this->push_back (Arg (arg)); } //////////////////////////////////////////////////////////////////////////////// @@ -154,7 +154,7 @@ void A3::capture_first (const std::string& arg) { // Break the new argument into parts that comprise a series. std::vector series; - series.push_back (Arg (arg, "")); + series.push_back (Arg (arg)); // Locate an appropriate place to insert the series. This would be // immediately after the program and command arguments. @@ -283,7 +283,7 @@ void A3::append_stdin () if (arg == "--") break; - this->push_back (Arg (arg, "")); + this->push_back (Arg (arg)); } } } @@ -387,7 +387,7 @@ void A3::resolve_aliases () this->clear (); std::vector ::iterator e; for (e = expanded.begin (); e != expanded.end (); ++e) - this->push_back (Arg (*e, "")); + this->push_back (Arg (*e)); expanded.clear (); } @@ -1466,7 +1466,7 @@ bool A3::is_dom (Nibbler& n, std::string& result) } //////////////////////////////////////////////////////////////////////////////// -// A duration may only be followed by '\0', ')' or ' '. +// A duration may only be followed by \0, ), +, -, *, / or ' '. // // This prevents the interpretation of '31st' as a duration ('31s'). bool A3::is_duration (Nibbler& n, std::string& result) @@ -1485,6 +1485,10 @@ bool A3::is_duration (Nibbler& n, std::string& result) char next = n.next (); if (next == '\0' || next == ')' || + next == '+' || + next == '-' || + next == '*' || + next == '/' || next == ' ') { result = n.str ().substr (start, n.cursor () - start); @@ -1649,7 +1653,7 @@ bool A3::is_tag (Nibbler& n, std::string& result) } //////////////////////////////////////////////////////////////////////////////// -// followed by either: '\0', ')' ',' or '-'. +// followed by either: \0, ), +, -, *, /, ' '. // // This prevents the interpretation of '3M' as a number. bool A3::is_number (Nibbler& n, double& d) @@ -1660,8 +1664,11 @@ bool A3::is_number (Nibbler& n, double& d) char next = n.next (); if (next == '\0' || next == ')' || - next == ' ' || - next == '-') + next == '+' || + next == '-' || + next == '*' || + next == '/' || + next == ' ') { return true; } @@ -1673,7 +1680,7 @@ bool A3::is_number (Nibbler& n, double& d) } //////////////////////////////////////////////////////////////////////////////// -// followed by either: '\0', ')' ',' or '-'. +// followed by either: \0, ), +, -, *, /, ' '. // // This prevents the interpretation of '3M' as a number. bool A3::is_integer (Nibbler& n, int& i) @@ -1684,8 +1691,11 @@ bool A3::is_integer (Nibbler& n, int& i) char next = n.next (); if (next == '\0' || next == ')' || - next == ' ' || - next == '-') + next == '+' || + next == '-' || + next == '*' || + next == '/' || + next == ' ') { return true; } @@ -1999,6 +2009,7 @@ void A3::dump (const std::string& label) for (unsigned int i = 0; i < this->size (); ++i) { std::string raw = (*this)[i]._raw; + std::string type = (*this)[i]._type; std::string category = (*this)[i]._category; Color c; @@ -2008,6 +2019,7 @@ void A3::dump (const std::string& label) c = color_map["none"]; view.set (0, i, raw, c); + view.set (1, i, type, c); view.set (2, i, category, c); } diff --git a/src/A3.h b/src/A3.h index 1bc463e33..e558d17c4 100644 --- a/src/A3.h +++ b/src/A3.h @@ -30,56 +30,12 @@ #include #include +#include #include #include #define ARGUMENTS_SEQUENCE_MAX_RANGE 1000 -class Arg -{ -public: - Arg () - : _raw ("") - , _category ("") - { - } - - Arg ( - const std::string& raw, - const std::string& category) - : _raw (raw) - , _category (category) - { - } - - Arg (const Arg& other) - { - _raw = other._raw; - _category = other._category; - } - - Arg& operator= (const Arg& other) - { - if (this != &other) - { - _raw = other._raw; - _category = other._category; - } - - return *this; - } - - bool operator== (const Arg& other) const - { - return _raw == other._raw && - _category == other._category; - } - -public: - std::string _raw; // Raw input token, never modified - std::string _category; // Categorized argument -}; - class A3 : public std::vector { public: diff --git a/src/Arg.cpp b/src/Arg.cpp new file mode 100644 index 000000000..0c049e6d3 --- /dev/null +++ b/src/Arg.cpp @@ -0,0 +1,100 @@ +//////////////////////////////////////////////////////////////////////////////// +// taskwarrior - a command line task list manager. +// +// Copyright 2006 - 2011, Paul Beckingham, Federico Hernandez. +// All rights reserved. +// +// This program is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation; either version 2 of the License, or (at your option) any later +// version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, write to the +// +// Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, +// Boston, MA +// 02110-1301 +// USA +// +//////////////////////////////////////////////////////////////////////////////// + +#include +#include + +extern Context context; + +//////////////////////////////////////////////////////////////////////////////// +Arg::Arg () +: _raw ("") +, _type ("") +, _category ("") +{ +} + +//////////////////////////////////////////////////////////////////////////////// +Arg::Arg (const std::string& raw) +: _raw (raw) +, _type ("") +, _category ("") +{ +} + +//////////////////////////////////////////////////////////////////////////////// +Arg::Arg ( + const std::string& raw, + const std::string& category) +: _raw (raw) +, _type ("") +, _category (category) +{ +} + +//////////////////////////////////////////////////////////////////////////////// +Arg::Arg ( + const std::string& raw, + const std::string& type, + const std::string& category) +: _raw (raw) +, _type (type) +, _category (category) +{ +} + +//////////////////////////////////////////////////////////////////////////////// +Arg::Arg (const Arg& other) +{ + _raw = other._raw; + _type = other._type; + _category = other._category; +} + +//////////////////////////////////////////////////////////////////////////////// +Arg& Arg::operator= (const Arg& other) +{ + if (this != &other) + { + _raw = other._raw; + _type = other._type; + _category = other._category; + } + + return *this; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Arg::operator== (const Arg& other) const +{ + return _raw == other._raw && + _type == other._type && + _category == other._category; +} + +//////////////////////////////////////////////////////////////////////////////// + diff --git a/src/Arg.h b/src/Arg.h new file mode 100644 index 000000000..2160924b3 --- /dev/null +++ b/src/Arg.h @@ -0,0 +1,54 @@ +//////////////////////////////////////////////////////////////////////////////// +// taskwarrior - a command line task list manager. +// +// Copyright 2006 - 2011, Paul Beckingham, Federico Hernandez. +// All rights reserved. +// +// This program is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation; either version 2 of the License, or (at your option) any later +// version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, write to the +// +// Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, +// Boston, MA +// 02110-1301 +// USA +// +//////////////////////////////////////////////////////////////////////////////// +#ifndef INCLUDED_ARG +#define INCLUDED_ARG +#define L10N // Localization complete. + +#include + +#define ARGUMENTS_SEQUENCE_MAX_RANGE 1000 + +class Arg +{ +public: + Arg (); + Arg (const std::string&); + Arg (const std::string&, const std::string&); + Arg (const std::string&, const std::string&, const std::string&); + Arg (const Arg&); + Arg& operator= (const Arg&); + bool operator== (const Arg&) const; + + +public: + std::string _raw; // Raw input token, never modified + std::string _type; // Data type + std::string _category; // Categorized argument +}; + +#endif +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1cf291a5f..0e3c2ac95 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,6 +7,7 @@ include_directories (${CMAKE_SOURCE_DIR} set (task_SRCS A3.cpp A3.h API.cpp API.h + Arg.cpp Arg.h Att.cpp Att.h Color.cpp Color.h Config.cpp Config.h diff --git a/src/E9.cpp b/src/E9.cpp index b85004999..90378046a 100644 --- a/src/E9.cpp +++ b/src/E9.cpp @@ -301,17 +301,8 @@ void E9::operator_lt (Term& result, Term& left, Term& right) else if (left._category == "date" || right._category == "date") { - Date left_date; - if (digitsOnly (left._value)) - left_date = Date (left._value); - else - left_date = Date (left._value, _dateformat); - - Date right_date; - if (digitsOnly (right._value)) - right_date = Date (right._value); - else - right_date = Date (right._value, _dateformat); + Date left_date (left._value, _dateformat); + Date right_date (right._value, _dateformat); result._raw = result._value = (left_date < right_date) ? "true" @@ -343,17 +334,8 @@ void E9::operator_lte (Term& result, Term& left, Term& right) else if (left._category == "date" || right._category == "date") { - Date left_date; - if (digitsOnly (left._value)) - left_date = Date (left._value); - else - left_date = Date (left._value, _dateformat); - - Date right_date; - if (digitsOnly (right._value)) - right_date = Date (right._value); - else - right_date = Date (right._value, _dateformat); + Date left_date (left._value, _dateformat); + Date right_date (right._value, _dateformat); result._raw = result._value = (left_date <= right_date) ? "true" @@ -385,17 +367,8 @@ void E9::operator_gte (Term& result, Term& left, Term& right) else if (left._category == "date" || right._category == "date") { - Date left_date; - if (digitsOnly (left._value)) - left_date = Date (left._value); - else - left_date = Date (left._value, _dateformat); - - Date right_date; - if (digitsOnly (right._value)) - right_date = Date (right._value); - else - right_date = Date (right._value, _dateformat); + Date left_date (left._value, _dateformat); + Date right_date (right._value, _dateformat); result._raw = result._value = (left_date >= right_date) ? "true" @@ -426,17 +399,8 @@ void E9::operator_gt (Term& result, Term& left, Term& right) else if (left._category == "date" || right._category == "date") { - Date left_date; - if (digitsOnly (left._value)) - left_date = Date (left._value); - else - left_date = Date (left._value, _dateformat); - - Date right_date; - if (digitsOnly (right._value)) - right_date = Date (right._value); - else - right_date = Date (right._value, _dateformat); + Date left_date (left._value, _dateformat); + Date right_date (right._value, _dateformat); result._raw = result._value = (left_date > right_date) ? "true" @@ -507,17 +471,8 @@ void E9::operator_equal ( else if (left._category == "date" || right._category == "date") { - Date left_date; - if (digitsOnly (left._value)) - left_date = Date (left._value); - else - left_date = Date (left._value, _dateformat); - - Date right_date; - if (digitsOnly (right._value)) - right_date = Date (right._value); - else - right_date = Date (right._value, _dateformat); + Date left_date (left._value, _dateformat); + Date right_date (right._value, _dateformat); result._raw = result._value = (left_date == right_date) ? "true"