class Parser
Constants
- ADDITION_KEYWORDS
- ALICIA_KEYS
- ASSIGNMENT_KEYWORDS
- CLASS_KEYWORDS
- COMMENT_KEYWORDS
- COMPARISON_KEYWORDS
- CONDITION_KEYWORDS
- DECREMENT_KEYWORDS
- DIVISION_KEYWORDS
- END_KEYWORDS
- FUNCTION_CALL_KEYWORDS
- FUNCTION_DEF_KEYWORDS
- GREATER_THAN_KEYWORDS
- INCREMENT_KEYWORDS
- LESS_THAN_KEYWORDS
- LOOP_KEYWORDS
- MAP
- MULTIPLICATION_KEYWORDS
- NEGATION_KEYWORDS
- OPERATOR_KEYWORDS
- PARAM_KEYWORDS
- PUT_KEYWORDS
KEYWORDS
LINE_KEYWORDS = [',', '.', '?', '!', ';']
- SUBTRACTION_KEYWORDS
- TRUE_KEYWORDS
- WRITER_FILE
Public Class Methods
check_for_keywords(line)
click to toggle source
# File lib/my_precious/parser.rb, line 121 def self.check_for_keywords(line) ALICIA_KEYS.each do |keywords| line = parse(line, keywords) end line end
find_replacement(keywords)
click to toggle source
# File lib/my_precious/parser.rb, line 201 def self.find_replacement(keywords) MAP.each do |hash| hash.each do |key, value| if value == keywords return key.to_s end end end return nil end
format_function(line)
click to toggle source
# File lib/my_precious/parser.rb, line 212 def self.format_function(line) phrase_array = line.split('') index = phrase_array.find_index { |i| i == "("} phrase_array.delete_at(index - 1) phrase_array.delete_at(index - 2) phrase_array.join("") end
get_quote(phrase)
click to toggle source
# File lib/my_precious/parser.rb, line 179 def self.get_quote(phrase) phrase_array = phrase.split('') index = phrase_array.find_index { |i| i == '"'} + 1 quote = "" while phrase_array[index] != '"' quote << phrase_array[index] index += 1 end quote end
only_valuable_words(line)
click to toggle source
# File lib/my_precious/parser.rb, line 147 def self.only_valuable_words(line) important_words = [] line_array = line.split(" ") line_array.each do |word| valuable_word = valuable?(word) if valuable_word important_words << word end end line = important_words.join(" ") end
parse(line, keywords)
click to toggle source
METHODS
# File lib/my_precious/parser.rb, line 129 def self.parse(line, keywords) keywords.each do |keyword| if line.include? keyword replacement = find_replacement(keywords) line = line.gsub(keyword, replacement) end end return line end
parse_file(file, output_file_name)
click to toggle source
# File lib/my_precious/parser.rb, line 32 def self.parse_file(file, output_file_name) str = "" writer_file_name = output_file_name file.each_with_index do |file, index| file.each_line do |line| line = parse_line(line, index) str << (line + "\n") end end write(str, writer_file_name) end
parse_line(line, index)
click to toggle source
# File lib/my_precious/parser.rb, line 44 def self.parse_line(line, index) #error handeling #ignore lines of length 1, its empty if line.length == 1 # write("#{line}") return line else #check if there are strings in line quote = "" if line.include? '"' quote = get_quote(line) line = line.gsub(quote, 'quote_placeholder') end #check if line is a comment comment = "" if COMMENT_KEYWORDS.any? { |word| line.include?(word) } line = parse(line, COMMENT_KEYWORDS) comment = store_important(line, '#') line = line.gsub(comment, 'comment_placeholder') end #check if line has params params = "" if ((PARAM_KEYWORDS.any? { |word| line.include?(word) }) && ((FUNCTION_DEF_KEYWORDS.any? { |word| line.include?(word) }) || (FUNCTION_CALL_KEYWORDS.any? { |word| line.include?(word) }))) line = parse(line, PARAM_KEYWORDS) params = remove_newline(store_important(line, '(')) line = line.gsub(params, 'param_placeholder ') + ')' end class_word = "" if CLASS_KEYWORDS.any? { |word| line.include?(word) } line = parse(line, CLASS_KEYWORDS) class_word = store_important(line, ':') line = line.gsub(class_word, ' class_word_placeholder') end #get rid of special chars line_array = line.split(" ") line_array.each_with_index do |word, index| line_array[index] = purify(word) end #check for keywords #calls if statements line = line_array.join(" ") line = check_for_keywords(line) #remove extra english words line = only_valuable_words(line).downcase #put string back into line if line.include? ('"quote_placeholder"') line = line.gsub('quote_placeholder', quote) end if line.include? ('comment_placeholder') line = line.gsub('comment_placeholder', comment) end if line.include? ('param_placeholder') line = line.gsub('param_placeholder', params.downcase) line = format_function(line) end if line.include? ('class_word_placeholder') line = line.gsub('class_word_placeholder', "#{class_word}") end # write("#{line}") return line end end
purify(word)
click to toggle source
# File lib/my_precious/parser.rb, line 139 def self.purify(word) word = word.gsub(/[!@%&.?,]/,'')# get rid of special chars end
remove_newline(line)
click to toggle source
# File lib/my_precious/parser.rb, line 143 def self.remove_newline(line) line.gsub(/\n/, "") end
store_important(phrase, key)
click to toggle source
# File lib/my_precious/parser.rb, line 190 def self.store_important(phrase, key) phrase_array = phrase.split('') index = phrase_array.find_index { |i| i == key} + 1 string = "" while index < phrase_array.length string << phrase_array[index] index += 1 end string end
valuable?(word)
click to toggle source
# File lib/my_precious/parser.rb, line 159 def self.valuable?(word) valuable = false if /[[:upper:]]/.match(word[0]) #if variable valuable = true elsif word == '"quote_placeholder"' #if string/quote valuable = true elsif word == '#comment_placeholder' #if comment valuable = true elsif word == '(param_placeholder' #if params valuable = true elsif word == 'class_word_placeholder' #if class valuable = true elsif word.to_i.to_s == word #if num valuable = true elsif OPERATOR_KEYWORDS.include? word #if operator valuable = true end valuable end
write(str, writer_file_name)
click to toggle source
# File lib/my_precious/parser.rb, line 220 def self.write(str, writer_file_name) writer_file = File.open(writer_file_name, 'w') writer_file.write(str) end