Add UserCodeID struct to represent user code IDs

This commit is contained in:
Josh Holtrop 2023-03-13 21:56:18 -04:00
parent 058945e08b
commit e432f62b05
2 changed files with 40 additions and 9 deletions

View File

@ -45,12 +45,12 @@ class <%= @classname %>
return Token(count);
}
bool is_valid()
bool is_valid() const
{
return token < count;
}
bool is_invalid()
bool is_invalid() const
{
return !is_valid();
}
@ -190,12 +190,41 @@ class <%= @classname %>
uint destination;
}
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
{
uint transition_table_index;
uint n_transitions;
uint token;
uint code_id;
UserCodeID code_id;
}
private struct Mode
@ -215,7 +244,11 @@ class <%= @classname %>
State(<%= state_table_entry[:transition_table_index] %>u,
<%= state_table_entry[:n_transitions] %>u,
<%= state_table_entry[:token] %>u,
<%= state_table_entry[:code_id] %>u),
<% if state_table_entry[:code_id] %>
UserCodeID(<%= state_table_entry[:code_id] %>u)),
<% else %>
UserCodeID.invalid()),
<% end %>
<% end %>
];
@ -284,7 +317,7 @@ class <%= @classname %>
* @return Token ID to accept, or _TOKEN_COUNT if the user code does
* not explicitly return a token.
*/
private uint user_code(uint code_id, string match, Result * result)
private uint user_code(UserCodeID code_id, string match, Result * result)
{
switch (code_id)
{
@ -316,7 +349,7 @@ class <%= @classname %>
if (match_info.accepting_state != null)
{
uint token_to_accept = match_info.accepting_state.token;
if (match_info.accepting_state.code_id != 0xFFFF_FFFFu)
if (match_info.accepting_state.code_id.is_valid())
{
uint user_code_token = user_code(match_info.accepting_state.code_id, m_input[m_input_position..(m_input_position + match_info.length)], &result);
/* A return of _TOKEN_COUNT from user_code() means
@ -386,7 +419,7 @@ class <%= @classname %>
}
current_state = dest;
if ((states[current_state].token != _TOKEN_COUNT) ||
(states[current_state].code_id != 0xFFFF_FFFFu))
(states[current_state].code_id.is_valid()))
{
attempt_match_info.accepting_state = &states[current_state];
*match_info = attempt_match_info;

View File

@ -37,8 +37,6 @@ class Propane
code_id =
if state.accepts && state.accepts.code_id
state.accepts.code_id
else
0xFFFF_FFFF
end
state_table << {
transition_table_index: transition_table.size,