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.
This commit is contained in:
Paul Beckingham 2011-08-19 22:42:19 -04:00
parent 7dd3e081c7
commit 816b07e868
6 changed files with 190 additions and 112 deletions

View file

@ -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 <std::string> 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 <Arg> 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 <std::string>::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)
}
////////////////////////////////////////////////////////////////////////////////
// <number> followed by either: '\0', ')' ',' or '-'.
// <number> 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)
}
////////////////////////////////////////////////////////////////////////////////
// <integer> followed by either: '\0', ')' ',' or '-'.
// <number> 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);
}

View file

@ -30,56 +30,12 @@
#include <vector>
#include <string>
#include <Arg.h>
#include <Nibbler.h>
#include <File.h>
#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 <Arg>
{
public:

100
src/Arg.cpp Normal file
View file

@ -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 <Arg.h>
#include <Context.h>
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;
}
////////////////////////////////////////////////////////////////////////////////

54
src/Arg.h Normal file
View file

@ -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 <string>
#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
////////////////////////////////////////////////////////////////////////////////

View file

@ -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

View file

@ -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"