Lexer: Added standalone token support

- Added default ctor.
- Added ::token method for classifying whole tokens.
- Stubbed token classifier methods.
This commit is contained in:
Paul Beckingham 2015-07-04 11:38:09 -04:00
parent 4141215d4b
commit 3b99559216
2 changed files with 203 additions and 0 deletions

View file

@ -38,6 +38,14 @@ static const unsigned int uuid_min_length = 8;
std::string Lexer::dateFormat = ""; std::string Lexer::dateFormat = "";
bool Lexer::isoEnabled = true; bool Lexer::isoEnabled = true;
////////////////////////////////////////////////////////////////////////////////
Lexer::Lexer ()
: _text ("")
, _cursor (0)
, _eos (0)
{
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Lexer::Lexer (const std::string& text) Lexer::Lexer (const std::string& text)
: _text (text) : _text (text)
@ -97,6 +105,35 @@ bool Lexer::token (std::string& token, Lexer::Type& type)
return false; 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 // This static method tokenizes the input and provides a vector of token/type
// results from a high-level lex. // results from a high-level lex.
@ -1220,4 +1257,143 @@ bool Lexer::isOneWord (const std::string& text)
return true; 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;
}
*/
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View file

@ -51,9 +51,11 @@ public:
dom, identifier, word, dom, identifier, word,
date, duration }; date, duration };
Lexer ();
Lexer (const std::string&); Lexer (const std::string&);
~Lexer (); ~Lexer ();
bool token (std::string&, Lexer::Type&); bool token (std::string&, Lexer::Type&);
Lexer::Type token (const std::string&);
static std::vector <std::pair <std::string, Lexer::Type>> tokens (const std::string&); static std::vector <std::pair <std::string, Lexer::Type>> tokens (const std::string&);
static std::vector <std::string> split (const std::string&); static std::vector <std::string> split (const std::string&);
static std::string typeToString (Lexer::Type); static std::string typeToString (Lexer::Type);
@ -103,6 +105,31 @@ public:
bool isWord (std::string&, Lexer::Type&); bool isWord (std::string&, Lexer::Type&);
bool isContiguous (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: private:
std::string _text; std::string _text;
std::size_t _cursor; std::size_t _cursor;