-
Notifications
You must be signed in to change notification settings - Fork 2
/
actionscollection.h
150 lines (126 loc) · 5.42 KB
/
actionscollection.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#pragma once
// #include "actionscollection.h"
#include <list>
#include <memory>
#include <string_view>
#include <vector>
#include "dictionary.h"
#include "jsonparser.h"
namespace bpatch
{
/// <summary>
/// class contains main entry points for processing.
/// * JSON parser callback to load settings for processing
/// * pipeline to process binary lexemes
/// </summary>
class ActionsCollection final: public TJsonCallBack, public StreamReplacer
{
public:
/// <summary>
/// constructor accepts vector with json data and process it
/// </summary>
/// <param name="dataSource">json data - vecor data will be held inside and modified
/// for inner simplicity of usage</param>
/// <param name="pLast">StreamReplacer to pass result of actions
/// in the end of processing</param>
ActionsCollection(std::vector<char>&& dataSource);
/// <summary>
/// callback from TJsonCallBack
/// </summary>
/// <param name="pJson">Json object just got its name in parser</param>
void OnJsonObjectBegins(TJSONObject* const pJson) override;
/// <summary>
/// callback from TJsonCallBack
/// </summary>
/// <param name="pJson">Json object just ends in parser</param>
void OnJsonObjectParsed(TJSONObject* const pJson) override;
/// <summary>
/// callback from TJsonCallBack
/// </summary>
/// <param name="pJson">Json array just begins in parser</param>
void OnJsonArrayBegins(TJSONObject* const pJson) override;
/// <summary>
/// callback from TJsonCallBack
/// </summary>
/// <param name="pJson">Json array just ends in parser</param>
void OnJsonArrayParsed(TJSONObject* const pJson) override;
/// <summary>
/// callback from StreamReplacer
/// </summary>
/// <param name="toProcess">this char is under processing right now</param>
/// <param name="aEod">this is sign that no more data</param>
void DoReplacements(const char toProcess, const bool aEod) const override;
/// <summary>
/// Set next replacer in chain of replacers
/// </summary>
/// <param name="pNext">replacer to call next</param>
virtual void SetNextReplacer(std::unique_ptr<StreamReplacer>&& pNext) override;
protected:
/// <summary>
/// throws error if we meet error in the expected logic
/// </summary>
/// <param name="message">text of the error</param>
static void ReportError(const char* const message);
/// <summary>
/// throws error if we meet duplicate name of lexeme
/// </summary>
/// <param name="aname">duplicate name of lexeme</param>
static void ReportDuplicateNameError(const std::string_view aname);
/// <summary>
/// throws error if we cannot find lexeme for composite lexeme
/// </summary>
/// <param name="aname">nonexistent name of lexeme</param>
static void ReportMissedNameError(const std::string_view aname);
/// <summary>
/// parse array of either 10 based integers or array of hex based strings
/// </summary>
/// <param name="pJson">array is inside</param>
/// <param name="base">is the base 10 or 16 - set 10 or 16 here</param>
/// <param name="errorMsg">error mesage for the case of parsing error</param>
/// <returns>result string view with data
/// data will be located in memory of main json vector (data_)</returns>
std::string_view ParseDictionaryArray(TJSONObject* const pJson, const int base, const char* const errorMsg);
/// <summary>
/// to parse arrays (composite)
/// </summary>
/// <param name="pJson">array with strings is here</param>
/// <param name="errorMsg">in case we have an object inside. or the result has no data</param>
/// <returns>vector with string_views</returns>
std::vector<std::string_view> ParseArray(TJSONObject* const pJson, const char* const errorMsg);
/// <summary>
/// we are creating and registering composite lexemes inside
/// we are finalizing initialization of dictionary
/// </summary>
void ProcessComposites();
/// <summary>
/// we are creating and making chain of replacers
/// last operation in initialization - dictionary is 100% ready
/// </summary>
void CreateChainOfReplacers();
protected:
// this is data for json parsing
// all lexemes from json file is inside
std::vector<char> jsondata_;
// here we hold all lexemes from our json
Dictionary dictionary_;
/// intermediate collections
/// since we are not expecting ordered data from json
/// = composite sets could be first, we accumulate composites. Than will parse
std::vector<std::pair<std::string_view, std::vector<std::string_view>>> composites_;
// replaces pairs
// need to be processed last
typedef std::pair<std::string_view, std::string_view> StringviewPair; // src + trg
typedef std::vector<StringviewPair> VectorStringviewPairs; // all pairs from one replace
/// <summary>
/// here we are holding chain of the replacers
/// </summary>
std::unique_ptr<StreamReplacer> replacersChain_;
/// <summary>
/// holds specific StreamReplacer which allows to change last Replacer in chain
/// </summary>
std::unique_ptr<StreamReplacer>* replacersLast_ = nullptr;
private:
// all replaces, will be cleared after initialization; need temporary object for loading/initialization only
std::vector<VectorStringviewPairs> replaces_;
}; // class ActionsCollection
};// namespace bpatch