• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vcfxb / wright-lang / 14051529229

25 Mar 2025 04:13AM UTC coverage: 75.076% (+20.9%) from 54.177%
14051529229

push

github

vcfxb
coveralls gh action needed a version

994 of 1324 relevant lines covered (75.08%)

29.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.58
/wright/src/parser/literal/integer.rs
1
//! Integer literal parsing implementation.
2

3
use crate::parser::error::{ParserError, ParserErrorKind};
4
use crate::parser::Parser;
5
use crate::{ast::literal::IntegerLiteral, lexer::token::TokenTy};
6
use num::{BigUint, Num};
7

8
impl IntegerLiteral {
9
    /// Parse an integer literal from the given [Parser].
10
    pub fn parse(parser: &mut Parser) -> Result<Self, ParserError> {
1✔
11
        // Get the token containing the integer literal from the parser.
12
        let int_lit_token = parser.next_if_is(TokenTy::IntegerLiteral).ok_or_else(|| {
1✔
13
            ParserErrorKind::ExpectedIntegerLiteral.at(parser.peek_fragment_or_rest_cloned())
×
14
        })?;
1✔
15

16
        // Get the string to pass to num for the rest of parsing.
17
        let mut parse_str: &str = int_lit_token.fragment.as_str();
1✔
18
        let mut chars = parse_str.chars();
1✔
19

1✔
20
        // Unwrap: Integer literals must be at minimum 1 character, enforced by the lexer.
1✔
21
        // use null byte as a sentinel value for the second one, since we're just using the prefix to check for
1✔
22
        // a radix to pass to num.
1✔
23
        let prefix: [char; 2] = [chars.next().unwrap(), chars.next().unwrap_or('\0')];
1✔
24

25
        // Determine the radix and remove any prefix in the process.
26
        let radix: u32 = match prefix {
1✔
27
            // Hexidecimal.
28
            ['0', 'x' | 'X'] => {
29
                parse_str = &parse_str[2..];
×
30
                16
×
31
            }
32

33
            // Binary.
34
            ['0', 'b' | 'B'] => {
35
                parse_str = &parse_str[2..];
×
36
                2
×
37
            }
38

39
            // Octal
40
            ['0', 'o'] => {
41
                parse_str = &parse_str[2..];
×
42
                8
×
43
            }
44

45
            // All other patterns are not radix-prefixes.
46
            _ => 10,
1✔
47
        };
48

49
        // Pass the remainder of parsing off to num.
50
        let value = BigUint::from_str_radix(parse_str, radix)
1✔
51
            // We can use expect here for now since we have validated the format of the string
1✔
52
            // on our own before passing it off.
1✔
53
            .expect("num should successfully parse");
1✔
54

1✔
55
        Ok(IntegerLiteral {
1✔
56
            fragment: int_lit_token.fragment,
1✔
57
            value,
1✔
58
        })
1✔
59
    }
1✔
60
}
61

62
#[cfg(test)]
63
mod tests {
64
    use num::BigUint;
65

66
    use crate::{ast::literal::IntegerLiteral, lexer::Lexer, parser::Parser};
67

68
    #[test]
69
    fn normal() {
1✔
70
        let mut parser = Parser::new(Lexer::new_test("1000"));
1✔
71

1✔
72
        let int_lit = IntegerLiteral::parse(&mut parser).unwrap();
1✔
73

1✔
74
        assert_eq!(int_lit.value, BigUint::new(vec![1000]));
1✔
75
        assert_eq!(parser.lexer.remaining.as_str(), "");
1✔
76
        assert_eq!(int_lit.fragment.as_str(), "1000");
1✔
77
    }
1✔
78

79
    // #[test]
80
    // fn ingore_underscores
81
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc