class Sie::Parser::BuildEntry

Constants

InvalidEntryError

Public Instance Methods

call() click to toggle source
# File lib/sie/parser/build_entry.rb, line 8
def call
  if first_token.known_entry_type?
    build_complete_entry
  elsif lenient
    build_empty_entry
  else
    raise_invalid_entry_error
  end
end

Private Instance Methods

attributes_with_tokens() click to toggle source
# File lib/sie/parser/build_entry.rb, line 41
def attributes_with_tokens
  line_entry_type.map { |attr_entry_type|
    token = tokens.shift
    next unless token

    if attr_entry_type.is_a?(String)
      [ attr_entry_type, token.value ]
    else
      unless token.is_a?(Tokenizer::BeginArrayToken)
        raise InvalidEntryError, "Unexpected token: #{token.inspect}"
      end

      hash_tokens = []
      while token = tokens.shift
        break if token.is_a?(Tokenizer::EndArrayToken)
        hash_tokens << token.value
      end

      [ attr_entry_type, *hash_tokens ]
    end
  }.compact
end
build_complete_entry() click to toggle source
# File lib/sie/parser/build_entry.rb, line 20
def build_complete_entry
  entry = build_empty_entry

  attributes_with_tokens.each do |attr, *attr_tokens|
    label = attr.is_a?(Hash) ? attr.fetch(:name) : attr

    if attr_tokens.size == 1
      entry.attributes[label] = attr_tokens.first
    else
      type = attr.fetch(:type)
      values = attr_tokens.
        each_slice(type.size).
        map { |slice| Hash[type.zip(slice)] }

      entry.attributes[label] = values
    end
  end

  entry
end
build_empty_entry() click to toggle source
# File lib/sie/parser/build_entry.rb, line 64
def build_empty_entry
  Entry.new(first_token.label)
end
line_entry_type() click to toggle source
# File lib/sie/parser/build_entry.rb, line 68
def line_entry_type
  first_token.entry_type
end
raise_invalid_entry_error() click to toggle source
# File lib/sie/parser/build_entry.rb, line 72
def raise_invalid_entry_error
  raise InvalidEntryError, "Unknown entry type: #{first_token.label}"
end