cairo_lang_primitive_token/
lib.rs

1#![deny(missing_docs)]
2//! This crate defines unfiorm and primitive form of the TokenStream.
3//! We want this to be as stable as possible and limit the changes here to bare minimum.
4
5/// Primitive representation of a token's span.
6pub struct PrimitiveSpan {
7    /// Start position of the span.
8    pub start: usize,
9    /// End position of the span.
10    pub end: usize,
11}
12
13/// Primitive representation of a single token.
14pub struct PrimitiveToken {
15    /// Plain code content that the token represents (includes whitespaces).
16    pub content: String,
17    /// Span of the token.
18    pub span: Option<PrimitiveSpan>,
19}
20
21impl PrimitiveToken {
22    /// Creates a new primitive token based upon content and provided span.
23    pub fn new(content: String, span: Option<PrimitiveSpan>) -> Self {
24        Self { content, span }
25    }
26}
27
28/// Trait that defines an object that can be turned into a PrimitiveTokenStream iterator.
29pub trait ToPrimitiveTokenStream {
30    /// Iterator type for PrimitiveTokens.
31    type Iter: Iterator<Item = PrimitiveToken>;
32
33    /// Method that turns given item to a PrimitiveTokenStream iterator.
34    fn to_primitive_token_stream(&self) -> Self::Iter;
35}