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