Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: minor SDK work #608

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ mod foreign_function_definition;
pub mod fs;
mod inline_snippets;

#[cfg(any(feature = "napi", feature = "wasm_core"))]
pub mod sdk;

mod limits;
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/pattern_compiler/auto_wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ fn wrap_pattern_in_file(
Ok(pattern)
}

pub(crate) fn wrap_pattern_in_before_and_after_each_file(
pub fn wrap_pattern_in_before_and_after_each_file(
pattern: Pattern<MarzanoQueryContext>,
context: &mut dyn SnippetCompilationContext,
) -> Result<Pattern<MarzanoQueryContext>> {
Expand Down
14 changes: 3 additions & 11 deletions crates/core/src/pattern_compiler/builder.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use super::{
auto_wrap::auto_wrap_pattern,
compiler::{
filter_libs, get_definition_info, get_definitions, CompilationContext, DefinitionInfo,
DefinitionInfoKinds, NodeCompilationContext, VariableLocations,
build_standard_global_vars, filter_libs, get_definition_info, get_definitions,
CompilationContext, DefinitionInfo, DefinitionInfoKinds, NodeCompilationContext,
VariableLocations,
},
pattern_compiler::PatternCompiler,
CompilationResult, NodeCompiler,
Expand Down Expand Up @@ -305,12 +306,3 @@ impl CompiledPatternBuilder {
Ok(result)
}
}

pub fn build_standard_global_vars() -> BTreeMap<String, usize> {
BTreeMap::from([
("$new_files".to_owned(), NEW_FILES_INDEX),
("$filename".to_owned(), FILENAME_INDEX),
("$program".to_owned(), PROGRAM_INDEX),
("$absolute_filename".to_owned(), ABSOLUTE_PATH_INDEX),
])
}
27 changes: 24 additions & 3 deletions crates/core/src/pattern_compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ use crate::{
};
use anyhow::{anyhow, bail, Result};
use grit_pattern_matcher::{
constants::{DEFAULT_FILE_NAME, GLOBAL_VARS_SCOPE_INDEX, MATCH_VAR},
constants::{
ABSOLUTE_PATH_INDEX, DEFAULT_FILE_NAME, FILENAME_INDEX, GLOBAL_VARS_SCOPE_INDEX, MATCH_VAR,
NEW_FILES_INDEX, PROGRAM_INDEX,
},
pattern::{
DynamicSnippetPart, GritFunctionDefinition, Pattern, PatternDefinition,
PredicateDefinition, Variable, VariableContent, VariableSource,
Expand Down Expand Up @@ -395,7 +398,7 @@ pub(crate) fn get_definitions(
global_vars
.iter()
.sorted_by(|x, y| Ord::cmp(x.1, y.1))
.map(|x| VariableSource::new(x.0.clone(), context.file.to_owned()))
.map(|x| VariableSource::new(x.0.clone(), DEFAULT_FILE_NAME.to_owned()))
.collect(),
);

Expand Down Expand Up @@ -702,7 +705,7 @@ pub fn src_to_problem(src: String, default_lang: TargetLanguage) -> Result<Probl
Ok(problem)
}

#[derive(Debug, Default)]
#[derive(Debug)]
pub struct VariableLocations {
/// List of scopes, each scope with an array of variables
pub(crate) locations: Vec<Vec<VariableSource>>,
Expand Down Expand Up @@ -746,6 +749,15 @@ impl VariableLocations {
}
variables
}

pub(crate) fn globals() -> Self {
let global_vars = build_standard_global_vars();
let locations = global_vars
.iter()
.map(|(name, index)| VariableSource::new_global(name.to_owned()))
.collect();
Self::new(vec![locations])
}
}

#[cfg(test)]
Expand Down Expand Up @@ -818,3 +830,12 @@ mod tests {
);
}
}

pub fn build_standard_global_vars() -> BTreeMap<String, usize> {
BTreeMap::from([
("$new_files".to_owned(), NEW_FILES_INDEX),
("$filename".to_owned(), FILENAME_INDEX),
("$program".to_owned(), PROGRAM_INDEX),
("$absolute_filename".to_owned(), ABSOLUTE_PATH_INDEX),
])
}
3 changes: 2 additions & 1 deletion crates/core/src/pattern_compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ pub(crate) mod variable_compiler;
pub(crate) mod where_compiler;
pub(crate) mod within_compiler;

pub use builder::build_standard_global_vars;
pub use auto_wrap::wrap_pattern_in_before_and_after_each_file;
pub use builder::CompiledPatternBuilder;
pub use compiler::build_standard_global_vars;
pub use compiler::{src_to_problem_libs, CompilationResult};
pub(crate) use node_compiler::NodeCompiler;
22 changes: 21 additions & 1 deletion crates/core/src/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
marzano_code_snippet::MarzanoCodeSnippet,
marzano_context::MarzanoContext,
marzano_resolved_pattern::{MarzanoFile, MarzanoResolvedPattern},
pattern_compiler::compiler::VariableLocations,
pattern_compiler::{build_standard_global_vars, compiler::VariableLocations},
};
use anyhow::{bail, Result};
use grit_pattern_matcher::{
Expand Down Expand Up @@ -83,6 +83,26 @@ impl Problem {
}
defs
}

pub fn new(
pattern: Pattern<MarzanoQueryContext>,
language: TargetLanguage,
built_ins: BuiltIns,
) -> Self {
Self::new_from_pattern(
pattern,
language,
built_ins,
false, // is_multifile
false, // has_limit
None, // name
VariableLocations::globals(),
vec![], // pattern_definitions
vec![], // predicate_definitions
vec![], // function_definitions
vec![], // foreign_function_definitions
)
}
}

enum FilePattern {
Expand Down
10 changes: 2 additions & 8 deletions crates/core/src/sdk/language_sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use marzano_language::target_language::TargetLanguage;

use crate::{
built_in_functions::BuiltIns,
pattern_compiler::{
auto_wrap::auto_wrap_pattern, build_standard_global_vars, compiler::VariableLocations,
},
pattern_compiler::{auto_wrap::auto_wrap_pattern, compiler::VariableLocations},
problem::{MarzanoQueryContext, Problem},
};

Expand Down Expand Up @@ -48,7 +46,6 @@ impl LanguageSdk {
pattern: Pattern<MarzanoQueryContext>,
) -> Result<Problem> {
let _logs: AnalysisLogs = vec![].into();
let global_vars = build_standard_global_vars();
let mut pattern_definitions = vec![];

let is_multifile = false;
Expand All @@ -69,10 +66,7 @@ impl LanguageSdk {
is_multifile,
false,
None,
VariableLocations::new(vec![global_vars
.into_keys()
.map(VariableSource::new_global)
.collect()]),
VariableLocations::globals(),
pattern_definitions,
vec![],
vec![],
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/sdk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ mod test_js;
#[cfg(feature = "napi")]
mod binding;

pub(crate) use compiler::StatelessCompilerContext;
pub use compiler::StatelessCompilerContext;
pub use language_sdk::LanguageSdk;
pub use pattern_sdk::UncompiledPatternBuilder;
2 changes: 1 addition & 1 deletion crates/language/src/markdown_inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct MarkdownInline {
}

impl MarkdownInline {
pub(crate) fn new(lang: Option<TSLanguage>) -> Self {
pub fn new(lang: Option<TSLanguage>) -> Self {
let language = LANGUAGE.get_or_init(|| lang.unwrap_or_else(language));
let node_types = NODE_TYPES.get_or_init(|| fields_for_nodes(language, NODE_TYPES_STRING));
let metavariable_sort = language.id_for_node_kind("grit_metavariable", true);
Expand Down
2 changes: 1 addition & 1 deletion resources/language-submodules/tree-sitter-c-sharp
Submodule tree-sitter-c-sharp updated 95 files
+0 −46 .editorconfig
+4 −37 .gitattributes
+36 −0 .github/workflows/build.yml
+0 −148 .github/workflows/ci.yml
+0 −19 .github/workflows/fuzz.yml
+0 −26 .github/workflows/lint.yml
+0 −35 .github/workflows/publish.yml
+33 −0 .github/workflows/publish_crate.yml
+13 −40 .gitignore
+6 −0 .npmignore
+0 −58 CMakeLists.txt
+0 −87 Cargo.lock
+17 −15 Cargo.toml
+1 −1 LICENSE
+0 −94 Makefile
+29 −28 Package.swift
+27 −16 README.md
+6 −17 binding.gyp
+0 −16 bindings/c/tree-sitter-c-sharp.h
+0 −10 bindings/c/tree-sitter-c-sharp.pc.in
+0 −13 bindings/go/binding.go
+0 −15 bindings/go/binding_test.go
+22 −14 bindings/node/binding.cc
+0 −9 bindings/node/binding_test.js
+0 −28 bindings/node/index.d.ts
+15 −7 bindings/node/index.js
+0 −11 bindings/python/tests/test_binding.py
+0 −42 bindings/python/tree_sitter_c_sharp/__init__.py
+0 −10 bindings/python/tree_sitter_c_sharp/__init__.pyi
+0 −27 bindings/python/tree_sitter_c_sharp/binding.c
+0 −0 bindings/python/tree_sitter_c_sharp/py.typed
+37 −0 bindings/rust/README.md
+10 −13 bindings/rust/build.rs
+4 −5 bindings/rust/lib.rs
+0 −16 bindings/swift/TreeSitterCSharp/c-sharp.h
+16 −0 bindings/swift/TreeSitterCSharp/csharp.h
+0 −12 bindings/swift/TreeSitterCSharpTests/TreeSitterCSharpTests.swift
+225 −81 corpus/attributes.txt
+663 −0 corpus/classes.txt
+92 −67 corpus/contextual-keywords.txt
+59 −0 corpus/enums.txt
+898 −913 corpus/expressions.txt
+134 −0 corpus/identifiers.txt
+335 −0 corpus/interfaces.txt
+1,263 −0 corpus/literals.txt
+307 −0 corpus/preprocessor.txt
+438 −0 corpus/query-syntax.txt
+527 −0 corpus/records.txt
+273 −0 corpus/source-file-structure.txt
+2,039 −0 corpus/statements.txt
+140 −0 corpus/structs.txt
+30 −14 corpus/type-events.txt
+207 −67 corpus/type-fields.txt
+494 −0 corpus/type-methods.txt
+18 −17 corpus/type-operators.txt
+106 −33 corpus/type-properties.txt
+0 −5 eslint.config.mjs
+0 −7 go.mod
+0 −36 go.sum
+1,272 −1,436 grammar.js
+0 −1,490 package-lock.json
+22 −47 package.json
+0 −33 pyproject.toml
+78 −36 queries/highlights.scm
+5 −5 queries/tags.scm
+5 −0 script/file_sizes.txt
+3 −0 script/known_failures.txt
+45 −0 script/parse-examples
+3 −0 script/update-file-sizes
+7 −0 script/update-known-failures
+0 −62 setup.py
+6,730 −7,493 src/grammar.json
+2,242 −1,419 src/node-types.json
+1,285,917 −769,343 src/parser.c
+31 −415 src/scanner.c
+0 −54 src/tree_sitter/alloc.h
+0 −290 src/tree_sitter/array.h
+13 −55 src/tree_sitter/parser.h
+3 −0 test.js
+0 −400 test/corpus/classes.txt
+0 −22 test/corpus/enums.txt
+0 −105 test/corpus/identifiers.txt
+0 −207 test/corpus/interfaces.txt
+0 −874 test/corpus/literals.txt
+0 −220 test/corpus/preprocessor.txt
+0 −248 test/corpus/query-syntax.txt
+0 −190 test/corpus/records.txt
+0 −188 test/corpus/source-file-structure.txt
+0 −1,034 test/corpus/statements.txt
+0 −69 test/corpus/structs.txt
+0 −305 test/corpus/type-methods.txt
+23 −22 test/highlight/baseline.cs
+2 −2 test/highlight/types.cs
+1 −1 test/highlight/var.cs
+0 −50 tree-sitter.json