- Improved ::isIdentifier, ::isUUID and ::isDOM.
This commit is contained in:
Paul Beckingham 2015-03-01 23:54:45 -05:00
parent 2af470bb90
commit 237d932ff9
3 changed files with 73 additions and 13 deletions

View file

@ -358,6 +358,7 @@ void Eval::evaluatePostfixStack (
throw std::string (STRING_EVAL_OP_EXPECTED); throw std::string (STRING_EVAL_OP_EXPECTED);
break; break;
case Lexer::Type::dom:
case Lexer::Type::identifier: case Lexer::Type::identifier:
{ {
bool found = false; bool found = false;
@ -873,6 +874,7 @@ std::string Eval::dump (
color_map[Lexer::Type::number] = Color ("rgb530 on gray6"); color_map[Lexer::Type::number] = Color ("rgb530 on gray6");
color_map[Lexer::Type::hex] = Color ("rgb303 on gray6"); color_map[Lexer::Type::hex] = Color ("rgb303 on gray6");
color_map[Lexer::Type::string] = Color ("rgb550 on gray6"); color_map[Lexer::Type::string] = Color ("rgb550 on gray6");
color_map[Lexer::Type::dom] = Color ("rgb045 on gray6");
color_map[Lexer::Type::identifier] = Color ("rgb035 on gray6"); color_map[Lexer::Type::identifier] = Color ("rgb035 on gray6");
color_map[Lexer::Type::date] = Color ("rgb150 on gray6"); color_map[Lexer::Type::date] = Color ("rgb150 on gray6");
color_map[Lexer::Type::duration] = Color ("rgb531 on gray6"); color_map[Lexer::Type::duration] = Color ("rgb531 on gray6");

View file

@ -73,6 +73,7 @@ bool Lexer::token (std::string& token, Lexer::Type& type)
// The sequence is specific, and must follow these rules: // The sequence is specific, and must follow these rules:
// - date < duration < uuid < identifier // - date < duration < uuid < identifier
// - uuid < hex < number
// - url < pair < identifier // - url < pair < identifier
// - hex < number // - hex < number
// - separator < tag < operator // - separator < tag < operator
@ -82,13 +83,14 @@ bool Lexer::token (std::string& token, Lexer::Type& type)
isString (token, type, '"') || isString (token, type, '"') ||
isDate (token, type) || isDate (token, type) ||
isDuration (token, type) || isDuration (token, type) ||
isURL (token, type) ||
isPair (token, type) ||
isDOM (token, type) ||
isUUID (token, type) || isUUID (token, type) ||
isHexNumber (token, type) || isHexNumber (token, type) ||
isNumber (token, type) || isNumber (token, type) ||
isSeparator (token, type) || isSeparator (token, type) ||
isList (token, type) || isList (token, type) ||
isURL (token, type) ||
isPair (token, type) ||
isTag (token, type) || isTag (token, type) ||
isPath (token, type) || isPath (token, type) ||
isSubstitution (token, type) || isSubstitution (token, type) ||
@ -150,6 +152,7 @@ const std::string Lexer::typeName (const Lexer::Type& type)
case Lexer::Type::substitution: return "substitution"; case Lexer::Type::substitution: return "substitution";
case Lexer::Type::pattern: return "pattern"; case Lexer::Type::pattern: return "pattern";
case Lexer::Type::op: return "op"; case Lexer::Type::op: return "op";
case Lexer::Type::dom: return "dom";
case Lexer::Type::identifier: return "identifier"; case Lexer::Type::identifier: return "identifier";
case Lexer::Type::word: return "word"; case Lexer::Type::word: return "word";
case Lexer::Type::date: return "date"; case Lexer::Type::date: return "date";
@ -491,6 +494,7 @@ bool Lexer::isDuration (std::string& token, Lexer::Type& type)
// XXXXXXXX-X // XXXXXXXX-X
// XXXXXXXX- // XXXXXXXX-
// XXXXXXXX // XXXXXXXX
// Followed only by EOS, whitespace, operator or list.
bool Lexer::isUUID (std::string& token, Lexer::Type& type) bool Lexer::isUUID (std::string& token, Lexer::Type& type)
{ {
std::size_t marker = _cursor; std::size_t marker = _cursor;
@ -950,23 +954,75 @@ bool Lexer::isOperator (std::string& token, Lexer::Type& type)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Lexer::Type::identifier // Lexer::Type::dom
// [ <idDigit>+ . ] <isIdentifierStart> [ <isIdentifierNext> ]* // [ <isUUID> | <isDigit>+ . ] <isIdentifier> [ . <isIdentifier> ]*
bool Lexer::isIdentifier (std::string& token, Lexer::Type& type) bool Lexer::isDOM (std::string& token, Lexer::Type& type)
{ {
std::size_t marker = _cursor; std::size_t marker = _cursor;
if (isDigit (_text[marker])) std::string extractedToken;
Lexer::Type extractedType;
if (isUUID (extractedToken, extractedType))
{ {
++marker; if (_text[_cursor] == '.')
while (isDigit (_text[marker])) ++_cursor;
++marker;
if (_text[marker] == '.')
++marker;
else else
{
_cursor = marker;
return false; return false;
} }
}
else
{
if (isDigit (_text[_cursor]))
{
++_cursor;
while (isDigit (_text[_cursor]))
++_cursor;
if (_text[_cursor] == '.')
++_cursor;
else
{
_cursor = marker;
return false;
}
}
}
if (! isOperator (extractedToken, extractedType) &&
isIdentifier (extractedToken, extractedType))
{
while (1)
{
if (_text[_cursor] == '.')
++_cursor;
else
break;
if (isOperator (extractedToken, extractedType) ||
! isIdentifier (extractedToken, extractedType))
{
_cursor = marker;
return false;
}
}
type = Lexer::Type::dom;
token = _text.substr (marker, _cursor - marker);
return true;
}
_cursor = marker;
return false;
}
////////////////////////////////////////////////////////////////////////////////
// Lexer::Type::identifier
// <isIdentifierStart> [ <isIdentifierNext> ]*
bool Lexer::isIdentifier (std::string& token, Lexer::Type& type)
{
std::size_t marker = _cursor;
if (isIdentifierStart (_text[marker])) if (isIdentifierStart (_text[marker]))
{ {
@ -1022,6 +1078,7 @@ std::string Lexer::typeToString (Lexer::Type type)
else if (type == Lexer::Type::substitution) return std::string ("\033[37;102m") + "substitution" + "\033[0m"; else if (type == Lexer::Type::substitution) return std::string ("\033[37;102m") + "substitution" + "\033[0m";
else if (type == Lexer::Type::pattern) return std::string ("\033[37;42m") + "pattern" + "\033[0m"; else if (type == Lexer::Type::pattern) return std::string ("\033[37;42m") + "pattern" + "\033[0m";
else if (type == Lexer::Type::op) return std::string ("\033[38;5;7m\033[48;5;203m") + "op" + "\033[0m"; else if (type == Lexer::Type::op) return std::string ("\033[38;5;7m\033[48;5;203m") + "op" + "\033[0m";
else if (type == Lexer::Type::dom) return std::string ("\033[38;5;15m\033[48;5;244m") + "dom" + "\033[0m";
else if (type == Lexer::Type::identifier) return std::string ("\033[38;5;15m\033[48;5;244m") + "identifier" + "\033[0m"; else if (type == Lexer::Type::identifier) return std::string ("\033[38;5;15m\033[48;5;244m") + "identifier" + "\033[0m";
else if (type == Lexer::Type::word) return std::string ("\033[38;5;15m\033[48;5;236m") + "word" + "\033[0m"; else if (type == Lexer::Type::word) return std::string ("\033[38;5;15m\033[48;5;236m") + "word" + "\033[0m";
else if (type == Lexer::Type::date) return std::string ("\033[38;5;15m\033[48;5;34m") + "date" + "\033[0m"; else if (type == Lexer::Type::date) return std::string ("\033[38;5;15m\033[48;5;34m") + "date" + "\033[0m";

View file

@ -48,7 +48,7 @@ public:
path, path,
substitution, pattern, substitution, pattern,
op, op,
identifier, word, dom, identifier, word,
date, duration }; date, duration };
Lexer (const std::string&); Lexer (const std::string&);
@ -97,6 +97,7 @@ public:
bool isSubstitution (std::string&, Lexer::Type&); bool isSubstitution (std::string&, Lexer::Type&);
bool isPattern (std::string&, Lexer::Type&); bool isPattern (std::string&, Lexer::Type&);
bool isOperator (std::string&, Lexer::Type&); bool isOperator (std::string&, Lexer::Type&);
bool isDOM (std::string&, Lexer::Type&);
bool isIdentifier (std::string&, Lexer::Type&); bool isIdentifier (std::string&, Lexer::Type&);
bool isWord (std::string&, Lexer::Type&); bool isWord (std::string&, Lexer::Type&);