Use plain integer type for UserCodeID

This commit is contained in:
Josh Holtrop 2023-07-08 17:01:54 -04:00
parent 36ad6e0d1b
commit 6327bd1e96

View File

@ -167,6 +167,9 @@ class <%= @classname %>
{ {
alias StateID = <%= get_type_for(@lexer.state_table.size) %>; alias StateID = <%= get_type_for(@lexer.state_table.size) %>;
enum StateID INVALID_STATE_ID = <%= @lexer.state_table.size %>u; enum StateID INVALID_STATE_ID = <%= @lexer.state_table.size %>u;
<% user_code_id_count = (@grammar.patterns.map(&:code_id).compact.max || 0) + 1 %>
alias UserCodeID = <%= get_type_for(user_code_id_count) %>;
enum UserCodeID INVALID_USER_CODE_ID = <%= user_code_id_count %>u;
private struct Transition private struct Transition
{ {
@ -175,35 +178,6 @@ class <%= @classname %>
StateID destination_state; StateID destination_state;
} }
private struct UserCodeID
{
enum count = <%= (@grammar.patterns.map(&:code_id).compact.max || -1) + 1%>;
uint user_code_id;
alias user_code_id this;
@disable this();
this(uint user_code_id)
{
this.user_code_id = user_code_id;
}
static UserCodeID invalid()
{
return UserCodeID(count);
}
bool is_valid() const
{
return user_code_id < count;
}
bool is_invalid() const
{
return !is_valid();
}
}
private struct State private struct State
{ {
<%= get_type_for(@lexer.transition_table.size - 1) %> transition_table_index; <%= get_type_for(@lexer.transition_table.size - 1) %> transition_table_index;
@ -236,9 +210,9 @@ class <%= @classname %>
Token.invalid(), Token.invalid(),
<% end %> <% end %>
<% if state_table_entry[:code_id] %> <% if state_table_entry[:code_id] %>
UserCodeID(<%= state_table_entry[:code_id] %>u), <%= state_table_entry[:code_id] %>u,
<% else %> <% else %>
UserCodeID.invalid(), INVALID_USER_CODE_ID,
<% end %> <% end %>
<%= state_table_entry[:accepts] %>), <%= state_table_entry[:accepts] %>),
<% end %> <% end %>
@ -341,7 +315,7 @@ class <%= @classname %>
{ {
case P_SUCCESS: case P_SUCCESS:
uint token_to_accept = match_info.accepting_state.token; uint token_to_accept = match_info.accepting_state.token;
if (match_info.accepting_state.code_id.is_valid()) if (match_info.accepting_state.code_id != INVALID_USER_CODE_ID)
{ {
Token user_code_token = user_code(match_info.accepting_state.code_id, m_input[m_input_position..(m_input_position + match_info.length)], &token_info); Token user_code_token = user_code(match_info.accepting_state.code_id, m_input[m_input_position..(m_input_position + match_info.length)], &token_info);
/* An invalid Token from user_code() means that the user /* An invalid Token from user_code() means that the user