-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.rb
89 lines (76 loc) · 1.41 KB
/
example.rb
1
2
3
4
5
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
81
82
83
84
85
86
87
88
89
#!/usr/bin/env ruby
require "psych"
require "pry"
# Handler for detecting scalar values
class YayHandler < Psych::Handler
# States:
# a -> start_map -> b
# b -> scalar -> c (push key)
# b -> end_map -> b (pop key)
# c -> scalar -> b (push value, pop key)
# c -> start_map -> b (push map)
# c -> end_map -> b (pop key)
attr_accessor :parser
def initialize(*args)
@state = :a
@prev = nil
@keys = []
end
def start_mapping(*args)
@prev = @state
case @state
when :a
@state = :b
when :c
@state = :b
else
fail
end
transition("start map")
end
def end_mapping
@prev = @state
case @state
when :b, :c
@state = :b
@keys.pop
else
fail
end
transition("end map")
end
def scalar(value, *args)
@prev = @state
case @state
when :b
@state = :c
@keys << value
when :c
@state = :b
puts "#{@keys.join('.')}:#{parser.mark.line + 1}: #{value}"
@keys.pop
else
fail
end
transition("scalar")
end
def transition(event)
# puts "#@prev -> (#{event}) -> #@state"
end
end
# yaml = File.read("/Users/timuruski/en.yml")
yaml = DATA
handler = YayHandler.new
parser = Psych::Parser.new(handler)
handler.parser = parser
parser.parse(yaml)
__END__
---
en:
page1:
button: Hello
page2:
button: Goodbye
fr:
page1:
button: Bonjour