From 6f1ce32775bd239dcffed12164788fd68893838f Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 5 Jun 2022 14:18:35 -0400 Subject: [PATCH] Move Lexer::DFA#build_tables to Lexer --- assets/parser.d.erb | 2 +- lib/propane/lexer.rb | 29 +++++++++++++++++++++++++++++ lib/propane/lexer/dfa.rb | 29 ----------------------------- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/assets/parser.d.erb b/assets/parser.d.erb index 3c44afb..cc0563d 100644 --- a/assets/parser.d.erb +++ b/assets/parser.d.erb @@ -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), diff --git a/lib/propane/lexer.rb b/lib/propane/lexer.rb index 4543cd8..f3983c8 100644 --- a/lib/propane/lexer.rb +++ b/lib/propane/lexer.rb @@ -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 diff --git a/lib/propane/lexer/dfa.rb b/lib/propane/lexer/dfa.rb index 8e025e7..d20248b 100644 --- a/lib/propane/lexer/dfa.rb +++ b/lib/propane/lexer/dfa.rb @@ -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)