mirror of
https://github.com/GothenburgBitFactory/timewarrior.git
synced 2025-07-07 20:06:39 +02:00
Lexer: Added hex number support
This commit is contained in:
parent
b9e5d94178
commit
53bb3952b8
3 changed files with 42 additions and 5 deletions
|
@ -51,8 +51,9 @@ bool Lexer::token (std::string& token, Lexer::Type& type)
|
|||
if (isEOS ())
|
||||
return false;
|
||||
|
||||
if (isString (token, type, "'\"") ||
|
||||
isWord (token, type))
|
||||
if (isString (token, type, "'\"") ||
|
||||
isHexNumber (token, type) ||
|
||||
isWord (token, type))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
|
@ -64,6 +65,7 @@ const std::string Lexer::typeName (const Lexer::Type& type)
|
|||
{
|
||||
switch (type)
|
||||
{
|
||||
case Lexer::Type::hex: return "hex";
|
||||
case Lexer::Type::string: return "string";
|
||||
case Lexer::Type::word: return "word";
|
||||
}
|
||||
|
@ -234,6 +236,34 @@ bool Lexer::isString (std::string& token, Lexer::Type& type, const std::string&
|
|||
return false;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Lexer::Type::hex
|
||||
// 0xX+
|
||||
bool Lexer::isHexNumber (std::string& token, Lexer::Type& type)
|
||||
{
|
||||
std::size_t marker = _cursor;
|
||||
|
||||
if (_eos - marker >= 3 &&
|
||||
_text[marker + 0] == '0' &&
|
||||
_text[marker + 1] == 'x')
|
||||
{
|
||||
marker += 2;
|
||||
|
||||
while (isHexDigit (_text[marker]))
|
||||
++marker;
|
||||
|
||||
if (marker - _cursor > 2)
|
||||
{
|
||||
token = _text.substr (_cursor, marker - _cursor);
|
||||
type = Lexer::Type::hex;
|
||||
_cursor = marker;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Lexer::Type::word
|
||||
// [^\s]+
|
||||
|
@ -262,6 +292,7 @@ bool Lexer::isWord (std::string& token, Lexer::Type& type)
|
|||
std::string Lexer::typeToString (Lexer::Type type)
|
||||
{
|
||||
if (type == Lexer::Type::string) return std::string ("\033[38;5;7m\033[48;5;3m") + "string" + "\033[0m";
|
||||
else if (type == Lexer::Type::hex) return std::string ("\033[38;5;7m\033[48;5;14m") + "hex" + "\033[0m";
|
||||
else if (type == Lexer::Type::word) return std::string ("\033[38;5;15m\033[48;5;236m") + "word" + "\033[0m";
|
||||
else return std::string ("\033[37;41m") + "unknown" + "\033[0m";
|
||||
}
|
||||
|
|
|
@ -35,7 +35,8 @@
|
|||
class Lexer
|
||||
{
|
||||
public:
|
||||
enum class Type { string,
|
||||
enum class Type { hex,
|
||||
string,
|
||||
word };
|
||||
|
||||
Lexer (const std::string&);
|
||||
|
@ -60,6 +61,7 @@ public:
|
|||
// Stream Classifiers.
|
||||
bool isEOS () const;
|
||||
bool isString (std::string&, Lexer::Type&, const std::string&);
|
||||
bool isHexNumber (std::string&, Lexer::Type&);
|
||||
bool isWord (std::string&, Lexer::Type&);
|
||||
|
||||
private:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue