-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.h
65 lines (51 loc) · 1.54 KB
/
parser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#ifndef SRC_PARSER_
#define SRC_PARSER_
#include <string>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include "log.h"
namespace wvm
{
template <typename ModuleType, typename DecoderType>
class Parser
{
private:
std::string file_name_;
public:
Parser(std::string file_name);
~Parser();
std::string file_name();
std::shared_ptr<ModuleType> parse();
};
template <typename ModuleType, typename DecoderType>
Parser<ModuleType, DecoderType>::Parser(std::string file_name) : file_name_(file_name)
{
}
template <typename ModuleType, typename DecoderType>
Parser<ModuleType, DecoderType>::~Parser()
{
}
template <typename ModuleType, typename DecoderType>
std::string Parser<ModuleType, DecoderType>::file_name()
{
return file_name_;
}
template <typename ModuleType, typename DecoderType>
std::shared_ptr<ModuleType> Parser<ModuleType, DecoderType>::parse()
{
LOG("-> parse");
std::ifstream readable{file_name_, std::ifstream::binary};
if (!readable.is_open())
{
throw std::runtime_error("failed to open file.");
}
std::shared_ptr<DecoderType> decoder_ptr = std::make_shared<DecoderType>(readable);
std::shared_ptr<ModuleType> module_ptr = std::make_shared<ModuleType>(decoder_ptr);
module_ptr->parseMagicNumber();
module_ptr->parseVersion();
module_ptr->parseSection();
return module_ptr;
}
}
#endif // SRC_PARSER_