class InlineTokenResolver

Matches and extracts tokens contained in text.

Public Class Methods

new(line) click to toggle source
# File lib/rosetta/services/inline_token_resolver.rb, line 12
def initialize(line)
  @line = line
  @tokens = []
  @current_counter = 0
  @base_counter = 0
end

Public Instance Methods

call() click to toggle source
# File lib/rosetta/services/inline_token_resolver.rb, line 19
def call
  resolve_text_into_tokens until end_of_line?
  consume_plain_text_up_to(@current_counter)

  @tokens << NewLine.new
  @tokens
end

Private Instance Methods

constantize_type(type) click to toggle source
# File lib/rosetta/services/inline_token_resolver.rb, line 77
def constantize_type(type)
  Object.const_get(type)
end
consume(token_type) click to toggle source
# File lib/rosetta/services/inline_token_resolver.rb, line 67
def consume(token_type)
  token = token_type.consume(@line[@base_counter..])
  length_consumed = token.source_text.length

  @current_counter += length_consumed
  @base_counter = @current_counter

  token
end
consume_plain_text_up_to(final_index) click to toggle source
# File lib/rosetta/services/inline_token_resolver.rb, line 57
def consume_plain_text_up_to(final_index)
  plain_text = @line[@base_counter..final_index]
  @base_counter = @current_counter
  @tokens << Text.new(plain_text) unless plain_text.length.zero?
end
end_of_line?() click to toggle source
# File lib/rosetta/services/inline_token_resolver.rb, line 29
def end_of_line?
  @current_counter >= @line.length - 1
end
handle_matching_token(token_type) click to toggle source
# File lib/rosetta/services/inline_token_resolver.rb, line 51
def handle_matching_token(token_type)
  # We don't want to consume the token type delimiter.
  consume_plain_text_up_to(@current_counter - 1) unless @current_counter.zero?
  @tokens << consume(token_type)
end
inline_token_classes() click to toggle source
# File lib/rosetta/services/inline_token_resolver.rb, line 63
def inline_token_classes
  @inline_token_classes ||= BasicToken::INLINE_CLASS_NAMES.map { |type| constantize_type(type) }
end
match_token_type() click to toggle source
# File lib/rosetta/services/inline_token_resolver.rb, line 43
def match_token_type
  current_line = @line[@current_counter..]

  inline_token_classes.find do |token_type|
    token_type.matches?(current_line)
  end
end
resolve_text_into_tokens() click to toggle source
# File lib/rosetta/services/inline_token_resolver.rb, line 33
def resolve_text_into_tokens
  matching_token_type = match_token_type

  if matching_token_type.nil?
    @current_counter += 1
  else
    handle_matching_token(matching_token_type)
  end
end