Class: VDF4R::Parser
- Inherits:
-
Object
- Object
- VDF4R::Parser
- Defined in:
- lib/vdf4r/parser.rb
Instance Method Summary collapse
-
#initialize(input) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
Constructor Details
#initialize(input) ⇒ Parser
Returns a new instance of Parser.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/vdf4r/parser.rb', line 11 def initialize(input) case when input.respond_to?(:each_line) @input = input.each_line.to_a when input.respond_to?(:lines) @input = input.lines else raise ArgumentError.new('input must respond to #each_line or #lines') end end |
Instance Method Details
#parse ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/vdf4r/parser.rb', line 22 def parse parser = VDF4R::KeyValuesParser.new store = Store.new key = nil path = [] @input.each do |line| node = parser.parse(line) raise "ungrammatical content: '#{line}'" if node.nil? next if [:blank, :comment].include?(node.value) if node.value.respond_to?(:to_ary) case node.value.length when 1 key = node.value.first when 2 k, v = node.value store.traverse(path)[k] = v end elsif node.value.kind_of?(Symbol) case node.value when :enter_object raise 'no preceding key for object' unless key raise 'too recursive' if path.length > MAX_RECURSION path.push key key = nil when :exit_object raise 'nesting unbalanced (excessive exit)' if path.empty? path.pop end end end raise 'nesting unbalanced (insufficient exit)' unless path.empty? store end |