Argument Parsing

- Implemented Arguments::extract_read_only_filter to isolate the
  arguments that pertain to read-only command filters
- Implemented Arguments::extract_write_filter to isolate the arguments
  that pertain to write command filters.
- Implemented Arguments::extract_modifications to isolate the arguments
  that pertain to write command modifications.
- Created stubbed Expression object.
- Began integration of Expression and Arguments into commands/CmdCustom.
This commit is contained in:
Paul Beckingham 2011-06-05 02:09:25 -04:00
parent 61e549c80c
commit 68a749ee16
6 changed files with 273 additions and 53 deletions

View file

@ -969,6 +969,150 @@ bool Arguments::extract_tag (
return false; return false;
} }
////////////////////////////////////////////////////////////////////////////////
Arguments Arguments::extract_read_only_filter ()
{
Arguments filter;
std::vector <std::pair <std::string, std::string> >::iterator i;
for (i = this->begin (); i != this->end (); ++i)
{
// Excluded.
if (i->second == "program" ||
i->second == "command" ||
i->second == "rc" ||
i->second == "override")
{
;
}
// Included.
else if (i->second == "tag" ||
i->second == "pattern" ||
i->second == "attribute" ||
i->second == "attmod" ||
i->second == "id" ||
i->second == "uuid" ||
i->second == "word")
{
filter.push_back (*i);
}
// Error.
else
{
// substitution
throw std::string ("A substitutions '") + i->first + "' is not allowed "
"in a read-only command filter.";
}
}
return filter;
}
////////////////////////////////////////////////////////////////////////////////
Arguments Arguments::extract_write_filter ()
{
Arguments filter;
std::vector <std::pair <std::string, std::string> >::iterator i;
for (i = this->begin (); i != this->end (); ++i)
{
// Only use args prior to command.
if (i->second == "command")
break;
// Excluded.
else if (i->second == "program" ||
i->second == "rc" ||
i->second == "override")
{
;
}
// Included.
else if (i->second == "tag" ||
i->second == "pattern" ||
i->second == "attribute" ||
i->second == "attmod" ||
i->second == "id" ||
i->second == "uuid" ||
i->second == "word")
{
filter.push_back (*i);
}
// Error.
else
{
// substitution
throw std::string ("A substitutions '")
+ i->first
+ "' is not allowed in a read-only command filter.";
}
}
return filter;
}
////////////////////////////////////////////////////////////////////////////////
Arguments Arguments::extract_modifications ()
{
Arguments modifications;
bool seen_command = false;
std::vector <std::pair <std::string, std::string> >::iterator i;
for (i = this->begin (); i != this->end (); ++i)
{
// Only use args after command.
if (i->second == "command")
{
seen_command = true;
}
else if (seen_command)
{
// Excluded.
if (i->second == "program" ||
i->second == "rc" ||
i->second == "override")
{
}
// Included.
else if (i->second == "tag" ||
i->second == "attribute" ||
i->second == "substitution" ||
i->second == "word")
{
modifications.push_back (*i);
}
// Error.
else
{
if (i->second == "pattern")
throw std::string ("A pattern '")
+ i->first
+ "' is not allowed when modifiying a task.";
else if (i->second == "attmod")
throw std::string ("Attribute modifiers '")
+ i->first
+ "' are not allowed when modifiying a task.";
else if (i->second == "id")
throw std::string ("A task id cannot be modified.");
else if (i->second == "uuid")
throw std::string ("A task uuid cannot be modified.");
}
}
}
return modifications;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
bool Arguments::valid_modifier (const std::string& modifier) bool Arguments::valid_modifier (const std::string& modifier)
{ {

View file

@ -64,6 +64,7 @@ public:
bool is_uuid (const std::string&); bool is_uuid (const std::string&);
bool is_tag (const std::string&); bool is_tag (const std::string&);
// TODO Decide if these are really useful.
bool extract_attr (const std::string&, std::string&, std::string&); bool extract_attr (const std::string&, std::string&, std::string&);
bool extract_attmod (const std::string&, std::string&, std::string&, std::string&, std::string&); bool extract_attmod (const std::string&, std::string&, std::string&, std::string&, std::string&);
bool extract_subst (const std::string&, std::string&, std::string&, bool&); bool extract_subst (const std::string&, std::string&, std::string&, bool&);
@ -72,11 +73,9 @@ public:
bool extract_uuid (const std::string&, std::vector <std::string>&); bool extract_uuid (const std::string&, std::vector <std::string>&);
bool extract_tag (const std::string&, char&, std::string&); bool extract_tag (const std::string&, char&, std::string&);
/* Arguments extract_read_only_filter ();
void extract_filter (); Arguments extract_write_filter ();
void extract_modifications (); Arguments extract_modifications ();
void extract_words ();
*/
bool valid_modifier (const std::string&); bool valid_modifier (const std::string&);

View file

@ -15,6 +15,7 @@ set (task_SRCS API.cpp API.h
Date.cpp Date.h Date.cpp Date.h
Directory.cpp Directory.h Directory.cpp Directory.h
Duration.cpp Duration.h Duration.cpp Duration.h
Expression.cpp Expression.h
File.cpp File.h File.cpp File.h
Filter.cpp Filter.h Filter.cpp Filter.h
Hooks.cpp Hooks.h Hooks.cpp Hooks.h

66
src/Expression.cpp Normal file
View file

@ -0,0 +1,66 @@
////////////////////////////////////////////////////////////////////////////////
// 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 <Expression.h>
////////////////////////////////////////////////////////////////////////////////
Expression::Expression ()
{
}
////////////////////////////////////////////////////////////////////////////////
Expression::Expression (Arguments& arguments)
{
}
////////////////////////////////////////////////////////////////////////////////
Expression::~Expression ()
{
}
////////////////////////////////////////////////////////////////////////////////
bool Expression::eval (Task& task)
{
return true;
}
////////////////////////////////////////////////////////////////////////////////
void Expression::toInfix ()
{
}
////////////////////////////////////////////////////////////////////////////////
void Expression::toPostfix ()
{
}
////////////////////////////////////////////////////////////////////////////////
void Expression::dump (const std::string& label)
{
}
////////////////////////////////////////////////////////////////////////////////

51
src/Expression.h Normal file
View file

@ -0,0 +1,51 @@
////////////////////////////////////////////////////////////////////////////////
// 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_EXPRESSION
#define INCLUDED_EXPRESSION
#define L10N // Localization complete.
#include <string>
#include <Arguments.h>
#include <Task.h>
class Expression
{
public:
Expression ();
Expression (Arguments&);
~Expression ();
bool eval (Task&);
void dump (const std::string&);
private:
void toInfix ();
void toPostfix ();
};
#endif
////////////////////////////////////////////////////////////////////////////////

View file

@ -31,6 +31,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <Context.h> #include <Context.h>
#include <ViewTask.h> #include <ViewTask.h>
#include <Expression.h>
#include <text.h> #include <text.h>
#include <main.h> #include <main.h>
#include <CmdCustom.h> #include <CmdCustom.h>
@ -81,62 +82,20 @@ int CmdCustom::execute (std::string& output)
split (sortOrder, reportSort, ','); split (sortOrder, reportSort, ',');
validateSortColumns (sortOrder); validateSortColumns (sortOrder);
/*
// Apply rc overrides.
std::vector <std::string> filterArgs;
std::vector <std::string> filteredArgs;
split (filterArgs, reportFilter, ' ');
// context.applyOverrides (filterArgs, filteredArgs);
*/
/*
{
Cmd cmd (_keyword);
Task task;
Sequence sequence;
Subst subst;
Filter filter;
context.parse (filteredArgs, cmd, task, sequence, subst, filter);
context.sequence.combine (sequence);
// Special case: Allow limit to be overridden by the command line.
if (!context.task.has ("limit") && task.has ("limit"))
context.task.set ("limit", task.get ("limit"));
Filter::iterator att;
for (att = filter.begin (); att != filter.end (); ++att)
context.filter.push_back (*att);
}
*/
// Load the data. // Load the data.
// TODO Replace with TDB2.
std::vector <Task> tasks; std::vector <Task> tasks;
context.tdb.lock (context.config.getBoolean ("locking")); context.tdb.lock (context.config.getBoolean ("locking"));
handleRecurrence (); handleRecurrence ();
// context.tdb.load (tasks, context.filter); Filter filter; // Blank
Filter filter; context.tdb.load (tasks, filter); // No filter.
context.tdb.load (tasks, filter); // TODO No filter.
context.tdb.commit (); context.tdb.commit ();
context.tdb.unlock (); context.tdb.unlock ();
/*
// Filter sequence.
if (context.sequence.size ())
context.filter.applySequence (tasks, context.sequence);
*/
//////////////////////////////////// ////////////////////////////////////
// TODO Create the filter Arguments f = context.args.extract_read_only_filter ();
Expression e (f);
//std::vector <std::string> filter_args; // TODO e.apply (tasks);
//context.args.extract_filter (filter_args);
// TODO Apply the filter
// Filter filter (context.args);
// std::vector <Task> filtered;
// thing.eval (filtered, context.args, tasks);
//////////////////////////////////// ////////////////////////////////////
// Sort the tasks. // Sort the tasks.