Expressions

- Fixed some compiler warnings.
- Added DOM detection of primitives: int, double, string.
- Began implementation of DOM task access.
- Implemented support for .startswith, .endswith, .word and .noword.
- Removed obsolete subst.t.cpp.
This commit is contained in:
Paul Beckingham 2011-06-09 22:19:10 -04:00
parent 199bb85d88
commit 2ab24fa08b
8 changed files with 119 additions and 191 deletions

View file

@ -189,15 +189,6 @@ void Arguments::categorize ()
std::vector <std::pair <std::string, std::string> >::iterator arg;
for (arg = this->begin (); arg != this->end (); ++arg)
{
/*
std::cout << "# " << leftJustify (arg->first, 36)
<< " found_command=" << (found_command ? "true " : "false")
<< " found_sequence=" << (found_sequence ? "true " : "false")
<< " found_something_after_sequence=" << (found_something_after_sequence ? "true " : "false")
<< " found_non_sequence=" << (found_non_sequence ? "true " : "false")
<< "\n";
*/
if (!terminated)
{
// Nothing after -- is to be interpreted in any way.
@ -545,7 +536,7 @@ std::vector <std::string> Arguments::list ()
std::vector <std::string> Arguments::operator_list ()
{
std::vector <std::string> all;
for (int i = 0; i < NUM_OPERATORS; ++i)
for (unsigned int i = 0; i < NUM_OPERATORS; ++i)
all.push_back (operators[i].op);
return all;
@ -628,6 +619,7 @@ bool Arguments::is_attr (const std::string& input)
n.getUntilEOS (value) ||
n.depleted ())
{
// TODO Validate and expand attribute name
return true;
}
}
@ -675,6 +667,8 @@ bool Arguments::is_attmod (const std::string& input)
n.getUntilEOS (value) ||
n.depleted ())
{
// TODO Validate and expand attribute name
// TODO Validate and expand modifier name
return true;
}
}

View file

@ -27,6 +27,7 @@
#include <sstream>
#include <Context.h>
#include <Nibbler.h>
#include <text.h>
#include <i18n.h>
#include <DOM.h>
@ -52,28 +53,59 @@ DOM::~DOM ()
}
////////////////////////////////////////////////////////////////////////////////
// TODO <id>. <-- context.tdb2
// TODO <uuid>. <-- context.tdb2
// rc.<name> <-- context.config
// TODO report.<name>. <-- context.reports
// TODO stats.<name> <-- context.stats
// TODO context.<name> <-- args, ...
// DOM Supported References:
// rc.<name>
//
// system.<name> <-- context.system
// system.version
// system.lua.version
// system.os
// context.program
// context.args
// context.width
// context.height
//
// <id>.<?>
// <id>.{entry,start,end,due,until,wait}
// <id>.{entry,start,end,due,until,wait}.year
// <id>.{entry,start,end,due,until,wait}.month
// <id>.{entry,start,end,due,until,wait}.day
// <id>.{entry,start,end,due,until,wait}.hour
// <id>.{entry,start,end,due,until,wait}.minute
// <id>.{entry,start,end,due,until,wait}.second
// <id>.description
// <id>.project
// <id>.priority
// <id>.parent
// <id>.status
// <id>.tags
// <id>.urgency
// <id>.recur
// <id>.depends
//
// <uuid>.<?>
//
// TODO report.<name>. <-- context.reports
// TODO stats.<name> <-- context.stats
//
// system.version
// system.lua.version
// system.os
const std::string DOM::get (const std::string& name)
{
int len = name.length ();
Nibbler n (name);
int id;
std::string uuid;
// Primitives
if (is_primitive (name))
return name;
// rc. --> context.config
if (len > 3 &&
else if (len > 3 &&
name.substr (0, 3) == "rc.")
{
return context.config.get (name.substr (3));
}
// context.*
else if (len > 8 &&
name.substr (0, 8) == "context.")
{
@ -103,8 +135,25 @@ const std::string DOM::get (const std::string& name)
throw std::string ("DOM: Cannot get unrecognized name '") + name + "'.";
}
// TODO <id>.
// TODO <uuid>.
// <id>.<name>
else if (n.getInt (id))
{
if (n.skip ('.'))
{
std::string ref;
n.getUntilEOS (ref);
if (ref == "description")
;
// TODO return task.get ("description");
}
}
// TODO <uuid>.<name>
else if (n.getUUID (uuid))
{
}
// TODO report.
// TODO stats.<name>
@ -167,4 +216,29 @@ void DOM::set (const std::string& name, const std::string& value)
}
////////////////////////////////////////////////////////////////////////////////
// TODO This should return a Variant.
bool DOM::is_primitive (const std::string& input)
{
std::string s;
double d;
int i;
// String?
Nibbler n (input);
if (n.getQuoted ('"', s) && n.depleted ())
return true;
// Number?
n = Nibbler (input);
if (n.getNumber (d) && n.depleted ())
return true;
// Integer?
n = Nibbler (input);
if (n.getInt (i) && n.depleted ())
return true;
return false;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -41,6 +41,7 @@ public:
void set (const std::string&, const std::string&);
private:
bool is_primitive (const std::string&);
};
#endif

View file

@ -39,10 +39,10 @@ extern Context context;
Expression::Expression (Arguments& arguments)
: _original (arguments)
{
expand_sequence ();
to_infix ();
expand_expression ();
to_postfix ();
expand_sequence (); // Convert sequence to expression.
to_infix (); // Old-style to infix.
expand_expression (); // Lex expressions to dom, op tokens.
to_postfix (); // Infix --> Postfix
}
////////////////////////////////////////////////////////////////////////////////
@ -211,6 +211,7 @@ void Expression::expand_attmod (const std::string& input)
// Always quote the value, so that empty values, or values containing spaces
// are preserved.
std::string raw_value = value;
value = "\"" + value + "\"";
if (mod == "before" || mod == "under" || mod == "below")
@ -263,19 +264,27 @@ void Expression::expand_attmod (const std::string& input)
}
else if (mod == "startswith" || mod == "left")
{
// TODO ?
_infix.push_back (std::make_pair (name, "dom"));
_infix.push_back (std::make_pair ("~", "op"));
_infix.push_back (std::make_pair ("^" + raw_value, "rx"));
}
else if (mod == "endswith" || mod == "right")
{
// TODO ?
_infix.push_back (std::make_pair (name, "dom"));
_infix.push_back (std::make_pair ("~", "op"));
_infix.push_back (std::make_pair (raw_value + "$", "rx"));
}
else if (mod == "word")
{
// TODO ?
_infix.push_back (std::make_pair (name, "dom"));
_infix.push_back (std::make_pair ("~", "op"));
_infix.push_back (std::make_pair ("\\b" + raw_value + "\\b", "rx"));
}
else if (mod == "noword")
{
// TODO ?
_infix.push_back (std::make_pair (name, "dom"));
_infix.push_back (std::make_pair ("!~", "op"));
_infix.push_back (std::make_pair ("\\b" + raw_value + "\\b", "rx"));
}
}
@ -363,7 +372,6 @@ void Expression::expand_expression ()
//
// Rules:
// 1. Two adjacent non-operator arguments have an 'and' inserted between them.
// 2. Any argument of type "exp" is lexed and replaced by tokens.
//
void Expression::to_infix ()
{