Skip to content

Commit 94db982

Browse files
initial commit
1 parent 790206e commit 94db982

14 files changed

+1162
-1
lines changed

.gitignore

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/target
2+
node_modules
3+
4+
# IDEs
5+
/.idea
6+
7+
node_modules
8+
.DS_Store
9+
dist
10+
dist-ssr
11+
*.local
12+
13+
# Created by https://www.toptal.com/developers/gitignore/api/node
14+
# Edit at https://www.toptal.com/developers/gitignore?templates=node
15+
16+
### Node ###
17+
# Logs
18+
logs
19+
*.log
20+
npm-debug.log*
21+
yarn-debug.log*
22+
yarn-error.log*
23+
lerna-debug.log*
24+
25+
# Diagnostic reports (https://nodejs.org/api/report.html)
26+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
27+
28+
# Runtime data
29+
pids
30+
*.pid
31+
*.seed
32+
*.pid.lock
33+
34+
# Directory for instrumented libs generated by jscoverage/JSCover
35+
lib-cov
36+
37+
# Coverage directory used by tools like istanbul
38+
coverage
39+
*.lcov
40+
41+
# nyc test coverage
42+
.nyc_output
43+
44+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
45+
.grunt
46+
47+
# Bower dependency directory (https://bower.io/)
48+
bower_components
49+
50+
# node-waf configuration
51+
.lock-wscript
52+
53+
# Compiled binary addons (https://nodejs.org/api/addons.html)
54+
build/Release
55+
56+
# Dependency directories
57+
node_modules/
58+
jspm_packages/
59+
60+
# TypeScript v1 declaration files
61+
typings/
62+
63+
# TypeScript cache
64+
*.tsbuildinfo
65+
66+
# Optional npm cache directory
67+
.npm
68+
69+
# Optional eslint cache
70+
.eslintcache
71+
72+
# Microbundle cache
73+
.rpt2_cache/
74+
.rts2_cache_cjs/
75+
.rts2_cache_es/
76+
.rts2_cache_umd/
77+
78+
# Optional REPL history
79+
.node_repl_history
80+
81+
# Output of 'npm pack'
82+
*.tgz
83+
84+
# Yarn Integrity file
85+
.yarn-integrity
86+
87+
# dotenv environment variables file
88+
.env
89+
.env.test
90+
91+
# parcel-bundler cache (https://parceljs.org/)
92+
.cache
93+
94+
# Next.js build output
95+
.next
96+
97+
# Nuxt.js build / generate output
98+
.nuxt
99+
dist
100+
101+
# Gatsby files
102+
.cache/
103+
# Comment in the public line in if your project uses Gatsby and not Next.js
104+
# https://nextjs.org/blog/next-9-1#public-directory-support
105+
# public
106+
107+
# vuepress build output
108+
.vuepress/dist
109+
110+
# Serverless directories
111+
.serverless/
112+
113+
# FuseBox cache
114+
.fusebox/
115+
116+
# DynamoDB Local files
117+
.dynamodb/
118+
119+
# TernJS port file
120+
.tern-port
121+
122+
# Stores VSCode versions used for testing VSCode extensions
123+
.vscode-test
124+
125+
# End of https://www.toptal.com/developers/gitignore/api/node
126+
127+
128+
#Added by cargo
129+
130+
/target
131+
Cargo.lock
132+
133+
*.node

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "tree-sitter-typescript"]
2+
path = tree-sitter-typescript
3+
url = [email protected]:tree-sitter/tree-sitter-typescript.git

Cargo.toml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "semgrep-rs"
3+
version = "0.1.0"
4+
authors = ["Herrington Darkholme <[email protected]>"]
5+
edition = "2018"
6+
description = "sem grep in Rs"
7+
keywords = ["ast", "pattern match", "codemod"]
8+
license = "MIT"
9+
repository = "https://github.com/HerringtonDarkholme/semgrep"
10+
11+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
12+
13+
[dependencies]
14+
tree-sitter = "0.20.6"
15+
[build-dependencies]
16+
cc="*"

README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
# semgreprs
1+
# Pattern Matcher
2+
1. Tree Pattern
3+
1. Node Kind
4+
5+
# Metavariable Matcher
6+
1. Regex
7+
2. Pattern
8+
9+
# Rule
10+
1. patterns
11+
2. patterns either
12+
3. pattern inside
13+
4. pattern-inside
14+
5. pattern-not-inside

build.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use std::path::PathBuf;
2+
3+
fn main() {
4+
let dir: PathBuf = ["tree-sitter-typescript", "tsx", "src"].iter().collect();
5+
6+
cc::Build::new()
7+
.include(&dir)
8+
.flag_if_supported("-Wno-unused-parameter")
9+
.flag_if_supported("-Wno-unused-but-set-variable")
10+
.flag_if_supported("-Wno-trigraphs")
11+
.file(dir.join("parser.c"))
12+
.file(dir.join("scanner.c"))
13+
.compile("tree-sitter-typescript");
14+
}

src/language.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub trait Language {
2+
fn skippable_kind_ids() -> &'static [u16];
3+
fn gen_meta_varaible(meta_var_source: &str) -> String;
4+
}

src/lib.rs

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use std::ops::{Deref, DerefMut};
2+
use std::rc::Rc;
3+
4+
mod ts_parser;
5+
mod language;
6+
mod matcher;
7+
mod meta_var;
8+
mod node;
9+
mod pattern;
10+
pub mod rule;
11+
12+
pub use pattern::Pattern;
13+
pub use node::Node;
14+
pub use meta_var::MetaVarMatcher;
15+
16+
pub struct Semgrep {
17+
root: Root,
18+
}
19+
20+
pub struct Root {
21+
inner: ts_parser::Tree,
22+
source: Rc<String>,
23+
}
24+
25+
impl Root {
26+
fn new(src: &str) -> Self {
27+
Self {
28+
inner: ts_parser::parse(src),
29+
source: Rc::new(src.into()),
30+
}
31+
}
32+
pub fn root(&self) -> Node {
33+
Node {
34+
inner: self.inner.root_node(),
35+
source: &self.source,
36+
}
37+
}
38+
}
39+
40+
// creational API
41+
impl Semgrep {
42+
pub fn new<S: AsRef<str>>(source: S) -> Self {
43+
Self {
44+
root: Root::new(source.as_ref()),
45+
}
46+
}
47+
pub fn generate(_n: &Node) -> String {
48+
todo!()
49+
}
50+
}
51+
52+
impl Deref for Semgrep {
53+
type Target = Root;
54+
fn deref(&self) -> &Self::Target {
55+
&self.root
56+
}
57+
}
58+
impl DerefMut for Semgrep {
59+
fn deref_mut(&mut self) -> &mut Self::Target {
60+
&mut self.root
61+
}
62+
}
63+
64+
#[cfg(test)]
65+
mod test {
66+
/*
67+
use super::*;
68+
#[test]
69+
fn test_replace() {
70+
let mut node = Semgrep::new("var a = 1;");
71+
node.replace("var $_$ = $_$", "let $_$ = $_$");
72+
let replaced = Semgrep::generate(&node);
73+
assert_eq!(replaced, "let a = 1");
74+
}
75+
*/
76+
}

0 commit comments

Comments
 (0)