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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use crate::chars::*;
use crate::err::*;
use crate::iter::*;
use crate::ptrait::*;
use crate::reader::*;
use crate::strings::*;
use crate::tuple::*;
use std::convert::TryFrom;

pub fn common_esc<'a>(it: &LCChars<'a>) -> ParseRes<'a, char> {
    '\\'.ig_then(or4('t'.asv('\t'), 'r'.asv('\r'), 'n'.asv('\n'), take_char))
        .parse(it)
}

/// ```rust
/// use gobble::*;
/// assert_eq!(common_str.parse_s(r#""hello\t\"world\"""#),Ok("hello\t\"world\"".to_string()));
/// ```
pub fn common_str<'a>(it: &LCChars<'a>) -> ParseRes<'a, String> {
    '"'.ig_then(chars_until(or(common_esc, take_char), '"'))
        .parse(it)
}

pub fn common_ident<'a>(it: &LCChars<'a>) -> ParseRes<'a, String> {
    string_2_parts(Alpha.min_n(1), (Alpha, NumDigit, '_').any()).parse(it)
}

pub fn common_uint<'a>(it: &LCChars<'a>) -> ParseRes<'a, usize> {
    let mut added = false;
    let mut res: usize = 0;
    let mut it = it.clone();
    loop {
        let it2 = it.clone();
        match it.next() {
            Some(v) if is_num(v) => {
                added = true;
                res = res
                    .checked_mul(10)
                    .ok_or(it.err("Num too big"))?
                    .checked_add(v as usize - '0' as usize)
                    .ok_or(it.err("Num too big"))?;
            }
            Some('_') => {}
            _ => {
                if added {
                    return Ok((it2, res));
                }
                return it.err_r("No numerical digits");
            }
        }
    }
}

/// A function for parsing integers
/// ```
/// use gobble::*;
/// let r = common_int.parse_s("32").unwrap();
/// assert_eq!(r,32);
/// ```
///
pub fn common_int<'a>(it: &LCChars<'a>) -> ParseRes<'a, isize> {
    //TODO add and mul without panic
    let mut it2 = it.clone();
    let (minus, it2) = match it2.next() {
        Some('-') => (-1, it2),
        Some(v) if is_num(v) => (1, it.clone()),
        _ => return it.err_cr(ECode::SMess("Not an int")),
    };

    let (it3, n) = common_uint(&it2)?;
    let n: isize = isize::try_from(n).map_err(|_| it3.err("Int Too Big"))?;
    Ok((it3, n * minus))
}

/// ```
/// use gobble::*;
/// let v = common_bool.parse_s("true").unwrap();
/// assert!(v);
/// ```
pub fn common_bool<'a>(it: &LCChars<'a>) -> ParseRes<'a, bool> {
    keyword("true")
        .map(|_| true)
        .or(keyword("false").map(|_| false))
        .parse(it)
}

pub fn dot_part<'a>(i: &LCChars<'a>) -> ParseRes<'a, f64> {
    let mut res = 0.;
    let mut exp = 0.1;
    let mut it = i.clone();
    if it.next() != Some('.') {
        return i.err_r("no_dot_part");
    }
    loop {
        let it2 = it.clone();
        match it.next() {
            Some('_') => {}
            Some(v) if is_num(v) => {
                res += (((v as i64) as f64) - 48.0) * exp;
                exp *= 0.1;
            }
            _ => return Ok((it2, res)),
        }
    }
}

fn do_exponent<'a>(mut n: f64, i: &LCChars<'a>) -> ParseRes<'a, f64> {
    let mut it = i.clone();
    if it.next() != Some('e') {
        return Ok((i.clone(), n));
    }
    let (it2, exp) = common_int(&it)?;
    if exp > 0 {
        for _ in 0..exp {
            n *= 10.;
        }
    }
    if exp < 0 {
        for _ in 0..-exp {
            n /= 10.;
        }
    }
    Ok((it2, n))
}

/// ```rust
/// use gobble::*;
/// let r = common_float.parse_s("32.").unwrap();
/// assert_eq!(r, 32.);
/// let r = common_float.parse_s("-23.4").unwrap();
/// assert_eq!(r, -23.4);
/// let r = common_float.parse_s("-23.4e2").unwrap();
/// assert_eq!(r, -2340.);
/// let r = common_float.parse_s("123.4e-2").unwrap();
/// assert_eq!(r, 1.234);
/// ```
pub fn common_float<'a>(it: &LCChars<'a>) -> ParseRes<'a, f64> {
    let (it2, n) = common_int(it)?;
    let minus = if n >= 0 { 1. } else { -1. };
    let it3 = it2.clone();
    let mut nf = n as f64;

    let (it3, dp) = dot_part(&it3)?;
    nf += dp * minus;
    do_exponent(nf, &it3)
}

#[cfg(test)]
pub mod test {
    use super::*;
    #[test]
    pub fn test_parse_numbers() {
        let r = common_int.parse_s("32").unwrap();
        assert_eq!(r, 32);
        let r = common_int.parse_s("-45023").unwrap();
        assert_eq!(r, -45023);
        let r = common_int.parse_s("34_234").unwrap();
        assert_eq!(r, 34234);
        assert!(common_int
            .parse_s("45654323456765432345676543212345654")
            .is_err());
        assert!(common_int.parse_s("   45").is_err());
    }

    #[test]
    pub fn parse_floats() {
        let r = common_float.parse_s("32.").unwrap();
        assert_eq!(r, 32.);
        let r = common_float.parse_s("-23.4").unwrap();
        assert_eq!(r, -23.4);
        let r = common_float.parse_s("-23.4e2").unwrap();
        assert_eq!(r, -2340.);
        let r = common_float.parse_s("123.4e-2").unwrap();
        assert_eq!(r, 1.234);
        assert!(common_float.parse_s("123").is_err());
    }
}