Compare commits

..

No commits in common. "d466189982a1705a4b134a023343c28332bf828b" and "11348ca351884573fa44b444c6cd28afe678039f" have entirely different histories.

2 changed files with 110 additions and 75 deletions

View File

@ -2,8 +2,8 @@
module <%= @grammar.modulename %>;
<% end %>
import std.stdio;
import std.sumtype;
<% @grammar.code_blocks.each do |code| %>
<%= code %>
@ -56,7 +56,7 @@ class <%= @classname %>
alias CodePoint = uint;
static immutable string[] token_names = [
static immutable string token_names[] = [
<% @grammar.tokens.each_with_index do |token, index| %>
"<%= token.name %>",
<% end %>
@ -71,23 +71,54 @@ class <%= @classname %>
static class Decoder
{
struct Success
struct Result
{
enum Type
{
SUCCESS,
EOF,
DECODE_ERROR,
}
private Type type;
CodePoint code_point;
uint code_point_length;
}
struct EOF {}
struct DecodeError {}
static SumType!(
Success,
EOF,
DecodeError)
decode_code_point(string input)
static Result success(CodePoint code_point, uint code_point_length)
{
return Result(Type.SUCCESS, code_point, code_point_length);
}
bool is_success()
{
return type == Type.SUCCESS;
}
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)
{
return cast(ReturnType!decode_code_point)EOF();
return Result.eof();
}
char c = input[0];
CodePoint code_point;
@ -127,11 +158,11 @@ class <%= @classname %>
}
else
{
return cast(ReturnType!decode_code_point)DecodeError();
return Result.decode_error();
}
if (input.length <= following_bytes)
{
return cast(ReturnType!decode_code_point)DecodeError();
return Result.decode_error();
}
code_point_length = following_bytes + 1u;
for (size_t i = 0u; i < following_bytes; i++)
@ -139,12 +170,12 @@ class <%= @classname %>
char b = input[i + 1u];
if ((b & 0xC0u) != 0x80u)
{
return cast(ReturnType!decode_code_point)DecodeError();
return Result.decode_error();
}
code_point = (code_point << 6u) | (b & 0x3Fu);
}
}
return cast(ReturnType!decode_code_point)Success(code_point, code_point_length);
return Result.success(code_point, code_point_length);
}
}
@ -206,13 +237,13 @@ class <%= @classname %>
}
<% transition_table, state_table, mode_table = @lexer.build_tables %>
private static immutable Transition[] transitions = [
private static immutable Transition transitions[] = [
<% transition_table.each do |transition_table_entry| %>
Transition(<%= transition_table_entry[:first] %>u, <%= transition_table_entry[:last] %>u, <%= transition_table_entry[:destination] %>u),
<% end %>
];
private static immutable State[] states = [
private static immutable State states[] = [
<% state_table.each do |state_table_entry| %>
State(<%= state_table_entry[:transition_table_index] %>u,
<%= state_table_entry[:n_transitions] %>u,
@ -230,7 +261,7 @@ class <%= @classname %>
<% end %>
];
private static immutable Mode[] modes = [
private static immutable Mode modes[] = [
<% mode_table.each do |mode_table_entry| %>
Mode(<%= mode_table_entry[:state_table_offset] %>),
<% end %>
@ -467,58 +498,63 @@ class <%= @classname %>
uint current_state = modes[m_mode].state_table_offset;
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;
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. */
if (longest_match.length > 0)
{
/* We have a match, so use it. */
return longest_match;
}
else if (attempt_match.length != 0)
{
/* There is a partial match - error! */
return FindLongestMatchResult.unexpected_input(attempt_match.length);
}
else
{
/* Valid EOF return. */
return FindLongestMatchResult.eof();
}
},
(Decoder.Success decoded) {
auto transition_result = transition(current_state, decoded.code_point);
if (transition_result.found())
{
lex_continue = true;
attempt_match.length += decoded.code_point_length;
if (decoded.code_point == '\n')
{
attempt_match.delta_row++;
attempt_match.delta_col = 0u;
}
else
{
attempt_match.delta_col++;
}
current_state = transition_result.destination();
if (states[current_state].accepts())
{
attempt_match.accepting_state = &states[current_state];
longest_match = attempt_match;
}
}
else if (longest_match.length > 0)
{
return longest_match;
}
else
{
return FindLongestMatchResult.unexpected_input(attempt_match.length + decoded.code_point_length);
}
});
if (decoded.is_eof())
{
/* We hit EOF. */
if (longest_match.length > 0)
{
/* We have a match, so use it. */
return longest_match;
}
else if (attempt_match.length != 0)
{
/* There is a partial match - error! */
return FindLongestMatchResult.unexpected_input(attempt_match.length);
}
else
{
/* Valid EOF return. */
return FindLongestMatchResult.eof();
}
}
else
{
auto transition_result = transition(current_state, decoded.code_point);
if (transition_result.found())
{
lex_continue = true;
attempt_match.length += decoded.code_point_length;
if (decoded.code_point == '\n')
{
attempt_match.delta_row++;
attempt_match.delta_col = 0u;
}
else
{
attempt_match.delta_col++;
}
current_state = transition_result.destination();
if (states[current_state].accepts())
{
attempt_match.accepting_state = &states[current_state];
longest_match = attempt_match;
}
}
else if (longest_match.length > 0)
{
return longest_match;
}
else
{
return FindLongestMatchResult.unexpected_input(attempt_match.length + decoded.code_point_length);
}
}
}
}
@ -606,19 +642,19 @@ class <%= @classname %>
}
<% state_table, shift_table, reduce_table = @parser.build_tables %>
private static immutable Shift[] shifts = [
private static immutable Shift shifts[] = [
<% shift_table.each do |shift| %>
Shift(<%= shift[:token_id] %>u, <%= shift[:state_id] %>u),
<% end %>
];
private static immutable Reduce[] reduces = [
private static immutable Reduce reduces[] = [
<% reduce_table.each do |reduce| %>
Reduce(<%= reduce[:token_id] %>u, <%= reduce[:rule_id] %>u, <%= reduce[:rule_set_id] %>u, <%= reduce[:n_states] %>u),
<% end %>
];
private static immutable State[] states = [
private static immutable State states[] = [
<% state_table.each do |state| %>
State(<%= state[:shift_index] %>u, <%= state[:n_shifts] %>u, <%= state[:reduce_index] %>u, <%= state[:n_reduces] %>u),
<% end %>

View File

@ -20,7 +20,7 @@ describe Propane do
end
def compile(*test_files)
result = system(*%w[ldc2 --unittest -of spec/run/testparser spec/run/testparser.d -Ispec], *test_files)
result = system(*%w[gdc -funittest -o spec/run/testparser spec/run/testparser.d -Ispec], *test_files)
expect(result).to be_truthy
end
@ -28,7 +28,6 @@ describe Propane do
stdout, stderr, status = Open3.capture3("spec/run/testparser")
File.binwrite("spec/run/.stderr", stderr)
File.binwrite("spec/run/.stdout", stdout)
stderr.sub!(/^.*modules passed unittests\n/, "")
Results.new(stdout, stderr, status)
end