Skip to content

Commit

Permalink
Tree query language and engine
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonyBlakey committed Jan 22, 2024
1 parent 0a50c4b commit 8cfaa52
Show file tree
Hide file tree
Showing 27 changed files with 5,088 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-donkeys-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/slang": patch
---

Add tree query implementation as `Query::parse` and `Cursor::query`
54 changes: 49 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ napi = { version = "2.14.2", features = ["compat-mode", "napi8", "serde-json"] }
napi-build = { version = "2.1.0" }
napi-derive = { version = "2.14.6" }
once_cell = { version = "1.19.0" }
nom = { version = "7.1.3" }
owo-colors = { version = "3.5.0", features = ["supports-colors"] }
proc-macro2 = { version = "1.0.53" }
quote = { version = "1.0.26" }
Expand All @@ -92,8 +93,9 @@ serde = { version = "1.0.158", features = ["derive", "rc"] }
serde_json = { version = "1.0.94", features = ["preserve_order"] }
serde_yaml = { version = "0.9.19" }
similar-asserts = { version = "1.4.2" }
strum = { version = "0.24.0" }
strum_macros = { version = "0.24.0" }
stack-graphs = { version = "0.12.0" }
strum = { version = "0.25.0", features = ["phf"] }
strum_macros = { version = "0.25.3" }
syn = { version = "2.0.29", features = [
"fold",
"full",
Expand Down
3 changes: 3 additions & 0 deletions crates/codegen/parser/generator/src/rust_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ impl RustGenerator {
"lexer.rs",
"parse_error.rs",
"parse_output.rs",
"query_engine.rs",
"query_model.rs",
"query_parser.rs",
"text_index.rs",
"napi/napi_cst.rs",
"napi/napi_cursor.rs",
Expand Down
1 change: 1 addition & 0 deletions crates/codegen/parser/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ napi-build = { workspace = true, optional = true }
ariadne = { workspace = true }
napi = { workspace = true, optional = true }
napi-derive = { workspace = true, optional = true }
nom = { workspace = true }
serde = { workspace = true }
strum = { workspace = true }
strum_macros = { workspace = true }
Expand Down
6 changes: 3 additions & 3 deletions crates/codegen/parser/runtime/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ impl PathRuleNode {

/// A pointer to a [`Node`] in a CST, used by the [`Cursor`] to implement the traversal.
#[derive(Clone, Debug, PartialEq, Eq)]
struct PathNode {
pub(super) struct PathNode {
/// The node the cursor is currently pointing to.
node: Node,
pub(super) node: Node,
/// The index of the current child node in the parent's children.
// Required to go to the next/previous sibling.
child_number: usize,
Expand Down Expand Up @@ -64,7 +64,7 @@ pub struct Cursor {
/// The list of ancestor rule nodes that the `current` node is a part of.
path: Vec<PathRuleNode>,
/// The node the cursor is currently pointing to.
current: PathNode,
pub(super) current: PathNode,
/// Whether the cursor is completed, i.e. at the root node as a result of traversal (or when `complete`d).
/// If `true`, the cursor cannot be moved.
is_completed: bool,
Expand Down
13 changes: 13 additions & 0 deletions crates/codegen/parser/runtime/src/kinds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ use napi_derive::napi;
strum_macros::Display,
strum_macros::EnumString,
)]
#[strum(use_phf)]
#[cfg_attr( feature = "slang_napi_interfaces", /* derives `Clone` and `Copy` */ napi(string_enum, namespace = "kinds") )]
#[cfg_attr(not(feature = "slang_napi_interfaces"), derive(Clone, Copy))]
pub enum TokenKind {
SKIPPED,
Identifier,
// Expanded by the template engine
// Used for testing
X,
Y,
Z,
}

#[derive(
Expand All @@ -32,12 +38,18 @@ pub enum TokenKind {
strum_macros::Display,
strum_macros::EnumString,
)]
#[strum(use_phf)]
#[cfg_attr( feature = "slang_napi_interfaces", /* derives `Clone` and `Copy` */ napi(string_enum, namespace = "kinds") )]
#[cfg_attr(not(feature = "slang_napi_interfaces"), derive(Clone, Copy))]
pub enum RuleKind {
LeadingTrivia,
TrailingTrivia,
// Expanded by the template engine
// Used for testing
A,
B,
C,
D,
}

impl RuleKind {
Expand All @@ -58,6 +70,7 @@ impl RuleKind {
strum_macros::Display,
strum_macros::EnumString,
)]
#[strum(use_phf)]
#[cfg_attr(feature = "slang_napi_interfaces", /* derives `Clone` and `Copy` */ napi(string_enum, namespace = "kinds"))]
#[cfg_attr(not(feature = "slang_napi_interfaces"), derive(Clone, Copy))]
pub enum FieldName {
Expand Down
9 changes: 9 additions & 0 deletions crates/codegen/parser/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ pub mod kinds;
pub(crate) mod lexer;
pub mod parse_error;
pub mod parse_output;
pub mod query_engine;
pub mod query_model;
pub(crate) mod query_parser;
pub mod text_index;

#[cfg(feature = "slang_napi_interfaces")]
pub mod napi;

#[cfg(test)]
pub mod query_engine_tests;

#[cfg(test)]
pub mod query_parser_tests;
Loading

0 comments on commit 8cfaa52

Please sign in to comment.