From 4beb3d201680ce33d9e42e5c2c701e677fd4f9e3 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 27 Jun 2021 23:08:14 -0400 Subject: [PATCH] Add some token constants --- lib/imbecile.rb | 9 +++++++++ lib/imbecile/regex/fa.rb | 14 ++++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/imbecile.rb b/lib/imbecile.rb index 513d025..bd23dcb 100644 --- a/lib/imbecile.rb +++ b/lib/imbecile.rb @@ -17,6 +17,15 @@ require_relative "imbecile/version" module Imbecile + # Decoding error. + TOKEN_DECODE_ERROR = 0xFFFFFFFD + + # Token ID for a "dropped" token. + TOKEN_DROP = 0xFFFFFFFE + + # Invalid token ID. + TOKEN_NONE = 0xFFFFFFFF + class Error < RuntimeError end diff --git a/lib/imbecile/regex/fa.rb b/lib/imbecile/regex/fa.rb index 9e829e6..7d11983 100644 --- a/lib/imbecile/regex/fa.rb +++ b/lib/imbecile/regex/fa.rb @@ -63,19 +63,17 @@ module Imbecile states = enumerate states.each do |state, id| accepts = - if state.accepts - if state.accepts.name - state.accepts.id - else - 0xFFFFFFFE # drop token - end + if state.accepts.nil? + TOKEN_NONE + elsif state.accepts.name + state.accepts.id else - 0xFFFFFFFF # not an accepting state + TOKEN_DROP end state_table << { transition_table_index: transition_table.size, n_transitions: state.transitions.size, - accepts: state.accepts ? state.accepts.id : 0xFFFFFFFF, + accepts: accepts, } state.transitions.each do |transition| transition_table << {