| 1 |
/* |
| 2 |
* FCML - Free Code Manipulation Library. |
| 3 |
* Copyright (C) 2010-2020 Slawomir Wojtasiak |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
*/ |
| 19 |
|
| 20 |
/** @file fcml_parser.hpp |
| 21 |
* C++ wrapper for instruction parser. |
| 22 |
* |
| 23 |
* @copyright Copyright (C) 2010-2020 Slawomir Wojtasiak. All rights reserved. |
| 24 |
* This project is released under the GNU Lesser General Public License. |
| 25 |
*/ |
| 26 |
|
| 27 |
#ifndef FCML_PARSER_HPP_ |
| 28 |
#define FCML_PARSER_HPP_ |
| 29 |
|
| 30 |
#include "fcml_common.hpp" |
| 31 |
#include "fcml_symbols.hpp" |
| 32 |
#include "fcml_errors.hpp" |
| 33 |
#include "fcml_dialect.hpp" |
| 34 |
|
| 35 |
#include "fcml_parser.h" |
| 36 |
|
| 37 |
namespace fcml { |
| 38 |
|
| 39 |
/** |
| 40 |
* Something failed while parsing. |
| 41 |
* @since 1.1.0 |
| 42 |
*/ |
| 43 |
class ParsingFailedException: public ErrorContainerAwareException { |
| 44 |
public: |
| 45 |
ParsingFailedException(const fcml_cstring msg, |
| 46 |
ErrorContainer &errorContainer, |
| 47 |
fcml_ceh_error error = FCML_CEH_GEC_NO_ERROR) : |
| 48 |
ErrorContainerAwareException(msg, errorContainer, error) { |
| 49 |
} |
| 50 |
}; |
| 51 |
|
| 52 |
/** Parser configuration. |
| 53 |
* @since 1.1.0 |
| 54 |
*/ |
| 55 |
class ParserConfig { |
| 56 |
public: |
| 57 |
|
| 58 |
/** |
| 59 |
* Default constructor. |
| 60 |
* @since 1.1.0 |
| 61 |
*/ |
| 62 |
ParserConfig() : |
| 63 |
_throwExceptionOnError(true), |
| 64 |
_ignoreUndefinedSymbols(false), |
| 65 |
_disableSymbolsDeclaration(true), |
| 66 |
_overrideLabels(false), |
| 67 |
_allocSymbolTableIfNeeded(false), |
| 68 |
_enableErrorMessages(true) { |
| 69 |
} |
| 70 |
|
| 71 |
/** @since 1.1.0 */ |
| 72 |
bool isAllocSymbolTableIfNeeded() const { |
| 73 |
return _allocSymbolTableIfNeeded; |
| 74 |
} |
| 75 |
|
| 76 |
/** @since 1.1.0 */ |
| 77 |
void setAllocSymbolTableIfNeeded(bool allocSymbolTableIfNeeded) { |
| 78 |
_allocSymbolTableIfNeeded = allocSymbolTableIfNeeded; |
| 79 |
} |
| 80 |
|
| 81 |
/** @since 1.1.0 */ |
| 82 |
bool isDisableSymbolsDeclaration() const { |
| 83 |
return _disableSymbolsDeclaration; |
| 84 |
} |
| 85 |
|
| 86 |
/** @since 1.1.0 */ |
| 87 |
void setDisableSymbolsDeclaration(bool disableSymbolsDeclaration) { |
| 88 |
_disableSymbolsDeclaration = disableSymbolsDeclaration; |
| 89 |
} |
| 90 |
|
| 91 |
/** @since 1.1.0 */ |
| 92 |
bool isEnableErrorMessages() const { |
| 93 |
return _enableErrorMessages; |
| 94 |
} |
| 95 |
|
| 96 |
/** @since 1.1.0 */ |
| 97 |
void setEnableErrorMessages(bool enableErrorMessages) { |
| 98 |
_enableErrorMessages = enableErrorMessages; |
| 99 |
} |
| 100 |
|
| 101 |
/** @since 1.1.0 */ |
| 102 |
bool isIgnoreUndefinedSymbols() const { |
| 103 |
return _ignoreUndefinedSymbols; |
| 104 |
} |
| 105 |
|
| 106 |
/** @since 1.1.0 */ |
| 107 |
void setIgnoreUndefinedSymbols(bool ignoreUndefinedSymbols) { |
| 108 |
_ignoreUndefinedSymbols = ignoreUndefinedSymbols; |
| 109 |
} |
| 110 |
|
| 111 |
/** @since 1.1.0 */ |
| 112 |
bool isOverrideLabels() const { |
| 113 |
return _overrideLabels; |
| 114 |
} |
| 115 |
|
| 116 |
/** @since 1.1.0 */ |
| 117 |
void setOverrideLabels(bool overrideLabels) { |
| 118 |
_overrideLabels = overrideLabels; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Gets true if exception should be thrown in case of error. |
| 123 |
* @return True if exception should be thrown in case of error. |
| 124 |
* @since 1.1.0 |
| 125 |
*/ |
| 126 |
bool isThrowExceptionOnError() const { |
| 127 |
return _throwExceptionOnError; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Sets exception on error flag. Has to be set to true if exception |
| 132 |
* should be thrown in case of error. |
| 133 |
* @param throwExceptionOnError True if exception should be thrown |
| 134 |
* in case of error. |
| 135 |
* @since 1.1.0 |
| 136 |
*/ |
| 137 |
void setThrowExceptionOnError(bool throwExceptionOnError) { |
| 138 |
_throwExceptionOnError = throwExceptionOnError; |
| 139 |
} |
| 140 |
|
| 141 |
private: |
| 142 |
bool _throwExceptionOnError; |
| 143 |
bool _ignoreUndefinedSymbols; |
| 144 |
bool _disableSymbolsDeclaration; |
| 145 |
bool _overrideLabels; |
| 146 |
bool _allocSymbolTableIfNeeded; |
| 147 |
bool _enableErrorMessages; |
| 148 |
}; |
| 149 |
|
| 150 |
/** Parser context. |
| 151 |
* @since 1.1.0 |
| 152 |
*/ |
| 153 |
class ParserContext { |
| 154 |
public: |
| 155 |
|
| 156 |
/** |
| 157 |
* Creates a parser context instance for optional instruction pointer. |
| 158 |
* |
| 159 |
* @param ip The instruction pointer. |
| 160 |
* @since 1.1.0 |
| 161 |
*/ |
| 162 |
ParserContext(fcml_ip ip = 0) : |
| 163 |
_ip(ip), _symbolTable(NULL) { |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Creates a parser context for given symbol table and optional |
| 168 |
* instruction pointer. |
| 169 |
* |
| 170 |
* @param symbolTable The symbol table. |
| 171 |
* @param ip The instruction pointer. |
| 172 |
* @since 1.1.0 |
| 173 |
*/ |
| 174 |
ParserContext(SymbolTable *symbolTable, fcml_ip ip = 0) : |
| 175 |
_ip(ip), _symbolTable(symbolTable) { |
| 176 |
} |
| 177 |
|
| 178 |
public: |
| 179 |
|
| 180 |
/** |
| 181 |
* Gets the parser configuration associated with the context. |
| 182 |
* |
| 183 |
* @return The parser configuration associated with the context. |
| 184 |
* @since 1.1.0 |
| 185 |
*/ |
| 186 |
const ParserConfig& getConfig() const { |
| 187 |
return _config; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Gets the parser configuration associated with the context. |
| 192 |
* |
| 193 |
* @return The parser configuration associated with the context. |
| 194 |
* @since 1.1.0 |
| 195 |
*/ |
| 196 |
ParserConfig& getConfig() { |
| 197 |
return _config; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Gets the instruction pointer. |
| 202 |
* |
| 203 |
* @return The instruction pointer. |
| 204 |
* @since 1.1.0 |
| 205 |
*/ |
| 206 |
fcml_ip getIp() const { |
| 207 |
return _ip; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Gets a new instruction pointer. |
| 212 |
* |
| 213 |
* @param ip The new instruction pointer. |
| 214 |
* @since 1.1.0 |
| 215 |
*/ |
| 216 |
void setIp(fcml_ip ip) { |
| 217 |
_ip = ip; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Increments instruction pointer by given number of bytes. |
| 222 |
* |
| 223 |
* @param ip The number of bytes the instruction pointer has |
| 224 |
* to be incremented by. |
| 225 |
* @since 1.1.0 |
| 226 |
*/ |
| 227 |
void incrementIP(fcml_ip ip) { |
| 228 |
_ip += ip; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Gets the symbol table associated with the context. |
| 233 |
* |
| 234 |
* @return The symbol table. |
| 235 |
* @since 1.1.0 |
| 236 |
*/ |
| 237 |
const SymbolTable* getSymbolTable() const { |
| 238 |
return _symbolTable; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Gets the symbol table associated with the context. |
| 243 |
* |
| 244 |
* @return The symbol table. |
| 245 |
* @since 1.1.0 |
| 246 |
*/ |
| 247 |
SymbolTable* getSymbolTable() { |
| 248 |
return _symbolTable; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Sets a symbol table for the instruction. |
| 253 |
* |
| 254 |
* @param symbolTable The symbol table for the parser context. |
| 255 |
* @since 1.1.0 |
| 256 |
*/ |
| 257 |
void setSymbolTable(SymbolTable *symbolTable) { |
| 258 |
_symbolTable = symbolTable; |
| 259 |
} |
| 260 |
|
| 261 |
private: |
| 262 |
|
| 263 |
/** The instruction pointer used by declared labels. */ |
| 264 |
fcml_ip _ip; |
| 265 |
/** The parser configuration. */ |
| 266 |
ParserConfig _config; |
| 267 |
/** The symbol table. */ |
| 268 |
SymbolTable *_symbolTable; |
| 269 |
|
| 270 |
}; |
| 271 |
|
| 272 |
/** Parser result. |
| 273 |
* @since 1.1.0 |
| 274 |
*/ |
| 275 |
class ParserResult { |
| 276 |
public: |
| 277 |
|
| 278 |
/** |
| 279 |
* Creates an empty parser result. |
| 280 |
* @since 1.1.0 |
| 281 |
*/ |
| 282 |
ParserResult() { |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Gets errors container with parsing errors. |
| 287 |
* |
| 288 |
* @return Errors container. |
| 289 |
* @since 1.1.0 |
| 290 |
*/ |
| 291 |
const ErrorContainer& getErrors() const { |
| 292 |
return _errors; |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Gets the parsed instruction. |
| 297 |
* |
| 298 |
* @return The parsed instruction. |
| 299 |
* @since 1.1.0 |
| 300 |
*/ |
| 301 |
const Instruction& getInstruction() const { |
| 302 |
return _instruction; |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Gets declared symbol is there is any. |
| 307 |
* @return Gets symbol if there is any. |
| 308 |
* @since 1.1.0 |
| 309 |
*/ |
| 310 |
const Nullable<Symbol>& getSymbol() const { |
| 311 |
return _symbol; |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* Cleans the parser result. |
| 316 |
* @since 1.1.0 |
| 317 |
*/ |
| 318 |
void clean() { |
| 319 |
_errors.clean(); |
| 320 |
_symbol.setNotNull(false); |
| 321 |
_symbol.setValue(Symbol()); |
| 322 |
} |
| 323 |
|
| 324 |
protected: |
| 325 |
|
| 326 |
friend class Parser; |
| 327 |
|
| 328 |
/** |
| 329 |
* Sets error container for the context. |
| 330 |
* @param errors A new error container. |
| 331 |
* @since 1.1.0 |
| 332 |
*/ |
| 333 |
void setErrors(const ErrorContainer &errors) { |
| 334 |
_errors = errors; |
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
* Sets an instruction for the container. |
| 339 |
* @param instruction The instruction. |
| 340 |
* @since 1.1.0 |
| 341 |
*/ |
| 342 |
void setInstruction(const Instruction &instruction) { |
| 343 |
_instruction = instruction; |
| 344 |
} |
| 345 |
|
| 346 |
/** |
| 347 |
* Sets symbol. |
| 348 |
* @param symbol The symbol. |
| 349 |
* @since 1.1.0 |
| 350 |
*/ |
| 351 |
void setSymbol(const Nullable<Symbol> &symbol) { |
| 352 |
_symbol = symbol; |
| 353 |
} |
| 354 |
|
| 355 |
private: |
| 356 |
|
| 357 |
/** Errors container. */ |
| 358 |
ErrorContainer _errors; |
| 359 |
/** Gets declared symbol. Take into account that it can be 'empty'. */ |
| 360 |
Nullable<Symbol> _symbol; |
| 361 |
/** The parsed instruction. */ |
| 362 |
Instruction _instruction; |
| 363 |
|
| 364 |
}; |
| 365 |
|
| 366 |
/** |
| 367 |
* Converts objects to their structures counterparts. |
| 368 |
* @since 1.1.0 |
| 369 |
* @remarks Internal API, not intended to be used outside. |
| 370 |
*/ |
| 371 |
class ParserTypeConverter { |
| 372 |
public: |
| 373 |
|
| 374 |
static void convert(ParserConfig &src, fcml_st_parser_config &dest) { |
| 375 |
dest.alloc_symbol_table_if_needed = src.isAllocSymbolTableIfNeeded(); |
| 376 |
dest.disable_symbols_declaration = src.isDisableSymbolsDeclaration(); |
| 377 |
dest.enable_error_messages = src.isEnableErrorMessages(); |
| 378 |
dest.ignore_undefined_symbols = src.isIgnoreUndefinedSymbols(); |
| 379 |
dest.override_labels = src.isOverrideLabels(); |
| 380 |
} |
| 381 |
|
| 382 |
}; |
| 383 |
|
| 384 |
/** Parser wrapper. |
| 385 |
* @since 1.1.0 |
| 386 |
*/ |
| 387 |
class Parser: protected DialectAware, protected SymbolTableAware { |
| 388 |
public: |
| 389 |
|
| 390 |
/** |
| 391 |
* Creates a parser instance for the given dialect. |
| 392 |
* |
| 393 |
* @param dialect The dialect instance. |
| 394 |
* @since 1.1.0 |
| 395 |
*/ |
| 396 |
Parser(const Dialect &dialect) : |
| 397 |
_dialect(dialect) { |
| 398 |
} |
| 399 |
|
| 400 |
/** |
| 401 |
* Parses instruction given in the parameters. |
| 402 |
* |
| 403 |
* @param ctx Parser context. |
| 404 |
* @param instruction Instruction mnemonic. |
| 405 |
* @param[out] parserResult Instruction result. |
| 406 |
* @return Error code. |
| 407 |
* @throw ParsingFailedException Parsing failed. |
| 408 |
* @since 1.1.0 |
| 409 |
*/ |
| 410 |
fcml_ceh_error parse(ParserContext &ctx, const fcml_cstring &instruction, |
| 411 |
ParserResult &parserResult) { |
| 412 |
|
| 413 |
// Prepare parser context. |
| 414 |
fcml_st_parser_context context = { 0 }; |
| 415 |
ParserTypeConverter::convert(ctx.getConfig(), context.configuration); |
| 416 |
context.ip = ctx.getIp(); |
| 417 |
SymbolTable *symbolTable = ctx.getSymbolTable(); |
| 418 |
context.symbol_table = |
| 419 |
(symbolTable) ? extractSymbolTable(*symbolTable) : NULL; |
| 420 |
context.dialect = extractDialect(_dialect); |
| 421 |
|
| 422 |
fcml_st_parser_result parser_result; |
| 423 |
::fcml_fn_parser_result_prepare(&parser_result); |
| 424 |
|
| 425 |
try { |
| 426 |
|
| 427 |
parserResult.clean(); |
| 428 |
|
| 429 |
// Prepare instruction. |
| 430 |
fcml_ceh_error error = ::fcml_fn_parse(&context, |
| 431 |
instruction.c_str(), &parser_result); |
| 432 |
|
| 433 |
ErrorContainer errorContainer; |
| 434 |
ErrorTypeConverter::convert(parser_result.errors, errorContainer); |
| 435 |
|
| 436 |
parserResult.setErrors(errorContainer); |
| 437 |
|
| 438 |
if (!error && !parser_result.instruction) { |
| 439 |
// Just in case, it should never happen. |
| 440 |
error = FCML_CEH_GEC_INTERNAL_ERROR; |
| 441 |
} |
| 442 |
|
| 443 |
if (error && ctx.getConfig().isThrowExceptionOnError()) { |
| 444 |
::fcml_fn_parser_result_free(&parser_result); |
| 445 |
throw ParsingFailedException( |
| 446 |
errorContainer.prepareErrorMessage( |
| 447 |
FCML_TEXT("Parsing failed")), errorContainer, |
| 448 |
error); |
| 449 |
} |
| 450 |
|
| 451 |
if (!error) { |
| 452 |
|
| 453 |
Instruction parsedInstruction; |
| 454 |
TypeConverter::convert(*(parser_result.instruction), |
| 455 |
parsedInstruction); |
| 456 |
|
| 457 |
parserResult.setInstruction(parsedInstruction); |
| 458 |
parserResult.setSymbol( |
| 459 |
parser_result.symbol ? |
| 460 |
Symbol(parser_result.symbol->symbol, |
| 461 |
parser_result.symbol->value) : |
| 462 |
Symbol()); |
| 463 |
|
| 464 |
} |
| 465 |
|
| 466 |
::fcml_fn_parser_result_free(&parser_result); |
| 467 |
|
| 468 |
} catch (std::exception &exc) { |
| 469 |
// If anything failed, free assembler results. |
| 470 |
::fcml_fn_parser_result_free(&parser_result); |
| 471 |
throw exc; |
| 472 |
} |
| 473 |
|
| 474 |
return FCML_CEH_GEC_NO_ERROR; |
| 475 |
} |
| 476 |
|
| 477 |
private: |
| 478 |
|
| 479 |
/** The dialect for the parser. */ |
| 480 |
const Dialect &_dialect; |
| 481 |
|
| 482 |
}; |
| 483 |
|
| 484 |
} |
| 485 |
|
| 486 |
#endif /* FCML_PARSER_HPP_ */ |