Class: Glaemscribe::API::ResolveVirtualsPostProcessorOperator

Inherits:
PostProcessorOperator show all
Defined in:
lib/api/post_processor/resolve_virtuals.rb

Instance Attribute Summary

Attributes inherited from PrePostProcessorOperator

#finalized_glaeml_element, #glaeml_element

Instance Method Summary collapse

Methods inherited from PrePostProcessorOperator

#eval_arg, #finalize_glaeml_element, #initialize

Constructor Details

This class inherits a constructor from Glaemscribe::API::PrePostProcessorOperator

Instance Method Details

#apply(tokens, charset) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/api/post_processor/resolve_virtuals.rb', line 40

def apply(tokens,charset)
  
  reset_trigger_states(charset)
  
  tokens.each_with_index{ |token,idx|
    
    if token == '*SPACE'
      reset_trigger_states(charset)
      next
    end
    
    # Check if token is a virtual char
    c = charset[token]
    next if c.nil? # May happen for empty tokens
    if c.virtual?
      # Try to replace
      last_trigger = @last_triggers[c]
      if last_trigger != nil
        tokens[idx] = last_trigger.names.first # Take the first name of the non-virtual replacement.
      end
    else
      # Update states of virtual classes
      charset.virtual_chars.each{|vc|
        rc                  = vc[token]
        @last_triggers[vc]  = rc if rc != nil 
      }
    end
  }
  tokens
end

#finalize(trans_options) ⇒ Object



28
29
30
31
# File 'lib/api/post_processor/resolve_virtuals.rb', line 28

def finalize(trans_options)
  super(trans_options)
  @last_triggers = {} # Allocate the lookup here to optimize
end

#reset_trigger_states(charset) ⇒ Object



33
34
35
36
37
38
# File 'lib/api/post_processor/resolve_virtuals.rb', line 33

def reset_trigger_states(charset)
  # For each virtual char in charset, maintain a state.
  charset.virtual_chars.each{ |vc|
    @last_triggers[vc] = nil # Clear the state
  }
end