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
use std::convert::TryFrom;

use nom::{
    branch::alt,
    bytes::complete::{escaped, is_not, tag},
    character::complete::{char, digit1, multispace0, one_of},
    combinator::{map, not, opt, recognize},
    multi::separated_list,
    number::complete::double,
    re_find,
    sequence::{delimited, preceded, terminated, tuple},
    IResult,
};

use super::Value;
use crate::value::Arg;
use crate::value::Arg::Kwarg;

pub fn parse_bool(input: &str) -> IResult<&str, Value> {
    alt((
        map(tag("True"), |_| Value::Bool(true)),
        map(tag("False"), |_| Value::Bool(false)),
    ))(input)
}

pub fn parse_str(input: &str) -> IResult<&str, Value> {
    // See: https://python-reference.readthedocs.io/en/latest/docs/str/escapes.html
    //
    // \a           ASCII bell
    // \b           ASCII backspace
    // \f           ASCII formfeed
    // \n           ASCII linefeed
    // \N{name}     character named NAME in the Unicode database
    // \r           ASCII carriage return
    // \t           ASCII horizontal tab
    // \uxxxx       character with 16-bit hex value XXXX
    // \Uxxxxxxxx   character with 32-bit hex value XXXXXXXX
    // \v           ASCII vertical tab
    // \ooo         character with octal value OOO
    // \hxx         Character with hex value XX
    let single_quoted_str_escape = r#"\'abfnNrtuUvx01234567"#;
    let double_quoted_str_escape = r#"\"abfnNrtuUvx01234567"#;

    let single_quoted = recognize(delimited(
        char('\''),
        escaped(is_not(r#"'\"#), '\\', one_of(single_quoted_str_escape)),
        char('\''),
    ));
    let double_quoted = recognize(delimited(
        char('"'),
        escaped(is_not(r#""\"#), '\\', one_of(double_quoted_str_escape)),
        char('"'),
    ));
    map(alt((single_quoted, double_quoted)), Value::Str)(input)
}

pub fn parse_int(input: &str) -> IResult<&str, Value> {
    map(
        tuple((opt(tag("-")), terminated(digit1, not(tag("."))))),
        |(sign, s): (Option<&str>, &str)| {
            let sign = if sign.is_some() { -1 } else { 1 };
            let i = s
                .parse::<i64>()
                .expect("sequence of digits can parse to int");
            Value::Int(sign * i)
        },
    )(input)
}

pub fn parse_float(input: &str) -> IResult<&str, Value> {
    map(double, Value::Float)(input)
}

fn parse_seq<'a>(
    open: char,
    f: impl Fn(Vec<Value<'a>>) -> Value<'a>,
    close: char,
) -> impl Fn(&'a str) -> IResult<&'a str, Value<'a>> {
    move |input: &'a str| -> IResult<&'a str, Value> {
        map(
            delimited(
                char(open),
                separated_list(comma_space, parse_value),
                char(close),
            ),
            &f,
        )(input)
    }
}

pub fn parse_list(input: &str) -> IResult<&str, Value> {
    parse_seq('[', Value::List, ']')(input)
}

pub fn parse_tuple(input: &str) -> IResult<&str, Value> {
    parse_seq('(', Value::Tuple, ')')(input)
}

pub fn parse_set(input: &str) -> IResult<&str, Value> {
    parse_seq('{', Value::Set, '}')(input)
}

fn colon_space(input: &str) -> IResult<&str, ()> {
    map(preceded(char(':'), multispace0), |_| ())(input)
}

fn parse_dict_key_value(input: &str) -> IResult<&str, (Value, Value)> {
    tuple((parse_value, preceded(colon_space, parse_value)))(input)
}

fn comma_space(input: &str) -> IResult<&str, ()> {
    map(preceded(char(','), multispace0), |_| ())(input)
}

pub fn parse_dict(input: &str) -> IResult<&str, Value> {
    map(
        delimited(
            char('{'),
            separated_list(comma_space, parse_dict_key_value),
            char('}'),
        ),
        Value::Dict,
    )(input)
}

fn identifier(input: &str) -> IResult<&str, &str> {
    re_find!(
        input,
        r"^([a-zA-Z_][a-zA-Z0-9_]*)(\.[a-zA-Z_][a-zA-Z0-9_]*)*"
    )
}

pub fn parse_symbol(input: &str) -> IResult<&str, Value> {
    map(identifier, Value::Symbol)(input)
}

fn parse_arg(input: &str) -> IResult<&str, Arg> {
    alt((
        map(
            tuple((identifier, preceded(char('='), parse_value))),
            |(ident, value)| Kwarg(ident, value),
        ),
        map(parse_value, Arg::Arg),
    ))(input)
}

pub fn parse_constructor(input: &str) -> IResult<&str, Value> {
    map(
        tuple((
            identifier,
            delimited(char('('), separated_list(comma_space, parse_arg), char(')')),
        )),
        |(name, kwargs)| Value::Constructor(name, kwargs),
    )(input)
}

pub fn parse_value(input: &str) -> IResult<&str, Value> {
    alt((
        parse_int,
        parse_float, // Appears after int parser because f64 is superset of i64
        parse_bool,
        parse_str,
        parse_list,
        parse_tuple,
        parse_dict,
        parse_set, // Appears after dict parser because `{}` is a dict, not a set.
        parse_constructor,
        parse_symbol,
    ))(input)
}

impl<'a> TryFrom<&'a str> for Value<'a> {
    type Error = nom::Err<(&'a str, nom::error::ErrorKind)>;

    fn try_from(input: &'a str) -> Result<Self, Self::Error> {
        match parse_value(input) {
            Ok((_rest, value)) => Ok(value),
            Err(err) => Err(err),
        }
    }
}