-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsimple.rl
64 lines (52 loc) · 1.2 KB
/
simple.rl
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
%%{
machine vim_print;
action H { @head = p }
action T { @tail = p }
action return { fret; }
action push_insert_mode { fcall insert_mode; }
action EmitMotion { @events << {motion: strokes} }
action EmitSwitch { @events << {switch: strokes} }
action EmitInput { @events << {input: strokes} }
action EmitEscape { @events << {escape: '<Esc>' } }
escape = 27 >H@T @EmitEscape;
input = (any - escape) >H@T @EmitInput;
motion = [hjklbwe0] >H@T @EmitMotion;
switch = [iIaAsSoO] >H@T @EmitSwitch;
insert_mode := (
input*
escape @return
);
normal_mode := (
motion |
switch @push_insert_mode
)*;
}%%
class VimParser
attr_accessor :head, :tail, :data
def initialize(listener)
@events = listener
%% write data;
end
def process(input)
@data = input.unpack("c*")
eof = @data.length
stack = []
%% write init;
%% write exec;
end
def strokes
@data[@head..@tail].pack('c*')
end
end
VimParser.new(recorder = []).process("helihello\e")
puts recorder
# {:motion=>"h"}
# {:motion=>"e"}
# {:motion=>"l"}
# {:switch=>"i"}
# {:input=>"h"}
# {:input=>"e"}
# {:input=>"l"}
# {:input=>"l"}
# {:input=>"o"}
# {:escape=>"<Esc>"}