Sha256: 7e7b3e783ec67803e04c07b10d92a42cd986508e8b3baa5463ebfc7181efa989

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

require 'rubygems'
require File.dirname(__FILE__) + '/../lib/sexp_path'
require 'ruby_parser'

# Example program, this will scan a file for anything
# matching the Sexp passed in.


pattern = ARGV.shift
paths = ARGV

if paths.empty? || !pattern
  puts "Prints classes and methods in a file"
  puts "usage:"
  puts "  ruby sexp_grep.rb <pattern> <path>"
  puts "example:"
  puts "  ruby sexp_grep.rb 't(:defn)' *.rb"
  exit
end

begin
  # Generate the pattern, we use a little instance_eval trickery here. 
  pattern = SexpPath::SexpQueryBuilder.instance_eval(pattern)
rescue Exception=>ex
  puts "Invalid Pattern: '#{pattern}'"
  puts "Trace:"
  puts ex
  puts ex.backtrace
  exit 1
end

# For each path the user defined, search for the SexpPath pattern
paths.each do |path|  
  # Parse it with RubyParser, and append line numbers
  sexp = RubyParser.new.parse(File.read(path), path)
  found = false
  
  # Search it with the given pattern, printing any results
  sexp.search_each(pattern) do |match|
    if !found
      puts path
      found = true
    end
    puts "%4i: %s" % [match.sexp.line, match.sexp.inspect]
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sexp_path-0.5.0 examples/sexp_grep.rb