|
| 1 | +#include "CLexicalAnalizer.h" |
| 2 | +#include "ctype.h" |
| 3 | +#include <string> |
| 4 | +#include "..\library\Types.h" |
| 5 | +#include <assert.h> |
| 6 | +#include <iostream> |
| 7 | + |
| 8 | +using std::vector; |
| 9 | +using std::string; |
| 10 | +using std::basic_istream; |
| 11 | +using std::char_traits; |
| 12 | +using std::getline; |
| 13 | +using std::exception; |
| 14 | +using std::stoul; |
| 15 | + |
| 16 | +#define ARRAY_SIZE(array) (sizeof((array))/sizeof((array[0]))) |
| 17 | + |
| 18 | +namespace NPeProtector |
| 19 | +{ |
| 20 | +namespace |
| 21 | +{ |
| 22 | + // todo move to impl |
| 23 | + const char * const sCategories[] = |
| 24 | + { |
| 25 | + ",", |
| 26 | +// ".", |
| 27 | + ":", |
| 28 | + "-", |
| 29 | + "+", |
| 30 | + "(", |
| 31 | + ")", |
| 32 | + "[", |
| 33 | + "]", |
| 34 | + "*", |
| 35 | + // keywords |
| 36 | + "IMPORT", |
| 37 | + "EXTERN", |
| 38 | + "DUP", |
| 39 | + "PTR", |
| 40 | + "SECTION", |
| 41 | + "DIRECTIVE" |
| 42 | + }; |
| 43 | + |
| 44 | + vector<string> splitLine(string line) |
| 45 | + { |
| 46 | + vector<string> result; |
| 47 | + |
| 48 | + // skip comments |
| 49 | + line = string(line, 0, line.find(';')); |
| 50 | + |
| 51 | + for (unsigned int beginPosition = 0, length = 0; beginPosition < line.size();) |
| 52 | + { |
| 53 | + if ((beginPosition + length == line.size()) || line[beginPosition + length] == ' ' || line[beginPosition + length] == '\t') |
| 54 | + { |
| 55 | + if (length > 0) |
| 56 | + { |
| 57 | + // add name |
| 58 | + result.push_back(line.substr(beginPosition, length)); |
| 59 | + beginPosition += length + 1; |
| 60 | + length = 0; |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + // skip space |
| 65 | + beginPosition += 1; |
| 66 | + } |
| 67 | + } |
| 68 | + else if (line[beginPosition + length] == '"') |
| 69 | + { |
| 70 | + if (length > 0) |
| 71 | + { |
| 72 | + // add name |
| 73 | + result.push_back(line.substr(beginPosition, length)); |
| 74 | + } |
| 75 | + |
| 76 | + const size_t beginStringPosition = beginPosition + length; |
| 77 | + |
| 78 | + // add string |
| 79 | + const size_t quotePosition = line.find("\"", beginStringPosition + 1); |
| 80 | + if (quotePosition == string::npos) |
| 81 | + { |
| 82 | + throw exception("wrong quote"); |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + const size_t endStringPosition = quotePosition + 1; |
| 87 | + result.push_back(line.substr(beginStringPosition, endStringPosition - beginStringPosition)); |
| 88 | + } |
| 89 | + beginPosition = quotePosition + 1; |
| 90 | + length = 0; |
| 91 | + } |
| 92 | + else if (!isalnum(line[beginPosition + length]) && line[beginPosition + length] != '_' && line[beginPosition + length] != '.') |
| 93 | + { |
| 94 | + if (length > 0) |
| 95 | + { |
| 96 | + // add name |
| 97 | + result.push_back(line.substr(beginPosition, length)); |
| 98 | + |
| 99 | + // add character |
| 100 | + result.push_back(line.substr(beginPosition + length, 1)); |
| 101 | + |
| 102 | + beginPosition += length + 1; |
| 103 | + length = 0; |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + // add character |
| 108 | + result.push_back(line.substr(beginPosition, 1)); |
| 109 | + beginPosition += 1; |
| 110 | + } |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + length += 1; |
| 115 | + } |
| 116 | + } |
| 117 | + return result; |
| 118 | + } |
| 119 | + |
| 120 | + vector<string> splitFile(basic_istream<char, char_traits<char> > & input) |
| 121 | + { |
| 122 | + vector<string> lines; |
| 123 | + string line; |
| 124 | + while (getline(input, line)) |
| 125 | + { |
| 126 | + lines.push_back(line); |
| 127 | + } |
| 128 | + return lines; |
| 129 | + } |
| 130 | + |
| 131 | + vector<vector<string> > split(basic_istream<char, char_traits<char> > & input) |
| 132 | + { |
| 133 | + vector<vector<string> > tokens; |
| 134 | + |
| 135 | + const vector<string> & lines = splitFile(input); |
| 136 | + |
| 137 | + for (unsigned int i = 0; i < lines.size(); ++i) |
| 138 | + { |
| 139 | + tokens.push_back(splitLine(lines[i])); |
| 140 | + } |
| 141 | + return tokens; |
| 142 | + } |
| 143 | + |
| 144 | + //TODO find count of vector |
| 145 | + SToken getToken(const string & stringToken) |
| 146 | + { |
| 147 | + assert(!stringToken.empty()); |
| 148 | + |
| 149 | + // scan for standard tokens |
| 150 | + for (int i = 0; i < ARRAY_SIZE(sCategories); ++i) |
| 151 | + { |
| 152 | + if (!_strcmpi(stringToken.c_str(), sCategories[i])) |
| 153 | + { |
| 154 | + return SToken(NCategory::EType(i)); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + // TODO: create loop for this |
| 159 | + |
| 160 | + // scan for instructions |
| 161 | + for (int i = 0; i < NInstruction::gSize; ++i) |
| 162 | + { |
| 163 | + if (!_strcmpi(stringToken.c_str(), NInstruction::gStrings[i])) |
| 164 | + { |
| 165 | + return SToken(NCategory::INSTRUCTION, i); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + // scan for registers |
| 170 | + for (int i = 0; i < NRegister::gSize; ++i) |
| 171 | + { |
| 172 | + if (!_strcmpi(stringToken.c_str(), NRegister::gStrings[i])) |
| 173 | + { |
| 174 | + return SToken(NCategory::REGISTER, i); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + // scan for prefixes |
| 179 | + for (int i = 0; i < NPrefix::gSize; ++i) |
| 180 | + { |
| 181 | + if (!_strcmpi(stringToken.c_str(), NPrefix::gStrings[i])) |
| 182 | + { |
| 183 | + return SToken(NCategory::PREFIX, i); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + // scan for data types |
| 188 | + for (int i = 0; i < NDataType::gSize; ++i) |
| 189 | + { |
| 190 | + if (!_strcmpi(stringToken.c_str(), NDataType::gStrings[i])) |
| 191 | + { |
| 192 | + return SToken(NCategory::DATA_TYPE, i); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + // scan for segments |
| 197 | + for (int i = 0; i < NSegment::gSize; ++i) |
| 198 | + { |
| 199 | + if (!_strcmpi(stringToken.c_str(), NSegment::gStrings[i])) |
| 200 | + { |
| 201 | + return SToken(NCategory::SEGMENT, i); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + if (stringToken[0] == '"') |
| 206 | + { |
| 207 | + return SToken(NCategory::STRING, 0, stringToken.substr(1, stringToken.size() - 2)); |
| 208 | + } |
| 209 | + |
| 210 | + if (isdigit(stringToken[0])) |
| 211 | + { |
| 212 | + int base = 10; |
| 213 | + string digits = stringToken; |
| 214 | + |
| 215 | + if (stringToken.back() == 'h' || stringToken.back() == 'H') |
| 216 | + { |
| 217 | + base = 16; |
| 218 | + digits = stringToken.substr(0, stringToken.size() - 1); |
| 219 | + } |
| 220 | + |
| 221 | + size_t index = 0; |
| 222 | + const unsigned int constant = stoul(digits, &index, base); |
| 223 | + if (index != digits.size()) |
| 224 | + { |
| 225 | + throw exception(("wrong number : " + stringToken).c_str()); |
| 226 | + } |
| 227 | + return SToken(NCategory::CONSTANT, 0, "", constant); |
| 228 | + } |
| 229 | + |
| 230 | + return SToken(NCategory::NAME, 0, stringToken); |
| 231 | + } |
| 232 | +} |
| 233 | + |
| 234 | +namespace NCategory |
| 235 | +{ |
| 236 | + const char * const gStrings[] = |
| 237 | + { |
| 238 | + "COMMA", // ',' |
| 239 | + //"DOT", // '.' for import |
| 240 | + "COLON", // ':' |
| 241 | + "MINUS", // '-' |
| 242 | + "PLUS", // '+' |
| 243 | + "OP_BRACKET", // '(' |
| 244 | + "CL_BRACKET", // ')' |
| 245 | + "OP_SQ_BRACKET", // '[' |
| 246 | + "CL_SQ_BRACKET", // ']' |
| 247 | + "ASTERIX", // '*' |
| 248 | + // keywords |
| 249 | + "IMPORT", |
| 250 | + "EXTERN", |
| 251 | + "DUP", |
| 252 | + "PTR", |
| 253 | + "SECTION", |
| 254 | + "DIRECTIVE", |
| 255 | + // groups |
| 256 | + "DATA_TYPE", // map to type NDataType::EType |
| 257 | + "PREFIX", // map to type NPrefix::EType |
| 258 | + "INSTRUCTION", // map to type NInstruction::EType |
| 259 | + "REGISTER", // map to type NRegister::EType |
| 260 | + "SEGMENT", // map to type NSegment::EType |
| 261 | + // undefined string |
| 262 | + "NAME", |
| 263 | + "STRING", |
| 264 | + "CONSTANT" |
| 265 | + }; |
| 266 | + const int gSize = ARRAY_SIZE(gStrings); |
| 267 | +} |
| 268 | + |
| 269 | + SToken::SToken() |
| 270 | + : mType() |
| 271 | + , mIndex() |
| 272 | + , mData() |
| 273 | + , mConstant() |
| 274 | + { |
| 275 | + } |
| 276 | + |
| 277 | + SToken::SToken(const NCategory::EType type, int index /*= 0*/, string name /*= string()*/, DWORD constant /*= 0*/) |
| 278 | + : mType(type) |
| 279 | + , mIndex(index) |
| 280 | + , mData(name) |
| 281 | + , mConstant(constant) |
| 282 | + { |
| 283 | + } |
| 284 | + |
| 285 | + bool isMatch(const vector<SToken> & tokens, const vector<NCategory::EType> & categories) |
| 286 | + { |
| 287 | + bool result = categories.empty(); |
| 288 | + if (!result) |
| 289 | + { |
| 290 | + unsigned int i = 0; |
| 291 | + for (; i < categories.size() && i < tokens.size(); ++i) |
| 292 | + { |
| 293 | + if (tokens[i].mType != categories[i]) |
| 294 | + { |
| 295 | + break; |
| 296 | + } |
| 297 | + } |
| 298 | + result = i == categories.size(); |
| 299 | + } |
| 300 | + return result; |
| 301 | + } |
| 302 | + |
| 303 | + vector<vector<SToken> > parse(basic_istream<char, char_traits<char> > & input) |
| 304 | + { |
| 305 | + const vector<vector<string> > & tokens = split(input); |
| 306 | + |
| 307 | + vector<vector<SToken> > result; |
| 308 | + |
| 309 | + for (unsigned int i = 0; i < tokens.size(); ++i) |
| 310 | + { |
| 311 | + vector<SToken> lineTokens; |
| 312 | + |
| 313 | + for (unsigned int j = 0; j < tokens[i].size(); ++j) |
| 314 | + { |
| 315 | + if (!tokens[i][j].empty()) |
| 316 | + { |
| 317 | + lineTokens.push_back(getToken(tokens[i][j])); |
| 318 | + } |
| 319 | + } |
| 320 | + result.push_back(lineTokens); |
| 321 | + } |
| 322 | + return result; |
| 323 | + } |
| 324 | +} |
0 commit comments