-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoader.rb
58 lines (55 loc) · 1.28 KB
/
Loader.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
$LOAD_PATH << './'
require 'Global.rb'
require 'Exception.rb'
class Loader
attr_reader :alphabet
attr_reader :tape
attr_reader :rules
def initialize(path)
@rules = []
File.open(path).each do |line|
if line.length == 1
next
end
begin
r = line.rindex(';') - 1
rescue
raise ParseException.new("Syntax error near \' "+line+" \', ';' Expected")
end
if line =~ /^TAPE=>/
if tape == nil
@tape = line[6..r]
else
raise ParseException.new("multiple tapes found")
end
elsif line.match(/^RULE=>/)
parseRule(line[6..r])
elsif line.match(/^ALPH=>/)
if alphabet == nil
@alphabet = line[6..line.length-2]
else
raise ParseException.new("multiple alphabet found")
end
else
raise ParseException.new("Syntax error near \'" + line +" \'")
end
end
if alphabet == nil or alphabet == ''
raise ParseException.new("No alphabet found in this file")
elsif tape == nil or tape == ''
raise ParseException.new("No tape found in this file")
elsif rules.length < 5
raise ParseException.new("No rules found in this file")
end
end
def parseRule(rule)
r = rule.split(' ')
if r.length != 5
raise ParseException.new("Syntax error near \'" + rule + " \'")
else
r.each do |e|
rules.push(e)
end
end
end
end