Skip to content

Commit

Permalink
Rust & Typescript Cursors for the CST
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonyBlakey committed Jul 14, 2023
1 parent ef0a2a5 commit bbb01ad
Show file tree
Hide file tree
Showing 44 changed files with 2,152 additions and 2,046 deletions.
18 changes: 18 additions & 0 deletions crates/codegen/syntax/src/code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,15 @@ impl CodeGenerator {
)
.unwrap();

codegen
.copy_file(
&codegen
.repo_root
.join("crates/codegen/syntax_templates/src/shared/cursor.rs"),
&output_dir.join("cursor.rs"),
)
.unwrap();

codegen
.copy_file(
&codegen
Expand Down Expand Up @@ -513,6 +522,15 @@ impl CodeGenerator {
)
.unwrap();

codegen
.copy_file(
&codegen
.repo_root
.join("crates/codegen/syntax_templates/src/shared/visitor.rs"),
&output_dir.join("visitor.rs"),
)
.unwrap();

{
// Use `format!` here because the content contains comments, that `quote!` throws away.
let content = format!(
Expand Down
12 changes: 2 additions & 10 deletions crates/codegen/syntax/src/rust_lib_code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,20 @@ impl CodeGenerator {
mod stream;

pub mod cst;
pub mod cst_visitor;
pub mod cursor;
pub mod kinds;
pub mod language;
pub mod parse_error;
pub mod parse_output;
pub mod text_index;
pub mod visitor;
};

codegen
.write_file(&output_dir.join("mod.rs"), &content.to_string())
.unwrap();
}

codegen
.copy_file(
&codegen
.repo_root
.join("crates/codegen/syntax_templates/src/rust/cst_visitor.rs"),
&output_dir.join("cst_visitor.rs"),
)
.unwrap();

codegen
.copy_file(
&codegen
Expand Down
18 changes: 15 additions & 3 deletions crates/codegen/syntax/src/typescript_lib_code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ impl CodeGenerator {
mod stream;

pub mod cst;
pub mod cst_types;
pub mod cst_ts_wrappers;
pub mod cursor;
pub mod cursor_ts_wrappers;
pub mod kinds;
pub mod language;
pub mod parse_error;
pub mod parse_output;
pub mod text_index;
pub mod visitor;
};

codegen
Expand All @@ -46,8 +49,17 @@ impl CodeGenerator {
.copy_file(
&codegen
.repo_root
.join("crates/codegen/syntax_templates/src/typescript/cst_types.rs"),
&output_dir.join("cst_types.rs"),
.join("crates/codegen/syntax_templates/src/typescript/cst_ts_wrappers.rs"),
&output_dir.join("cst_ts_wrappers.rs"),
)
.unwrap();

codegen
.copy_file(
&codegen
.repo_root
.join("crates/codegen/syntax_templates/src/typescript/cursor_ts_wrappers.rs"),
&output_dir.join("cursor_ts_wrappers.rs"),
)
.unwrap();

Expand Down
125 changes: 0 additions & 125 deletions crates/codegen/syntax_templates/src/rust/cst_visitor.rs

This file was deleted.

5 changes: 4 additions & 1 deletion crates/codegen/syntax_templates/src/rust/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#[path = "../shared/cst.rs"]
pub mod cst;
#[path = "../shared/cursor.rs"]
pub mod cursor;
#[path = "../shared/language.rs"]
pub mod language;
#[path = "../shared/parse_error.rs"]
Expand All @@ -18,7 +20,8 @@ pub mod scanner_macros;
pub mod stream;
#[path = "../shared/text_index.rs"]
pub mod text_index;
#[path = "../shared/visitor.rs"]
pub mod visitor;

pub mod cst_visitor;
pub mod kinds;
pub mod parse_output;
31 changes: 31 additions & 0 deletions crates/codegen/syntax_templates/src/shared/cst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::rc::Rc;
use serde::Serialize;

use super::{
cursor::Cursor,
kinds::{RuleKind, TokenKind},
text_index::TextIndex,
};
Expand Down Expand Up @@ -52,4 +53,34 @@ impl Node {
Self::Token(node) => (&node.text).into(),
}
}

pub fn cursor(&self) -> Cursor {
Cursor::new(self.clone())
}

pub fn is_token_kind(&self, kinds: &[TokenKind]) -> Option<&Rc<TokenNode>> {
if let Node::Token(token_node) = self {
if kinds.contains(&token_node.kind) {
return Some(token_node);
}
}
return None;
}

pub fn is_token_matching<F: Fn(&Rc<TokenNode>) -> bool>(&self, predicate: F) -> bool {
matches!(self, Node::Token(token_node) if predicate(&token_node))
}

pub fn is_rule_kind(&self, kinds: &[RuleKind]) -> Option<&Rc<RuleNode>> {
if let Node::Rule(rule_node) = self {
if kinds.contains(&rule_node.kind) {
return Some(rule_node);
}
}
return None;
}

pub fn is_rule_matching<F: Fn(&Rc<RuleNode>) -> bool>(&self, predicate: F) -> bool {
matches!(self, Node::Rule(rule_node) if predicate(&rule_node))
}
}
Loading

0 comments on commit bbb01ad

Please sign in to comment.