Move Lexer::DFA#build_tables to Lexer

This commit is contained in:
Josh Holtrop 2022-06-05 14:18:35 -04:00
parent 57a3e9d9f6
commit 6f1ce32775
3 changed files with 30 additions and 30 deletions

View File

@ -123,7 +123,7 @@ class <%= classname %>
uint accepts;
}
<% transition_table, state_table = lexer.dfa.build_tables %>
<% transition_table, state_table = lexer.build_tables %>
private static const 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),

View File

@ -9,5 +9,34 @@ class Propane
@dfa = DFA.new(tokens)
end
def build_tables
transition_table = []
state_table = []
states = @dfa.enumerate
states.each do |state, id|
accepts =
if state.accepts.nil?
TOKEN_NONE
elsif state.accepts.name
state.accepts.id
else
TOKEN_DROP
end
state_table << {
transition_table_index: transition_table.size,
n_transitions: state.transitions.size,
accepts: accepts,
}
state.transitions.each do |transition|
transition_table << {
first: transition.code_point_range.first,
last: transition.code_point_range.last,
destination: states[transition.destination],
}
end
end
[transition_table, state_table]
end
end
end

View File

@ -22,35 +22,6 @@ class Propane
@start_state = @states[0]
end
def build_tables
transition_table = []
state_table = []
states = enumerate
states.each do |state, id|
accepts =
if state.accepts.nil?
TOKEN_NONE
elsif state.accepts.name
state.accepts.id
else
TOKEN_DROP
end
state_table << {
transition_table_index: transition_table.size,
n_transitions: state.transitions.size,
accepts: accepts,
}
state.transitions.each do |transition|
transition_table << {
first: transition.code_point_range.first,
last: transition.code_point_range.last,
destination: states[transition.destination],
}
end
end
[transition_table, state_table]
end
private
def register_nfa_state_set(nfa_state_set)