diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-07-18 18:05:45 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-07-18 18:05:45 +0200 |
| commit | 00266f6a58843583f2b46489d1e67d50f023d253 (patch) | |
| tree | 9edbd3cfb1ddd9d303a97c4d8deae98e997390ea /src/syntax/func | |
| parent | 6f1319e91fe18289c4f9193a4157bde9ee3c53b8 (diff) | |
| parent | 38a24247422ed433a9743e7eacf5037ccf877fa0 (diff) | |
Merge pull request #2 from typst/expressions
Add parsing capabilities for mathematical expressions
Diffstat (limited to 'src/syntax/func')
| -rw-r--r-- | src/syntax/func/mod.rs | 38 |
1 files changed, 15 insertions, 23 deletions
diff --git a/src/syntax/func/mod.rs b/src/syntax/func/mod.rs index 53bbc14e..fd516208 100644 --- a/src/syntax/func/mod.rs +++ b/src/syntax/func/mod.rs @@ -42,22 +42,27 @@ impl FuncArgs { } /// Add an argument. - pub fn add(&mut self, arg: FuncArg) { - match arg { - FuncArg::Pos(item) => self.pos.add(item), - FuncArg::Key(pair) => self.key.add(pair), + 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=FuncArg> { - self.pos.items.into_iter().map(|item| FuncArg::Pos(item)) - .chain(self.key.pairs.into_iter().map(|pair| FuncArg::Key(pair))) + 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<FuncArg> for FuncArgs { - fn from_iter<I: IntoIterator<Item=FuncArg>>(iter: I) -> Self { +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); @@ -70,24 +75,11 @@ impl FromIterator<FuncArg> for FuncArgs { #[derive(Debug, Clone, PartialEq)] pub enum FuncArg { /// A positional argument. - Pos(Spanned<Expr>), + Pos(Expr), /// A keyword argument. Key(Pair), } -impl FuncArg { - /// The full span of this argument. - /// - /// In case of a positional argument this is just the span of the expression - /// and in case of a keyword argument the combined span of key and value. - pub fn span(&self) -> Span { - match self { - FuncArg::Pos(item) => item.span, - FuncArg::Key(Pair { key, value }) => Span::merge(key.span, value.span), - } - } -} - /// Extra methods on [`Options`](Option) used for argument parsing. pub trait OptionExt: Sized { /// Add an error about a missing argument `arg` with the given span if the |
