Class: Languages::Ruby::RepetitionRuby

Inherits:
Languages::Repetition show all
Defined in:
lib/kuniri/language/ruby/repetition_ruby.rb

Overview

Class responsible for handling Ruby repetition data structures.

Instance Method Summary collapse

Instance Method Details

#detect_repetition(pLine) ⇒ Object (protected)

Override



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kuniri/language/ruby/repetition_ruby.rb', line 31

def detect_repetition(pLine)
  regexExp = /^\s*while\s+(.*)/
  return pLine[regexExp, 0] if regexExp =~ pLine

  regexExp = /^\s*for\s+(\w+)\s+in\s+(.+)(\bdo\b)?/
  return pLine[regexExp, 0] if regexExp =~ pLine

  regexExp = /^\s*until\s+(.*)(\bdo\b)?/
  return pLine[regexExp, 0] if regexExp =~ pLine

  regexExp = /^\s*end\s+while\s+(.+)/
  return pLine[regexExp, 0] if regexExp =~ pLine

  regexExp = /(lambda)\s+do(.*)\s*/
  return pLine[regexExp, 0] if regexExp =~ pLine

  # TODO: BUG in this case -> puts "test.each do |x|"
  regexExp = /\.(\s*\w+)\s+do(.*)\s*/
  return pLine[regexExp, 0] if regexExp =~ pLine

  return nil
end

#get_expression(pType, pString) ⇒ Object (protected)

Override



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/kuniri/language/ruby/repetition_ruby.rb', line 78

def get_expression(pType, pString)
  case pType
    when Languages::FOR_LABEL
      pString.slice!("for")
      pString.slice!("do")
    when Languages::WHILE_LABEL
      pString.slice!("while")
      pString.slice!("do")
    when Languages::UNTIL_LABEL
      pString.slice!("until")
      pString.slice!("do")
    when Languages::DO_WHILE_LABEL
      pString.slice!("end")
      pString.slice!("while")
    else
      pString.slice!(".")
  end
  leftStrip = pString.lstrip
  rightStrip = leftStrip.rstrip

  return rightStrip
end

#get_repetition(pLine) ⇒ Object

Get Ruby repetition structure.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kuniri/language/ruby/repetition_ruby.rb', line 16

def get_repetition(pLine)
  result = detect_repetition(pLine)
  return nil unless result
  #result = remove_unnecessary_information(result)
  repetitionCaptured = Languages::RepetitionData.new
  repetitionCaptured.type = repetition_type(result)
  repetitionCaptured.expression = get_expression(
                                            repetitionCaptured.type,
                                            result)
  return repetitionCaptured
end

#remove_unnecessary_information(pString) ⇒ Object (protected)

Override



102
103
104
105
# File 'lib/kuniri/language/ruby/repetition_ruby.rb', line 102

def remove_unnecessary_information(pString)
  return pString.gsub(/\./,"") if pString =~ /\./
  return pString
end

#repetition_type(pString) ⇒ Object (protected)

Override



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/kuniri/language/ruby/repetition_ruby.rb', line 55

def repetition_type(pString)
  regexExp = /^\s+while|^while/
  return Languages::WHILE_LABEL if regexExp =~ pString

  regexExp = /^\s+for|^for/
  return Languages::FOR_LABEL if regexExp =~ pString

  regexExp = /^\s+until|^until/
  return Languages::UNTIL_LABEL if regexExp =~ pString

  regexExp = /^\s*end\s+while\s+/
  return Languages::DO_WHILE_LABEL if regexExp =~ pString

  regexExp = /^lambda\s+/
  return Languages::LAMBDA_LABEL if regexExp =~ pString

  regexExp = /\.\w+/
  return pString[/\w+/, 0].upcase if regexExp =~ pString

  return nil
end