Token should build its own NFA

This commit is contained in:
Josh Holtrop 2021-06-06 14:09:28 -04:00
parent afea886ecb
commit 701903def2
3 changed files with 14 additions and 7 deletions

View File

@ -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

View File

@ -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

View File

@ -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