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
use crate::ast::utils;
use crate::error::ParseError;
use crate::parser::Parser;
use crate::source::Source;
use crate::token::{Kind, Token};
use crate::traits::{Parse, Resolve};
use runestick::unit::Span;

/// A byte literal.
#[derive(Debug, Clone)]
pub struct LitByte {
    /// The token corresponding to the literal.
    pub token: Token,
}

impl LitByte {
    /// Access the span of the expression.
    pub fn span(&self) -> Span {
        self.token.span
    }
}

/// Parse a byte literal.
///
/// # Examples
///
/// ```rust
/// use rune::{parse_all, ast};
///
/// parse_all::<ast::LitByte>("b'a'").unwrap();
/// parse_all::<ast::LitByte>("b'\\0'").unwrap();
/// parse_all::<ast::LitByte>("b'\\n'").unwrap();
/// parse_all::<ast::LitByte>("b'\\r'").unwrap();
/// parse_all::<ast::LitByte>("b'\\\\''").unwrap();
/// ```
impl Parse for LitByte {
    fn parse(parser: &mut Parser<'_>) -> Result<Self, ParseError> {
        let token = parser.token_next()?;

        Ok(match token.kind {
            Kind::LitByte => LitByte { token },
            _ => {
                return Err(ParseError::ExpectedByte {
                    actual: token.kind,
                    span: token.span,
                })
            }
        })
    }
}

impl<'a> Resolve<'a> for LitByte {
    type Output = u8;

    fn resolve(&self, source: Source<'a>) -> Result<u8, ParseError> {
        let span = self.token.span;
        let string = source.source(span.trim_start(2).trim_end(1))?;

        let mut it = string
            .char_indices()
            .map(|(n, c)| (span.start + n, c))
            .peekable();

        let (n, c) = match it.next() {
            Some(c) => c,
            None => {
                return Err(ParseError::BadByteLiteral { span });
            }
        };

        let c = match c {
            '\\' => utils::parse_byte_escape(span.with_start(n), &mut it)?,
            c if c.is_ascii() && !c.is_control() => c as u8,
            _ => {
                return Err(ParseError::BadByteLiteral { span });
            }
        };

        // Too many characters in literal.
        if it.next().is_some() {
            return Err(ParseError::BadByteLiteral { span });
        }

        Ok(c)
    }
}