-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stable Stream Token interface (#6707)
- Loading branch information
Showing
9 changed files
with
102 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "cairo-lang-primitive-token" | ||
version = "1.0.0" | ||
edition.workspace = true | ||
repository.workspace = true | ||
license-file.workspace = true | ||
description = "Primitive representation of a TokenStream" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#![deny(missing_docs)] | ||
//! This crate defines unfiorm and primitive form of the TokenStream. | ||
//! We want this to be as stable as possible and limit the changes here to bare minimum. | ||
|
||
/// Primitive representation of a token's span. | ||
pub struct PrimitiveSpan { | ||
/// Start position of the span. | ||
pub start: usize, | ||
/// End position of the span. | ||
pub end: usize, | ||
} | ||
|
||
/// Primitive representation of a single token. | ||
pub struct PrimitiveToken { | ||
/// Plain code content that the token represents (includes whitespaces). | ||
pub content: String, | ||
/// Span of the token. | ||
pub span: Option<PrimitiveSpan>, | ||
} | ||
|
||
impl PrimitiveToken { | ||
/// Creates a new primitive token based upon content and provided span. | ||
pub fn new(content: String, span: Option<PrimitiveSpan>) -> Self { | ||
Self { content, span } | ||
} | ||
} | ||
|
||
/// Trait that defines an object that can be turned into a PrimitiveTokenStream iterator. | ||
pub trait ToPrimitiveTokenStream { | ||
/// Iterator type for PrimitiveTokens. | ||
type Iter: Iterator<Item = PrimitiveToken>; | ||
|
||
/// Method that turns given item to a PrimitiveTokenStream iterator. | ||
fn to_primitive_token_stream(&self) -> Self::Iter; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use cairo_lang_primitive_token::{PrimitiveSpan, PrimitiveToken, ToPrimitiveTokenStream}; | ||
|
||
use super::SyntaxNode; | ||
use super::db::SyntaxGroup; | ||
|
||
pub struct SyntaxNodeWithDb<'a, Db: SyntaxGroup> { | ||
node: &'a SyntaxNode, | ||
db: &'a Db, | ||
} | ||
|
||
impl<'a, Db: SyntaxGroup> SyntaxNodeWithDb<'a, Db> { | ||
pub fn new(node: &'a SyntaxNode, db: &'a Db) -> Self { | ||
Self { node, db } | ||
} | ||
} | ||
|
||
impl<'a, Db: SyntaxGroup> ToPrimitiveTokenStream for SyntaxNodeWithDb<'a, Db> { | ||
type Iter = SyntaxNodeWithDbIterator<'a, Db>; | ||
|
||
fn to_primitive_token_stream(&self) -> Self::Iter { | ||
// The lifetime of the iterator should extend 'a because it derives from both node and db | ||
SyntaxNodeWithDbIterator::new(Box::new(self.node.tokens(self.db)), self.db) | ||
} | ||
} | ||
|
||
pub struct SyntaxNodeWithDbIterator<'a, Db: SyntaxGroup> { | ||
inner: Box<dyn Iterator<Item = SyntaxNode> + 'a>, | ||
db: &'a Db, | ||
} | ||
|
||
impl<'a, Db: SyntaxGroup> SyntaxNodeWithDbIterator<'a, Db> { | ||
pub fn new(inner: Box<dyn Iterator<Item = SyntaxNode> + 'a>, db: &'a Db) -> Self { | ||
Self { inner, db } | ||
} | ||
} | ||
|
||
impl<Db: SyntaxGroup> Iterator for SyntaxNodeWithDbIterator<'_, Db> { | ||
type Item = PrimitiveToken; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
self.inner.next().map(|node| { | ||
let span = node.span(self.db).to_str_range(); | ||
PrimitiveToken { | ||
content: node.get_text(self.db), | ||
span: Some(PrimitiveSpan { start: span.start, end: span.end }), | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters