Skip to content

Commit

Permalink
Make function to get all syntax tree leaves / Serialization of Span (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
wawel37 authored Oct 15, 2024
1 parent 7a2cdc9 commit 65d3246
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
14 changes: 11 additions & 3 deletions crates/cairo-lang-filesystem/src/span.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::iter::Sum;
use std::ops::{Add, Range, Sub};

use serde::{Deserialize, Serialize};

use crate::db::FilesGroup;
use crate::ids::FileId;

Expand All @@ -10,7 +12,9 @@ mod test;

/// Byte length of an utf8 string.
// This wrapper type is used to avoid confusion with non-utf8 sizes.
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(
Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
pub struct TextWidth(u32);
impl TextWidth {
pub fn from_char(c: char) -> Self {
Expand Down Expand Up @@ -45,7 +49,9 @@ impl Sum for TextWidth {
}

/// Byte offset inside a utf8 string.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(
Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
pub struct TextOffset(TextWidth);
impl TextOffset {
pub fn add_width(self, width: TextWidth) -> Self {
Expand All @@ -67,7 +73,9 @@ impl Sub for TextOffset {
}

/// A range of text offsets that form a span (like text selection).
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[derive(
Copy, Clone, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize,
)]
pub struct TextSpan {
pub start: TextOffset,
pub end: TextOffset,
Expand Down
8 changes: 8 additions & 0 deletions crates/cairo-lang-syntax/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,14 @@ impl SyntaxNode {
pub fn preorder<'db>(&self, db: &'db dyn SyntaxGroup) -> Preorder<'db> {
Preorder::new(self.clone(), db)
}

/// Gets all the leaves of the SyntaxTree, where the self node is the root of a tree.
pub fn tokens<'a>(&'a self, db: &'a dyn SyntaxGroup) -> impl Iterator<Item = Self> + 'a {
self.preorder(db).filter_map(|event| match event {
WalkEvent::Enter(node) if node.green_node(db).kind.is_terminal() => Some(node),
_ => None,
})
}
}

/// Trait for the typed view of the syntax tree. All the internal node implementations are under
Expand Down

0 comments on commit 65d3246

Please sign in to comment.