Module: Fly::HCL

Defined in:
lib/fly.io-rails/hcl.rb

Overview

a very liberal HCL scanner

Class Method Summary collapse

Class Method Details

.parse(string) ⇒ Object



6
7
8
9
10
11
12
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
# File 'lib/fly.io-rails/hcl.rb', line 6

def self.parse(string)
  result = []
  name = nil
  stack = []
  block = {}
  cursor = block
  result.push block

  hcl = StringScanner.new(string)
  until hcl.eos?
	hcl.scan(%r{\s*(\#.*|//.*|/\*[\S\s]*?\*/)*})

	if hcl.scan(/[a-zA-Z]\S*|\d[.\d]*|"[^"]*"/)
	  if cursor.is_a? Array
 cursor.push token(hcl.matched)
	  elsif name == nil
 name = token(hcl.matched)
	  else
 hash = {}
 cursor[name] = hash
 name = token(hcl.matched)
 cursor = hash
	  end
	elsif hcl.scan(/=/)
	  hcl.scan(/\s*/)
	  if hcl.scan(/\[/)
 list = []
 cursor[name] = list
        name = nil
 stack.push cursor
 cursor = list
	  elsif hcl.scan(/\{/)
 hash = {}
 cursor[name] = hash
 name = nil
 stack.push cursor
 cursor = hash
	  elsif hcl.scan(/.*/)
 cursor[name] = token(hcl.matched)
 name = nil
	  end
	elsif hcl.scan(/\{/)
	  hash = {}
	  if cursor.is_a? Array
 cursor << hash
	  else
 cursor[name] = hash
	  end
	  name = nil
	  stack.push cursor
	  cursor = hash
	elsif hcl.scan(/\[/)
	  list = []
	  stack.push cursor
	  cursor = list
	elsif hcl.scan(/\}|\]/)
	  cursor = stack.pop

	  if stack.empty?
 block = {}
 cursor = block
 result.push block
	  end
	elsif hcl.scan(/[,=:]/)
      nil
	elsif hcl.scan(/.*/)
      unless hcl.matched.empty?
 STDERR.puts "unexpected input: #{hcl.matched.inspect}"
      end
	end
  end

  result.pop if result.last.empty?
  result
end