Class: Yadriggy::SourceCode
- Inherits:
-
Object
- Object
- Yadriggy::SourceCode
- Defined in:
- lib/yadriggy/source_code.rb
Overview
Retrieves source code in the S-expression style.
Defined Under Namespace
Classes: Cons
Class Method Summary collapse
- .def_at?(line, t, prog) ⇒ Boolean
- .find_sexp(prog, line) ⇒ Object
- .find_sexp2(prog, line, current) ⇒ Object
-
.get_sexp(proc) ⇒ Object
Gets an S-expression.
- .max(a, b) ⇒ Object
- .min(a, b) ⇒ Object
- .read_pry_history ⇒ Object
Class Method Details
.def_at?(line, t, prog) ⇒ Boolean
102 103 104 105 106 |
# File 'lib/yadriggy/source_code.rb', line 102 def self.def_at?(line, t, prog) (t == :def || t == :defs) && prog[1].is_a?(Array) && prog[1][0] == :@ident && prog[1][2][0] == line end |
.find_sexp(prog, line) ⇒ Object
55 56 57 |
# File 'lib/yadriggy/source_code.rb', line 55 def self.find_sexp(prog, line) find_sexp2(prog, line, [1, nil]) end |
.find_sexp2(prog, line, current) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/yadriggy/source_code.rb', line 60 def self.find_sexp2(prog, line, current) if prog.nil? || !prog.is_a?(Array) return nil else t = prog[0] if t == :@ident || t == :@tstring_content || t == :@const || t == :@int || t == :@float || t == :@kw || t == :@label || t == :@gvar || t == :@CHAR #current[0] = prog[2][0] current_line = prog[2][0] if line < current_line && !current[1].nil? return current[1] else current[0] = current_line return nil end else is_block = (t == :brace_block || t == :do_block || t == :def || t == :defs || t == :lambda) if is_block && line == current[0] || def_at?(line, t, prog) return prog else current[1] = nil prog.each do |e| r = find_sexp2(e, line, current) return r unless r.nil? end if is_block if line <= current[0] return prog else current[1] = prog return nil end else nil end end end end end |
.get_sexp(proc) ⇒ Object
Gets an S-expression.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/yadriggy/source_code.rb', line 13 def self.get_sexp(proc) return nil unless proc.is_a?(Proc) || proc.is_a?(Method) || proc.is_a?(UnboundMethod) file_name, line = proc.source_location return nil if file_name.nil? src = if file_name == "(pry)" then read_pry_history else File.read(file_name) end prog = Ripper.sexp(src) prog && [file_name, find_sexp(prog, line)] end |
.max(a, b) ⇒ Object
51 52 53 |
# File 'lib/yadriggy/source_code.rb', line 51 def self.max(a, b) if a > b then a else b end end |
.min(a, b) ⇒ Object
47 48 49 |
# File 'lib/yadriggy/source_code.rb', line 47 def self.min(a, b) if a < b then a else b end end |
.read_pry_history ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/yadriggy/source_code.rb', line 25 def self.read_pry_history cmds = Pry.commands # The line number seems wrong if the source code is in pry_history. # To correct the line number, a blank line is added to source. source = "\n" lineno = 0 lineno1 = Pry.history.original_lines File.foreach(Pry.config.history.file) do |line| lineno += 1 if lineno > lineno1 if cmds.select {|k,v| v.matches?(line) }.empty? source << line else # source << "\n" end end end source end |