blob: 6b3907b62f5b080ba1dc1c0a2318938cfdc27231 (
plain)
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
|
require "../util.cr"
require "./thue.cr"
require "./rule.cr"
class Thue::Parser
def initialize(code_io : IO)
@raw_code = code_io.gets_to_end
code_io.close # just in case
end
def parse
rules = [] of Rule
line_iterator = @raw_code.lines.each
line_iterator.each do |line|
case line
when "", "::="
# stops iteration of lines to be picked up by string builder
break
# returned match objects are provably not_nil, safe
when .matches?(R_INPUT_RULE)
m = line.match(R_INPUT_RULE).not_nil!
rules << InputRule.new(m[1])
when .matches?(R_OUTPUT_RULE)
m = line.match(R_OUTPUT_RULE).not_nil!
rules << OutputRule.new(m[1], m[2])
when .matches?(R_NORMAL_RULE)
m = line.match(R_NORMAL_RULE).not_nil!
rules << NormalRule.new(m[1], m[2])
else
raise Util::ParserError.new("No separator between code and initial state near '#{line}'")
end
end
state = String.build do |str|
# resumes from where rule parser left off
line_iterator.each do |line|
str << line
str << '\n'
end
end
{rules, state}
end
end
|