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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//! Standard grammars.

use nom::{
    bytes::complete::take_while_m_n,
    combinator::{not, peek},
    number::complete::{double, float},
    sequence::terminated,
};
use num_traits::Num;

use core::{f32, f64, fmt, marker::PhantomData};

use crate::{Features, Grammar, NomResult, Span};

// FIXME: `1.foo()` does not work because float parser consumes `1.`

/// Single-type numeric grammar parameterized by the literal type.
#[derive(Debug)]
pub struct NumGrammar<T>(PhantomData<T>);

/// Type alias for a grammar on `f32` literals.
pub type F32Grammar = NumGrammar<f32>;
/// Type alias for a grammar on `f64` literals.
pub type F64Grammar = NumGrammar<f64>;

impl<T: NumLiteral> Grammar for NumGrammar<T> {
    type Lit = T;
    type Type = ();

    const FEATURES: Features = Features {
        type_annotations: false,
        ..Features::all()
    };

    fn parse_literal(input: Span<'_>) -> NomResult<'_, Self::Lit> {
        T::parse(input)
    }

    fn parse_type(_input: Span<'_>) -> NomResult<'_, Self::Type> {
        unimplemented!()
    }
}

/// Numeric literal used in `NumGrammar`s.
pub trait NumLiteral: 'static + Copy + Num + fmt::Debug {
    /// Tries to parse a literal.
    fn parse(input: Span<'_>) -> NomResult<'_, Self>;
}

/// Ensures that the child parser does not consume a part of a larger expression by rejecting
/// if the part following the input is an alphanumeric char or `_`.
///
/// For example, `float` parses `-Inf`, which can lead to parser failure if it's a part of
/// a larger expression (e.g., `-Infer(2, 3)`).
pub fn ensure_no_overlap<'a, T>(
    parser: impl Fn(Span<'a>) -> NomResult<'a, T>,
) -> impl Fn(Span<'a>) -> NomResult<'a, T> {
    terminated(
        parser,
        peek(not(take_while_m_n(1, 1, |c: char| {
            c.is_ascii_alphabetic() || c == '_'
        }))),
    )
}

impl NumLiteral for f32 {
    fn parse(input: Span<'_>) -> NomResult<'_, Self> {
        ensure_no_overlap(float)(input)
    }
}

impl NumLiteral for f64 {
    fn parse(input: Span<'_>) -> NomResult<'_, Self> {
        ensure_no_overlap(double)(input)
    }
}

#[cfg(feature = "num-complex")]
mod complex {
    use super::*;

    use nom::{
        branch::alt,
        character::complete::one_of,
        combinator::{map, opt},
        sequence::tuple,
    };
    use num_complex::Complex;

    fn complex_parser<'a, T: Num>(
        num_parser: impl Fn(Span<'a>) -> NomResult<'a, T>,
    ) -> impl Fn(Span<'a>) -> NomResult<'a, Complex<T>> {
        let i_parser = map(one_of("ij"), |_| Complex::new(T::zero(), T::one()));

        let parser = tuple((num_parser, opt(one_of("ij"))));
        let parser = map(parser, |(value, maybe_imag)| {
            if maybe_imag.is_some() {
                Complex::new(T::zero(), value)
            } else {
                Complex::new(value, T::zero())
            }
        });

        alt((i_parser, parser))
    }

    impl NumLiteral for num_complex::Complex32 {
        fn parse(input: Span<'_>) -> NomResult<'_, Self> {
            ensure_no_overlap(complex_parser(float))(input)
        }
    }

    impl NumLiteral for num_complex::Complex64 {
        fn parse(input: Span<'_>) -> NomResult<'_, Self> {
            ensure_no_overlap(complex_parser(double))(input)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Expr, GrammarExt, UnaryOp};

    use assert_matches::assert_matches;
    use core::f32::INFINITY;

    #[test]
    fn parsing_infinity() {
        let parsed = F32Grammar::parse_statements(Span::new("Inf")).unwrap();
        let ret = parsed.return_value.unwrap().extra;
        assert_matches!(ret, Expr::Literal(lit) if lit == INFINITY);

        let parsed = F32Grammar::parse_statements(Span::new("-Inf")).unwrap();
        let ret = parsed.return_value.unwrap().extra;
        assert_matches!(ret, Expr::Literal(lit) if lit == -INFINITY);

        let parsed = F32Grammar::parse_statements(Span::new("Infty")).unwrap();
        let ret = parsed.return_value.unwrap().extra;
        assert_matches!(ret, Expr::Variable);

        let parsed = F32Grammar::parse_statements(Span::new("Infer(1)")).unwrap();
        let ret = parsed.return_value.unwrap().extra;
        assert_matches!(ret, Expr::Function { .. });

        let parsed = F32Grammar::parse_statements(Span::new("-Infty")).unwrap();
        let ret = parsed.return_value.unwrap().extra;
        assert_matches!(ret, Expr::Unary { .. });

        let parsed = F32Grammar::parse_statements(Span::new("-Infer(2, 3)")).unwrap();
        let ret = parsed.return_value.unwrap().extra;
        assert_matches!(ret, Expr::Unary { .. });
    }

    #[cfg(feature = "num-complex")]
    #[test]
    fn parsing_i() {
        use num_complex::Complex32;

        let parsed = NumGrammar::<Complex32>::parse_statements(Span::new("i")).unwrap();
        let ret = parsed.return_value.unwrap().extra;
        assert_matches!(ret, Expr::Literal(lit) if lit == Complex32::i());

        let parsed = NumGrammar::<Complex32>::parse_statements(Span::new("i + 5")).unwrap();
        let ret = parsed.return_value.unwrap().extra;
        let lhs = &ret.binary_lhs().unwrap().extra;
        assert_matches!(*lhs, Expr::Literal(lit) if lit == Complex32::i());

        let parsed = NumGrammar::<Complex32>::parse_statements(Span::new("5 - i")).unwrap();
        let ret = parsed.return_value.unwrap().extra;
        let rhs = &ret.binary_rhs().unwrap().extra;
        assert_matches!(*rhs, Expr::Literal(lit) if lit == Complex32::i());

        // `i` should not be parsed as a literal if it's a part of larger expression.
        let parsed = NumGrammar::<Complex32>::parse_statements(Span::new("ix + 5")).unwrap();
        let ret = parsed.return_value.unwrap().extra;
        let lhs = &ret.binary_lhs().unwrap().extra;
        assert_matches!(*lhs, Expr::Variable);

        let parsed = NumGrammar::<Complex32>::parse_statements(Span::new("-i + 5")).unwrap();
        let ret = parsed.return_value.unwrap().extra;
        let lhs = &ret.binary_lhs().unwrap().extra;
        let inner_lhs = match lhs {
            Expr::Unary { inner, op } if op.extra == UnaryOp::Neg => &inner.extra,
            _ => panic!("Unexpected LHS: {:?}", lhs),
        };
        assert_matches!(inner_lhs, Expr::Literal(lit) if *lit == Complex32::i());

        let parsed = NumGrammar::<Complex32>::parse_statements(Span::new("-ix + 5")).unwrap();
        let ret = parsed.return_value.unwrap().extra;
        let lhs = &ret.binary_lhs().unwrap().extra;
        let inner_lhs = match lhs {
            Expr::Unary { inner, op } if op.extra == UnaryOp::Neg => &inner.extra,
            _ => panic!("Unexpected LHS: {:?}", lhs),
        };
        assert_matches!(inner_lhs, Expr::Variable);
    }
}