Skip to content

Commit

Permalink
feat: SDK basics (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante authored Sep 22, 2024
1 parent 3725163 commit 1470218
Show file tree
Hide file tree
Showing 58 changed files with 808 additions and 351 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@ expression: "parsed_lines[0]"
__typename: PatternInfo
messages: []
variables:
- name: $new_files
scopedName: 0_0_$new_files
ranges: []
- name: $program
scopedName: 0_1_$program
ranges: []
- name: $filename
scopedName: 0_2_$filename
ranges: []
- name: $absolute_filename
scopedName: 0_3_$absolute_filename
ranges: []
- name: $body
scopedName: 14_0_$body
ranges:
Expand All @@ -28,9 +16,6 @@ variables:
column: 21
startByte: 15
endByte: 20
- name: $match
scopedName: 14_1_$match
ranges: []
sourceFile: "`function () { $body }`"
parsedPattern: "[..]"
valid: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,6 @@ expression: "parsed_lines[0]"
__typename: PatternInfo
messages: []
variables:
- name: $new_files
scopedName: 0_0_$new_files
ranges: []
- name: $program
scopedName: 0_1_$program
ranges: []
- name: $filename
scopedName: 0_2_$filename
ranges: []
- name: $absolute_filename
scopedName: 0_3_$absolute_filename
ranges: []
- name: $match
scopedName: 14_0_$match
ranges: []
- name: $body
scopedName: 15_0_$body
ranges:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,7 @@ expression: v
---
__typename: PatternInfo
messages: []
variables:
- name: $new_files
scopedName: 0_0_$new_files
ranges: []
- name: $program
scopedName: 0_1_$program
ranges: []
- name: $filename
scopedName: 0_2_$filename
ranges: []
- name: $absolute_filename
scopedName: 0_3_$absolute_filename
ranges: []
- name: $match
scopedName: 15_0_$match
ranges: []
variables: []
sourceFile: "engine marzano(0.1)\nlanguage js\n\nfunction adder() js {\n console.log(\"We are in JavaScript now!\");\n return 10 % 3\n}\n\n`x` => adder()"
parsedPattern: "[..]"
valid: true
Expand Down
15 changes: 0 additions & 15 deletions crates/cli_bin/tests/snapshots/parse__parses_grit_file.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@ expression: v
__typename: PatternInfo
messages: []
variables:
- name: $new_files
scopedName: 0_0_$new_files
ranges: []
- name: $program
scopedName: 0_1_$program
ranges: []
- name: $filename
scopedName: 0_2_$filename
ranges: []
- name: $absolute_filename
scopedName: 0_3_$absolute_filename
ranges: []
- name: $msg
scopedName: 14_0_$msg
ranges:
Expand All @@ -28,9 +16,6 @@ variables:
column: 18
startByte: 26
endByte: 30
- name: $match
scopedName: 14_1_$match
ranges: []
sourceFile: "language js\n\n`console.log($msg)`\n"
parsedPattern: "[..]"
valid: true
Expand Down
18 changes: 8 additions & 10 deletions crates/core/src/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ mod test {
};

use crate::problem::MarzanoQueryContext;
use crate::stateless::StatelessCompilerContext;
use crate::sdk::StatelessCompilerContext;
use crate::{
pattern_compiler::{CompilationResult, PatternBuilder},
pattern_compiler::{CompilationResult, CompiledPatternBuilder},
test_utils::{run_on_test_files, SyntheticFile},
};

Expand Down Expand Up @@ -46,14 +46,12 @@ mod test {

assert!(!callback_called.load(std::sync::atomic::Ordering::SeqCst));

let mut builder = PatternBuilder::start_empty(src, lang).unwrap();
let mut builder = CompiledPatternBuilder::start_empty(src, lang).unwrap();
builder = builder.matches_callback(Box::new(move |binding, context, state, logs| {
assert!(state.find_var_in_scope("$foo").is_some());
assert!(state.find_var_in_scope("$bar").is_some());
assert!(state.find_var_in_scope("$dude").is_none());
assert!(state.find_var_in_scope("$baz").is_none());
let _registered_var = state.register_var("fuzz");
assert!(state.find_var_in_scope("fuzz").is_some());

let pattern = Pattern::Contains(Box::new(Contains::new(
Pattern::<MarzanoQueryContext>::StringConstant(StringConstant::new(
Expand Down Expand Up @@ -107,13 +105,13 @@ mod test {
let matches_found = Arc::new(AtomicUsize::new(0));
let matches_found_clone = Arc::clone(&matches_found);

let mut builder = PatternBuilder::start_empty(src, lang).unwrap();
let mut builder = CompiledPatternBuilder::start_empty(src, lang).unwrap();

builder = builder.matches_callback(Box::new(move |binding, context, state, logs| {
let this_lang = TargetLanguage::from_string("js", None).unwrap();

let console_builder =
PatternBuilder::start_empty("call_expression()", this_lang).unwrap();
CompiledPatternBuilder::start_empty("call_expression()", this_lang).unwrap();
let console_pattern = console_builder
.compile(None, None, false)
.unwrap()
Expand Down Expand Up @@ -167,13 +165,13 @@ mod test {
let matches_found = Arc::new(AtomicUsize::new(0));
let matches_found_clone = Arc::clone(&matches_found);

let mut builder = PatternBuilder::start_empty(src, lang).unwrap();
let mut builder = CompiledPatternBuilder::start_empty(src, lang).unwrap();

builder = builder.matches_callback(Box::new(move |binding, context, state, logs| {
let this_lang = TargetLanguage::from_string("js", None).unwrap();

let console_builder =
PatternBuilder::start_empty("`console.log(name)`", this_lang.clone()).unwrap();
CompiledPatternBuilder::start_empty("`console.log(name)`", this_lang).unwrap();
let console_pattern = console_builder
.compile(None, None, false)
.unwrap()
Expand All @@ -188,7 +186,7 @@ mod test {
matches_found_clone.fetch_add(1, std::sync::atomic::Ordering::SeqCst);

// Ok we are operating on the right function, now verify more things
let mut compiler = StatelessCompilerContext::new(this_lang.clone());
let mut compiler = StatelessCompilerContext::new(this_lang);
let standalone_snippet = compiler.parse_snippet("console.log(name)").unwrap();
let standalone_contains = p_contains(standalone_snippet);

Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod equivalence;
mod foreign_function_definition;
pub mod fs;
mod inline_snippets;
mod stateless;
pub mod sdk;

mod lazy;
mod limits;
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/optimizer/hoist_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub fn extract_filename_pattern<Q: QueryContext>(

impl<Q: QueryContext> FilenamePatternExtractor<Q> for Bubble<Q> {
fn extract_filename_pattern(&self) -> Result<Option<Pattern<Q>>> {
extract_filename_pattern(&self.pattern_def.pattern)
extract_filename_pattern(self.pattern_def.pattern())
}
}

Expand Down
Loading

0 comments on commit 1470218

Please sign in to comment.