class Nodaire::Indental::Parser

@private

Constants

Category

Attributes

category[RW]
data[R]
symbolize_names[R]

Public Class Methods

new(source, strict, options = {}) click to toggle source
Calls superclass method Nodaire::Parser::new
# File lib/nodaire/indental/parser.rb, line 12
def initialize(source, strict, options = {})
  super(strict, options)

  @symbolize_names = option(:symbolize_names, false)
  @data = {}
  @category = nil

  parse! Nodaire::Indental::Lexer.tokenize(source)
end

Private Instance Methods

add_category!(token) click to toggle source
# File lib/nodaire/indental/parser.rb, line 82
def add_category!(token)
  self.category = Category.new(
    name: token.key,
    list_id: nil
  )
  data[token.key] = {}
end
add_key_value!(token) click to toggle source
# File lib/nodaire/indental/parser.rb, line 90
def add_key_value!(token)
  data[category.name][token.key] = token.value
  category.list_id = nil
end
add_list!(token) click to toggle source
# File lib/nodaire/indental/parser.rb, line 95
def add_list!(token)
  data[category.name][token.key] = []
  category.list_id = token.key
end
add_list_item!(token) click to toggle source
# File lib/nodaire/indental/parser.rb, line 100
def add_list_item!(token)
  data[category.name][category.list_id] << token.value
end
normalize_key(key) click to toggle source
# File lib/nodaire/indental/parser.rb, line 112
def normalize_key(key)
  return if key.nil?

  if symbolize_names
    Nodaire.symbolize(key)
  else
    key.upcase
  end
end
normalize_token(token) click to toggle source
# File lib/nodaire/indental/parser.rb, line 108
def normalize_token(token)
  token.tap { |t| t.key = normalize_key(t.key) }
end
parse!(tokens) click to toggle source
# File lib/nodaire/indental/parser.rb, line 29
def parse!(tokens)
  tokens.each { |token| parse_token!(normalize_token(token)) }
end
parse_category!(token) click to toggle source
# File lib/nodaire/indental/parser.rb, line 43
def parse_category!(token)
  if data.include?(token.key)
    oops! 'Duplicate category', token.line_num
    self.category = nil
  else
    add_category! token
  end
end
parse_error!(token) click to toggle source
# File lib/nodaire/indental/parser.rb, line 104
def parse_error!(token)
  oops! token.value, token.line_num
end
parse_key_value!(token) click to toggle source
# File lib/nodaire/indental/parser.rb, line 52
def parse_key_value!(token)
  return oops!('No category specified', token.line_num) if category.nil?

  if data[category.name].include?(token.key)
    oops! 'Duplicate key', token.line_num
    category.list_id = nil
  else
    add_key_value! token
  end
end
parse_list_item!(token) click to toggle source
# File lib/nodaire/indental/parser.rb, line 74
def parse_list_item!(token)
  if category.nil? || category.list_id.nil?
    oops! 'No list specified', token.line_num
  else
    add_list_item! token
  end
end
parse_list_name!(token) click to toggle source
# File lib/nodaire/indental/parser.rb, line 63
def parse_list_name!(token)
  return oops!('No category specified', token.line_num) if category.nil?

  if data[category.name].include?(token.key)
    oops! 'Duplicate key for list', token.line_num
    category.list_id = nil
  else
    add_list! token
  end
end
parse_token!(token) click to toggle source
# File lib/nodaire/indental/parser.rb, line 33
def parse_token!(token)
  case token.type
  when :category then parse_category! token
  when :key_value then parse_key_value! token
  when :list_name then parse_list_name! token
  when :list_item then parse_list_item! token
  when :error then parse_error! token
  end
end