Class: Parser
Overview
Parser is used to validate the user input and parse it to the tokenizer
Constant Summary
Constants included
from Environment
Environment::PROD, Environment::TEST
Instance Method Summary
collapse
Methods included from Validator
#balanced_brackets?, #balanced_quotes?, #valid_function, #valid_var, #valid_var_name
#unbalanced_brackets_error, #unbalanced_quotes_error
Constructor Details
#initialize(env_type = Environment::TEST) ⇒ Parser
Returns a new instance of Parser.
17
18
19
20
|
# File 'lib/lisp/interpreter/parser.rb', line 17
def initialize(env_type = Environment::TEST)
@env_type = env_type
@tokenizer = Tokenizer.new
end
|
Instance Method Details
#fetch_file_read(token) ⇒ Object
88
89
90
91
|
# File 'lib/lisp/interpreter/parser.rb', line 88
def fetch_file_read(token)
res = read_file token
print_result res unless res.to_s.empty?
end
|
#parse(token) ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/lisp/interpreter/parser.rb', line 93
def parse(token)
return fetch_file_read token if token.start_with? 'ghci'
token_error = validate_token token
result =
if token_error.nil?
@tokenizer.tokenize split_token token
else
token_error
end
print_result result unless result.to_s.empty?
end
|
#print_result(result) ⇒ Object
113
114
115
116
117
118
|
# File 'lib/lisp/interpreter/parser.rb', line 113
def print_result(result)
to_remove = result.to_s.list? || result.to_s.pair? || result.to_s.quote?
result = result.delete('\'') if to_remove
puts result if @env_type == Environment::PROD
result
end
|
#read_file(token) ⇒ Object
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/lisp/interpreter/parser.rb', line 77
def read_file(token)
res = read_file_helper token
return res if res.is_a? String
filename = token[5..-1]
if !File.exist? filename
'File with name "' + filename + '" does not exist!'
else
read_file_executor filename
end
end
|
#read_file_executor(file) ⇒ Object
70
71
72
73
74
75
|
# File 'lib/lisp/interpreter/parser.rb', line 70
def read_file_executor(file)
f = File.open(file)
expr = ''
last_value = ''
read_file_reader f, last_value, expr
end
|
#read_file_helper(token) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/lisp/interpreter/parser.rb', line 47
def read_file_helper(token)
pattern = /^ghci .*\.[.ss|.scm]+$/
result = (token =~ pattern)
if result.nil?
token = token[5..-1].nil? ? token[4..-1] : token[5..-1]
msg = 'File with name "' + token + '" is not valid scheme file'
return msg
return false
end
true
end
|
#read_file_reader(f, value, expr) ⇒ Object
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/lisp/interpreter/parser.rb', line 59
def read_file_reader(f, value, expr)
while (c = f.read(1))
expr << c
if (validate_token expr).nil? && expr != ''
last_value = parse expr
expr = ''
end
end
last_value
end
|
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/lisp/interpreter/parser.rb', line 22
def run
loop do
print 'zakichan> ' if @env_type == Environment::PROD
token = ''
until (validate_token token).nil? && token != ''
crr_input = STDIN.gets.chomp
token << crr_input
break if crr_input == ''
end
parse token
end
end
|
#split_token(token) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/lisp/interpreter/parser.rb', line 35
def split_token(token)
result = []
token.split(/\s+(?=(?:[^"]*"[^"]*")*[^"]*$)/).each do |t|
if !t.string? && (t.include?('(') || t.include?(')'))
t.to_s.split(/(\(|\))/).each { |p| result << p }
else
result << t
end
end
result
end
|
#validate_token(token) ⇒ Object
105
106
107
108
109
110
111
|
# File 'lib/lisp/interpreter/parser.rb', line 105
def validate_token(token)
if !balanced_brackets? token
unbalanced_brackets_error
elsif !balanced_quotes? token
unbalanced_quotes_error
end
end
|