Store whether a state accepts, not whether it drops

This commit is contained in:
Josh Holtrop 2023-07-08 08:29:33 -04:00
parent 8a377b4950
commit c88338698a
4 changed files with 7 additions and 24 deletions

View File

@ -191,12 +191,7 @@ class <%= @classname %>
uint n_transitions; uint n_transitions;
Token token; Token token;
UserCodeID code_id; UserCodeID code_id;
bool drop; bool accepts;
bool accepts() const
{
return drop || token.is_valid() || code_id.is_valid();
}
} }
private struct Mode private struct Mode
@ -225,7 +220,7 @@ class <%= @classname %>
<% else %> <% else %>
UserCodeID.invalid(), UserCodeID.invalid(),
<% end %> <% end %>
<%= state_table_entry[:drop] %>), <%= state_table_entry[:accepts] %>),
<% end %> <% end %>
]; ];
@ -269,7 +264,7 @@ class <%= @classname %>
for (;;) for (;;)
{ {
size_t result = attempt_lex_token(out_token_info); size_t result = attempt_lex_token(out_token_info);
if (out_token_info.token < _TOKEN_COUNT) if (result != P_DROP)
{ {
return result; return result;
} }
@ -340,7 +335,7 @@ class <%= @classname %>
{ {
m_input_col += match_info.delta_col; m_input_col += match_info.delta_col;
} }
if (match_info.accepting_state.drop) if (token_to_accept == _TOKEN_COUNT)
{ {
return P_DROP; return P_DROP;
} }
@ -410,7 +405,7 @@ class <%= @classname %>
attempt_match.delta_col++; attempt_match.delta_col++;
} }
current_state = transition_result.destination(); current_state = transition_result.destination();
if (states[current_state].accepts()) if (states[current_state].accepts)
{ {
attempt_match.accepting_state = &states[current_state]; attempt_match.accepting_state = &states[current_state];
longest_match = attempt_match; longest_match = attempt_match;

View File

@ -148,7 +148,7 @@ class Propane
end end
consume!(/\s+/) consume!(/\s+/)
consume!(/;/, "expected `;'") consume!(/;/, "expected `;'")
@patterns << Pattern.new(pattern: pattern, line_number: @line_number, drop: true, mode: @mode) @patterns << Pattern.new(pattern: pattern, line_number: @line_number, mode: @mode)
@mode = nil @mode = nil
true true
end end

View File

@ -24,7 +24,6 @@ class Propane
} }
states = mode_info[:dfa].enumerate states = mode_info[:dfa].enumerate
states.each do |state, id| states.each do |state, id|
drop = state.accepts && state.accepts.drop?
token = token =
if state.accepts && state.accepts.token if state.accepts && state.accepts.token
state.accepts.token.id state.accepts.token.id
@ -36,7 +35,7 @@ class Propane
state_table << { state_table << {
transition_table_index: transition_table.size, transition_table_index: transition_table.size,
n_transitions: state.transitions.size, n_transitions: state.transitions.size,
drop: drop, accepts: !!state.accepts,
token: token, token: token,
code_id: code_id, code_id: code_id,
} }

View File

@ -40,8 +40,6 @@ class Propane
# Optional parameters. # Optional parameters.
# @option options [String, nil] :code # @option options [String, nil] :code
# Code block to execute when the pattern is matched. # Code block to execute when the pattern is matched.
# @option options [Boolean] :drop
# Whether this is a drop pattern.
# @option options [String, nil] :pattern # @option options [String, nil] :pattern
# Pattern. # Pattern.
# @option options [Token, nil] :token # @option options [Token, nil] :token
@ -52,7 +50,6 @@ class Propane
# Lexer mode for this pattern. # Lexer mode for this pattern.
def initialize(options) def initialize(options)
@code = options[:code] @code = options[:code]
@drop = options[:drop]
@pattern = options[:pattern] @pattern = options[:pattern]
@token = options[:token] @token = options[:token]
@line_number = options[:line_number] @line_number = options[:line_number]
@ -63,14 +60,6 @@ class Propane
@nfa = regex.nfa @nfa = regex.nfa
end end
# Whether the pattern is a drop pattern.
#
# @return [Boolean]
# Whether the pattern is a drop pattern.
def drop?
@drop
end
end end
end end