Module: Pry::Hackage

Defined in:
lib/pry/hack.rb

Overview

The main module containing the hack code

Defined Under Namespace

Classes: Hack, ModOpHack

Constant Summary collapse

VERSION =
0.1
REGEX_REGEX =

Regular expressions kept constant for optimization and intended for use to match patterns that may be used by a hack in the future.

[%r/\A\/(.+?)\/(.+?)*\Z/, %r/%r(.)([^\1\\]|\\.)*\1/]
SYMBOL_REGEX =
%r/\A\:(.+?)\Z/
KEYWORDS =
%w[BEGIN END alias and begin break case class def defined? do else elsif end ensure for
if in module next not or redo rescue retry return super then when while yield]

Class Method Summary collapse

Class Method Details

.hack_line(str) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
209
210
211
212
213
214
215
# File 'lib/pry/hack.rb', line 160

def self.hack_line(str)
 # A dirty syntax hack used to enable and disable pry-hack when a hack goes
 # wrong and you are unable to fix it because of a side effect. The syntax is as follows
 # -*- pry-hack: disable -*-
 # or to enable
 # -*- pry-hack: enable -*-
 if str =~ /#\s*-\*-\s*pry-hack:\s*(disable|enable)\s*-\*-\s*/
   self.config.hack.enabled = ($1 == "enable") ? true : false
 end
 return str unless ::Pry.config.hack.enabled      
 stack  = []
 tstack = []
 state  = nil

 lexer = RubyLexer.new("(pry)", str)
 tstack = lexer.to_set.to_a[1..-2].select {|x| !(x.is_a? RubyLexer::FileAndLineToken) }
 tstack.map!(&:to_s)
   while c = tstack.shift
     if state.nil?
       state= case c
              when '.'
                # self.config.hack.s.meth
                :meth
              when '%'
                # self.config.hack.s.modop
                :modop
              when Hackage::SYMBOL_REGEX
                # self.config.hack.s.symbol_prefix
                :symbol_prefix
              when *Hackage::REGEX_REGEX
                # self.config.hack.s.regex
                :regex
              end
      stack.push c
      next 
    else
      if state == :modop && tstack[0] =~ /[^A-Za-z0-9#]/
         char = Regexp.escape(tstack[0])
         char_to_match = Regexp.escape(ModOpHack::PAIR_MATCHES[tstack[0]] || char) 
         c += (tstack.shift + " ") until c =~ /^.#{char}.*#{char_to_match}/ 
      end
      hacks = (self.config.hack.s.send(state).values.sort_by {|x| x.score(c) })
      if hacks.nil?
       stack.push c
       next
      end 
      hacks.compact!
      hacks.reject! {|x| x.score(c).nil? }
      if hacks.any? && stack.last == '%' then stack.pop end   # Do this so that you can make sure not to have a double modulo occurance
      c = (hacks.any?) ? hacks.last.run(binding) : c
      state = nil
      stack.push c
    end
  end  
  return stack.join
end