123 lines
5 KiB
C++
123 lines
5 KiB
C++
/*****************************************************************************
|
|
* Copyright (c) 2020, rrcc FM10K-Documentation Contributors
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions are met:
|
|
*
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
* this list of conditions and the following disclaimer.
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
* * Neither the name of the copyright holder nor the names of its contributors
|
|
* may be used to endorse or promote products derived from this software
|
|
* without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*****************************************************************************/
|
|
|
|
#pragma once
|
|
|
|
|
|
#include <cstdint>
|
|
#include <utility>
|
|
#include <vector>
|
|
#include <algorithm>
|
|
#include <unordered_map>
|
|
#include "Function.h"
|
|
|
|
class Parser {
|
|
size_t offset;
|
|
const std::vector<uint8_t> &bytes;
|
|
std::vector<Function> functions;
|
|
std::unordered_map<std::string, std::vector<Token>> variables;
|
|
std::unordered_map<uint32_t, std::string> reservations;
|
|
|
|
|
|
uint8_t getByte() {
|
|
return bytes.size() <= offset ? 0 : bytes.at(offset++);
|
|
}
|
|
|
|
static std::vector<uint8_t> getAlphaValues() {
|
|
return {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
|
|
'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
|
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', '.',};
|
|
}
|
|
|
|
static std::vector<uint8_t> getAlphanumericValues() {
|
|
return {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
|
|
'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
|
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', '.', '0', '1', '2', '3', '4', '5', '6', '7', '8',
|
|
'9'};
|
|
}
|
|
|
|
static std::vector<uint8_t> getOperatorValues() {
|
|
return {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
|
|
'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
|
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '_', '~', '0', '1', '2', '3', '4', '5', '6', '7', '8',
|
|
'9'};
|
|
}
|
|
|
|
static std::vector<uint8_t> getNumericValues() {
|
|
return {'x', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D',
|
|
'E', 'F'};
|
|
}
|
|
|
|
static bool isIteratorAllowedValues(std::string::const_iterator it, std::string::const_iterator end,
|
|
const std::vector<uint8_t> &values);
|
|
|
|
static bool isAllowedValues(uint8_t c, const std::vector<uint8_t> &values);
|
|
|
|
static bool isOperator(const std::string &v);
|
|
|
|
static bool isRegisterLocation(const std::string &v);
|
|
|
|
static bool isCodeLabel(const std::string &v);
|
|
|
|
static bool isCodeLocation(const std::string &v);
|
|
|
|
static bool isImmediate(const std::string &v);
|
|
|
|
static bool isLocalLabel(const std::string &v);
|
|
|
|
static bool isLabel(const std::string &v);
|
|
|
|
static bool isOffset(const std::string &v);
|
|
|
|
static bool isDirective(const std::string &v);
|
|
|
|
static bool isAlphaNumeric(const std::string &v);
|
|
|
|
public:
|
|
Parser(const std::vector<uint8_t> &bytes, std::unordered_map<std::string, std::vector<Token>> variables, std::unordered_map<uint32_t, std::string> reservations) : offset(0), bytes(bytes), variables(std::move(variables)), reservations(std::move(reservations)) {
|
|
|
|
}
|
|
|
|
Parser(const std::vector<uint8_t> &bytes) : offset(0), bytes(bytes) {
|
|
|
|
}
|
|
|
|
void parse();
|
|
|
|
const std::vector<Function> &getTree() const {
|
|
return functions;
|
|
}
|
|
|
|
const auto &getVariables() const {
|
|
return variables;
|
|
}
|
|
|
|
const auto &getReservations() const {
|
|
return reservations;
|
|
}
|
|
};
|