Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
closes #152
  • Loading branch information
rodrigosiqueira committed Mar 5, 2017
2 parents d31e630 + 82df3a5 commit 3eafcfb
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 126 deletions.
4 changes: 2 additions & 2 deletions .rubocop.yml
Expand Up @@ -54,9 +54,9 @@ Metrics/ClassLength:

# Offense count: 28
Metrics/CyclomaticComplexity:
Max: 7
Max: 10

# Offense count: 139
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 14
Max: 25
Expand Up @@ -11,6 +11,7 @@ module Languages
ELSE_LABEL ||= 'ELSE'
ELSIF_LABEL ||= 'ELSIF'
UNLESS_LABEL ||= 'UNLESS'
TERNARY_LABEL ||= 'TERNARY'
CASE_LABEL ||= 'CASE'
WHEN_LABEL ||= 'WHEN'
WHILE_LABEL ||= 'WHILE'
Expand Down
Expand Up @@ -16,11 +16,13 @@ class BasicStructure < Languages::BasicData
attr_accessor :type
attr_accessor :expression
attr_accessor :level
attr_accessor :singleLine

def initialize
@type = 'none'
@expression = 'empty'
@level = 0
@singleLine = false
end
# class
end
Expand Down
136 changes: 62 additions & 74 deletions lib/kuniri/language/ruby/conditional_ruby.rb
Expand Up @@ -15,83 +15,71 @@ module Ruby
# Class responsible for handling ruby conditional statements.
class ConditionalRuby < Languages::Conditional

public

# Get ruby conditional.
# @see Languages::Conditional
def get_conditional(pLine)
result = detect_conditional(pLine)
return nil unless result

conditionalCaptured = Languages::ConditionalData.new
conditionalCaptured.type = conditional_type(pLine)
unless conditionalCaptured.type == Languages::ELSE_LABEL
conditionalCaptured.expression = get_expression(result)
end

return conditionalCaptured
end
# Get ruby conditional.
# @see Languages::Conditional
def get_conditional(pLine)
@line = pLine
result = detect_conditional(pLine)
return nil unless result
return result
end

protected

# Override.
def detect_conditional(pLine)
regexExp = /^\s*if\s+(.*)/
return pLine.scan(regexExp)[0].join("") if regexExp =~ pLine

regexExp = /^\s*case\s+(.*)/
return pLine.scan(regexExp)[0].join("") if regexExp =~ pLine

regexExp = /^\s*when\s+(.*)/
return pLine.scan(regexExp)[0].join("") if regexExp =~ pLine

regexExp = /^\s*unless\s+(.*)/
return pLine.scan(regexExp)[0].join("") if regexExp =~ pLine

regexExp = /^\s*elsif\s+(.*)/
return pLine.scan(regexExp)[0].join("") if regexExp =~ pLine

regexExp = /^\s*else\s*/
return pLine.scan(regexExp)[0].gsub(" ", "") if regexExp =~ pLine

# Override.
def detect_conditional(pLine)
case pLine
when IF_REGEX
return build_data(IF_REGEX, Languages::IF_LABEL)
when IF_INLINE_REGEX
return build_data(IF_INLINE_REGEX, Languages::IF_LABEL, true)
when CASE_REGEX
return build_data(CASE_REGEX, Languages::CASE_LABEL)
when WHEN_REGEX
return build_data(WHEN_REGEX, Languages::WHEN_LABEL)
when UNLESS_REGEX
return build_data(UNLESS_REGEX, Languages::UNLESS_LABEL)
when UNLESS_INLINE_REGEX
return build_data(UNLESS_INLINE_REGEX, Languages::UNLESS_LABEL, true)
when ELSIF_REGEX
return build_data(ELSIF_REGEX, Languages::ELSIF_LABEL)
when ELSE_REGEX
return build_data(ELSE_REGEX, Languages::ELSE_LABEL, false, false)
when TERNARY_REGEX
return build_data(TERNARY_REGEX, Languages::TERNARY_LABEL, true)
else
return nil
end

# Override
def conditional_type(pString)
regexExp = /^\s+if|^if/
return Languages::IF_LABEL if regexExp =~ pString

regexExp = /^\s+case|^case/
return Languages::CASE_LABEL if regexExp =~ pString

regexExp = /^\s+when|^when/
return Languages::WHEN_LABEL if regexExp =~ pString

regexExp = /^\s+unless|^unless/
return Languages::UNLESS_LABEL if regexExp =~ pString

regexExp = /^\s+elsif|^elsif/
return Languages::ELSIF_LABEL if regexExp =~ pString

regexExp = /^\s+else|^else/
return Languages::ELSE_LABEL if regexExp =~ pString

return nil
end

# Override
def get_expression(pString)
return pString.strip
end

private

IF_REGEX = /^\s*if\s+(.*)/
IF_INLINE_REGEX = /.+\s+if\s+(.*)/
CASE_REGEX = /^\s*case\s+(.*)/
WHEN_REGEX = /^\s*when\s+(.*)/
UNLESS_REGEX = /^\s*unless\s+(.*)/
UNLESS_INLINE_REGEX = /.+\s+unless\s+(.*)/
ELSIF_REGEX = /^\s*elsif\s+(.*)/
ELSE_REGEX = /^\s*else\s*/
TERNARY_REGEX = /(.*)\?.*\:.*/

@line = ''

def build_data(pRegex, pLabel, pSingleLine = false, pExpression = true)
conditionalCaptured = Languages::ConditionalData.new
conditionalCaptured.type = pLabel
conditionalCaptured.singleLine = pSingleLine
if pExpression
conditionalCaptured.expression = get_expression(@line[pRegex, 1])
end

# Override
def get_expression(pString)
leftStrip = pString.lstrip
rightStrip = leftStrip.rstrip
return rightStrip
end

# Class
end

# Module
end

# Module
end
return conditionalCaptured
end
end # End of Class
end # End of Ruby Module
end # End of Languages Module
Expand Up @@ -24,7 +24,6 @@ def initialize(pLanguage)
end

def handle_line(pLine)

if ! (conditional = @language.line_inspect(CONDITIONAL_ID, pLine)).nil?
conditional_capture if isNestedStructure?(conditional.type)
elsif ! (repetition = @language.line_inspect(REPETITION_ID, pLine)).nil?
Expand Down Expand Up @@ -85,8 +84,10 @@ def execute(pElementFile, pLine)

addBasicStructure(pLine, flag, classIndex, pElementFile)

#if (@language.endBlockHandler.has_end_of_block?(pLine) || singleLine)
if (@language.endBlockHandler.has_end_of_block?(pLine))
element = @language.processed_line
singleLineFlag = element.nil? ? false : element.singleLine

if (@language.endBlockHandler.has_end_of_block?(pLine) || singleLineFlag)
updateLevel(flag, pElementFile, classIndex)
end
return pElementFile
Expand Down

0 comments on commit 3eafcfb

Please sign in to comment.