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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
use std::borrow::Cow;
use std::collections::{BTreeMap, HashMap};
use std::env::var;
use std::fmt::Formatter;

use pest::iterators::Pair;
use pest::Parser;

use crate::error::{Error, Result};
use crate::{Inputs, Value};

#[derive(pest_derive::Parser)]
#[grammar = "grammar.pest"]
pub struct AstParser;

impl std::fmt::Display for Rule {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self:?}")
    }
}

struct CornParser<'a> {
    input_block: Option<Pair<'a, Rule>>,
    inputs: Inputs<'a>,
}

impl<'a> CornParser<'a> {
    pub fn new(input_block: Option<Pair<'a, Rule>>) -> Self {
        let inputs = HashMap::new();
        Self {
            input_block,
            inputs,
        }
    }

    pub fn parse(mut self, object_block: Pair<'a, Rule>) -> Result<Value> {
        if let Some(input_block) = self.input_block.take() {
            self.parse_assign_block(input_block)?;
        }

        let value_block = self.parse_object(object_block)?;
        Ok(Value::Object(value_block))
    }

    /// Parses a pair of tokens (marked as a `Rule`) into a `Value`.
    fn parse_value(&self, pair: Pair<'a, Rule>) -> Result<Value<'a>> {
        match pair.as_rule() {
            Rule::object => Ok(Value::Object(self.parse_object(pair)?)),
            Rule::array => Ok(Value::Array(self.parse_array(pair)?)),
            Rule::string => Ok(Value::String(self.parse_string(pair)?)),
            Rule::integer => Ok(Value::Integer(Self::parse_integer(pair))),
            Rule::float => Ok(Value::Float(Self::parse_float(&pair))),
            Rule::boolean => Ok(Value::Boolean(Self::parse_bool(&pair))),
            Rule::null => Ok(Value::Null(None)),
            Rule::input => {
                let key = pair.as_str();
                self.get_input(key)
            }
            _ => unreachable!(),
        }
    }

    fn parse_bool(pair: &Pair<'_, Rule>) -> bool {
        assert_eq!(pair.as_rule(), Rule::boolean);
        match pair.as_str() {
            "true" => true,
            "false" => false,
            _ => unreachable!(),
        }
    }

    fn parse_integer(pair: Pair<'_, Rule>) -> i64 {
        assert_eq!(pair.as_rule(), Rule::integer);
        let sub_pair = pair
            .into_inner()
            .next()
            .expect("integers should contain a sub-rule of their type");

        match sub_pair.as_rule() {
            Rule::decimal_integer => sub_pair
                .as_str()
                .replace('_', "")
                .parse()
                .expect("decimal integer rules should match valid rust integers"),
            Rule::hex_integer => i64::from_str_radix(&sub_pair.as_str()[2..], 16)
                .expect("hex integer rules contain valid hex values"),
            _ => unreachable!(),
        }
    }

    fn parse_float(pair: &Pair<'_, Rule>) -> f64 {
        assert_eq!(pair.as_rule(), Rule::float);
        pair.as_str()
            .parse()
            .expect("float rules should match valid rust floats")
    }

    /// Collects each `char` in a `Rule::string`
    /// to form a single `String`.
    fn parse_string(&self, pair: Pair<'a, Rule>) -> Result<Cow<'a, str>> {
        assert_eq!(pair.as_rule(), Rule::string);

        let mut full_string = String::new();

        let pairs = pair
            .into_inner()
            .next()
            .expect("string rules should contain a valid string value")
            .into_inner();

        for pair in pairs {
            match pair.as_rule() {
                Rule::char => full_string.push(Self::parse_char(&pair)),
                Rule::input => {
                    let input_name = pair.as_str();
                    let value = self.get_input(input_name)?;
                    match value {
                        Value::String(value) => full_string.push_str(&value),
                        _ => return Err(Error::InvalidInterpolationError(input_name.to_string())),
                    }
                }
                _ => unreachable!(),
            };
        }

        Ok(Cow::Owned(full_string))
    }

    fn parse_char(pair: &Pair<'a, Rule>) -> char {
        let str = pair.as_str();
        let mut chars = str.chars();

        let first_char = chars.next().expect("character to exist");
        if first_char != '\\' {
            return first_char;
        }

        let second_char = chars.next().expect("character to exist");
        if second_char != 'u' {
            return match second_char {
                'n' => '\n',
                'r' => '\r',
                't' => '\t',
                '"' => '\"',
                '\\' => '\\',
                _ => unreachable!(),
            };
        }

        let num =
            u32::from_str_radix(&str[3..], 16).expect("valid hex characters to exist after \\u");
        char::from_u32(num).unwrap_or('\u{FFFD}')
    }

    /// Parses each rule in a `Rule::array`
    /// to form a vector of `Value`s.
    fn parse_array(&self, block: Pair<'a, Rule>) -> Result<Vec<Value<'a>>> {
        assert_eq!(block.as_rule(), Rule::array);

        let mut arr = vec![];

        for pair in block.into_inner() {
            match pair.as_rule() {
                Rule::spread => {
                    let input = pair
                        .into_inner()
                        .next()
                        .expect("spread operators should contain an input");

                    let input_name = input.as_str();
                    let value = self.parse_value(input)?;

                    match value {
                        Value::Array(other) => arr.extend(other),
                        _ => return Err(Error::InvalidSpreadError(input_name.to_string())),
                    }
                }
                _ => arr.push(self.parse_value(pair)?),
            };
        }

        Ok(arr)
    }

    /// Parses each key/value pair in a `Rule::object`
    /// to form a `BTreeMap` of Values.
    ///
    /// A `BTreeMap` is used to ensure keys
    /// always output in the same order.
    fn parse_object(&self, block: Pair<'a, Rule>) -> Result<BTreeMap<&'a str, Value<'a>>> {
        assert_eq!(block.as_rule(), Rule::object);

        let mut obj = BTreeMap::new();

        for pair in block.into_inner() {
            match pair.as_rule() {
                Rule::pair => {
                    let mut path_rules = pair.into_inner();
                    let path = path_rules
                        .next()
                        .expect("object pairs should contain a key")
                        .as_str();
                    let value = self.parse_value(
                        path_rules
                            .next()
                            .expect("object pairs should contain a value"),
                    )?;

                    obj = Self::add_at_path(
                        obj,
                        path.split('.').collect::<Vec<_>>().as_slice(),
                        value,
                    )?;
                }
                Rule::spread => {
                    let input = pair
                        .into_inner()
                        .next()
                        .expect("spread operators should contain an input");

                    let input_name = input.as_str();
                    let value = self.parse_value(input)?;

                    match value {
                        Value::Object(other) => obj.extend(other),
                        _ => return Err(Error::InvalidSpreadError(input_name.to_string())),
                    }
                }
                _ => unreachable!(),
            }
        }

        Ok(obj)
    }

    /// Adds `Value` at the `path` in `obj`.
    ///
    /// `path` is an array where each entry represents another object key,
    /// for example `foo.bar` is represented as `["foo", "bar"]`.
    ///
    /// Objects are created up to the required depth recursively.
    fn add_at_path(
        mut obj: BTreeMap<&'a str, Value<'a>>,
        path: &[&'a str],
        value: Value<'a>,
    ) -> Result<BTreeMap<&'a str, Value<'a>>> {
        let (part, path_rest) = path
            .split_first()
            .expect("paths should contain at least 1 segment");

        if path_rest.is_empty() {
            obj.insert(part, value);
            return Ok(obj);
        }

        let child_obj = obj
            .remove(part)
            .unwrap_or_else(|| Value::Object(BTreeMap::new()));

        match child_obj {
            Value::Object(map) => {
                obj.insert(
                    part,
                    Value::Object(Self::add_at_path(map, path_rest, value)?),
                );

                Ok(obj)
            }
            _ => Err(Error::InvalidPathError(path.join("."))),
        }
    }

    /// Parses the `let { } in` block at the start of files.
    /// Each input is inserted into into `self.inputs`.
    fn parse_assign_block(&mut self, block: Pair<'a, Rule>) -> Result<()> {
        assert_eq!(block.as_rule(), Rule::assign_block);

        for pair in block.into_inner() {
            let mut assign_rules = pair.into_inner();
            let name = assign_rules
                .next()
                .expect("input assignments should have a name")
                .as_str();

            let value = self.parse_value(
                assign_rules
                    .next()
                    .expect("input assignments should have a value"),
            )?;

            self.inputs.insert(name, value);
        }

        Ok(())
    }

    /// Attempts to get an input value from the `inputs` map.
    /// If the `key` starts with `$env_` the system environment variables will be consulted first.
    fn get_input(&self, key: &'a str) -> Result<Value<'a>> {
        if let Some(env_name) = key.strip_prefix("$env_") {
            let var = var(env_name);

            if let Ok(var) = var {
                return Ok(Value::String(Cow::Owned(var)));
            }
        }

        if let Some(value) = self.inputs.get(key) {
            Ok(value.clone())
        } else {
            Err(Error::InputResolveError(key.to_string()))
        }
    }
}

/// Parses the input string into a `Config`
/// containing the resolved inputs
/// and a map of values representing the top-level object.
///
/// # Examples
///
/// ```rust
/// use corn::parse;
///
/// let corn = "{foo = 42}";
///
/// let config = parse(corn).unwrap();
/// let json = serde_json::to_string(&config).unwrap();
///
/// assert_eq!(json, "{\"foo\":42}");
/// ```
///
/// # Errors
///
/// Will fail if the input contains a syntax error.
/// Will fail if the input contains invalid Corn for another reason,
/// including references to undefined inputs or dot-notation for non-object values.
/// Will fail if the input cannot be deserialized for any reaon.
///
/// Any of the above will return a specific error type with details.
///
/// # Panics
///
/// If the internal AST parser produces a tree in an invalid structure,
/// the function will panic.
/// This indicates a severe error in the library and should never occur.
pub fn parse(file: &str) -> Result<Value> {
    let rules = AstParser::parse(Rule::config, file);

    match rules {
        Ok(mut rules) => {
            let first_block = rules.next().expect("should be at least 1 rule");

            match first_block.as_rule() {
                Rule::assign_block => {
                    let parser = CornParser::new(Some(first_block));
                    let object_block = rules.next().expect("should always be an object block");
                    parser.parse(object_block)
                }
                Rule::object => {
                    let parser = CornParser::new(None);
                    parser.parse(first_block)
                }
                _ => unreachable!(),
            }
        }
        Err(error) => Err(Error::ParserError(Box::new(error))),
    }
}