1use tenda_common::span::SourceSpan;
2
3#[derive(Debug, Clone, PartialEq)]
4pub struct Token {
5 pub kind: TokenKind,
6 pub lexeme: String,
7 pub literal: Option<Literal>,
8 pub span: SourceSpan,
9}
10
11impl Token {
12 pub fn new(
13 kind: TokenKind,
14 lexeme: String,
15 literal: Option<Literal>,
16 span: SourceSpan,
17 ) -> Token {
18 Token {
19 kind,
20 lexeme,
21 literal,
22 span,
23 }
24 }
25
26 pub fn eoi(span: SourceSpan) -> Token {
27 Token::new(TokenKind::Eof, "EOF".to_string(), None, span)
28 }
29
30 pub fn clone_ref(&self) -> Token {
31 (*self).clone()
32 }
33}
34
35impl<T> From<Token> for Result<Option<Token>, T> {
36 fn from(val: Token) -> Self {
37 Ok(Some(val))
38 }
39}
40
41#[derive(Debug, Clone, Copy, PartialEq)]
42pub enum TokenKind {
43 Number,
44 String,
45 True,
46 False,
47 Nil,
48 Equals,
49 Not,
50 Or,
51 And,
52 Greater,
53 GreaterOrEqual,
54 Less,
55 LessOrEqual,
56 Let,
57 If,
58 Function,
59 Then,
60 Else,
61 Return,
62 BlockEnd,
63 While,
64 Do,
65 Continue,
66 Identifier,
67 EqualSign,
68 Until,
69 ForOrBreak,
70 Each,
71 In,
72 Has,
73 Colon,
74 Plus,
75 Minus,
76 Star,
77 Slash,
78 Percent,
79 Caret,
80 LeftParen,
81 RightParen,
82 LeftBracket,
83 RightBracket,
84 LeftBrace,
85 RightBrace,
86 Comma,
87 Dot,
88 Arrow,
89 Newline,
90 Eof,
91}
92
93#[derive(Debug, Clone, PartialEq)]
94pub enum Literal {
95 Number(f64),
96 String(String),
97 Boolean(bool),
98 Nil,
99}
100
101impl Literal {
102 pub const TRUE_LITERAL: &'static str = "verdadeiro";
103 pub const FALSE_LITERAL: &'static str = "falso";
104 pub const NIL_LITERAL: &'static str = "Nada";
105 pub const POSITIVE_INFINITY_LITERAL: &'static str = "infinito";
106 pub const NEGATIVE_INFINITY_LITERAL: &'static str = "-infinito";
107 pub const NAN_LITERAL: &'static str = "NaN";
108}