From 701903def220262bafbcdd102bf919d8185e8de8 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 6 Jun 2021 14:09:28 -0400 Subject: [PATCH] Token should build its own NFA --- lib/imbecile/grammar.rb | 8 ++------ lib/imbecile/grammar/token.rb | 11 +++++++++++ lib/imbecile/regex/dfa.rb | 2 +- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/imbecile/grammar.rb b/lib/imbecile/grammar.rb index 3b7bb36..68cbe2b 100644 --- a/lib/imbecile/grammar.rb +++ b/lib/imbecile/grammar.rb @@ -46,13 +46,9 @@ module Imbecile end # Build NFA from each token expression. - i = 0 nfas = @tokens.map do |token| - regex = Regex.new(token.pattern) - regex.nfa.end_state.accepts = "#{i}:#{token.name}" - puts regex.nfa - i += 1 - regex.nfa + puts token.nfa + token.nfa end dfa = Regex::DFA.new(nfas) puts dfa diff --git a/lib/imbecile/grammar/token.rb b/lib/imbecile/grammar/token.rb index 3b858ee..573744f 100644 --- a/lib/imbecile/grammar/token.rb +++ b/lib/imbecile/grammar/token.rb @@ -15,10 +15,21 @@ module Imbecile # Token ID. attr_reader :id + # @return [Regex::NFA] + # Regex NFA for matching the token. + attr_reader :nfa + def initialize(name, pattern, id) @name = name @pattern = pattern @id = id + regex = Regex.new(pattern) + regex.nfa.end_state.accepts = self + @nfa = regex.nfa + end + + def to_s + @name end end diff --git a/lib/imbecile/regex/dfa.rb b/lib/imbecile/regex/dfa.rb index 00db7f7..707079d 100644 --- a/lib/imbecile/regex/dfa.rb +++ b/lib/imbecile/regex/dfa.rb @@ -38,7 +38,7 @@ module Imbecile nfa_state_set.each do |nfa_state| if nfa_state.accepts if state.accepts - if nfa_state.accepts.to_i < state.accepts.to_i + if nfa_state.accepts.id < state.accepts.id state.accepts = nfa_state.accepts end else