openMSX
SymbolManager.hh
Go to the documentation of this file.
1#ifndef SYMBOL_MANAGER_HH
2#define SYMBOL_MANAGER_HH
3
4#include "function_ref.hh"
5#include "hash_map.hh"
6#include "zstring_view.hh"
7
8#include <cassert>
9#include <cstdint>
10#include <optional>
11#include <span>
12#include <string>
13#include <string_view>
14#include <vector>
15
16namespace openmsx {
17
18class CommandController;
19
20struct Symbol
21{
22 Symbol(std::string n, uint16_t v)
23 : name(std::move(n)), value(v) {} // clang-15 workaround
24
25 std::string name;
26 uint16_t value;
27
28 auto operator<=>(const Symbol&) const = default;
29};
30
32{
33 enum class Type {
34 AUTO_DETECT = 0,
35 ASMSX,
36 GENERIC, // includes PASMO, SJASM, TNIASM0, TNIASM1
37 HTC,
38 LINKMAP,
39 NOICE,
40 VASM,
41
43 LAST = VASM + 1,
44 };
45 [[nodiscard]] static zstring_view toString(Type type);
46 [[nodiscard]] static std::optional<Type> parseType(std::string_view str);
47
48 std::string filename;
49 std::vector<Symbol> symbols;
51};
52
54{
55 virtual void notifySymbolsChanged() = 0;
56};
57
59{
60public:
61 explicit SymbolManager(CommandController& commandController);
62
63 void setObserver(SymbolObserver* observer_) {
64 assert(!observer || !observer_);
65 observer = observer_;
66 }
67
68 // Load or reload a file.
69 // * Throws on failure to read from file. In that case the existing file is left unchanged.
70 // * Parse errors in the file are ignored.
71 // * When the file contains no symbols and when 'allowEmpty=false' the
72 // existing file is not replaced and this method returns 'false'.
73 // Otherwise it return 'true'.
74 enum class LoadEmpty { ALLOWED, NOT_ALLOWED };
75 bool reloadFile(const std::string& filename, LoadEmpty loadEmpty, SymbolFile::Type type);
76
77 void removeFile(std::string_view filename);
78 void removeAllFiles();
79
80 [[nodiscard]] const auto& getFiles() const { return files; }
81 [[nodiscard]] std::span<Symbol const * const> lookupValue(uint16_t value);
82 [[nodiscard]] std::optional<uint16_t> parseSymbolOrValue(std::string_view s) const;
83
84 [[nodiscard]] static std::string getFileFilters();
85 [[nodiscard]] static SymbolFile::Type getTypeForFilter(std::string_view filter);
86
87//private:
88 // These should not be called directly, except by the unittest
89 [[nodiscard]] static std::optional<unsigned> isHexDigit(char c);
90 [[nodiscard]] static std::optional<uint16_t> is4DigitHex(std::string_view s);
91 [[nodiscard]] static std::optional<uint16_t> parseValue(std::string_view str);
92 [[nodiscard]] static std::optional<Symbol> checkLabel(std::string_view label, uint16_t value);
93 [[nodiscard]] static std::optional<Symbol> checkLabelAndValue(std::string_view label, std::string_view value);
94 [[nodiscard]] static SymbolFile::Type detectType(const std::string& filename, std::string_view buffer);
95 [[nodiscard]] static SymbolFile loadLines(
96 const std::string& filename, std::string_view buffer, SymbolFile::Type type,
97 function_ref<std::optional<Symbol>(std::span<std::string_view>)> lineParser);
98 [[nodiscard]] static SymbolFile loadGeneric(const std::string& filename, std::string_view buffer);
99 [[nodiscard]] static SymbolFile loadNoICE(const std::string& filename, std::string_view buffer);
100 [[nodiscard]] static SymbolFile loadHTC(const std::string& filename, std::string_view buffer);
101 [[nodiscard]] static SymbolFile loadVASM(const std::string& filename, std::string_view buffer);
102 [[nodiscard]] static SymbolFile loadASMSX(const std::string& filename, std::string_view buffer);
103 [[nodiscard]] static SymbolFile loadLinkMap(const std::string& filename, std::string_view buffer);
104 [[nodiscard]] static SymbolFile loadSymbolFile(const std::string& filename, SymbolFile::Type type);
105
106private:
107 void refresh();
108
109private:
110 CommandController& commandController;
111 SymbolObserver* observer = nullptr; // only one for now, could become a vector later
112 std::vector<SymbolFile> files;
113 hash_map<uint16_t, std::vector<const Symbol*>> lookupValueCache; // calculated from 'files'
114};
115
116
117} // namespace openmsx
118
119#endif
const auto & getFiles() const
static std::optional< unsigned > isHexDigit(char c)
bool reloadFile(const std::string &filename, LoadEmpty loadEmpty, SymbolFile::Type type)
std::span< Symbol const *const > lookupValue(uint16_t value)
static SymbolFile loadNoICE(const std::string &filename, std::string_view buffer)
static SymbolFile loadHTC(const std::string &filename, std::string_view buffer)
static SymbolFile loadSymbolFile(const std::string &filename, SymbolFile::Type type)
static SymbolFile loadASMSX(const std::string &filename, std::string_view buffer)
static std::string getFileFilters()
static std::optional< uint16_t > is4DigitHex(std::string_view s)
void removeFile(std::string_view filename)
static SymbolFile loadVASM(const std::string &filename, std::string_view buffer)
static SymbolFile loadLines(const std::string &filename, std::string_view buffer, SymbolFile::Type type, function_ref< std::optional< Symbol >(std::span< std::string_view >)> lineParser)
static std::optional< Symbol > checkLabel(std::string_view label, uint16_t value)
static std::optional< uint16_t > parseValue(std::string_view str)
static SymbolFile loadLinkMap(const std::string &filename, std::string_view buffer)
std::optional< uint16_t > parseSymbolOrValue(std::string_view s) const
static SymbolFile::Type getTypeForFilter(std::string_view filter)
static SymbolFile loadGeneric(const std::string &filename, std::string_view buffer)
void setObserver(SymbolObserver *observer_)
static std::optional< Symbol > checkLabelAndValue(std::string_view label, std::string_view value)
static SymbolFile::Type detectType(const std::string &filename, std::string_view buffer)
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
This file implemented 3 utility functions:
Definition Autofire.cc:11
STL namespace.
static std::optional< Type > parseType(std::string_view str)
static zstring_view toString(Type type)
std::vector< Symbol > symbols
virtual void notifySymbolsChanged()=0
auto operator<=>(const Symbol &) const =default
Symbol(std::string n, uint16_t v)
std::string name