-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tokenizer.cpp
97 lines (87 loc) · 1.86 KB
/
Tokenizer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "Tokenizer.h"
namespace shell
{
bool Tokenizer::process(const std::string& tLine)
{
bool isBetweenQuotes{ false };
bool isReadingToken{ false };
size_t tokenStart{ 0 };
size_t tokenEnd{ 0 };
size_t tokensSaved{ 0 };
auto saveToken = [&]
{
if (tokensSaved == 0)
{
m_commandName = tLine.substr(tokenStart, tokenEnd - tokenStart);
}
else if (tLine[tokenStart] == '-')
{
m_flags.emplace_back(tLine.substr(tokenStart, tokenEnd - tokenStart));
}
else
{
m_arguments.emplace_back(tLine.substr(tokenStart, tokenEnd - tokenStart));
}
++tokensSaved;
};
for (size_t i{ 0 }; i < tLine.size(); ++i)
{
if (isBetweenQuotes)
{
if (tLine[i] != '"')
{
continue;
}
isReadingToken = false;
isBetweenQuotes = false;
tokenEnd = i;
saveToken();
continue;
}
if (isReadingToken)
{
if (!std::isspace(tLine[i]))
{
continue;
}
isReadingToken = false;
tokenEnd = i;
saveToken();
continue;
}
if (!std::isspace(tLine[i]))
{
tokenStart = i;
isReadingToken = true;
if (tLine[i] == '"')
{
++tokenStart;
isBetweenQuotes = true;
}
}
}
if (isBetweenQuotes)
{
return false; // skipcq: CXX-C2015
}
return true;
}
void Tokenizer::clear()
{
m_arguments.clear();
m_flags.clear();
m_commandName = "";
}
const std::string& Tokenizer::getCommandName() const
{
return m_commandName;
}
const std::vector<std::string>& Tokenizer::getFlags() const
{
return m_flags;
}
const std::vector<std::string>& Tokenizer::getArguments() const
{
return m_arguments;
}
} // namespace shell