oxedyne/fe2o3/fe2o3_austenite/src/eval/args.rs
3.8 KiB, 1 run
created by r1870400018:63200, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | // U2 owns this file. Every native function in every unit parses its arguments through these methods; |
| 2 | // their names and semantics are Typst's `Args`. |
| 3 | |
| 4 | use crate::eval::value::{ |
| 5 | FromValue, |
| 6 | Value, |
| 7 | }; |
| 8 | use crate::syntax::Span; |
| 9 | |
| 10 | use oxedyne_fe2o3_core::prelude::*; |
| 11 | |
| 12 | #[derive(Clone, Debug)] |
| 13 | pub struct Arg { |
| 14 | pub span: Span, // the whole argument, `name: value` |
| 15 | pub value_span: Span, // the value's own expression, where Typst reports a fault of the value |
| 16 | pub name: Option<String>, |
| 17 | pub value: Value, |
| 18 | } |
| 19 | |
| 20 | /// Call arguments in source order. Taking an argument removes it; `finish` then rejects leftovers. |
| 21 | #[derive(Clone, Debug, Default)] |
| 22 | pub struct Args { |
| 23 | pub span: Span, |
| 24 | pub items: Vec<Arg>, |
| 25 | } |
| 26 | |
| 27 | impl Args { |
| 28 | pub fn new(span: Span) -> Self { Self { span, items: Vec::new() } } |
| 29 | |
| 30 | pub fn push(&mut self, span: Span, value: Value) { |
| 31 | self.items.push(Arg { span, value_span: span, name: None, value }); |
| 32 | } |
| 33 | |
| 34 | pub fn push_named<S: Into<String>>(&mut self, span: Span, name: S, value: Value) { |
| 35 | self.items.push(Arg { span, value_span: span, name: Some(name.into()), value }); |
| 36 | } |
| 37 | |
| 38 | /// As [`push_named`](Self::push_named), told where the value's own expression stands. |
| 39 | pub fn push_named_at<S: Into<String>>(&mut self, span: Span, value_span: Span, name: S, value: Value) { |
| 40 | self.items.push(Arg { span, value_span, name: Some(name.into()), value }); |
| 41 | } |
| 42 | |
| 43 | /// Puts a value in front of the positional arguments: a method's receiver, or `.with`'s bound ones. |
| 44 | pub fn prepend(&mut self, span: Span, value: Value) { |
| 45 | self.items.insert(0, Arg { span, value_span: span, name: None, value }); |
| 46 | } |
| 47 | |
| 48 | /// The next positional argument cast to `T`, or `None` when there is none. |
| 49 | pub fn eat<T: FromValue>(&mut self) -> Outcome<Option<T>> { |
| 50 | match self.items.iter().position(|a| a.name.is_none()) { |
| 51 | Some(i) => { |
| 52 | let a = self.items.remove(i); |
| 53 | Ok(Some(res!(T::from_value(a.value)))) |
| 54 | } |
| 55 | None => Ok(None), |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /// The next positional argument, required: absent is "missing argument: `what`". |
| 60 | pub fn expect<T: FromValue>(&mut self, what: &str) -> Outcome<T> { |
| 61 | match res!(self.eat::<T>()) { |
| 62 | Some(v) => Ok(v), |
| 63 | None => Err(err!("missing argument: {}", what; Input, Missing)), |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /// The first positional argument the predicate accepts, removed and cast. |
| 68 | pub fn find<T: FromValue>(&mut self, pred: impl Fn(&Value) -> bool) -> Outcome<Option<T>> { |
| 69 | match self.items.iter().position(|a| a.name.is_none() && pred(&a.value)) { |
| 70 | Some(i) => { |
| 71 | let a = self.items.remove(i); |
| 72 | Ok(Some(res!(T::from_value(a.value)))) |
| 73 | } |
| 74 | None => Ok(None), |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /// Every remaining positional argument, cast. |
| 79 | pub fn all<T: FromValue>(&mut self) -> Outcome<Vec<T>> { |
| 80 | let mut out = Vec::new(); |
| 81 | while let Some(v) = res!(self.eat::<T>()) { |
| 82 | out.push(v); |
| 83 | } |
| 84 | Ok(out) |
| 85 | } |
| 86 | |
| 87 | /// A named argument; given twice, the last wins, as in Typst. |
| 88 | pub fn named<T: FromValue>(&mut self, name: &str) -> Outcome<Option<T>> { |
| 89 | let mut found = None; |
| 90 | let mut i = 0; |
| 91 | while i < self.items.len() { |
| 92 | if self.items[i].name.as_deref() == Some(name) { |
| 93 | found = Some(self.items.remove(i).value); |
| 94 | } else { |
| 95 | i += 1; |
| 96 | } |
| 97 | } |
| 98 | match found { |
| 99 | Some(v) => Ok(Some(res!(T::from_value(v)))), |
| 100 | None => Ok(None), |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /// Every argument, positional and named, moved out; the span stays. |
| 105 | pub fn take(&mut self) -> Args { |
| 106 | Args { span: self.span, items: std::mem::take(&mut self.items) } |
| 107 | } |
| 108 | |
| 109 | /// How many positional arguments remain. |
| 110 | pub fn pos_count(&self) -> usize { self.items.iter().filter(|a| a.name.is_none()).count() } |
| 111 | |
| 112 | pub fn is_empty(&self) -> bool { self.items.is_empty() } |
| 113 | |
| 114 | /// Fails on any argument not taken. |
| 115 | pub fn finish(self) -> Outcome<()> { |
| 116 | match self.items.first() { |
| 117 | None => Ok(()), |
| 118 | Some(Arg { name: Some(n), .. }) => Err(err!("unexpected argument: {}", n; Input, Unexpected)), |
| 119 | Some(_) => Err(err!("unexpected argument"; Input, Unexpected)), |
| 120 | } |
| 121 | } |
| 122 | } |