ryan 0.2.3

Ryan: a configuration language for the practical programmer
Documentation
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
use pest::iterators::Pair;
use std::fmt::Display;
use std::rc::Rc;

use crate::rc_world;

use super::expression::Expression;
use super::value::Value;
use super::Context;
use super::ErrorLogger;
use super::Rule;
use super::State;

/// An operation involving two Ryan values.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum BinaryOperator {
    /// Logical and.
    And,
    /// Logical or.
    Or,
    /// Strict equality.
    Equals,
    /// Strict inequality.
    NotEquals,
    // #[deprecated]
    /// Whether the value is of a certain type (to be deprecated).
    TypeMatches,
    /// Greater than comparison.
    GreaterThen,
    /// Greater than or equality comparison.
    GreaterEqual,
    /// Lesser than comparison.
    LesserThen,
    /// Lesser than or equality comparison.
    LesserEqual,
    /// Set inclusion
    IsContainedIn,
    /// Addition or concatenation.
    Plus,
    /// Subtraction.
    Minus,
    /// Multiplication.
    Times,
    /// Division.
    Divided,
    /// Remainder.
    Remainder,
    /// Returns the right side when the left side is `null`.
    Default,
    /// Pattern application.
    Juxtaposition,
}

impl Display for BinaryOperator {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::And => write!(f, "and")?,
            Self::Or => write!(f, "or")?,
            Self::Equals => write!(f, "==")?,
            Self::NotEquals => write!(f, "!=")?,
            Self::TypeMatches => write!(f, ":")?,
            Self::GreaterThen => write!(f, ">")?,
            Self::GreaterEqual => write!(f, ">=")?,
            Self::LesserThen => write!(f, "<")?,
            Self::LesserEqual => write!(f, "<=")?,
            Self::IsContainedIn => write!(f, "in")?,
            Self::Plus => write!(f, "+")?,
            Self::Minus => write!(f, "-")?,
            Self::Times => write!(f, "*")?,
            Self::Divided => write!(f, "/")?,
            Self::Remainder => write!(f, "%")?,
            Self::Default => write!(f, "?")?,
            Self::Juxtaposition => {}
        }

        Ok(())
    }
}

impl BinaryOperator {
    pub(super) fn parse(pair: Pair<'_, Rule>) -> Self {
        match pair.as_rule() {
            Rule::andOp => BinaryOperator::And,
            Rule::orOp => BinaryOperator::Or,
            Rule::equalsOp => BinaryOperator::Equals,
            Rule::notEqualsOp => BinaryOperator::NotEquals,
            Rule::typeMatchesOp => BinaryOperator::TypeMatches,
            Rule::greaterOp => BinaryOperator::GreaterThen,
            Rule::greaterEqualOp => BinaryOperator::GreaterEqual,
            Rule::lesserOp => BinaryOperator::LesserThen,
            Rule::lesserEqualOp => BinaryOperator::LesserEqual,
            Rule::isContainedOp => BinaryOperator::IsContainedIn,
            Rule::plusOp => BinaryOperator::Plus,
            Rule::minusOp => BinaryOperator::Minus,
            Rule::timesOp => BinaryOperator::Times,
            Rule::dividedOp => BinaryOperator::Divided,
            Rule::remainderOp => BinaryOperator::Remainder,
            Rule::defaultOp => BinaryOperator::Default,
            Rule::juxtapositionOp => BinaryOperator::Juxtaposition,
            _ => unreachable!(),
        }
    }
}

/// An operation involving one Ryan value, where the value follows it.
#[derive(Debug, Clone, PartialEq)]
pub enum PrefixOperator {
    /// Logical negation.
    Not,
}

impl Display for PrefixOperator {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Not => write!(f, "not")?,
        }

        Ok(())
    }
}

impl PrefixOperator {
    pub(super) fn parse(pair: Pair<'_, Rule>) -> Self {
        match pair.as_rule() {
            Rule::notOp => PrefixOperator::Not,
            _ => unreachable!(),
        }
    }
}

/// An operation involving one Ryan value, where the value precedes it.
#[derive(Debug, Clone, PartialEq)]
pub enum PostfixOperator {
    /// Get the value associated with a key in a dictionary using the familiar `.` notation.
    Access(Rc<str>),
    /// Access the value in a deeply nested Ryan object using the supplied path.
    Path(Vec<Expression>),
    /// Cast the value as integer.
    CastInt,
    /// Cast the value as float.
    CastFloat,
    /// Cast the value as text.
    CastText,
}

impl Display for PostfixOperator {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Access(field) => write!(f, ".{field}")?,
            Self::Path(exprs) => {
                write!(f, "[")?;
                crate::utils::fmt_list(f, exprs)?;
                write!(f, "]")?;
            },
            Self::CastInt => {
                write!(f, "as int")?;
            }
            Self::CastFloat => {
                write!(f, "as float")?;
            }
            Self::CastText => {
                write!(f, "as text")?;
            }
        }

        Ok(())
    }
}

impl PostfixOperator {
    pub(super) fn parse(logger: &mut ErrorLogger, pair: Pair<'_, Rule>) -> Self {
        match pair.as_rule() {
            Rule::accessOp => {
                let mut field = None;
                for pair in pair.into_inner() {
                    match pair.as_rule() {
                        Rule::identifier => field = Some(rc_world::str_to_rc(pair.as_str())),
                        _ => unreachable!(),
                    }
                }

                PostfixOperator::Access(
                    field.expect("there is always a field in an access operation"),
                )
            }
            Rule::pathOp => {
                let mut exprs = vec![];
                for pair in pair.into_inner() {
                    match pair.as_rule() {
                        Rule::expression => {
                            exprs.push(Expression::parse(logger, pair.into_inner()))
                        }
                        _ => unreachable!(),
                    }
                }

                PostfixOperator::Path(exprs)
            },
            Rule::castInt => {
                PostfixOperator::CastInt
            },
            Rule::castFloat => {
                PostfixOperator::CastFloat
            }
            Rule::castText => {
                PostfixOperator::CastText
            }
            _ => unreachable!(),
        }
    }
}

/// An operation involving two Ryan expressions and a binary operator.
#[derive(Debug, Clone, PartialEq)]
pub struct BinaryOperation {
    /// The left side of the operation.
    pub left: Expression,
    /// The binary operator to be used.
    pub op: BinaryOperator,
    /// The right side of the operation.
    pub right: Expression,
}

impl Display for BinaryOperation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let BinaryOperator::Juxtaposition = self.op {
            write!(f, "{} {}", self.left, self.right)
        } else {
            write!(f, "{} {} {}", self.left, self.op, self.right)
        }
    }
}

impl BinaryOperation {
    pub(super) fn eval(&self, state: &mut State<'_>) -> Option<Value> {
        let left = self.left.eval(state)?;

        // These are short-circuiting operations...
        let left = match (left, self.op) {
            (Value::Bool(true), BinaryOperator::Or) => return Some(Value::Bool(true)),
            (Value::Bool(false), BinaryOperator::And) => return Some(Value::Bool(false)),
            (left, BinaryOperator::Default) if left != Value::Null => return Some(left),
            (left, _) => left, // not short-circuiting... carry on!
        };

        let right = self.right.eval(state)?;
        let result = match (left, self.op, right) {
            (Value::PatternMatches(id, pats), BinaryOperator::Juxtaposition, arg) => {
                state.push_ctx(Context::SubstitutingPattern(Some(id)));
                let mut evalued = None;
                let mut last_error = None;

                for pat in pats {
                    match pat.r#match(&arg, state)? {
                        Ok(found) => {
                            evalued = Some(found);
                            break;
                        }
                        Err(err) => last_error = Some(err),
                    }
                }

                if let Some(evalued) = evalued {
                    state.pop_ctx();
                    evalued
                } else {
                    state.raise(format!(
                        "{}",
                        last_error.expect("there is at least one patter in a pattern match")
                    ))?;
                    return None;
                }
            }
            (Value::NativePatternMatch(pat), BinaryOperator::Juxtaposition, arg) => {
                pat.r#match(arg, state)?
            }
            (value, BinaryOperator::Juxtaposition, Value::List(list)) => {
                match value.extract_path(&list) {
                    Ok(val) => val,
                    Err(err) => {
                        state.raise(err);
                        return None;
                    }
                }
            }
            (Value::Null, BinaryOperator::Default, val) => val.clone(),
            (first, BinaryOperator::Default, _) => first,
            (Value::Bool(left), BinaryOperator::Or, Value::Bool(right)) => {
                Value::Bool(left || right)
            }
            (Value::Bool(left), BinaryOperator::And, Value::Bool(right)) => {
                Value::Bool(left && right)
            }
            (left, BinaryOperator::Equals, right) => Value::Bool(left == right),
            (left, BinaryOperator::NotEquals, right) => Value::Bool(left != right),
            (left, BinaryOperator::TypeMatches, Value::Type(r#type)) => {
                Value::Bool(r#type.matches(&left))
            }
            (Value::Integer(left), BinaryOperator::GreaterThen, Value::Integer(right)) => {
                Value::Bool(left > right)
            }
            (Value::Integer(left), BinaryOperator::GreaterThen, Value::Float(right)) => {
                Value::Bool(left as f64 > right)
            }
            (Value::Float(left), BinaryOperator::GreaterThen, Value::Integer(right)) => {
                Value::Bool(left > right as f64)
            }
            (Value::Float(left), BinaryOperator::GreaterThen, Value::Float(right)) => {
                Value::Bool(left > right)
            }

            (Value::Integer(left), BinaryOperator::GreaterEqual, Value::Integer(right)) => {
                Value::Bool(left >= right)
            }
            (Value::Integer(left), BinaryOperator::GreaterEqual, Value::Float(right)) => {
                Value::Bool(left as f64 >= right)
            }
            (Value::Float(left), BinaryOperator::GreaterEqual, Value::Integer(right)) => {
                Value::Bool(left >= right as f64)
            }
            (Value::Float(left), BinaryOperator::GreaterEqual, Value::Float(right)) => {
                Value::Bool(left >= right)
            }

            (Value::Integer(left), BinaryOperator::LesserThen, Value::Integer(right)) => {
                Value::Bool(left < right)
            }
            (Value::Integer(left), BinaryOperator::LesserThen, Value::Float(right)) => {
                Value::Bool((left as f64) < right)
            }
            (Value::Float(left), BinaryOperator::LesserThen, Value::Integer(right)) => {
                Value::Bool(left < right as f64)
            }
            (Value::Float(left), BinaryOperator::LesserThen, Value::Float(right)) => {
                Value::Bool(left < right)
            }

            (Value::Integer(left), BinaryOperator::LesserEqual, Value::Integer(right)) => {
                Value::Bool(left <= right)
            }
            (Value::Integer(left), BinaryOperator::LesserEqual, Value::Float(right)) => {
                Value::Bool(left as f64 <= right)
            }
            (Value::Float(left), BinaryOperator::LesserEqual, Value::Integer(right)) => {
                Value::Bool(left <= right as f64)
            }
            (Value::Float(left), BinaryOperator::LesserEqual, Value::Float(right)) => {
                Value::Bool(left <= right)
            }

            (val, BinaryOperator::IsContainedIn, Value::List(list)) => {
                Value::Bool(list.iter().any(|item| *item == val))
            }
            (Value::Text(key), BinaryOperator::IsContainedIn, Value::Map(map)) => {
                Value::Bool(map.contains_key(&*key))
            }
            (Value::Text(sub), BinaryOperator::IsContainedIn, Value::Text(text)) => {
                Value::Bool(text.contains(&*sub))
            }

            (Value::Integer(left), BinaryOperator::Plus, Value::Integer(right)) => {
                Value::Integer(left + right)
            }
            (Value::Integer(left), BinaryOperator::Plus, Value::Float(right)) => {
                Value::Float(left as f64 + right)
            }
            (Value::Float(left), BinaryOperator::Plus, Value::Integer(right)) => {
                Value::Float(left + right as f64)
            }
            (Value::Float(left), BinaryOperator::Plus, Value::Float(right)) => {
                Value::Float(left + right)
            }

            (Value::Integer(left), BinaryOperator::Minus, Value::Integer(right)) => {
                Value::Integer(left - right)
            }
            (Value::Integer(left), BinaryOperator::Minus, Value::Float(right)) => {
                Value::Float(left as f64 - right)
            }
            (Value::Float(left), BinaryOperator::Minus, Value::Integer(right)) => {
                Value::Float(left - right as f64)
            }
            (Value::Float(left), BinaryOperator::Minus, Value::Float(right)) => {
                Value::Float(left - right)
            }

            (Value::Integer(left), BinaryOperator::Times, Value::Integer(right)) => {
                Value::Integer(left * right)
            }
            (Value::Integer(left), BinaryOperator::Times, Value::Float(right)) => {
                Value::Float(left as f64 * right)
            }
            (Value::Float(left), BinaryOperator::Times, Value::Integer(right)) => {
                Value::Float(left * right as f64)
            }
            (Value::Float(left), BinaryOperator::Times, Value::Float(right)) => {
                Value::Float(left * right)
            }

            (Value::Integer(_), BinaryOperator::Divided, Value::Integer(0)) => {
                Value::Float(f64::NAN)
            }
            (Value::Integer(left), BinaryOperator::Divided, Value::Integer(right)) => {
                Value::Integer(left / right)
            }
            (Value::Integer(left), BinaryOperator::Divided, Value::Float(right)) => {
                Value::Float(left as f64 / right)
            }
            (Value::Float(left), BinaryOperator::Divided, Value::Integer(right)) => {
                Value::Float(left / right as f64)
            }
            (Value::Float(left), BinaryOperator::Divided, Value::Float(right)) => {
                Value::Float(left / right)
            }

            (Value::Integer(_), BinaryOperator::Remainder, Value::Integer(0)) => {
                Value::Float(f64::NAN)
            }
            (Value::Integer(left), BinaryOperator::Remainder, Value::Integer(right)) => {
                Value::Integer(left % right)
            }
            (Value::Integer(left), BinaryOperator::Remainder, Value::Float(right)) => {
                Value::Float(left as f64 % right)
            }
            (Value::Float(left), BinaryOperator::Remainder, Value::Integer(right)) => {
                Value::Float(left % right as f64)
            }
            (Value::Float(left), BinaryOperator::Remainder, Value::Float(right)) => {
                Value::Float(left % right)
            }

            (Value::Text(left), BinaryOperator::Plus, Value::Text(right)) => {
                let cat = left.as_ref().to_string() + &right;
                Value::Text(rc_world::string_to_rc(cat))
            }
            (Value::List(left), BinaryOperator::Plus, Value::List(right)) => Value::List(Rc::from(
                left.iter()
                    .chain(right.as_ref())
                    .cloned()
                    .collect::<Vec<_>>(),
            )),
            (Value::Map(left), BinaryOperator::Plus, Value::Map(right)) => Value::Map(Rc::new(
                left.iter()
                    .chain(right.as_ref())
                    .map(|(key, value)| (key.clone(), value.clone()))
                    .collect(),
            )),
            (left, op, right) => {
                state.raise(format!(
                    "Operator `{}` cannot be applied to `{}` and `{}`",
                    op, left, right,
                ))?;
                return None;
            }
        };

        Some(result)
    }
}

/// An operation involving a Ryan expression and a prefix operator.
#[derive(Debug, Clone, PartialEq)]
pub struct PrefixOperation {
    /// The prefix operator.
    pub op: PrefixOperator,
    /// The expression on which the prefix operator is applied.
    pub right: Expression,
}

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

impl PrefixOperation {
    pub(super) fn eval(&self, state: &mut State) -> Option<Value> {
        let right = self.right.eval(state)?;

        let result = match (&self.op, &right) {
            (PrefixOperator::Not, Value::Bool(b)) => Value::Bool(!*b),
            _ => {
                state.raise(format!(
                    "Operator `{}` cannot be applied to `{}`",
                    self.op, right,
                ))?;
                return None;
            }
        };

        Some(result)
    }
}

/// An operation involving a Ryan expression and a postfix operator.
#[derive(Debug, Clone, PartialEq)]
pub struct PostfixOperation {
    /// The expression on which the postfix operator is applied.
    pub left: Expression,
    /// The postfix operator to be applied.
    pub op: PostfixOperator,
}

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

impl PostfixOperation {
    pub(super) fn eval(&self, state: &mut State) -> Option<Value> {
        let left = self.left.eval(state)?;

        let result = match (&left, &self.op) {
            (Value::Map(dict), PostfixOperator::Access(field)) => {
                if let Some(value) = dict.get(field) {
                    value.clone()
                } else {
                    state.raise(format!("Key `{}` not present in `{}`", field, left,))?;
                    return None;
                }
            }
            (left, PostfixOperator::Path(path)) => {
                match left.extract_path(
                    &path
                        .iter()
                        .map(|item| item.eval(state))
                        .collect::<Option<Vec<_>>>()?,
                ) {
                    Ok(value) => value,
                    Err(err) => {
                        state.raise(err);
                        return None;
                    }
                }
            }
            (Value::Bool(b), PostfixOperator::CastInt) => {
                Value::Integer(*b as i64)
            },
            (Value::Float(f), PostfixOperator::CastInt) => {
                Value::Integer(*f as i64)
            }
            (Value::Integer(i), PostfixOperator::CastInt) => {
                Value::Integer(*i as i64)
            }
            (Value::Bool(b), PostfixOperator::CastFloat) => {
                Value::Float(*b as i64 as f64)
            }
            (Value::Float(f), PostfixOperator::CastFloat) => {
                Value::Float(*f as f64)
            }
            (Value::Integer(i), PostfixOperator::CastFloat) => {
                Value::Float(*i as f64)
            }
            (left, PostfixOperator::CastText) => {
                Value::Text(rc_world::string_to_rc(left.to_string()))
            }
            _ => {
                state.raise(format!(
                    "Operator `{}` cannot be applied to `{}`",
                    self.op, left,
                ))?;
                return None;
            }
        };

        Some(result)
    }
}