13
14
15
16
17
18
19
20
21
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
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
88
|
# File 'lib/vdf/parse.rb', line 13
def parse(text)
lines = text.lines
object = {}
stack = [object]
expect = false
skip_lines = 0
lines.each_with_index do |line, i|
if skip_lines > 0
skip_lines -= 1
next
end
line.strip!
next if line == -'' || line[0] == -'/'
if line.start_with?(-'{')
expect = false
next
elsif expect
raise ParserError, "Invalid syntax on line #{i+1}"
end
if line.start_with?(-'}')
stack.pop
next
end
loop do
m = REGEX.match(line)
if m.nil?
raise ParserError, "Invalid syntax on line #{i+1}"
end
key = m[2] || m[3]
val = m[6] || m[8]
if val.nil?
if stack[-1][key].nil?
stack[-1][key] = {}
end
stack.push(stack[-1][key])
expect = true
else
if m[7].nil? && m[8].nil?
line << -"\n" << lines[i+skip_lines+1].chomp
skip_lines += 1
next
end
stack[stack.length - 1][key] = begin
begin
Integer(val)
rescue ArgumentError
Float(val)
end
rescue ArgumentError
case val.downcase
when -"true"
true
when -"false"
false
when -"null"
nil
else
val
end
end
end
break
end
end
return object
end
|