forked from puppetlabs/showoff
/
commandline_parser.rb
67 lines (50 loc) · 1.09 KB
/
commandline_parser.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
require 'parslet'
# For parsing commandline slide content.
class CommandlineParser < Parslet::Parser
rule(:prompt) do
str('$') | str('#') | str('>>')
end
rule(:text) do
match['[:print:]'].repeat
end
rule(:singleline_input) do
(str("\\\n").absent? >> match['[:print:]']).repeat
end
rule(:input) do
multiline_input | singleline_input
end
rule(:multiline_input) do
# some command \
# continued \
# \
# and stop
( singleline_input >> str('\\') >> newline ).repeat(1) >> singleline_input
end
rule(:command) do
# $ some command
# some output
( prompt.as(:prompt) >> space? >> input.as(:input) >> output? ).as(:command)
end
rule(:output) do
# output
prompt.absent? >> text
end
rule(:output?) do
#
# some text
# some text
#
# some text
( newline >> ( ( output >> newline ).repeat >> output.maybe ).as(:output) ).maybe
end
rule(:commands) do
command.repeat
end
rule(:newline) do
str("\n") | str("\r\n")
end
rule(:space?) do
match['[:space:]'].repeat
end
root(:commands)
end