1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use crate::ast;
use crate::{Parse, ParseError, Parser, Peeker, Spanned, Storage, ToTokens};
use runestick::Span;
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, Spanned)]
pub enum Lit {
Bool(ast::LitBool),
Byte(ast::LitByte),
Str(ast::LitStr),
ByteStr(ast::LitByteStr),
Char(ast::LitChar),
Number(ast::LitNumber),
}
impl Lit {
pub fn new<T>(lit: T) -> Self
where
T: crate::macros::IntoLit,
{
crate::macros::current_context(|ctx| Self::new_with(lit, ctx.macro_span(), ctx.storage()))
}
pub fn new_with<T>(lit: T, span: Span, storage: &Storage) -> Self
where
T: crate::macros::IntoLit,
{
T::into_lit(lit, span, storage)
}
pub(crate) fn peek_in_expr(p: &mut Peeker<'_>) -> bool {
match p.nth(0) {
K![true] | K![false] => true,
K![byte] => true,
K![number] => true,
K![char] => true,
K![str] => true,
K![bytestr] => true,
_ => false,
}
}
}
impl Parse for Lit {
fn parse(p: &mut Parser<'_>) -> Result<Self, ParseError> {
match p.nth(0)? {
K![true] | K![false] => return Ok(Lit::Bool(p.parse()?)),
K![byte(_)] => return Ok(Lit::Byte(p.parse()?)),
K![number(_)] => return Ok(Lit::Number(p.parse()?)),
K![char(_)] => return Ok(Lit::Char(p.parse()?)),
K![str(_)] => return Ok(Lit::Str(p.parse()?)),
K![bytestr(_)] => return Ok(Lit::ByteStr(p.parse()?)),
_ => (),
}
Err(ParseError::expected(
&p.next()?,
r#"expected literal like `"Hello World"` or 42"#,
))
}
}