Skip to content

Commit

Permalink
Merge pull request #17 from betacraft/RUB-15
Browse files Browse the repository at this point in the history
RUB-15 Refactor ruby vernac parser
  • Loading branch information
ashish91 authored Oct 17, 2023
2 parents 0dd1491 + c9af8d2 commit 6af3bac
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 64 deletions.
7 changes: 3 additions & 4 deletions keyword_parser.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
require_relative "./lib/ruby-vernac-parser"

arg = ARGV
RubyVernacParser.new(
source_file: arg[0],
language: arg[1],
keywords_file: arg[2],
source_file: ARGV[0],
language: ARGV[1],
keywords_file: ARGV[2],
message_text: true
).execute

107 changes: 107 additions & 0 deletions lib/ruby-vernac-parser.deprecated.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
require "forwardable"
require "yaml"

require_relative "rubyvernac/parser/language_parser"
require_relative "rubyvernac/parser/language_alias_loader"

class RubyVernacParser
extend Forwardable

def_delegators :@language_alias_loader, :create_aliases
attr_reader :keywords, :input_bytes, :message_text,
:source_file, :language, :keywords_file

def initialize(source_file: nil, language: "ruby",
keywords_file: nil, message_text: false)
# @args = ARGV
@source_file = source_file
@message_text = message_text
@language = language
@keywords_file = keywords_file
@keywords = {}

@language_alias_loader = Rubyvernac::Parser::LanguageAliasLoader.new
begin
# validate_args
read_input_file if @source_file
read_keywords if @keywords_file
rescue
@error = true # Do nothing and stop execution
end
end

def execute
return if @error
if source_file.nil? || keywords_file.nil?
raise "Arguments - source_file, keywords_file\n"
end

# running script
begin
output = `#{command} #{temp_file_path}`
rescue => err
print "Error running script- #{err}"
return
ensure
File.delete(temp_file_path) if File.exist?(temp_file_path)
end
print "Script Output - \n" if message_text
print output
end

def parse
return if @error
print "Parsed code -\n#{parsed_string}"
end

private

def validate_args
if args.size != 3
print "Arguments - parser source_file language keywords_file\n"
raise
end
end

def read_input_file
@input_bytes = File.read(source_file)
rescue => err
print "Error reading input file, #{err}\n"
raise
end

def read_keywords
keywords = YAML.load_file(@keywords_file)

keywords.each do |keyword_in_eng, keyword_in_non_eng|
@keywords[keyword_in_non_eng.to_s] = keyword_in_eng.to_s
end
end

def parsed_string
@parsed_string ||= Rubyvernac::Parser::LanguageParser.run(
byte_string: input_bytes,
keywords: keywords,
language: language
)
end

def command
@command ||= case language
when "ruby"
"ruby"
else
"run"
end
end

def temp_file_path
@temp_file_path ||= begin
file_path = "#{source_file}.tmp"
File.write(file_path, parsed_string)
file_path
end

@temp_file_path
end
end
86 changes: 27 additions & 59 deletions lib/ruby-vernac-parser.rb
Original file line number Diff line number Diff line change
@@ -1,91 +1,69 @@
require 'forwardable'
require 'yaml'

require_relative "./rubyvernac/parser/language_parser"
require_relative "./rubyvernac/parser/language_alias_loader"
require_relative "./rubyvernac/utils/file_handler"
require_relative "./rubyvernac/utils/yaml_handler"

class RubyVernacParser
extend Forwardable

def_delegators :@language_alias_loader, :create_aliases
attr_reader :keywords, :input_bytes, :message_text,
:source_file, :language, :keywords_file

def initialize(source_file: nil, language: "ruby",
keywords_file: nil, message_text: false)
# @args = ARGV
def initialize(source_file: nil, language: "ruby", keywords_file: nil)
@source_file = source_file
@message_text = message_text
@language = language
@keywords_file = keywords_file
@keywords = {}

@file_handler = Rubyvernac::Utils::FileHandler.new
@yaml_handler = Rubyvernac::Utils::YamlHandler.new
@parser = Rubyvernac::Parser::LanguageParser.new

@language = language
@language_alias_loader = Rubyvernac::Parser::LanguageAliasLoader.new
begin
# validate_args
read_input_file if @source_file
read_keywords if @keywords_file
rescue
@error = true # Do nothing and stop execution
end
end

def execute
return if @error
if source_file.nil? || keywords_file.nil?
raise "Arguments - source_file, keywords_file\n"
end
translated_code = translate_code

write_to_temp_file(translated_code)
# running script
begin
output = `#{command} #{temp_file_path}`
rescue => err
print "Error running script- #{err}"
return
ensure
File.delete(temp_file_path) if File.exist?(temp_file_path)
@file_handler.delete_file(temp_file_path)
end
print "Script Output - \n" if message_text
print "Script Output - \n"
print "#{output}"
end

def parse
return if @error
print "Parsed code -\n#{parsed_string}"
translated_code = translate_code

print "Parsed code -\n#{translated_code}"
end

private

def validate_args
if args.size != 3
print "Arguments - parser source_file language keywords_file\n"
raise
end
end
def translate_code
source = @file_handler.read_file(@source_file)
keywords = @yaml_handler.stringified_load_file(@keywords_file)

def read_input_file
begin
@input_bytes = File.read(source_file)
rescue => err
print "Error reading input file, #{err}\n"
raise
end
@parser.parse(
byte_string: source,
keywords: keywords,
language: language
)
end

def read_keywords
_keywords = YAML.load_file(@keywords_file)

_keywords.each do |keyword_in_eng, keyword_in_non_eng|
@keywords[keyword_in_non_eng.to_s] = keyword_in_eng.to_s
end
def write_to_temp_file(content)
@file_handler.write_to_file(temp_file_path, content)
end

def parsed_string
@parsed_string ||= Rubyvernac::Parser::LanguageParser.run(
byte_string: input_bytes,
keywords: keywords,
language: language
)
def temp_file_path
"#{@source_file}.tmp"
end

def command
Expand All @@ -97,14 +75,4 @@ def command
end
end

def temp_file_path
@temp_file_path ||= (
file_path = "#{source_file}.tmp"
File.open(file_path, 'w') { |file| file.write(parsed_string) }
file_path
)

@temp_file_path
end

end
2 changes: 1 addition & 1 deletion lib/rubyvernac/parser/language_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(byte_string, keywords, language)
@language = language
end

def run
def parse(byte_string:, keywords:, language:)
case language
when "ruby"
line_comment_string = "#"
Expand Down
4 changes: 4 additions & 0 deletions lib/rubyvernac/utils/file_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def read_file(abs_path)
File.read(abs_path)
end

def delete_file(abs_path)
File.delete(abs_path) if File.exist?(abs_path)
end

def extract_filename_from_path(abs_path)
File.basename(abs_path)
end
Expand Down
26 changes: 26 additions & 0 deletions lib/rubyvernac/utils/yaml_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "yaml"

module Rubyvernac
module Utils

class YamlHandler

def stringified_load_file(filepath)
content = load_file(filepath)

stringified_content = {}
content.each do |key, val|
stringified_content[key.to_s] = val
end

stringified_content
end

def load_file(filepath)
YAML.load_file(filepath)
end

end

end
end

0 comments on commit 6af3bac

Please sign in to comment.