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

vcfxb / wright-lang / 11538878638

27 Oct 2024 08:09AM UTC coverage: 56.717% (-3.9%) from 60.593%
11538878638

push

github

vcfxb
WIP items

205 of 568 branches covered (36.09%)

Branch coverage included in aggregate %.

18 of 69 new or added lines in 3 files covered. (26.09%)

1 existing line in 1 file now uncovered.

652 of 943 relevant lines covered (69.14%)

20.36 hits per line

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

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

3
use num::{BigUint, Num};
4

5
use crate::parser::Parser;
6
use crate::{ast::literal::IntegerLiteral, lexer::token::TokenTy};
7
use crate::parser::error::{ParserError, ParserErrorKind};
8

9
impl IntegerLiteral {
10
    /// Parse an integer literal from the given [Parser].
11
    pub fn parse(parser: &mut Parser) -> Result<Self, ParserError> {
1✔
12
        // Get the token containing the integer literal from the parser.
13
        let Some(int_lit_token) = parser.next_if_is(TokenTy::IntegerLiteral) else {
1!
NEW
14
            return match parser.peek_fragment() {
×
NEW
15
                Some(frag) => Err(ParserError {
×
NEW
16
                    kind: ParserErrorKind::ExpectedIntegerLiteral,
×
NEW
17
                    location: frag.clone(),
×
NEW
18
                    help: None,
×
NEW
19
                }),
×
20

NEW
21
                None => Err(ParserError {
×
NEW
22
                    kind: ParserErrorKind::ExpectedIntegerLiteral,
×
NEW
23
                    location: parser.lexer.remaining.clone(),
×
NEW
24
                    help: Some("found end of source".into()),
×
NEW
25
                }),
×
26
            };
NEW
27
        };
×
28

29
        // Get the string to pass to num for the rest of parsing.
30
        let mut parse_str: &str = int_lit_token.fragment.as_str();
1✔
31
        let mut chars = parse_str.chars();
1✔
32

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

38
        // Determine the radix and remove any prefix in the process.
39
        let radix: u32 = match prefix {
1!
40
            // Hexidecimal.
41
            ['0', 'x' | 'X'] => {
NEW
42
                parse_str = &parse_str[2..];
×
NEW
43
                16
×
44
            }
45

46
            // Binary.
47
            ['0', 'b' | 'B'] => {
NEW
48
                parse_str = &parse_str[2..];
×
NEW
49
                2
×
50
            }
51

52
            // Octal
53
            ['0', 'o'] => {
NEW
54
                parse_str = &parse_str[2..];
×
NEW
55
                8
×
56
            }
57

58
            // All other patterns are not radix-prefixes.
59
            _ => 10,
1✔
60
        };
61

62
        // Pass the remainder of parsing off to num.
63
        let value = BigUint::from_str_radix(parse_str, radix)
1✔
64
            // We can use expect here for now since we have validated the format of the string
65
            // on our own before passing it off.
66
            .expect("num should successfully parse");
67

68
        Ok(IntegerLiteral {
1✔
69
            fragment: int_lit_token.fragment,
1✔
70
            value,
71
        })
72
    }
1✔
73
}
74

75
#[cfg(test)]
76
mod tests {
77
    use num::BigUint;
78

79
    use crate::{ast::literal::IntegerLiteral, lexer::Lexer, parser::Parser};
80

81
    #[test]
82
    fn normal() {
2✔
83
        let mut parser = Parser::new(Lexer::new_test("1000"));
1✔
84

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

87
        assert_eq!(int_lit.value, BigUint::new(vec![1000]));
1!
88
        assert_eq!(parser.lexer.remaining.as_str(), "");
1!
89
        assert_eq!(int_lit.fragment.as_str(), "1000");
1!
90
    }
2✔
91

92
    // #[test]
93
    // fn ingore_underscores
94
}
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