HyperDbg Debugger
Loading...
Searching...
No Matches
CommandParser Class Reference

Public Types

enum class  TokenType { NumHex , NumDec , String , BracketString }
 
using Token = std::pair<TokenType, std::string>
 

Public Member Functions

BOOL GetHexNum (std::string &str)
 
BOOL IsDecNum (std::string &str)
 
std::vector< Tokenparse (const std::string &input)
 

Member Typedef Documentation

◆ Token

using CommandParser::Token = std::pair<TokenType, std::string>

Member Enumeration Documentation

◆ TokenType

enum class CommandParser::TokenType
strong
Enumerator
NumHex 
NumDec 
String 
BracketString 

Member Function Documentation

◆ GetHexNum()

BOOL CommandParser::GetHexNum ( std::string & str)
inline
56 {
57 if (str.empty() || (str.size() == 1 && str[0] == '0'))
58 return FALSE;
59
60 std::string Prefix("0x");
61 if (!str.compare(0, Prefix.size(), Prefix))
62 {
63 std::string num(str.substr(Prefix.size()));
64
65 try
66 {
67 size_t pos;
68 auto ul = std::stoul(num, &pos, 16);
69 str = num; // modify
70 return TRUE;
71 }
72 catch (...)
73 {
74 return FALSE;
75 }
76 }
77 else
78 {
79 try
80 {
81 size_t pos;
82 auto ul = std::stoul(str, &pos, 16);
83 return TRUE;
84 }
85 catch (...)
86 {
87 return FALSE;
88 }
89 }
90 }
#define TRUE
Definition BasicTypes.h:55
#define FALSE
Definition BasicTypes.h:54

◆ IsDecNum()

BOOL CommandParser::IsDecNum ( std::string & str)
inline
96 {
97 if (str.empty() || (str.size() == 1 && str[0] == '0') || str.size() < 3)
98 return FALSE;
99
100 std::string Prefix("0n");
101 std::string num(str.substr(Prefix.size()));
102 if (!str.compare(0, Prefix.size(), Prefix))
103 {
104 try
105 {
106 size_t pos;
107 auto ul = std::stoul(num, &pos, 10);
108 str = num; // modify
109 return TRUE;
110 }
111 catch (...)
112 {
113 return FALSE;
114 }
115 }
116 return FALSE;
117 }

◆ parse()

std::vector< Token > CommandParser::parse ( const std::string & input)
inline
120 {
121 std::vector<Token> tokens;
122 std::string current;
123 bool InQuotes = FALSE;
124 bool InBracket = FALSE;
125
126 for (size_t i = 0; i < input.length(); ++i)
127 {
128 char c = input[i];
129
130 if (c == '/') // start comment parse
131 {
132 //
133 // if we're in a script braket, skip; it'll be handled later
134 //
135 if (!(tokens.back().second == "script"))
136 {
137 size_t j = i;
138 c = input[++j];
139 if (c == '/') // start to look fo comments
140 {
141 size_t EndPose = input.find('\n', i);
142 if (EndPose != std::string::npos)
143 {
144 //
145 // here we could get the comment but for now we just skip
146 //
147 std::string comment(input.substr(i, EndPose - i));
148 i = i + (EndPose - i);
149 if (input[i + 1] == ' ' || input[i + 1] == '\n') // handling " " and "\n"
150 {
151 i++;
152 if (input[i + 1] == ' ' || input[i + 1] == '\n')
153 {
154 i++;
155 }
156 }
157 continue;
158 }
159 }
160 else if (c == '*')
161 {
162 size_t EndPose = input.find("*/", i);
163 if (EndPose != std::string::npos)
164 {
165 // here we could get the comment but for now we just skip
166 std::string comment(input.substr(i, EndPose - i + 2)); // */ is two byte long
167 i = (i + (EndPose - i)) + 1; // one for /
168 if (input[i + 1] == ' ' || input[i + 1] == '\n') // handling " " and "\n"
169 {
170 i++;
171 if (input[i + 1] == ' ' || input[i + 1] == '\n')
172 {
173 i++;
174 }
175 }
176 continue;
177 }
178 }
179 }
180 }
181
182 if (InQuotes)
183 {
184 if (c == '"' && input[i - 1] != '\\' && !InBracket)
185 {
186 if (input[i + 1] == ' ' || input[i + 1] == '\n') // handling " " and "\n"
187 {
188 i++;
189 if (input[i + 1] == ' ' || input[i + 1] == '\n')
190 {
191 i++;
192 }
193 }
194
195 InQuotes = FALSE;
196 tokens.emplace_back(TokenType::String, current);
197 current.clear();
198 continue;
199 }
200 }
201
202 if (InBracket)
203 {
204 if (c == '}')
205 {
206 if (input[i + 1] == ' ' || input[i + 1] == '\n') // handling " " and "\n"
207 {
208 i++;
209 if (input[i + 1] == ' ' || input[i + 1] == '\n')
210 {
211 i++;
212 }
213 }
214
215 InBracket = FALSE;
216
217 tokens.emplace_back(TokenType::BracketString, current);
218 current.clear();
219 continue;
220 }
221 }
222
223 if (c == ' ' && !InQuotes && !InBracket)
224 {
225 if (!current.empty() && current != " ")
226 {
227 addToken(tokens, current);
228 current.clear();
229 continue;
230 }
231 }
232 else if (c == '"' && input[i - 1] != '\\' && !InBracket)
233 {
234 InQuotes = TRUE;
235 continue; // dont inclue '"' in string
236 }
237 else if (c == '{' && !InQuotes && !InBracket)
238 {
239 InBracket = TRUE;
240 continue; // dont inclue '{' in string
241 }
242 current += c;
243 }
244
245 if (!current.empty() && current != " ")
246 {
247 addToken(tokens, current);
248 }
249
250 return tokens;
251 }

The documentation for this class was generated from the following file: