Class: RubyLexUtils
Constant Summary collapse
- BLANK_LINE_REGEX =
Regular expression to detect blank lines
/^\s*$/
- LONELY_ELSE_REGEX =
Regular expression to detect lines containing only ‘else’
/^\s*else\s*$/
- KEY_KEYWORDS =
[ 'class'.freeze, 'module'.freeze, 'def'.freeze, 'undef'.freeze, 'begin'.freeze, 'rescue'.freeze, 'ensure'.freeze, 'end'.freeze, 'if'.freeze, 'unless'.freeze, 'then'.freeze, 'elsif'.freeze, 'else'.freeze, 'case'.freeze, 'when'.freeze, 'while'.freeze, 'until'.freeze, 'for'.freeze, 'break'.freeze, 'next'.freeze, 'redo'.freeze, 'retry'.freeze, 'in'.freeze, 'do'.freeze, 'return'.freeze, 'alias'.freeze ]
Instance Method Summary collapse
-
#contains_begin?(text) ⇒ Boolean
Whether the text contains the ‘begin’ keyword.
-
#contains_block_beginning?(text) ⇒ Boolean
Whether the text contains a keyword which starts a block.
-
#contains_end?(text) ⇒ Boolean
Whether the text contains the ‘end’ keyword.
-
#contains_keyword?(text) ⇒ Boolean
Whether the text contains a Ruby keyword.
- #continue_block?(text) ⇒ Boolean
-
#each_lexed_segment(text) {|line, instrumentable, inside_begin, line_no| ... } ⇒ Object
Yields each lexed segment and if the segment is instrumentable.
-
#initialize ⇒ RubyLexUtils
constructor
Create a new RubyLex and StringIO to hold the text to operate on.
-
#remove_comments(text, progress_dialog = nil) ⇒ String
The text with all comments removed.
- #ripper_lex_without_warning(code) ⇒ Object
Constructor Details
#initialize ⇒ RubyLexUtils
Create a new RubyLex and StringIO to hold the text to operate on
82 83 84 85 |
# File 'lib/openc3/utilities/ruby_lex_utils.rb', line 82 def initialize @lex = RubyLex.new @lex_io = StringIO.new('') end |
Instance Method Details
#contains_begin?(text) ⇒ Boolean
Returns Whether the text contains the ‘begin’ keyword.
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/openc3/utilities/ruby_lex_utils.rb', line 99 def contains_begin?(text) @lex.reinitialize @lex_io.string = text @lex.set_input(@lex_io) tokens = ripper_lex_without_warning(text) tokens.each do |token| if token[1] == :on_kw and token[2] == 'begin' return true end end return false end |
#contains_block_beginning?(text) ⇒ Boolean
Returns Whether the text contains a keyword which starts a block. i.e. ‘do’, ‘{’, or ‘begin’.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/openc3/utilities/ruby_lex_utils.rb', line 149 def contains_block_beginning?(text) @lex.reinitialize @lex_io.string = text @lex.set_input(@lex_io) tokens = ripper_lex_without_warning(text) tokens.each do |token| if token[1] == :on_kw if token[2] == 'begin' || token[2] == 'do' return true end elsif token[1] == :on_lbrace return true end end return false end |
#contains_end?(text) ⇒ Boolean
Returns Whether the text contains the ‘end’ keyword.
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/openc3/utilities/ruby_lex_utils.rb', line 114 def contains_end?(text) @lex.reinitialize @lex_io.string = text @lex.set_input(@lex_io) tokens = ripper_lex_without_warning(text) tokens.each do |token| if token[1] == :on_kw and token[2] == 'end' return true end end return false end |
#contains_keyword?(text) ⇒ Boolean
Returns Whether the text contains a Ruby keyword.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/openc3/utilities/ruby_lex_utils.rb', line 129 def contains_keyword?(text) @lex.reinitialize @lex_io.string = text @lex.set_input(@lex_io) tokens = ripper_lex_without_warning(text) tokens.each do |token| if token[1] == :on_kw if KEY_KEYWORDS.include?(token[2]) return true end elsif token[1] == :on_lbrace and !token[3].allbits?(Ripper::EXPR_BEG | Ripper::EXPR_LABEL) return true end end return false end |
#continue_block?(text) ⇒ Boolean
166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/openc3/utilities/ruby_lex_utils.rb', line 166 def continue_block?(text) @lex.reinitialize @lex_io.string = text @lex.set_input(@lex_io) tokens = RubyLex.ripper_lex_without_warning(text) index = tokens.length - 1 while index > 0 token = tokens[index] return true if token[1] == :on_kw and token[2] == "do" index -= 1 end return false end |
#each_lexed_segment(text) {|line, instrumentable, inside_begin, line_no| ... } ⇒ Object
Yields each lexed segment and if the segment is instrumentable
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/openc3/utilities/ruby_lex_utils.rb', line 217 def each_lexed_segment(text) inside_begin = false lex = RubyLex.new lex_io = StringIO.new(text) lex.set_input(lex_io) lex.line = '' line = '' continue_indent = nil begin_indent = nil previous_indent = 0 while lexed = lex.lex #puts "lexed = #{lexed.chomp}, indent = #{lex.indent}, continue = #{lex.continue}" lex.line_no += lexed.count("\n") lex.line.concat lexed line.concat lexed if lex.continue if not continue_block?(lexed) unless continue_indent if (lex.indent - previous_indent) > 1 continue_indent = lex.indent - 1 else continue_indent = previous_indent end end #puts "continue_indent = #{continue_indent}" next end elsif continue_indent if lex.indent > continue_indent next else yield line, !contains_keyword?(line), inside_begin, lex.exp_line_no line = '' lex.exp_line_no = lex.line_no if lex.indent == 0 lex.line = '' end next end end previous_indent = lex.indent continue_indent = nil # Detect the beginning and end of begin blocks so we can not catch exceptions there if contains_begin?(line) if contains_end?(line) # Assume the user is being fancy with a single line begin; end; # Ignore else inside_begin = true begin_indent = lex.indent unless begin_indent # Don't restart for nested begins end end if inside_begin and lex.indent < begin_indent begin_indent = nil inside_begin = false end loop do # loop to allow restarting for nested conditions # Yield blank lines and lonely else lines before the actual line while (index = line.index("\n")) one_line = line[0..index] if BLANK_LINE_REGEX.match?(one_line) yield one_line, true, inside_begin, lex.exp_line_no lex.exp_line_no += 1 line = line[(index + 1)..-1] elsif LONELY_ELSE_REGEX.match?(one_line) yield one_line, false, inside_begin, lex.exp_line_no lex.exp_line_no += 1 line = line[(index + 1)..-1] else break end end if contains_keyword?(line) if contains_block_beginning?(line) section = '' line.each_line do |lexed_part| section << lexed_part if contains_block_beginning?(section) yield section, false, inside_begin, lex.exp_line_no break end lex.exp_line_no += 1 end lex.exp_line_no += 1 remainder = line[(section.length)..-1] line = remainder next unless remainder.empty? else yield line, false, inside_begin, lex.exp_line_no end elsif !line.empty? num_left_brackets = line.count('{') num_right_brackets = line.count('}') if num_left_brackets != num_right_brackets # Don't instrument lines with unequal numbers of { and } brackets yield line, false, inside_begin, lex.exp_line_no else yield line, true, inside_begin, lex.exp_line_no end end line = '' lex.exp_line_no = lex.line_no break end # loop do if lex.indent == 0 lex.line = '' end end # while lexed end |
#remove_comments(text, progress_dialog = nil) ⇒ String
Returns The text with all comments removed.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/openc3/utilities/ruby_lex_utils.rb', line 184 def remove_comments(text, progress_dialog = nil) @lex.reinitialize @lex_io.string = text @lex.set_input(@lex_io) comments_removed = "" token_count = 0 progress = 0.0 tokens = ripper_lex_without_warning(text) tokens.each do |token| token_count += 1 if token[1] != :on_comment comments_removed << token[2] else newline_count = token[2].count("\n") comments_removed << ("\n" * newline_count) end if progress_dialog and token_count % 10000 == 0 progress += 0.01 progress = 0.0 if progress >= 0.99 progress_dialog.set_overall_progress(progress) end end return comments_removed end |