This repository was archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecoder.cpp
More file actions
165 lines (144 loc) · 4.23 KB
/
decoder.cpp
File metadata and controls
165 lines (144 loc) · 4.23 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <cstring>
#include <utility>
#include <iostream>
#include "decoder.hpp"
#include "utils.hpp"
namespace lib_ruby_parser
{
extern "C"
{
void LIB_RUBY_PARSER_drop_input_error(InputError *input_error);
void LIB_RUBY_PARSER_drop_decoder_result(DecoderResult *decoder_result);
}
// input error
InputError::Value::Value()
{
std::memset(this, 0, sizeof(Value));
}
InputError::Value::Value(InputError::Value &&other)
{
std::memcpy(this, &other, sizeof(Value));
std::memset(&other, 0, sizeof(Value));
}
InputError::Value::~Value() {}
InputError::Value &InputError::Value::operator=(InputError::Value &&other)
{
std::memcpy(this, &other, sizeof(Value));
std::memset(&other, 0, sizeof(Value));
return *this;
}
InputError::InputError(Tag tag_,
Value as_) : tag(tag_),
as(std::move(as_)) {}
InputError InputError::UnsupportedEncoding(String unsupported_encoding)
{
InputError::Value value;
value.unsupported_encoding = std::move(unsupported_encoding);
return InputError(InputError::Tag::UNSUPPORTED_ENCODING, std::move(value));
}
InputError InputError::DecodingError(String decoding_error)
{
InputError::Value value;
value.decoding_error = std::move(decoding_error);
return InputError(InputError::Tag::DECODING_ERROR, std::move(value));
}
InputError::~InputError()
{
LIB_RUBY_PARSER_drop_input_error(this);
}
// decoder result
DecoderResult::Value::Value()
{
std::memset(this, 0, sizeof(Value));
}
DecoderResult::Value::Value(Value &&other)
{
std::memcpy(this, &other, sizeof(Value));
std::memset(&other, 0, sizeof(Value));
}
DecoderResult::Value::~Value() {}
DecoderResult::Value &DecoderResult::Value::operator=(DecoderResult::Value &&other)
{
std::memcpy(this, &other, sizeof(Value));
std::memset(&other, 0, sizeof(Value));
return *this;
}
DecoderResult::DecoderResult(Tag tag_,
Value as_) : tag(tag_),
as(std::move(as_)) {}
DecoderResult::~DecoderResult()
{
LIB_RUBY_PARSER_drop_decoder_result(this);
}
DecoderResult DecoderResult::Ok(ByteList decoded)
{
DecoderResult::Value value;
value.ok = std::move(decoded);
return DecoderResult(DecoderResult::Tag::OK, std::move(value));
}
DecoderResult DecoderResult::Err(InputError err)
{
DecoderResult::Value value;
value.err = std::move(err);
return DecoderResult(DecoderResult::Tag::ERR, std::move(value));
}
// decoder
String string_from_string_blob(StringBlob blob)
{
union U
{
String t;
StringBlob b;
U() { std::memset(this, 0, sizeof(U)); }
~U() {}
};
U u;
u.b = blob;
return std::move(u.t);
}
ByteList byte_list_from_byte_list_blob(ByteListBlob blob)
{
union U
{
ByteList t;
ByteListBlob b;
U() { std::memset(this, 0, sizeof(U)); }
~U() {}
};
U u;
u.b = blob;
return std::move(u.t);
}
DecoderResultBlob decoder_result_to_blob(DecoderResult decoder_result)
{
union U
{
DecoderResult t;
DecoderResultBlob b;
U() { std::memset(this, 0, sizeof(U)); }
~U() {}
};
U u;
u.t = std::move(decoder_result);
return u.b;
}
Decoder::Decoder(DecoderFunction f_, void *state_) : f(f_), state(state_) {}
// maybe decoder
MaybeDecoder::MaybeDecoder(Decoder decoder_) : decoder(decoder_) {}
MaybeDecoder MaybeDecoder::Some(Decoder decoder_)
{
return MaybeDecoder(decoder_);
}
MaybeDecoder MaybeDecoder::None()
{
return MaybeDecoder(Decoder(nullptr, nullptr));
}
bool MaybeDecoder::is_some() const
{
return this->decoder.f != nullptr;
}
bool MaybeDecoder::is_none() const
{
return this->decoder.f == nullptr;
}
}