Merge branch 'parser2' into 2.4.0

This commit is contained in:
Paul Beckingham 2014-10-25 19:43:13 -04:00
commit 07bb370637
9 changed files with 1771 additions and 24 deletions

1588
src/CLI.cpp Normal file

File diff suppressed because it is too large Load diff

110
src/CLI.h Normal file
View file

@ -0,0 +1,110 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2006 - 2014, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// http://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDED_CLI
#define INCLUDED_CLI
#include <string>
#include <vector>
#include <map>
// Represents a single argument.
class A
{
public:
A ();
A (const std::string&, const std::string&);
A (const std::string&, const int);
A (const std::string&, const double);
~A ();
A (const A&);
A& operator= (const A&);
void clear ();
bool hasTag (const std::string&) const;
void tag (const std::string&);
void unTag (const std::string&);
void unTagAll ();
void attribute (const std::string&, const std::string&);
void attribute (const std::string&, const int);
void attribute (const std::string&, const double);
const std::string attribute (const std::string&) const;
void removeAttribute (const std::string&);
const std::string dump () const;
public:
std::string _name;
std::vector <std::string> _tags;
std::map <std::string, std::string> _attributes;
};
// Represents the command line.
class CLI
{
public:
CLI ();
~CLI ();
void alias (const std::string&, const std::string&);
void entity (const std::string&, const std::string&);
void initialize (int, const char**);
void add (const std::string&);
void analyze (bool parse = true);
const std::string getFilter ();
const std::vector <std::string> getWords ();
const std::vector <std::string> getModifications ();
const std::string dump () const;
private:
void aliasExpansion ();
void findOverrides ();
void categorize ();
bool exactMatch (const std::string&, const std::string&) const;
bool canonicalize (std::string&, const std::string&, const std::string&) const;
void desugarTags ();
void desugarAttributes ();
void desugarAttributeModifiers ();
void desugarPatterns ();
void findIDs ();
void findUUIDs ();
void insertIDExpr ();
void desugarPlainArgs ();
void findOperators ();
void insertJunctions ();
void decomposeModAttributes ();
void decomposeModAttributeModifiers ();
void decomposeModTags ();
void decomposeModSubstitutions ();
public:
std::multimap <std::string, std::string> _entities;
std::map <std::string, std::string> _aliases;
std::vector <std::string> _original_args;
std::vector <A> _args;
std::vector <std::pair <int, int> > _id_ranges;
std::vector <std::string> _uuid_list;
};
#endif

View file

@ -5,7 +5,8 @@ include_directories (${CMAKE_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/src/columns
${TASK_INCLUDE_DIRS})
set (task_SRCS Color.cpp Color.h
set (task_SRCS CLI.cpp CLI.h
Color.cpp Color.h
Config.cpp Config.h
Context.cpp Context.h
DOM.cpp DOM.h

View file

@ -108,6 +108,7 @@ int Context::initialize (int argc, const char** argv)
// Scan command line for 'rc:<file>' only.
Parser::getOverrides (argc, argv, rc_file._data);
cli.initialize (argc, argv); // task arg0 arg1 ...
// TASKRC environment variable overrides the command line.
char* override = getenv ("TASKRC");
@ -133,11 +134,27 @@ int Context::initialize (int argc, const char** argv)
// Process 'rc:<file>' command line override.
parser.findOverrides (); // rc:<file> rc.<name>:<value>
parser.getOverrides (home_dir, rc_file); // <-- <file>
/*
home_dir = rc_file;
std::string::size_type last_slash = rc._data.rfind ("/");
if (last_slash != std::string::npos)
home_dir = rc_file.parent ();
else
home_dir = ".";
std::cout << "# home_dir=" << home_dir << "\n";
*/
// The data location, Context::data_dir, is determined from the assumed
// location (~/.task), or set by data.location in the config file, or
// overridden by rc.data.location on the command line.
parser.getDataLocation (data_dir); // <-- rc.data.location=<location>
/*
if (cli._overrides.find ("data.location") != cli._overrides.end ())
data_dir = cli._overrides["data.location"];
else
data_dir = config.get ("data.location");
std::cout << "# data_dir=" << data_dir << "\n";
*/
override = getenv ("TASKDATA");
if (override)
@ -171,35 +188,55 @@ int Context::initialize (int argc, const char** argv)
for (cmd = commands.begin (); cmd != commands.end (); ++cmd)
{
parser.entity ("cmd", cmd->first);
cli.entity ("cmd", cmd->first);
if (cmd->first[0] == '_')
{
parser.entity ("helper", cmd->first);
cli.entity ("helper", cmd->first);
}
if (cmd->second->read_only ())
{
parser.entity ("readcmd", cmd->first);
cli.entity ("readcmd", cmd->first);
}
else
{
parser.entity ("writecmd", cmd->first);
cli.entity ("writecmd", cmd->first);
}
}
// Instantiate built-in column objects.
Column::factory (columns);
std::map <std::string, Column*>::iterator col;
for (col = columns.begin (); col != columns.end (); ++col)
{
parser.entity ("attribute", col->first);
cli.entity ("attribute", col->first);
}
// Entities: Pseudo-attributes. Hard-coded.
parser.entity ("pseudo", "limit");
cli.entity ("pseudo", "limit");
// Entities: Modifiers.
for (unsigned int i = 0; i < NUM_MODIFIER_NAMES; ++i)
{
parser.entity ("modifier", modifierNames[i]);
cli.entity ("modifier", modifierNames[i]);
}
// Entities: Operators.
std::vector <std::string> operators;
Eval::getOperators (operators);
std::vector <std::string>::iterator op;
for (op = operators.begin (); op != operators.end (); ++op)
{
parser.entity ("operator", *op);
cli.entity ("operator", *op);
}
// Now the entities are loaded, parsing may resume.
parser.findBinary (); // <task|tw|t|cal|calendar>
@ -211,6 +248,7 @@ int Context::initialize (int argc, const char** argv)
staticInitialization (); // Decouple code from Context.
parser.parse (); // Parse all elements.
cli.analyze (); // Parse all elements.
tdb2.set_location (data_dir); // Prepare the task database.
@ -752,7 +790,10 @@ void Context::loadAliases ()
std::map <std::string, std::string>::iterator i;
for (i = config.begin (); i != config.end (); ++i)
if (i->first.substr (0, 6) == "alias.")
{
parser.alias (i->first.substr (6), i->second);
cli.alias (i->first.substr (6), i->second);
}
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -38,6 +38,7 @@
#include <File.h>
#include <Directory.h>
#include <Parser.h>
#include <CLI.h>
#include <Timer.h>
class Context
@ -84,6 +85,7 @@ private:
public:
std::string program;
Parser parser;
CLI cli;
std::string home_dir;
File rc_file;
Path data_dir;

View file

@ -74,6 +74,8 @@ void Filter::subset (const std::vector <Task>& input, std::vector <Task>& output
if (context.config.getInteger ("debug.parser") >= 1)
{
context.debug (context.cli.dump ());
Tree* t = context.parser.tree ();
if (t)
context.debug (t->dump ());
@ -121,6 +123,8 @@ void Filter::subset (std::vector <Task>& output)
if (context.config.getInteger ("debug.parser") >= 1)
{
context.debug (context.cli.dump ());
Tree* t = context.parser.tree ();
if (t)
context.debug (t->dump ());
@ -210,24 +214,27 @@ bool Filter::pendingOnly ()
Tree* tree = context.parser.tree ();
// To skip loading completed.data, there should be:
// - 'status:pending'
// - no 'or' operators
// - no 'xor' operators
// - 'status' in filter
// - no 'completed'
// - no 'deleted'
// - no 'xor'
// - no 'or'
int countStatus = 0;
int countPending = 0;
int countId = 0;
int countOr = 0;
int countXor = 0;
std::vector <Tree*>::iterator i;
for (i = tree->_branches.begin (); i != tree->_branches.end (); ++i)
std::vector <A>::iterator a;
for (a = context.cli._args.begin (); a != context.cli._args.end (); ++a)
{
if ((*i)->hasTag ("FILTER"))
if (a->hasTag ("FILTER"))
{
if ((*i)->hasTag ("ID")) ++countId;
if ((*i)->hasTag ("OP") && (*i)->attribute ("raw") == "or") ++countOr;
if ((*i)->hasTag ("OP") && (*i)->attribute ("raw") == "xor") ++countXor;
if ((*i)->hasTag ("ATTRIBUTE") && (*i)->attribute ("name") == "status") ++countStatus;
if ((*i)->hasTag ("ATTRIBUTE") && (*i)->attribute ("raw") == "pending") ++countPending;
if (a->hasTag ("ID")) ++countId;
if (a->hasTag ("OP") && a->attribute ("raw") == "or") ++countOr;
if (a->hasTag ("OP") && a->attribute ("raw") == "xor") ++countXor;
if (a->hasTag ("ATTRIBUTE") && a->attribute ("name") == "status") ++countStatus;
if ( a->attribute ("raw") == "pending") ++countPending;
}
}
@ -240,7 +247,7 @@ bool Filter::pendingOnly ()
return false;
// Only one 'status == pending' is allowed.
if (countStatus == 1 && countPending != 1)
if (countStatus != 1 || countPending != 1)
return false;
return true;

View file

@ -851,7 +851,7 @@ void Parser::findSubstitution ()
}
////////////////////////////////////////////////////////////////////////////////
// +tag
// +tag|-tag
void Parser::findTag ()
{
bool action = true;

View file

@ -1907,9 +1907,13 @@ float Task::urgency_blocking () const
void Task::modify (modType type, bool text_required /* = false */)
{
std::string text = "";
Tree* tree = context.parser.tree ();
if (tree)
if (context.config.getInteger ("debug.parser") >= 1)
{
context.debug (context.cli.dump ());
context.debug (tree->dump ());
}
context.debug ("Task::modify");
std::string label = " MODIFICATION ";

View file

@ -81,15 +81,6 @@ int CmdCustom::execute (std::string& output)
split (sortOrder, reportSort, ',');
validateSortColumns (sortOrder);
/*
TODO Wow, this addition causes memory errors.
// Surround the command-line filter with parentheses, to protect it from
// the 'and' placed between the report filter and the command line filter.
context.parser.captureFirst ("(");
context.parser.captureLast (")");
*/
// Prepend the argument list with those from the report filter.
std::string lexeme;
Lexer::Type type;
@ -97,7 +88,10 @@ int CmdCustom::execute (std::string& output)
lex.ambiguity (false);
std::vector <std::string> filterArgs;
while (lex.token (lexeme, type))
{
filterArgs.push_back (lexeme);
context.cli.add (lexeme);
}
std::vector <std::string>::reverse_iterator arg;
for (arg = filterArgs.rbegin (); arg != filterArgs.rend (); ++ arg)