From 3b99559216f9052839426c9741a7f262e84f595e Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 4 Jul 2015 11:38:09 -0400 Subject: [PATCH] Lexer: Added standalone token support - Added default ctor. - Added ::token method for classifying whole tokens. - Stubbed token classifier methods. --- src/Lexer.cpp | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Lexer.h | 27 ++++++++ 2 files changed, 203 insertions(+) diff --git a/src/Lexer.cpp b/src/Lexer.cpp index b773fd865..1c9ccf644 100644 --- a/src/Lexer.cpp +++ b/src/Lexer.cpp @@ -38,6 +38,14 @@ static const unsigned int uuid_min_length = 8; std::string Lexer::dateFormat = ""; bool Lexer::isoEnabled = true; +//////////////////////////////////////////////////////////////////////////////// +Lexer::Lexer () +: _text ("") +, _cursor (0) +, _eos (0) +{ +} + //////////////////////////////////////////////////////////////////////////////// Lexer::Lexer (const std::string& text) : _text (text) @@ -97,6 +105,35 @@ bool Lexer::token (std::string& token, Lexer::Type& type) return false; } +//////////////////////////////////////////////////////////////////////////////// +// Classify the whole token. +Lexer::Type Lexer::token (const std::string& token) +{ +/* + if (isString (token, '\'')) return Lexer::Type:string; + else if (isString (token, '"')) return Lexer::Type:string; + else if (isDate (token)) return Lexer::Type:date; + else if (isDuration (token)) return Lexer::Type:duration; + else if (isURL (token)) return Lexer::Type:url; + else if (isPair (token)) return Lexer::Type:pair; + else if (isSet (token)) return Lexer::Type:set; + else if (isDOM (token)) return Lexer::Type:dom; + else if (isUUID (token)) return Lexer::Type:uuid; + else if (isHexNumber (token)) return Lexer::Type:hex; + else if (isNumber (token)) return Lexer::Type:number; + else if (isSeparator (token)) return Lexer::Type:separator; + else*/ if (isTag (token)) return Lexer::Type::tag; +/* + else if (isPath (token)) return Lexer::Type:path; + else if (isSubstitution (token)) return Lexer::Type:substitution; + else if (isPattern (token)) return Lexer::Type:pattern; + else if (isOperator (token)) return Lexer::Type:op; + else if (isIdentifier (token)) return Lexer::Type:identifier; + else if (isWord (token)) return Lexer::Type:word; +*/ + return Lexer::Type::word; +} + //////////////////////////////////////////////////////////////////////////////// // This static method tokenizes the input and provides a vector of token/type // results from a high-level lex. @@ -1220,4 +1257,143 @@ bool Lexer::isOneWord (const std::string& text) return true; } +/* +//////////////////////////////////////////////////////////////////////////////// +bool Lexer::isString (const std::string& input) +{ + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Lexer::isDate (const std::string& input) +{ + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Lexer::isDuration (const std::string& input) +{ + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Lexer::isUUID (const std::string& input) +{ + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Lexer::isNumber (const std::string& input) +{ + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Lexer::isHexNumber (const std::string& input) +{ + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Lexer::isSeparator (const std::string& input) +{ + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Lexer::isURL (const std::string& input) +{ + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Lexer::isPair (const std::string& input) +{ + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Lexer::isSet (const std::string& input) +{ + + return false; +} +*/ + +//////////////////////////////////////////////////////////////////////////////// +bool Lexer::isTag (const std::string& input) +{ + return (input[0] == '+' || + input[0] == '-') && + isIdentifierStart (input[0]) && + input.length () > 1; +} + +/* +//////////////////////////////////////////////////////////////////////////////// +bool Lexer::isPath (const std::string& input) +{ + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Lexer::isSubstitution (const std::string& input) +{ + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Lexer::isPattern (const std::string& input) +{ + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Lexer::isOperator (const std::string& input) +{ + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Lexer::isDOM (const std::string& input) +{ + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Lexer::isIdentifier (const std::string& input) +{ + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Lexer::isWord (const std::string& input) +{ + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Lexer::isContiguous (const std::string& input) +{ + + return false; +} +*/ + //////////////////////////////////////////////////////////////////////////////// diff --git a/src/Lexer.h b/src/Lexer.h index 34f706b43..9ae46e9bd 100644 --- a/src/Lexer.h +++ b/src/Lexer.h @@ -51,9 +51,11 @@ public: dom, identifier, word, date, duration }; + Lexer (); Lexer (const std::string&); ~Lexer (); bool token (std::string&, Lexer::Type&); + Lexer::Type token (const std::string&); static std::vector > tokens (const std::string&); static std::vector split (const std::string&); static std::string typeToString (Lexer::Type); @@ -103,6 +105,31 @@ public: bool isWord (std::string&, Lexer::Type&); bool isContiguous (std::string&, Lexer::Type&); + // Token Classifiers. +/* + bool isString (const std::string&); + bool isDate (const std::string&); + bool isDuration (const std::string&); + bool isUUID (const std::string&); + bool isNumber (const std::string&); + bool isHexNumber (const std::string&); + bool isSeparator (const std::string&); + bool isURL (const std::string&); + bool isPair (const std::string&); + bool isSet (const std::string&); +*/ + bool isTag (const std::string&); +/* + bool isPath (const std::string&); + bool isSubstitution (const std::string&); + bool isPattern (const std::string&); + bool isOperator (const std::string&); + bool isDOM (const std::string&); + bool isIdentifier (const std::string&); + bool isWord (const std::string&); + bool isContiguous (const std::string&); +*/ + private: std::string _text; std::size_t _cursor;