radix_transactions/manifest/
token.rs

1use sbor::rust::fmt;
2use sbor::rust::fmt::Debug;
3
4/// The span of tokens. The `start` and `end` are Unicode code points / UTF-32 - as opposed to a
5/// byte-based / UTF-8 index.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub struct Span {
8    /// The start of the span, inclusive
9    pub start: Position,
10    /// The end of the span, exclusive
11    pub end: Position,
12}
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub struct Position {
16    /// A 0-indexed cursor indicating the next unicode char from the start
17    /// In case of end of file it equals to text length.
18    pub full_index: usize,
19    /// A 0-indexed cursor indicating the line number (assuming \n is a line break)
20    pub line_idx: usize,
21    /// A 0-indexed cursor indicating the character offset in the line
22    pub line_char_index: usize,
23}
24
25impl Position {
26    pub fn advance(mut self, next_char: char) -> Self {
27        self.full_index += 1;
28        if next_char == '\n' {
29            self.line_idx += 1;
30            self.line_char_index = 0;
31        } else {
32            self.line_char_index += 1;
33        }
34        self
35    }
36
37    pub fn line_number(self) -> usize {
38        self.line_idx + 1
39    }
40}
41
42#[macro_export]
43macro_rules! position {
44    ($full_index:expr, $line_idx:expr, $line_char_index:expr) => {
45        Position {
46            full_index: $full_index,
47            line_idx: $line_idx,
48            line_char_index: $line_char_index,
49        }
50    };
51}
52
53#[macro_export]
54macro_rules! span {
55    (start = ($start_full_index:expr, $start_line_idx:expr, $start_line_char_index:expr),
56         end = ($end_full_index:expr, $end_line_idx:expr, $end_line_char_index:expr)) => {
57        Span {
58            start: position!($start_full_index, $start_line_idx, $start_line_char_index),
59            end: position!($end_full_index, $end_line_idx, $end_line_char_index),
60        }
61    };
62}
63
64#[derive(Debug, Clone, PartialEq, Eq)]
65pub enum Token {
66    // ==============
67    // Literals
68    // ==============
69    BoolLiteral(bool),
70    I8Literal(i8),
71    I16Literal(i16),
72    I32Literal(i32),
73    I64Literal(i64),
74    I128Literal(i128),
75    U8Literal(u8),
76    U16Literal(u16),
77    U32Literal(u32),
78    U64Literal(u64),
79    U128Literal(u128),
80    StringLiteral(String),
81
82    Ident(String),
83
84    /* Punctuations */
85    OpenParenthesis,
86    CloseParenthesis,
87    LessThan,
88    GreaterThan,
89    Comma,
90    Semicolon,
91    FatArrow,
92}
93
94impl fmt::Display for Token {
95    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
96        match self {
97            Token::BoolLiteral(value) => write!(f, "'{:?}'", value),
98            Token::I8Literal(value) => write!(f, "'{:?}i8'", value),
99            Token::I16Literal(value) => write!(f, "'{:?}i16'", value),
100            Token::I32Literal(value) => write!(f, "'{:?}i32'", value),
101            Token::I64Literal(value) => write!(f, "'{:?}i64'", value),
102            Token::I128Literal(value) => write!(f, "'{:?}i128'", value),
103            Token::U8Literal(value) => write!(f, "'{:?}u8'", value),
104            Token::U16Literal(value) => write!(f, "'{:?}u16'", value),
105            Token::U32Literal(value) => write!(f, "'{:?}u32'", value),
106            Token::U64Literal(value) => write!(f, "'{:?}u64'", value),
107            Token::U128Literal(value) => write!(f, "'{:?}u128'", value),
108            Token::StringLiteral(value) => write!(f, "{:?}", value),
109            Token::Ident(value) => write!(f, "'{}'", value),
110            Token::OpenParenthesis => write!(f, "'('"),
111            Token::CloseParenthesis => write!(f, "')'",),
112            Token::LessThan => write!(f, "'<'"),
113            Token::GreaterThan => write!(f, "'>'",),
114            Token::Comma => write!(f, "','"),
115            Token::Semicolon => write!(f, "';'",),
116            Token::FatArrow => write!(f, "'=>'"),
117        }
118    }
119}
120
121#[derive(Debug, Clone, PartialEq, Eq)]
122pub struct TokenWithSpan {
123    pub token: Token,
124    pub span: Span,
125}