y_lang/ast/
integer.rs

1use pest::iterators::Pair;
2
3use super::{Position, Rule};
4
5#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
6pub struct Integer<T> {
7    pub value: i64,
8    pub position: Position,
9    pub info: T,
10}
11
12impl Integer<()> {
13    pub fn from_pair(pair: Pair<Rule>, file: &str) -> Integer<()> {
14        let (line, col) = pair.line_col();
15
16        match pair.as_rule() {
17            Rule::decimalNumber => Integer {
18                value: pair.as_str().parse::<i64>().unwrap(),
19                position: (file.to_owned(), line, col),
20                info: (),
21            },
22            Rule::hexNumber => {
23                let value = pair.as_str();
24                let without_prefix = value.trim_start_matches("0x");
25                Integer {
26                    value: i64::from_str_radix(without_prefix, 16).unwrap(),
27                    position: (file.to_owned(), line, col),
28                    info: (),
29                }
30            }
31            _ => unreachable!(),
32        }
33    }
34}