summaryrefslogtreecommitdiff
path: root/src/syntax/func/mod.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-08-02 16:31:34 +0200
committerLaurenz <laurmaedje@gmail.com>2020-08-02 16:31:34 +0200
commit533374db14087ac54fdc86afa5f009487ac1b850 (patch)
tree0970eb1ca893fe45369d622b5bc1f226f0f66004 /src/syntax/func/mod.rs
parent2188ef6b899cc10c84ed985e9ad9049fcc3eb662 (diff)
Refactor argument parsing 🔬
Diffstat (limited to 'src/syntax/func/mod.rs')
-rw-r--r--src/syntax/func/mod.rs107
1 files changed, 0 insertions, 107 deletions
diff --git a/src/syntax/func/mod.rs b/src/syntax/func/mod.rs
deleted file mode 100644
index 37dccc3d..00000000
--- a/src/syntax/func/mod.rs
+++ /dev/null
@@ -1,107 +0,0 @@
-//! Primitives for argument parsing in library functions.
-
-use std::iter::FromIterator;
-use crate::diagnostic::{Diagnostic, Diagnostics};
-use super::expr::{Expr, Ident, Tuple, Object, Pair};
-use super::span::{Span, Spanned};
-
-pub_use_mod!(maps);
-pub_use_mod!(keys);
-pub_use_mod!(values);
-
-/// An invocation of a function.
-#[derive(Debug, Clone, PartialEq)]
-pub struct FuncCall<'s> {
- pub header: FuncHeader,
- /// The body as a raw string containing what's inside of the brackets.
- pub body: Option<Spanned<&'s str>>,
-}
-
-/// The parsed header of a function (everything in the first set of brackets).
-#[derive(Debug, Clone, PartialEq)]
-pub struct FuncHeader {
- pub name: Spanned<Ident>,
- pub args: FuncArgs,
-}
-
-/// The positional and keyword arguments passed to a function.
-#[derive(Debug, Default, Clone, PartialEq)]
-pub struct FuncArgs {
- /// The positional arguments.
- pub pos: Tuple,
- /// They keyword arguments.
- pub key: Object,
-}
-
-impl FuncArgs {
- /// Create new empty function arguments.
- pub fn new() -> FuncArgs {
- FuncArgs {
- pos: Tuple::new(),
- key: Object::new(),
- }
- }
-
- /// Add an argument.
- pub fn add(&mut self, arg: Spanned<FuncArg>) {
- match arg.v {
- FuncArg::Pos(item) => self.pos.add(Spanned::new(item, arg.span)),
- FuncArg::Key(pair) => self.key.add(Spanned::new(pair, arg.span)),
- }
- }
-
- /// Iterate over all arguments.
- pub fn into_iter(self) -> impl Iterator<Item=Spanned<FuncArg>> {
- let pos = self.pos.items.into_iter()
- .map(|spanned| spanned.map(|item| FuncArg::Pos(item)));
-
- let key = self.key.pairs.into_iter()
- .map(|spanned| spanned.map(|pair| FuncArg::Key(pair)));
-
- pos.chain(key)
- }
-}
-
-impl FromIterator<Spanned<FuncArg>> for FuncArgs {
- fn from_iter<I: IntoIterator<Item=Spanned<FuncArg>>>(iter: I) -> Self {
- let mut args = FuncArgs::new();
- for item in iter.into_iter() {
- args.add(item);
- }
- args
- }
-}
-
-/// Either a positional or keyword argument.
-#[derive(Debug, Clone, PartialEq)]
-pub enum FuncArg {
- /// A positional argument.
- Pos(Expr),
- /// A keyword argument.
- Key(Pair),
-}
-
-/// Extra methods on [`Options`](Option) used for argument parsing.
-pub trait OptionExt<T>: Sized {
- /// Calls `f` with `val` if this is `Some(val)`.
- fn with(self, f: impl FnOnce(T));
-
- /// Add an error about a missing argument `arg` with the given span if the
- /// option is `None`.
- fn or_missing(self, diagnostics: &mut Diagnostics, span: Span, arg: &str) -> Self;
-}
-
-impl<T> OptionExt<T> for Option<T> {
- fn with(self, f: impl FnOnce(T)) {
- if let Some(val) = self {
- f(val);
- }
- }
-
- fn or_missing(self, diagnostics: &mut Diagnostics, span: Span, arg: &str) -> Self {
- if self.is_none() {
- diagnostics.push(error!(span, "missing argument: {}", arg));
- }
- self
- }
-}