Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre parser #231

Open
wants to merge 13 commits into
base: pre_parse
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Metrics/LineLength:

# Offense count: 86
Metrics/AbcSize:
Max: 22
Max: 25

# Offense count: 32
# Configuration parameters: CountComments.
Expand Down
13 changes: 10 additions & 3 deletions lib/kuniri/language/language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# This source code is licensed under the GNU lesser general public license,
# Version 3. See the file COPYING for more details

require 'fileutils'

require_relative '../state_machine/OO_structured_fsm/attribute_state'
require_relative '../state_machine/OO_structured_fsm/class_state'
require_relative '../state_machine/OO_structured_fsm/constructor_state'
Expand Down Expand Up @@ -113,8 +115,14 @@ def set_source(pSource)
# method, work like a hook for give more flexibility to implements any
# needed steps.
def analyse_source(fileElement, source)
analyse_first_step(fileElement, source)
@preParser.pre_parse(source, temp_file_path(source))
analyse_first_step(fileElement, File.open(temp_file_path(source)))
analyse_second_step
FileUtils.rm(temp_file_path(source))
end

def temp_file_path(source)
return source.path + '.tmp'
end

def analyse_first_step(_fileElement, _source)
Expand Down Expand Up @@ -214,8 +222,7 @@ def less_nested

# Verify if is nested or not
def nested?
return true if @countNestedCondLoop.positive?
return false
return countNestedCondLoop.positive?
end

# Reset nested structure
Expand Down
2 changes: 2 additions & 0 deletions lib/kuniri/language/ruby/ruby_syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
require_relative 'method_ruby'
require_relative 'aggregation_ruby'
require_relative '../metadata'
require_relative '../../../kuniri/preparser/ruby.rb'

module Languages

# Handling the ruby syntax for extract information.
class RubySyntax < Languages::Language
def initialize
super
@preParser = RubyPreParser.new
@externRequirementHandler = Languages::Ruby::ExternRequirementRuby.new
@variableHandler = Languages::Ruby::VariableGlobalRuby.new
@functionHandler = Languages::Ruby::FunctionBehaviorRuby.new
Expand Down
47 changes: 47 additions & 0 deletions lib/kuniri/preparser/preparser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class PreParser
def pre_parse(source, out_path)
pre_parsed_lines = []
source.each do |line|
pre_parsed_line = pre_parse_line(line)
pre_parsed_lines.push(pre_parsed_line) unless (pre_parsed_line.empty?)
end
pre_parsed_lines = pre_parse_multiple_lines(pre_parsed_lines)
out_file = File.open(out_path, 'w')
out_file.write(pre_parsed_lines.join("\n"))
out_file.close
end

def pre_parse_line(pre_parsed_line)
pre_parsed_line = remove_whitespaces(pre_parsed_line)
pre_parsed_line = remove_newline(pre_parsed_line)
return pre_parsed_line
end

def pre_parse_multiple_lines(lines)
lines = zip_multiple_lines_command(lines)
lines = split_multiple_command_lines(lines)
return lines
end

def split_multiple_command_line(_lines)
raise NotImplementedError
end

def zip_multiple_lines_command(_lines)
raise NotImplementedError
end

def remove_whitespaces(line)
return line.strip
end

def remove_comments(_line)
raise NotImplementedError
end

def remove_newline(line)
no_newline = line
no_newline.slice("\n")
return no_newline
end
end
43 changes: 43 additions & 0 deletions lib/kuniri/preparser/ruby.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require_relative 'preparser.rb'

class RubyPreParser < PreParser

def initialize
@LINE_CONTINUATION_CHARS = ['.', ',', '(']
@on_multiline_comment = false
end

def remove_comments(line)
@on_multiline_comment = true if line =~ /^=begin(.*?)/
if @on_multiline_comment
pre_parsed_line = ''
else
comment_regex = /(?<non_comm>[^#]*)#.*/
match = line.match(comment_regex)
pre_parsed_line = match.nil? ? line : match['non_comm']
end
@on_multiline_comment = false if line =~ /^=end/
return pre_parsed_line
end

def zip_multiple_lines_command(lines)
zipped_lines = []
curr_line = ''
append_next = true
lines.each do |line|
if append_next
curr_line << line
else
zipped_lines.push(curr_line)
curr_line = line
end
append_next = @LINE_CONTINUATION_CHARS.include?(line[-1])
end
zipped_lines.push(curr_line)
return zipped_lines
end

def split_multiple_command_lines(lines)
return lines
end
end
4 changes: 2 additions & 2 deletions spec/language/ruby/ruby_syntax_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ def openFile(file)
@syntax.analyse_source(instances[0], instances[1])

expect(@syntax.fileElements[0].comments)
.to eq('Comment 1: Multiple line.')
.to eq('Comment 1: Multiple line')
expect(@syntax.fileElements[0].global_functions[0].comments)
.to eq('Global Function number one multiple line')
expect(@syntax.fileElements[0].global_variables[0].comments)
Expand All @@ -557,7 +557,7 @@ def openFile(file)
@syntax.analyse_source(instances[0], instances[1])

expect(@syntax.fileElements[0].classes[0].comments)
.to eq('This is the first class of this file.')
.to eq('This is the first class of this file')
expect(@syntax.fileElements[0].classes[0].constructors[0].comments)
.to eq('Constructor initialize')
expect(@syntax.fileElements[0].classes[0].methods[0].comments)
Expand Down
30 changes: 30 additions & 0 deletions spec/preparser/ruby_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require_relative '../spec_helper'


RSpec.describe Object do

before :all do
@pre_parser = RubyPreParser.new
end


context "zip multiple lines" do
it "Multiple-line function declaration" do
multi_line_function = ["def foo(a,","b,","c)"]
zipped_function = @pre_parser.zip_multiple_lines_command(multi_line_function)
expect(zipped_function).to eq ["def foo(a,b,c)"]
end
it "Multiple-line method chaining" do
multi_line_chain = ["foo.", "a.", "b"]
zipped_chain = @pre_parser.zip_multiple_lines_command(multi_line_chain)
expect(zipped_chain).to eq ["foo.a.b"]
end
it "Multiple-line function with one new line per parameter" do
multi_line_function = ["def foo(", "a,","b,","c)"]
zipped_function = @pre_parser.zip_multiple_lines_command(multi_line_function)
expect(zipped_function).to eq ["def foo(a,b,c)"]
end
end

end

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
=begin
This is the
first class
of this file.
of this file
=end

class Xpto
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=begin
Comment 1:
Multiple line.
Multiple line
=end

require_relative 'simple_multiple_line_comment_class.rb'
Expand Down