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
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::iter::Iterator;

use codemap::Spanned;

use peekmore::PeekMoreIterator;

use crate::error::SassResult;
use crate::Token;

#[derive(Debug)]
pub(crate) struct ParsedNumber {
    /// The full number excluding the decimal
    ///
    /// E.g. for `1.23`, this would be `"123"`
    pub num: String,

    /// The length of the decimal
    ///
    /// E.g. for `1.23`, this would be `2`
    pub dec_len: usize,

    /// The number following e in a scientific notated number
    ///
    /// E.g. for `1e23`, this would be `"23"`,
    /// for `1`, this would be an empty string
    // TODO: maybe we just return a bigint?
    pub times_ten: String,

    /// Whether or not `times_ten` is negative
    ///
    /// E.g. for `1e-23` this would be `true`,
    /// for `1e23` this would be `false`
    pub times_ten_is_postive: bool,
}

impl ParsedNumber {
    pub const fn new(
        num: String,
        dec_len: usize,
        times_ten: String,
        times_ten_is_postive: bool,
    ) -> Self {
        Self {
            num,
            dec_len,
            times_ten,
            times_ten_is_postive,
        }
    }
}

pub(crate) fn eat_number<I: Iterator<Item = Token>>(
    toks: &mut PeekMoreIterator<I>,
) -> SassResult<Spanned<ParsedNumber>> {
    let mut whole = String::with_capacity(1);
    // TODO: merge this span with chars
    let span = if let Some(tok) = toks.peek() {
        tok.pos()
    } else {
        todo!()
    };
    eat_whole_number(toks, &mut whole);

    if toks.peek().is_none() {
        return Ok(Spanned {
            node: ParsedNumber::new(whole, 0, String::new(), true),
            span,
        });
    }

    let mut dec = String::new();

    let next_tok = *toks.peek().unwrap();

    if next_tok.kind == '.' {
        toks.next();
        eat_whole_number(toks, &mut dec);

        if dec.is_empty() {
            return Err(("Expected digit.", next_tok.pos()).into());
        }
    }

    let mut times_ten = String::new();
    let mut times_ten_is_postive = true;
    loop {
        match toks.peek() {
            // TODO: https://github.com/rust-lang/rust/issues/54883
            Some(Token { kind: 'e', .. }) | Some(Token { kind: 'E', .. }) => {
                if toks.peek_forward(1).is_none() {
                    break;
                } else {
                    let Token { kind, pos } = *toks.peek().unwrap();
                    match kind {
                        '-' => {
                            toks.next();
                            times_ten_is_postive = false;
                        }
                        '0'..='9' => {}
                        _ => break,
                    }

                    toks.next();

                    eat_whole_number(toks, &mut times_ten);

                    if times_ten.is_empty() && !times_ten_is_postive {
                        if let Some(t) = toks.peek() {
                            return Err(("Expected digit.", t.pos()).into());
                        }
                        return Err(("Expected digit.", pos).into());
                    }
                }
            }
            Some(..) | None => break,
        }
        break;
    }

    toks.reset_view();

    whole.push_str(&dec);

    Ok(Spanned {
        node: ParsedNumber::new(whole, dec.len(), times_ten, times_ten_is_postive),
        span,
    })
}

fn eat_whole_number<I: Iterator<Item = Token>>(toks: &mut PeekMoreIterator<I>, buf: &mut String) {
    while let Some(c) = toks.peek() {
        if !c.kind.is_numeric() {
            break;
        }
        let tok = toks.next().unwrap();
        buf.push(tok.kind);
    }
}