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 Printer
#display_result, #finalize_result, #find_result_type, #format_result
Methods included from Validator
#balanced_brackets?, #balanced_quotes?, #valid_function, #valid_var, #valid_var_name
#arg_err_build, #no_procedure_build, #type_err, #unbalanced_brackets_error, #unbalanced_quotes_error, #unbound_symbol_err
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
#parse(token) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/lisp/interpreter/parser.rb', line 44
def parse(token)
return read_file token if (token.start_with? 'ghci') && token.size > 4
token_error = validate_token token
result =
if token_error.nil?
@tokenizer.tokenize split_token token
else
token_error
end
finalize_result result unless result.to_s.empty?
end
|
#read_file(token) ⇒ Object
94
95
96
97
98
99
100
|
# File 'lib/lisp/interpreter/parser.rb', line 94
def read_file(token)
res = read_file_helper token
return finalize_result res if res.is_a? String
filename = token[5..-1]
return read_file_executor filename if File.exist? filename
finalize_result 'File with name "' + filename + '" does not exist!'
end
|
#read_file_execute_lines(f, expr) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/lisp/interpreter/parser.rb', line 76
def read_file_execute_lines(f, expr)
last_value = ''
f.each do |line|
expr << line
if (validate_token expr).nil? && expr != ''
last_value = parse expr
expr = ''
end
end
last_value
end
|
#read_file_executor(file) ⇒ Object
88
89
90
91
92
|
# File 'lib/lisp/interpreter/parser.rb', line 88
def read_file_executor(file)
f = File.open(file)
expr = ''
read_file_execute_lines f, expr
end
|
#read_file_helper(token) ⇒ Object
68
69
70
71
72
73
74
|
# File 'lib/lisp/interpreter/parser.rb', line 68
def read_file_helper(token)
pattern = /^ghci .*\.[.ss|.scm]+$/
result = (token =~ pattern)
return unless result.nil?
token = token[5..-1].nil? ? token[4..-1] : token[5..-1]
'File with name "' + token.to_s + '" is not valid scheme file'
end
|
35
36
37
38
39
40
41
42
|
# File 'lib/lisp/interpreter/parser.rb', line 35
def read_input(token)
until (validate_token token).nil? && token != ''
crr_input = STDIN.gets.chomp
token << crr_input
break if crr_input == ''
end
token
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
begin
token = read_input('')
rescue Interrupt
puts 'Exiting..!'
sleep(1)
end
parse token
end
end
|
#split_token(token) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/lisp/interpreter/parser.rb', line 56
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
102
103
104
105
106
107
108
|
# File 'lib/lisp/interpreter/parser.rb', line 102
def validate_token(token)
if !balanced_brackets? token
unbalanced_brackets_error
elsif !balanced_quotes? token
unbalanced_quotes_error
end
end
|