From 21553d90446d41e8aefc12a5912c1317f888578c Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 21 Feb 2015 10:01:59 -0800 Subject: [PATCH] Lexer2 - Implemented ::isDate and ::isDuration. --- src/Lexer2.cpp | 61 +++++++++++++++++++++++++++----------------------- src/Lexer2.h | 5 +++++ 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/src/Lexer2.cpp b/src/Lexer2.cpp index 7accf8a4a..d16eeff48 100644 --- a/src/Lexer2.cpp +++ b/src/Lexer2.cpp @@ -27,11 +27,18 @@ #include #include #include +#include +#include +#include #include static const std::string uuid_pattern = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; static const int uuid_min_length = 8; +std::string Lexer2::dateFormat = ""; +bool Lexer2::isoEnabled = true; +bool Lexer2::ambiguity = true; + //////////////////////////////////////////////////////////////////////////////// Lexer2::Lexer2 (const std::string& text) : _text (text) @@ -405,37 +412,37 @@ bool Lexer2::isString (std::string& token, Lexer2::Type& type, int quote) // bool Lexer2::isDate (std::string& token, Lexer2::Type& type) { -#if 0 // Try an ISO date parse. - if (isoEnabled) + if (Lexer2::isoEnabled) { - std::string::size_type iso_i = 0; - std::string iso_result; + std::size_t iso_i = 0; ISO8601d iso; - iso.ambiguity (_ambiguity); - if (iso.parse (_input.substr (_shift_counter), iso_i)) + iso.ambiguity (Lexer2::ambiguity); + if (iso.parse (_text.substr (_cursor), iso_i)) { - result = _input.substr (_shift_counter, iso_i); - while (iso_i--) shift (); + type = Lexer2::Type::date; + token = _text.substr (_cursor, iso_i); + _cursor += iso_i; return true; } } // Try a legacy rc.dateformat parse here. - if (Lexer::dateFormat != "") + if (Lexer2::dateFormat != "") { try { - std::string::size_type legacy_i = 0; - Date legacyDate (_input.substr (_shift_counter), legacy_i, Lexer::dateFormat, false, false); - result = _input.substr (_shift_counter, legacy_i); - while (legacy_i--) shift (); + std::size_t legacy_i = 0; + Date legacyDate (_text.substr (_cursor), legacy_i, Lexer2::dateFormat, false, false); + + type = Lexer2::Type::date; + token = _text.substr (_cursor, legacy_i); + _cursor += legacy_i; return true; } catch (...) { /* Never mind. */ } } -#endif return false; } @@ -445,27 +452,25 @@ bool Lexer2::isDate (std::string& token, Lexer2::Type& type) // bool Lexer2::isDuration (std::string& token, Lexer2::Type& type) { -#if 0 - std::string::size_type iso_i = 0; - std::string iso_result; + std::size_t marker = 0; + ISO8601p iso; - if (iso.parse (_input.substr (_shift_counter), iso_i)) + if (iso.parse (_text.substr (_cursor), marker)) { - result = _input.substr (_shift_counter, iso_i); - while (iso_i--) shift (); + type = Lexer2::Type::duration; + token = _text.substr (_cursor, marker); + _cursor += marker; return true; } - std::string::size_type dur_i = 0; - std::string dur_result; Duration dur; - if (dur.parse (_input.substr (_shift_counter), dur_i)) + if (dur.parse (_text.substr (_cursor), marker)) { - result = _input.substr (_shift_counter, dur_i); - while (dur_i--) shift (); + type = Lexer2::Type::duration; + token = _text.substr (_cursor, marker); + _cursor += marker; return true; } -#endif return false; } @@ -954,8 +959,8 @@ std::string Lexer2::typeToString (Lexer2::Type type) else if (type == Lexer2::Type::op) return std::string ("\033[38;5;7m\033[48;5;203m") + "op" + "\033[0m"; else if (type == Lexer2::Type::identifier) return std::string ("\033[38;5;15m\033[48;5;244m") + "identifier" + "\033[0m"; else if (type == Lexer2::Type::word) return std::string ("\033[38;5;15m\033[48;5;236m") + "word" + "\033[0m"; - else if (type == Lexer2::Type::date) return std::string ("\033[38;5;15m\033[48;5;34") + "date" + "\033[0m"; - else if (type == Lexer2::Type::duration) return std::string ("\033[38;5;15m\033[48;5;34") + "duration" + "\033[0m"; + else if (type == Lexer2::Type::date) return std::string ("\033[38;5;15m\033[48;5;34m") + "date" + "\033[0m"; + else if (type == Lexer2::Type::duration) return std::string ("\033[38;5;15m\033[48;5;34m") + "duration" + "\033[0m"; else return std::string ("\033[37;41m") + "unknown" + "\033[0m"; } diff --git a/src/Lexer2.h b/src/Lexer2.h index ccacd3387..9cf3ea1e3 100644 --- a/src/Lexer2.h +++ b/src/Lexer2.h @@ -37,6 +37,11 @@ class Lexer2 { public: + // These are overridable. + static std::string dateFormat; + static bool isoEnabled; + static bool ambiguity; + enum class Type { uuid, number, hex, string, list, url, pair, separator,