mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-06-26 10:54:26 +02:00
Parsing
- Integrated modified Nibbler and test code from kronisk. These changes make both test and Nibbler standalone objects, with configurable features.
This commit is contained in:
parent
a262d41828
commit
fb38dca1db
4 changed files with 74 additions and 12 deletions
|
@ -25,20 +25,20 @@
|
||||||
//
|
//
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
#define L10N // Localization complete.
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <Nibbler.h>
|
#include <Nibbler.h>
|
||||||
|
#ifdef NIBBLER_FEATURE_DATE
|
||||||
#include <Date.h>
|
#include <Date.h>
|
||||||
#include <text.h>
|
#endif
|
||||||
|
#ifdef NIBBLER_FEATURE_REGEX
|
||||||
#include <RX.h>
|
#include <RX.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
const char* c_digits = "0123456789";
|
const char* c_digits = "0123456789"; // TODO Not used?
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
Nibbler::Nibbler ()
|
Nibbler::Nibbler ()
|
||||||
|
@ -139,6 +139,7 @@ bool Nibbler::getUntil (const std::string& terminator, std::string& result)
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
#ifdef NIBBLER_FEATURE_REGEX
|
||||||
bool Nibbler::getUntilRx (const std::string& regex, std::string& result)
|
bool Nibbler::getUntilRx (const std::string& regex, std::string& result)
|
||||||
{
|
{
|
||||||
if (_cursor < _length)
|
if (_cursor < _length)
|
||||||
|
@ -162,6 +163,7 @@ bool Nibbler::getUntilRx (const std::string& regex, std::string& result)
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
bool Nibbler::getUntilOneOf (const std::string& chars, std::string& result)
|
bool Nibbler::getUntilOneOf (const std::string& chars, std::string& result)
|
||||||
|
@ -529,6 +531,7 @@ bool Nibbler::getLiteral (const std::string& literal)
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
#ifdef NIBBLER_FEATURE_REGEX
|
||||||
bool Nibbler::getRx (const std::string& regex, std::string& result)
|
bool Nibbler::getRx (const std::string& regex, std::string& result)
|
||||||
{
|
{
|
||||||
if (_cursor < _length)
|
if (_cursor < _length)
|
||||||
|
@ -553,6 +556,7 @@ bool Nibbler::getRx (const std::string& regex, std::string& result)
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
bool Nibbler::getUUID (std::string& result)
|
bool Nibbler::getUUID (std::string& result)
|
||||||
|
@ -685,6 +689,7 @@ bool Nibbler::getDateISO (time_t& t)
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
#ifdef NIBBLER_FEATURE_DATE
|
||||||
bool Nibbler::getDate (const std::string& format, time_t& t)
|
bool Nibbler::getDate (const std::string& format, time_t& t)
|
||||||
{
|
{
|
||||||
std::string::size_type i = _cursor;
|
std::string::size_type i = _cursor;
|
||||||
|
@ -989,6 +994,7 @@ bool Nibbler::getDate (const std::string& format, time_t& t)
|
||||||
_cursor = i;
|
_cursor = i;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// Assumes that the options are sorted by decreasing length, so that if the
|
// Assumes that the options are sorted by decreasing length, so that if the
|
||||||
|
@ -1120,6 +1126,7 @@ bool Nibbler::skipWS ()
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
#ifdef NIBBLER_FEATURE_REGEX
|
||||||
bool Nibbler::skipRx (const std::string& regex)
|
bool Nibbler::skipRx (const std::string& regex)
|
||||||
{
|
{
|
||||||
if (_cursor < _length)
|
if (_cursor < _length)
|
||||||
|
@ -1143,6 +1150,7 @@ bool Nibbler::skipRx (const std::string& regex)
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
bool Nibbler::skipAllOneOf (const std::string& chars)
|
bool Nibbler::skipAllOneOf (const std::string& chars)
|
||||||
|
@ -1219,6 +1227,21 @@ bool Nibbler::depleted ()
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Override of ispunct, that considers #, $ and @ not to be punctuation.
|
||||||
|
//
|
||||||
|
// ispunct: ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~
|
||||||
|
// Punctuation: ! " % & ' ( ) * + , - . / : ; < = > ? [ \ ] ^ _ ` { | } ~
|
||||||
|
// delta: # $ @
|
||||||
|
//
|
||||||
|
bool Nibbler::isPunctuation (char c)
|
||||||
|
{
|
||||||
|
if (c == '@' || c == '#' || c == '$')
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return ispunct (c);
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
std::string Nibbler::dump ()
|
std::string Nibbler::dump ()
|
||||||
{
|
{
|
||||||
|
|
|
@ -29,6 +29,12 @@
|
||||||
#define INCLUDED_NIBBLER
|
#define INCLUDED_NIBBLER
|
||||||
#define L10N // Localization complete.
|
#define L10N // Localization complete.
|
||||||
|
|
||||||
|
#define NIBBLER_FEATURE_DATE
|
||||||
|
//#undef NIBBLER_FEATURE_DATE
|
||||||
|
|
||||||
|
#define NIBBLER_FEATURE_REGEX
|
||||||
|
//#undef NIBBLER_FEATURE_REGEX
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
@ -44,7 +50,9 @@ public:
|
||||||
|
|
||||||
bool getUntil (char, std::string&);
|
bool getUntil (char, std::string&);
|
||||||
bool getUntil (const std::string&, std::string&);
|
bool getUntil (const std::string&, std::string&);
|
||||||
|
#ifdef NIBBLER_FEATURE_REGEX
|
||||||
bool getUntilRx (const std::string&, std::string&);
|
bool getUntilRx (const std::string&, std::string&);
|
||||||
|
#endif
|
||||||
bool getUntilOneOf (const std::string&, std::string&);
|
bool getUntilOneOf (const std::string&, std::string&);
|
||||||
bool getUntilWS (std::string&);
|
bool getUntilWS (std::string&);
|
||||||
bool getUntilEOL (std::string&);
|
bool getUntilEOL (std::string&);
|
||||||
|
@ -64,10 +72,14 @@ public:
|
||||||
bool getNumber (double&);
|
bool getNumber (double&);
|
||||||
bool getUnsignedNumber (double&);
|
bool getUnsignedNumber (double&);
|
||||||
bool getLiteral (const std::string&);
|
bool getLiteral (const std::string&);
|
||||||
|
#ifdef NIBBLER_FEATURE_REGEX
|
||||||
bool getRx (const std::string&, std::string&);
|
bool getRx (const std::string&, std::string&);
|
||||||
|
#endif
|
||||||
bool getUUID (std::string&);
|
bool getUUID (std::string&);
|
||||||
bool getDateISO (time_t&);
|
bool getDateISO (time_t&);
|
||||||
|
#ifdef NIBBLER_FEATURE_DATE
|
||||||
bool getDate (const std::string&, time_t&);
|
bool getDate (const std::string&, time_t&);
|
||||||
|
#endif
|
||||||
bool getOneOf (const std::vector <std::string>&, std::string&);
|
bool getOneOf (const std::vector <std::string>&, std::string&);
|
||||||
bool getName (std::string&);
|
bool getName (std::string&);
|
||||||
bool getWord (std::string&);
|
bool getWord (std::string&);
|
||||||
|
@ -77,7 +89,9 @@ public:
|
||||||
bool skipAll (char);
|
bool skipAll (char);
|
||||||
bool skipAllOneOf (const std::string&);
|
bool skipAllOneOf (const std::string&);
|
||||||
bool skipWS ();
|
bool skipWS ();
|
||||||
|
#ifdef NIBBLER_FEATURE_REGEX
|
||||||
bool skipRx (const std::string&);
|
bool skipRx (const std::string&);
|
||||||
|
#endif
|
||||||
|
|
||||||
char next ();
|
char next ();
|
||||||
std::string next (const int quantity);
|
std::string next (const int quantity);
|
||||||
|
@ -89,6 +103,7 @@ public:
|
||||||
|
|
||||||
bool depleted ();
|
bool depleted ();
|
||||||
|
|
||||||
|
static bool isPunctuation (char);
|
||||||
std::string dump ();
|
std::string dump ();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -26,8 +26,10 @@
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#include <Context.h>
|
#include <Context.h>
|
||||||
#include <Date.h>
|
|
||||||
#include <Nibbler.h>
|
#include <Nibbler.h>
|
||||||
|
#ifdef NIBBLER_FEATURE_DATE
|
||||||
|
#include <Date.h>
|
||||||
|
#endif
|
||||||
#include <test.h>
|
#include <test.h>
|
||||||
|
|
||||||
Context context;
|
Context context;
|
||||||
|
@ -35,7 +37,19 @@ Context context;
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
int main (int argc, char** argv)
|
int main (int argc, char** argv)
|
||||||
{
|
{
|
||||||
|
#ifdef NIBBLER_FEARURE_DATE
|
||||||
|
#ifdef NIBBLER_FEATURE_REGEX
|
||||||
UnitTest t (292);
|
UnitTest t (292);
|
||||||
|
#else
|
||||||
|
UnitTest t (268);
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
#ifdef NIBBLER_FEATURE_REGEX
|
||||||
|
UnitTest t (242);
|
||||||
|
#else
|
||||||
|
UnitTest t (218);
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -44,7 +58,10 @@ int main (int argc, char** argv)
|
||||||
int i;
|
int i;
|
||||||
double d;
|
double d;
|
||||||
time_t ti;
|
time_t ti;
|
||||||
|
|
||||||
|
#ifdef NIBBLER_FEATURE_DATE
|
||||||
Date dt;
|
Date dt;
|
||||||
|
#endif
|
||||||
std::vector <std::string> options;
|
std::vector <std::string> options;
|
||||||
|
|
||||||
// Make sure the nibbler behaves itself with trivial input.
|
// Make sure the nibbler behaves itself with trivial input.
|
||||||
|
@ -65,7 +82,9 @@ int main (int argc, char** argv)
|
||||||
t.notok (n.getUntilEOL (s), "trivial: getUntilEOL");
|
t.notok (n.getUntilEOL (s), "trivial: getUntilEOL");
|
||||||
t.notok (n.getUntilEOS (s), "trivial: getUntilEOS");
|
t.notok (n.getUntilEOS (s), "trivial: getUntilEOS");
|
||||||
t.notok (n.getDateISO (ti), "trivial: getDateISO");
|
t.notok (n.getDateISO (ti), "trivial: getDateISO");
|
||||||
|
#ifdef NIBBLER_FEATURE_DATE
|
||||||
t.notok (n.getDate ("YYYYMMDD", ti), "trivial: getDate");
|
t.notok (n.getDate ("YYYYMMDD", ti), "trivial: getDate");
|
||||||
|
#endif
|
||||||
t.notok (n.getOneOf (options, s), "trivial: getOneOf");
|
t.notok (n.getOneOf (options, s), "trivial: getOneOf");
|
||||||
t.ok (n.depleted (), "trivial: depleted");
|
t.ok (n.depleted (), "trivial: depleted");
|
||||||
|
|
||||||
|
@ -81,6 +100,7 @@ int main (int argc, char** argv)
|
||||||
t.notok (n.getUntil (' ', s), " '' : getUntil (' ') -> false");
|
t.notok (n.getUntil (' ', s), " '' : getUntil (' ') -> false");
|
||||||
t.ok (n.depleted (), " '' : depleted () -> true");
|
t.ok (n.depleted (), " '' : depleted () -> true");
|
||||||
|
|
||||||
|
#ifdef NIBBLER_FEATURE_REGEX
|
||||||
// bool getUntilRx (const std::string&, std::string&);
|
// bool getUntilRx (const std::string&, std::string&);
|
||||||
t.diag ("Nibbler::getUntilRx");
|
t.diag ("Nibbler::getUntilRx");
|
||||||
n = Nibbler ("one two");
|
n = Nibbler ("one two");
|
||||||
|
@ -95,6 +115,7 @@ int main (int argc, char** argv)
|
||||||
t.ok (n.getUntilRx ("$", s), " 'two' : getUntilRx ('$') -> true");
|
t.ok (n.getUntilRx ("$", s), " 'two' : getUntilRx ('$') -> true");
|
||||||
t.is (s, "two", " 'two' : getUntilRx ('$') -> 'two'");
|
t.is (s, "two", " 'two' : getUntilRx ('$') -> 'two'");
|
||||||
t.ok (n.depleted (), " '' : depleted () -> true");
|
t.ok (n.depleted (), " '' : depleted () -> true");
|
||||||
|
#endif
|
||||||
|
|
||||||
// bool getUntilOneOf (const std::string&, std::string&);
|
// bool getUntilOneOf (const std::string&, std::string&);
|
||||||
t.diag ("Nibbler::getUntilOneOf");
|
t.diag ("Nibbler::getUntilOneOf");
|
||||||
|
@ -170,6 +191,7 @@ int main (int argc, char** argv)
|
||||||
t.is (s, "foo", " 'foo' : getUntilEOS () -> 'foo'");
|
t.is (s, "foo", " 'foo' : getUntilEOS () -> 'foo'");
|
||||||
t.ok (n.depleted (), " '' : depleted () -> true");
|
t.ok (n.depleted (), " '' : depleted () -> true");
|
||||||
|
|
||||||
|
#ifdef NIBBLER_FEATURE_REGEX
|
||||||
// bool skipRx (const std::string&);
|
// bool skipRx (const std::string&);
|
||||||
t.diag ("Nibbler::skipRx");
|
t.diag ("Nibbler::skipRx");
|
||||||
n = Nibbler ("one two");
|
n = Nibbler ("one two");
|
||||||
|
@ -178,6 +200,7 @@ int main (int argc, char** argv)
|
||||||
t.ok (n.skipRx ("e+"), " 'e two' : skipRx ('e+') -> true");
|
t.ok (n.skipRx ("e+"), " 'e two' : skipRx ('e+') -> true");
|
||||||
t.ok (n.skipRx ("...."), " ' two' : skipRx ('....') -> true");
|
t.ok (n.skipRx ("...."), " ' two' : skipRx ('....') -> true");
|
||||||
t.ok (n.depleted (), " '' : depleted () -> true");
|
t.ok (n.depleted (), " '' : depleted () -> true");
|
||||||
|
#endif
|
||||||
|
|
||||||
// bool getQuoted (char, std::string&);
|
// bool getQuoted (char, std::string&);
|
||||||
t.diag ("Nibbler::getQuoted");
|
t.diag ("Nibbler::getQuoted");
|
||||||
|
@ -280,6 +303,7 @@ int main (int argc, char** argv)
|
||||||
t.ok (n.getLiteral ("bar"), " 'bar' : getLiteral ('bar') -> true");
|
t.ok (n.getLiteral ("bar"), " 'bar' : getLiteral ('bar') -> true");
|
||||||
t.ok (n.depleted (), " '' : depleted () -> true");
|
t.ok (n.depleted (), " '' : depleted () -> true");
|
||||||
|
|
||||||
|
#ifdef NIBBLER_FEATURE_REGEX
|
||||||
// bool getRx (const std::string&, std::string&);
|
// bool getRx (const std::string&, std::string&);
|
||||||
t.diag ("Nibbler::getRx");
|
t.diag ("Nibbler::getRx");
|
||||||
n = Nibbler ("one two three");
|
n = Nibbler ("one two three");
|
||||||
|
@ -293,6 +317,7 @@ int main (int argc, char** argv)
|
||||||
t.ok (n.getRx ("th...", s), " 'three' : getRx ('th...') -> true");
|
t.ok (n.getRx ("th...", s), " 'three' : getRx ('th...') -> true");
|
||||||
t.is (s, "three", " 'three' : getRx ('th...') -> 'three'");
|
t.is (s, "three", " 'three' : getRx ('th...') -> 'three'");
|
||||||
t.ok (n.depleted (), " '' : depleted () -> true");
|
t.ok (n.depleted (), " '' : depleted () -> true");
|
||||||
|
#endif
|
||||||
|
|
||||||
// bool getUUID (std::string&);
|
// bool getUUID (std::string&);
|
||||||
t.diag ("Nibbler::getUUID");
|
t.diag ("Nibbler::getUUID");
|
||||||
|
@ -321,6 +346,7 @@ int main (int argc, char** argv)
|
||||||
t.is (ti, 1234567890, "'20090213T233130Z': getDateISO () -> 1234567890");
|
t.is (ti, 1234567890, "'20090213T233130Z': getDateISO () -> 1234567890");
|
||||||
t.ok (n.depleted (), "depleted");
|
t.ok (n.depleted (), "depleted");
|
||||||
|
|
||||||
|
#ifdef NIBBLER_FEATURE_DATE
|
||||||
// bool getDate (time_t&, const std::string&);
|
// bool getDate (time_t&, const std::string&);
|
||||||
t.diag ("Nibbler::getDate");
|
t.diag ("Nibbler::getDate");
|
||||||
n = Nibbler ("1/1/2008");
|
n = Nibbler ("1/1/2008");
|
||||||
|
@ -401,6 +427,7 @@ int main (int argc, char** argv)
|
||||||
t.is (dt.hour (), 12, "ctor (std::string) -> h");
|
t.is (dt.hour (), 12, "ctor (std::string) -> h");
|
||||||
t.is (dt.minute (), 34, "ctor (std::string) -> N");
|
t.is (dt.minute (), 34, "ctor (std::string) -> N");
|
||||||
t.is (dt.second (), 56, "ctor (std::string) -> S");
|
t.is (dt.second (), 56, "ctor (std::string) -> S");
|
||||||
|
#endif
|
||||||
|
|
||||||
// bool getOneOf (const std::vector <std::string>&, std::string&);
|
// bool getOneOf (const std::vector <std::string>&, std::string&);
|
||||||
t.diag ("Nibbler::getOneOf");
|
t.diag ("Nibbler::getOneOf");
|
||||||
|
|
|
@ -29,9 +29,6 @@
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <main.h>
|
|
||||||
#include <util.h>
|
|
||||||
#include <text.h>
|
|
||||||
#include <test.h>
|
#include <test.h>
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -394,9 +391,9 @@ void UnitTest::is (
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
void UnitTest::diag (const std::string& text)
|
void UnitTest::diag (const std::string& text)
|
||||||
{
|
{
|
||||||
std::string trimmed = trim (text, " \t\n\r\f");
|
std::string::size_type start = text.find_first_not_of (" \t\n\r\f");
|
||||||
|
std::string::size_type end = text.find_last_not_of (" \t\n\r\f");
|
||||||
std::cout << "# " << trimmed << "\n";
|
std::cout << "# " << text.substr (start, end - start + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue