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
use crate::priv_prelude::*;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub struct LitString {
pub span: Span,
pub parsed: String,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub struct LitChar {
pub span: Span,
pub parsed: char,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub struct LitInt {
pub span: Span,
pub parsed: BigUint,
pub ty_opt: Option<(LitIntType, Span)>,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub enum LitIntType {
U8,
U16,
U32,
U64,
I8,
I16,
I32,
I64,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub enum Literal {
String(LitString),
Char(LitChar),
Int(LitInt),
}
impl Peek for Literal {
fn peek(peeker: Peeker<'_>) -> Option<Literal> {
peeker.peek_literal().ok().map(Literal::clone)
}
}
impl Parse for Literal {
fn parse(parser: &mut Parser) -> ParseResult<Literal> {
match parser.take() {
Some(literal) => Ok(literal),
None => Err(parser.emit_error(ParseErrorKind::ExpectedLiteral)),
}
}
}
impl LitString {
pub fn span(&self) -> Span {
self.span.clone()
}
}
impl LitChar {
pub fn span(&self) -> Span {
self.span.clone()
}
}
impl LitInt {
pub fn span(&self) -> Span {
match &self.ty_opt {
Some((_lit_int_ty, span)) => Span::join(self.span.clone(), span.clone()),
None => self.span.clone(),
}
}
}
impl Literal {
pub fn span(&self) -> Span {
match self {
Literal::String(lit_string) => lit_string.span(),
Literal::Char(lit_char) => lit_char.span(),
Literal::Int(lit_int) => lit_int.span(),
}
}
}