Convert Decoder.decode_code_point return type to a SumType
This commit is contained in:
parent
9895733a05
commit
d466189982
@ -2,8 +2,8 @@
|
|||||||
module <%= @grammar.modulename %>;
|
module <%= @grammar.modulename %>;
|
||||||
|
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
import std.stdio;
|
import std.stdio;
|
||||||
|
import std.sumtype;
|
||||||
|
|
||||||
<% @grammar.code_blocks.each do |code| %>
|
<% @grammar.code_blocks.each do |code| %>
|
||||||
<%= code %>
|
<%= code %>
|
||||||
@ -71,54 +71,23 @@ class <%= @classname %>
|
|||||||
|
|
||||||
static class Decoder
|
static class Decoder
|
||||||
{
|
{
|
||||||
struct Result
|
struct Success
|
||||||
{
|
{
|
||||||
enum Type
|
|
||||||
{
|
|
||||||
SUCCESS,
|
|
||||||
EOF,
|
|
||||||
DECODE_ERROR,
|
|
||||||
}
|
|
||||||
private Type type;
|
|
||||||
CodePoint code_point;
|
CodePoint code_point;
|
||||||
uint code_point_length;
|
uint code_point_length;
|
||||||
|
|
||||||
static Result success(CodePoint code_point, uint code_point_length)
|
|
||||||
{
|
|
||||||
return Result(Type.SUCCESS, code_point, code_point_length);
|
|
||||||
}
|
}
|
||||||
|
struct EOF {}
|
||||||
|
struct DecodeError {}
|
||||||
|
|
||||||
bool is_success()
|
static SumType!(
|
||||||
{
|
Success,
|
||||||
return type == Type.SUCCESS;
|
EOF,
|
||||||
}
|
DecodeError)
|
||||||
|
decode_code_point(string input)
|
||||||
static Result eof()
|
|
||||||
{
|
|
||||||
return Result(Type.EOF);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool is_eof()
|
|
||||||
{
|
|
||||||
return type == Type.EOF;
|
|
||||||
}
|
|
||||||
|
|
||||||
static Result decode_error()
|
|
||||||
{
|
|
||||||
return Result(Type.DECODE_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool is_decode_error()
|
|
||||||
{
|
|
||||||
return type == Type.DECODE_ERROR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static Result decode_code_point(string input)
|
|
||||||
{
|
{
|
||||||
if (input.length == 0u)
|
if (input.length == 0u)
|
||||||
{
|
{
|
||||||
return Result.eof();
|
return cast(ReturnType!decode_code_point)EOF();
|
||||||
}
|
}
|
||||||
char c = input[0];
|
char c = input[0];
|
||||||
CodePoint code_point;
|
CodePoint code_point;
|
||||||
@ -158,11 +127,11 @@ class <%= @classname %>
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return Result.decode_error();
|
return cast(ReturnType!decode_code_point)DecodeError();
|
||||||
}
|
}
|
||||||
if (input.length <= following_bytes)
|
if (input.length <= following_bytes)
|
||||||
{
|
{
|
||||||
return Result.decode_error();
|
return cast(ReturnType!decode_code_point)DecodeError();
|
||||||
}
|
}
|
||||||
code_point_length = following_bytes + 1u;
|
code_point_length = following_bytes + 1u;
|
||||||
for (size_t i = 0u; i < following_bytes; i++)
|
for (size_t i = 0u; i < following_bytes; i++)
|
||||||
@ -170,12 +139,12 @@ class <%= @classname %>
|
|||||||
char b = input[i + 1u];
|
char b = input[i + 1u];
|
||||||
if ((b & 0xC0u) != 0x80u)
|
if ((b & 0xC0u) != 0x80u)
|
||||||
{
|
{
|
||||||
return Result.decode_error();
|
return cast(ReturnType!decode_code_point)DecodeError();
|
||||||
}
|
}
|
||||||
code_point = (code_point << 6u) | (b & 0x3Fu);
|
code_point = (code_point << 6u) | (b & 0x3Fu);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Result.success(code_point, code_point_length);
|
return cast(ReturnType!decode_code_point)Success(code_point, code_point_length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -498,14 +467,10 @@ class <%= @classname %>
|
|||||||
uint current_state = modes[m_mode].state_table_offset;
|
uint current_state = modes[m_mode].state_table_offset;
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
auto decoded = Decoder.decode_code_point(m_input[(m_input_position + attempt_match.length)..(m_input.length)]);
|
|
||||||
if (decoded.is_decode_error())
|
|
||||||
{
|
|
||||||
return FindLongestMatchResult.decode_error();
|
|
||||||
}
|
|
||||||
bool lex_continue = false;
|
bool lex_continue = false;
|
||||||
if (decoded.is_eof())
|
Decoder.decode_code_point(m_input[(m_input_position + attempt_match.length)..(m_input.length)]).match!(
|
||||||
{
|
(Decoder.DecodeError de) {return FindLongestMatchResult.decode_error();},
|
||||||
|
(Decoder.EOF e) {
|
||||||
/* We hit EOF. */
|
/* We hit EOF. */
|
||||||
if (longest_match.length > 0)
|
if (longest_match.length > 0)
|
||||||
{
|
{
|
||||||
@ -522,9 +487,8 @@ class <%= @classname %>
|
|||||||
/* Valid EOF return. */
|
/* Valid EOF return. */
|
||||||
return FindLongestMatchResult.eof();
|
return FindLongestMatchResult.eof();
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
else
|
(Decoder.Success decoded) {
|
||||||
{
|
|
||||||
auto transition_result = transition(current_state, decoded.code_point);
|
auto transition_result = transition(current_state, decoded.code_point);
|
||||||
if (transition_result.found())
|
if (transition_result.found())
|
||||||
{
|
{
|
||||||
@ -554,7 +518,7 @@ class <%= @classname %>
|
|||||||
{
|
{
|
||||||
return FindLongestMatchResult.unexpected_input(attempt_match.length + decoded.code_point_length);
|
return FindLongestMatchResult.unexpected_input(attempt_match.length + decoded.code_point_length);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user