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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/vdf4r/parser.rb', line 32
def parse
parser = VDF4R::KeyValuesParser.new
store = Store.new
key = nil
partial_value = nil
path = []
@input.each_with_index do |line, index|
node = parser.parse(Parser.clean(line))
if node.nil?
raise "ungrammatical content at line #{index+1}: '#{line}'" if node.nil?
end
begin
_, (encounter, context) = node.value
rescue NoMethodError => e
end
case encounter
when :blank, :comment
when :enter_object
raise 'no preceding key for object' unless key
raise 'too recursive' if path.length > MAX_RECURSION
raise 'enter during multi-line value' if partial_value
path.push key
key = nil
when :exit_object
raise 'nesting unbalanced (excessive exit)' if path.empty?
raise 'exit during multi-line value' if partial_value
path.pop
when :key_value
k, v = context
store.traverse(path)[k] = Parser.dirty(v)
when :key
key = context[0]
when :key_enter_value
key, partial_value = context
when :key_continue_value
partial_value += "\n#{context[0]}"
when :key_exit_value
v = partial_value + "\n#{context[0]}"
partial_value = nil
store.traverse(path)[k] = Parser.dirty(v)
else
raise 'unknown node value'
end
end
raise 'nesting unbalanced (insufficient exit)' unless path.empty?
store
end
|