lua_parser/expression/
literal.rs

1use crate::IntOrFloat;
2use crate::Span;
3use crate::Token;
4use lua_tokenizer::TokenType;
5
6#[derive(Clone, Copy, Debug)]
7pub struct ExprNil {
8    pub span: Span,
9}
10impl ExprNil {
11    pub fn new(span: Span) -> Self {
12        Self { span }
13    }
14    /// get the span of the nil expression
15    pub fn span(&self) -> Span {
16        self.span
17    }
18}
19impl From<Token> for ExprNil {
20    fn from(t: Token) -> Self {
21        Self::new(t.span)
22    }
23}
24
25/// lua numeric literal value
26#[derive(Clone, Copy, Debug)]
27pub struct ExprNumeric {
28    pub value: IntOrFloat,
29    pub span: Span,
30}
31impl ExprNumeric {
32    pub fn new(value: IntOrFloat, span: Span) -> Self {
33        Self { value, span }
34    }
35    /// get the span of the numeric literal
36    pub fn span(&self) -> Span {
37        self.span
38    }
39}
40impl From<Token> for ExprNumeric {
41    fn from(t: Token) -> Self {
42        match t.token_type {
43            TokenType::Numeric(value) => Self::new(value.into(), t.span),
44            _ => unreachable!(),
45        }
46    }
47}
48
49/// lua string literal value
50#[derive(Clone, Debug)]
51pub struct ExprString {
52    pub value: Vec<u8>,
53    pub span: Span,
54}
55impl ExprString {
56    pub fn new(value: Vec<u8>, span: Span) -> Self {
57        Self { value, span }
58    }
59    /// get the span of the string literal
60    pub fn span(&self) -> Span {
61        self.span
62    }
63}
64impl From<Token> for ExprString {
65    fn from(t: Token) -> Self {
66        match t.token_type {
67            TokenType::String(s) => Self::new(s, t.span),
68            TokenType::Ident(s) => Self::new(s.into_bytes(), t.span),
69            _ => unreachable!(),
70        }
71    }
72}
73
74/// lua boolean literal value
75#[derive(Clone, Copy, Debug)]
76pub struct ExprBool {
77    pub value: bool,
78    pub span: Span,
79}
80impl ExprBool {
81    pub fn new(value: bool, span: Span) -> Self {
82        Self { value, span }
83    }
84    /// get the span of the boolean literal
85    pub fn span(&self) -> Span {
86        self.span
87    }
88}
89impl From<Token> for ExprBool {
90    fn from(t: Token) -> Self {
91        match t.token_type {
92            TokenType::Bool(value) => Self::new(value, t.span),
93            _ => unreachable!(),
94        }
95    }
96}
97
98/// `...`
99#[derive(Clone, Copy, Debug)]
100pub struct ExprVariadic {
101    pub span: Span,
102}
103impl ExprVariadic {
104    pub fn new(span: Span) -> Self {
105        Self { span }
106    }
107    /// get the span of the variadic expression
108    pub fn span(&self) -> Span {
109        self.span
110    }
111}
112impl From<Token> for ExprVariadic {
113    fn from(t: Token) -> Self {
114        Self { span: t.span }
115    }
116}