Getty Ritter 2 years ago
commit
be3d9b231b
5 changed files with 636 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 310 0
      Cargo.lock
  3. 16 0
      Cargo.toml
  4. 121 0
      src/main.rs
  5. 188 0
      src/pattern.rs

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+/target

+ 310 - 0
Cargo.lock

@@ -0,0 +1,310 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "ansi_term"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
+dependencies = [
+ "winapi",
+]
+
+[[package]]
+name = "atty"
+version = "0.2.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
+dependencies = [
+ "hermit-abi",
+ "libc",
+ "winapi",
+]
+
+[[package]]
+name = "autocfg"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
+
+[[package]]
+name = "bitflags"
+version = "1.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
+
+[[package]]
+name = "blart"
+version = "0.1.0"
+dependencies = [
+ "clap",
+ "crossbeam",
+ "fehler",
+ "lib-ruby-parser",
+ "walkdir",
+]
+
+[[package]]
+name = "cfg-if"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+
+[[package]]
+name = "clap"
+version = "2.33.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
+dependencies = [
+ "ansi_term",
+ "atty",
+ "bitflags",
+ "strsim",
+ "textwrap",
+ "unicode-width",
+ "vec_map",
+]
+
+[[package]]
+name = "crossbeam"
+version = "0.8.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4ae5588f6b3c3cb05239e90bd110f257254aecd01e4635400391aeae07497845"
+dependencies = [
+ "cfg-if",
+ "crossbeam-channel",
+ "crossbeam-deque",
+ "crossbeam-epoch",
+ "crossbeam-queue",
+ "crossbeam-utils",
+]
+
+[[package]]
+name = "crossbeam-channel"
+version = "0.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "06ed27e177f16d65f0f0c22a213e17c696ace5dd64b14258b52f9417ccb52db4"
+dependencies = [
+ "cfg-if",
+ "crossbeam-utils",
+]
+
+[[package]]
+name = "crossbeam-deque"
+version = "0.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "94af6efb46fef72616855b036a624cf27ba656ffc9be1b9a3c931cfc7749a9a9"
+dependencies = [
+ "cfg-if",
+ "crossbeam-epoch",
+ "crossbeam-utils",
+]
+
+[[package]]
+name = "crossbeam-epoch"
+version = "0.9.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4ec02e091aa634e2c3ada4a392989e7c3116673ef0ac5b72232439094d73b7fd"
+dependencies = [
+ "cfg-if",
+ "crossbeam-utils",
+ "lazy_static",
+ "memoffset",
+ "scopeguard",
+]
+
+[[package]]
+name = "crossbeam-queue"
+version = "0.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9b10ddc024425c88c2ad148c1b0fd53f4c6d38db9697c9f1588381212fa657c9"
+dependencies = [
+ "cfg-if",
+ "crossbeam-utils",
+]
+
+[[package]]
+name = "crossbeam-utils"
+version = "0.8.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d82cfc11ce7f2c3faef78d8a684447b40d503d9681acebed6cb728d45940c4db"
+dependencies = [
+ "cfg-if",
+ "lazy_static",
+]
+
+[[package]]
+name = "fehler"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d5729fe49ba028cd550747b6e62cd3d841beccab5390aa398538c31a2d983635"
+dependencies = [
+ "fehler-macros",
+]
+
+[[package]]
+name = "fehler-macros"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ccb5acb1045ebbfa222e2c50679e392a71dd77030b78fb0189f2d9c5974400f9"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "hermit-abi"
+version = "0.1.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "lazy_static"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+
+[[package]]
+name = "lib-ruby-parser"
+version = "3.0.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ee3ebfbaaf084b4cac6d6f5a4740b47e6c9956fb1f3fb21bce07902dbffb1ea8"
+
+[[package]]
+name = "libc"
+version = "0.2.97"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "12b8adadd720df158f4d70dfe7ccc6adb0472d7c55ca83445f6a5ab3e36f8fb6"
+
+[[package]]
+name = "memoffset"
+version = "0.6.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "59accc507f1338036a0477ef61afdae33cde60840f4dfe481319ce3ad116ddf9"
+dependencies = [
+ "autocfg",
+]
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.27"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f0d8caf72986c1a598726adc988bb5984792ef84f5ee5aa50209145ee8077038"
+dependencies = [
+ "unicode-xid",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "same-file"
+version = "1.0.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
+dependencies = [
+ "winapi-util",
+]
+
+[[package]]
+name = "scopeguard"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
+
+[[package]]
+name = "strsim"
+version = "0.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
+
+[[package]]
+name = "syn"
+version = "1.0.73"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f71489ff30030d2ae598524f61326b902466f72a0fb1a8564c001cc63425bcc7"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-xid",
+]
+
+[[package]]
+name = "textwrap"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
+dependencies = [
+ "unicode-width",
+]
+
+[[package]]
+name = "unicode-width"
+version = "0.1.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
+
+[[package]]
+name = "unicode-xid"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
+
+[[package]]
+name = "vec_map"
+version = "0.8.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
+
+[[package]]
+name = "walkdir"
+version = "2.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56"
+dependencies = [
+ "same-file",
+ "winapi",
+ "winapi-util",
+]
+
+[[package]]
+name = "winapi"
+version = "0.3.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
+dependencies = [
+ "winapi-i686-pc-windows-gnu",
+ "winapi-x86_64-pc-windows-gnu",
+]
+
+[[package]]
+name = "winapi-i686-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+
+[[package]]
+name = "winapi-util"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
+dependencies = [
+ "winapi",
+]
+
+[[package]]
+name = "winapi-x86_64-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

+ 16 - 0
Cargo.toml

@@ -0,0 +1,16 @@
+[package]
+name = "blart"
+version = "0.1.0"
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+lib-ruby-parser = "*"
+clap = "*"
+fehler = "*"
+walkdir = "*"
+crossbeam = "*"
+
+[profile.release]
+debug = true

+ 121 - 0
src/main.rs

@@ -0,0 +1,121 @@
+mod pattern;
+
+use walkdir::WalkDir;
+
+#[derive(Debug)]
+struct Args {
+    targets: Vec<String>,
+    num_threads: u8,
+}
+
+impl Args {
+    fn parse() -> Args {
+        let matches = clap::App::new("blart")
+            .version("0.1")
+            .author("Getty Ritter <blart@infinitenegativeutility.com>")
+            .about("Paul Blart, Rubo Cop")
+            .arg(clap::Arg::with_name("threads")
+                 .short("j")
+                 .value_name("THREADS")
+                 .help("Spawn the specified number of worker threads")
+                 .takes_value(true))
+            .arg(clap::Arg::with_name("targets")
+                 .help("The Ruby sources and directories to search")
+                 .multiple(true))
+            .get_matches();
+
+        let targets = if let Some(values) = matches.values_of("targets") {
+            values.map(|x| x.to_owned()).collect::<Vec<String>>()
+        } else {
+            vec![".".to_owned()]
+        };
+
+        let num_threads = if let Some(val) = matches.value_of("threads") {
+            match val.parse() {
+                Ok(x) => x,
+                Err(_) => {
+                    panic!("Invalid number of threads: {}", val);
+                }
+            }
+        } else {
+            8
+        };
+        Args { targets, num_threads }
+    }
+}
+
+fn is_ruby_source(path: &std::path::Path) -> bool {
+    if let Some(ext) = path.extension() {
+        return ext == "rb";
+    }
+    false
+}
+
+fn main() {
+    let args = Args::parse();
+    println!("Got args: {:?}", args);
+    let Args {
+        targets,
+        num_threads,
+    } = args;
+
+    let p = pattern::Pattern {
+        node_type: pattern::NodeType::Send,
+        name: Some("ActiveRecord".to_owned()),
+        children: vec![],
+        rest: false,
+    };
+
+    let (work_send, work_recv) = crossbeam::channel::unbounded();
+    let (result_send, result_recv) = crossbeam::channel::unbounded();
+
+    // produce a thread which populates the channel with work
+    let producer = std::thread::spawn(move || {
+        for target in targets {
+            for entry in WalkDir::new(target).into_iter().filter_map(|e| e.ok()) {
+                if is_ruby_source(entry.path()) {
+                    work_send.send(entry.into_path()).expect("Unable to send work from producer thread");
+                }
+            }
+        }
+    });
+
+    // produce a set of threads which can grab work to be done
+    let workers: Vec<std::thread::JoinHandle<()>> = (0..num_threads).map(|id| {
+        let receiver = work_recv.clone();
+        let sender = result_send.clone();
+        let pat = p.clone();
+        std::thread::spawn(move || {
+            while let Ok(ref msg) = receiver.recv() {
+                use std::io::Read;
+
+                let mut buf = Vec::new();
+                {
+                    let mut f = std::fs::File::open(msg).expect("Unable to read file");
+                    f.read_to_end(&mut buf).expect("Unable to read file");
+                }
+                let parser = lib_ruby_parser::Parser::new(buf, std::default::Default::default());
+                let lib_ruby_parser::ParserResult { ast, diagnostics, .. } = parser.do_parse();
+                if let Some(ast) = ast {
+                    let matches = pat.find_matches(&*ast);
+                    if matches.len() > 0 {
+                        sender.send(format!("{:?}: found matches: {:?}", msg, matches)).unwrap();
+                    }
+                } else {
+                    // sender.send(format!("Unable to parse {:?}", msg)).unwrap();
+                }
+            }
+        })
+    }).collect();
+    drop(result_send);
+
+    while let Ok(msg) = result_recv.recv() {
+        println!("{}", msg);
+    }
+
+    // join all the threads
+    producer.join().expect("Producer thread panicked!");
+    for w in workers {
+        w.join().expect("Worker thread panicked!");
+    }
+}

+ 188 - 0
src/pattern.rs

@@ -0,0 +1,188 @@
+use lib_ruby_parser as p;
+
+#[derive(Eq, PartialEq, Copy, Clone)]
+pub enum NodeType {
+    Send,
+    Local,
+    Const,
+    Anything,
+}
+
+#[derive(Eq, PartialEq, Clone)]
+pub struct Pattern {
+    pub node_type: NodeType,
+    pub name: Option<String>,
+    pub children: Vec<Pattern>,
+    pub rest: bool,
+}
+
+impl Pattern {
+    pub fn matches(&self, p: &p::Node) -> bool {
+        match p {
+            p::Node::Const(cnst) => {
+                if let Some(name) = &self.name {
+                    &cnst.name == name
+                } else {
+                    false
+                }
+            }
+            _ => false,
+        }
+    }
+
+    pub fn find_matches<'a>(&self, p: &'a p::Node) -> Vec<&'a p::Node> {
+        let mut matches = Vec::new();
+        self.find_matches_ref(p, &mut matches);
+        matches
+    }
+
+    fn find_matches_ref<'a, 'b>(&self, p: &'a p::Node, res: &'b mut Vec<&'a p::Node>) {
+        if self.matches(p) {
+            res.push(p);
+        }
+
+        match p {
+            p::Node::Alias(_) => {}
+            p::Node::AndAsgn(_) => {}
+            p::Node::And(_) => {}
+            p::Node::Arg(_) => {}
+            p::Node::Args(_) => {}
+            p::Node::Array(_) => {}
+            p::Node::ArrayPattern(_) => {}
+            p::Node::ArrayPatternWithTail(_) => {}
+            p::Node::BackRef(_) => {}
+            p::Node::Begin(begin) => {
+                for stmt in begin.statements.iter() {
+                    self.find_matches_ref(stmt, res);
+                }
+            }
+            p::Node::Block(_) => {}
+            p::Node::BlockPass(_) => {}
+            p::Node::Blockarg(_) => {}
+            p::Node::Break(_) => {}
+            p::Node::Case(_) => {}
+            p::Node::CaseMatch(_) => {}
+            p::Node::Casgn(_) => {}
+            p::Node::Cbase(_) => {}
+            p::Node::Class(class) => {
+                self.find_matches_ref(&class.name, res);
+                if let Some(ref superclass) = class.superclass {
+                    self.find_matches_ref(superclass, res);
+                }
+                if let Some(ref body) = class.body {
+                    self.find_matches_ref(body, res);
+                }
+            }
+            p::Node::Complex(_) => {}
+            p::Node::Const(_) => {}
+            p::Node::ConstPattern(_) => {}
+            p::Node::CSend(_) => {}
+            p::Node::Cvar(_) => {}
+            p::Node::Cvasgn(_) => {}
+            p::Node::Def(_) => {}
+            p::Node::Defined(_) => {}
+            p::Node::Defs(_) => {}
+            p::Node::Dstr(_) => {}
+            p::Node::Dsym(_) => {}
+            p::Node::EFlipFlop(_) => {}
+            p::Node::EmptyElse(_) => {}
+            p::Node::Encoding(_) => {}
+            p::Node::Ensure(_) => {}
+            p::Node::Erange(_) => {}
+            p::Node::False(_) => {}
+            p::Node::File(_) => {}
+            p::Node::FindPattern(_) => {}
+            p::Node::Float(_) => {}
+            p::Node::For(_) => {}
+            p::Node::ForwardArg(_) => {}
+            p::Node::ForwardedArgs(_) => {}
+            p::Node::Gvar(_) => {}
+            p::Node::Gvasgn(_) => {}
+            p::Node::Hash(_) => {}
+            p::Node::Kwargs(_) => {}
+            p::Node::HashPattern(_) => {}
+            p::Node::Heredoc(_) => {}
+            p::Node::If(_) => {}
+            p::Node::IfGuard(_) => {}
+            p::Node::IfMod(_) => {}
+            p::Node::IfTernary(_) => {}
+            p::Node::IFlipFlop(_) => {}
+            p::Node::MatchPattern(_) => {}
+            p::Node::MatchPatternP(_) => {}
+            p::Node::InPattern(_) => {}
+            p::Node::Index(_) => {}
+            p::Node::IndexAsgn(_) => {}
+            p::Node::Int(_) => {}
+            p::Node::Irange(_) => {}
+            p::Node::Ivar(_) => {}
+            p::Node::Ivasgn(_) => {}
+            p::Node::Kwarg(_) => {}
+            p::Node::KwBegin(_) => {}
+            p::Node::Kwnilarg(_) => {}
+            p::Node::Kwoptarg(_) => {}
+            p::Node::Kwrestarg(_) => {}
+            p::Node::Kwsplat(_) => {}
+            p::Node::Lambda(_) => {}
+            p::Node::Line(_) => {}
+            p::Node::Lvar(_) => {}
+            p::Node::Lvasgn(_) => {}
+            p::Node::Masgn(_) => {}
+            p::Node::MatchAlt(_) => {}
+            p::Node::MatchAs(_) => {}
+            p::Node::MatchCurrentLine(_) => {}
+            p::Node::MatchNilPattern(_) => {}
+            p::Node::MatchRest(_) => {}
+            p::Node::MatchVar(_) => {}
+            p::Node::MatchWithLvasgn(_) => {}
+            p::Node::Mlhs(_) => {}
+            p::Node::Module(module) => {
+                self.find_matches_ref(&module.name, res);
+                if let Some(ref body) = module.body {
+                    self.find_matches_ref(body, res);
+                }
+            }
+            p::Node::Next(_) => {}
+            p::Node::Nil(_) => {}
+            p::Node::NthRef(_) => {}
+            p::Node::Numblock(_) => {}
+            p::Node::OpAsgn(_) => {}
+            p::Node::Optarg(_) => {}
+            p::Node::Or(_) => {}
+            p::Node::OrAsgn(_) => {}
+            p::Node::Pair(_) => {}
+            p::Node::Pin(_) => {}
+            p::Node::Postexe(_) => {}
+            p::Node::Preexe(_) => {}
+            p::Node::Procarg0(_0) => {}
+            p::Node::Rational(_) => {}
+            p::Node::Redo(_) => {}
+            p::Node::RegOpt(_) => {}
+            p::Node::Regexp(_) => {}
+            p::Node::Rescue(_) => {}
+            p::Node::RescueBody(_) => {}
+            p::Node::Restarg(_) => {}
+            p::Node::Retry(_) => {}
+            p::Node::Return(_) => {}
+            p::Node::SClass(_) => {}
+            p::Node::Self_(__) => {}
+            p::Node::Send(_) => {}
+            p::Node::Shadowarg(_) => {}
+            p::Node::Splat(_) => {}
+            p::Node::Str(_) => {}
+            p::Node::Super(_) => {}
+            p::Node::Sym(_) => {}
+            p::Node::True(_) => {}
+            p::Node::Undef(_) => {}
+            p::Node::UnlessGuard(_) => {}
+            p::Node::Until(_) => {}
+            p::Node::UntilPost(_) => {}
+            p::Node::When(_) => {}
+            p::Node::While(_) => {}
+            p::Node::WhilePost(_) => {}
+            p::Node::XHeredoc(_) => {}
+            p::Node::Xstr(_) => {}
+            p::Node::Yield(_) => {}
+            p::Node::ZSuper(_) => {}
+        }
+    }
+}