Class: RubyLexUtils
- Defined in:
- lib/openc3/utilities/ruby_lex_utils.rb,
lib/openc3/utilities/ruby_lex_utils.rb
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 ]
- KEYWORD_TOKENS =
Ruby keywords
[RubyToken::TkCLASS, RubyToken::TkMODULE, RubyToken::TkDEF, RubyToken::TkUNDEF, RubyToken::TkBEGIN, RubyToken::TkRESCUE, RubyToken::TkENSURE, RubyToken::TkEND, RubyToken::TkIF, RubyToken::TkUNLESS, RubyToken::TkTHEN, RubyToken::TkELSIF, RubyToken::TkELSE, RubyToken::TkCASE, RubyToken::TkWHEN, RubyToken::TkWHILE, RubyToken::TkUNTIL, RubyToken::TkFOR, RubyToken::TkBREAK, RubyToken::TkNEXT, RubyToken::TkREDO, RubyToken::TkRETRY, RubyToken::TkIN, RubyToken::TkDO, RubyToken::TkRETURN, RubyToken::TkIF_MOD, RubyToken::TkUNLESS_MOD, RubyToken::TkWHILE_MOD, RubyToken::TkUNTIL_MOD, RubyToken::TkALIAS, RubyToken::TklBEGIN, RubyToken::TklEND, RubyToken::TkfLBRACE]
- BLOCK_BEGINNING_TOKENS =
Ruby keywords which define the beginning of a block: do, {, begin
[RubyToken::TkDO, RubyToken::TkfLBRACE, RubyToken::TkBEGIN]
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_keyword?(text) ⇒ Boolean
Whether the text contains a Ruby keyword.
-
#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
84 85 86 87 |
# File 'lib/openc3/utilities/ruby_lex_utils.rb', line 84 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.
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/openc3/utilities/ruby_lex_utils.rb', line 101 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’.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/openc3/utilities/ruby_lex_utils.rb', line 136 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_keyword?(text) ⇒ Boolean
Returns Whether the text contains a Ruby keyword.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/openc3/utilities/ruby_lex_utils.rb', line 116 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 |
#each_lexed_segment(text) {|line, instrumentable, inside_begin, line_no| ... } ⇒ Object
Yields each lexed segment and if the segment is instrumentable
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 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 |
# File 'lib/openc3/utilities/ruby_lex_utils.rb', line 190 def each_lexed_segment(text) inside_begin = false indent = 0 lex = RubyLex.new lex_io = StringIO.new(text) lex.set_input(lex_io) lex.line = '' while lexed = lex.lex lex.line_no += lexed.count("\n") lex.line.concat lexed next if lex.ltype or lex.continue # Detect the beginning and end of begin blocks so we can not catch exceptions there if indent == 0 and contains_begin?(lex.line) inside_begin = true indent = lex.indent else indent += lex.indent if indent > 0 end if inside_begin and indent <= 0 indent = 0 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 = lex.line.index("\n")) line = lex.line[0..index] if BLANK_LINE_REGEX.match?(line) yield line, true, inside_begin, lex.exp_line_no lex.exp_line_no += 1 lex.line = lex.line[(index + 1)..-1] elsif LONELY_ELSE_REGEX.match?(line) yield line, false, inside_begin, lex.exp_line_no lex.exp_line_no += 1 lex.line = lex.line[(index + 1)..-1] else break end end if contains_keyword?(lex.line) if contains_block_beginning?(lex.line) section = '' lex.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 = lex.line[(section.length)..-1] lex.line = remainder next unless remainder.empty? else yield lex.line, false, inside_begin, lex.exp_line_no end elsif !lex.line.empty? num_left_brackets = lex.line.count('{') num_right_brackets = lex.line.count('}') if num_left_brackets != num_right_brackets # Don't instrument lines with unequal numbers of { and } brackets yield lex.line, false, inside_begin, lex.exp_line_no else yield lex.line, true, inside_begin, lex.exp_line_no end end lex.line = '' lex.exp_line_no = lex.line_no lex.indent = 0 break end # loop do end # while lexed end |
#remove_comments(text, progress_dialog = nil) ⇒ String
Returns The text with all comments removed.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/openc3/utilities/ruby_lex_utils.rb', line 157 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 |