diff options
| author | Laurenz <laurmaedje@gmail.com> | 2019-12-04 19:34:29 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2019-12-04 19:35:28 +0100 |
| commit | 9fb31defd037a90bf8f9e38fa33acae23a70b269 (patch) | |
| tree | e0fd887792a59cbb3262a5d3157d0c786df56d60 /src/syntax/mod.rs | |
| parent | ace57c34206a13b4bc3885b944cc51e274f30b0f (diff) | |
Expand functionality of function! macro 🛰
Diffstat (limited to 'src/syntax/mod.rs')
| -rw-r--r-- | src/syntax/mod.rs | 87 |
1 files changed, 10 insertions, 77 deletions
diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index 9406fdf4..33413317 100644 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -2,13 +2,15 @@ use std::fmt::{self, Display, Formatter}; -use crate::func::Function; +use crate::func::LayoutFunc; use crate::size::Size; mod tokens; #[macro_use] mod parsing; +mod span; +pub use span::{Span, Spanned}; pub use tokens::{tokenize, Tokens}; pub use parsing::{parse, ParseContext, ParseError, ParseResult}; @@ -90,7 +92,13 @@ pub enum Node { #[derive(Debug)] pub struct FuncCall { pub header: Spanned<FuncHeader>, - pub body: Spanned<Box<dyn Function>>, + pub body: Spanned<Box<dyn LayoutFunc>>, +} + +impl PartialEq for FuncCall { + fn eq(&self, other: &FuncCall) -> bool { + (self.header == other.header) && (&self.body == &other.body) + } } /// Contains header information of a function invocation. @@ -134,12 +142,6 @@ pub enum Expression { Bool(bool), } -impl PartialEq for FuncCall { - fn eq(&self, other: &FuncCall) -> bool { - (self.header == other.header) && (&self.body == &other.body) - } -} - impl Display for Expression { fn fmt(&self, f: &mut Formatter) -> fmt::Result { use Expression::*; @@ -154,72 +156,3 @@ impl Display for Expression { } debug_display!(Expression); - -/// Annotates a value with the part of the source code it corresponds to. -#[derive(Copy, Clone, Eq, PartialEq)] -pub struct Spanned<T> { - pub val: T, - pub span: Span, -} - -impl<T> Spanned<T> { - pub fn new(val: T, span: Span) -> Spanned<T> { - Spanned { val, span } - } - - pub fn value(self) -> T { - self.val - } - - pub fn span_map<F, U>(self, f: F) -> Spanned<U> where F: FnOnce(T) -> U { - Spanned::new(f(self.val), self.span) - } -} - -impl<T> Display for Spanned<T> where T: std::fmt::Debug { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "({:?}:{})", self.val, self.span) - } -} - -debug_display!(Spanned; T where T: std::fmt::Debug); - -/// Describes a slice of source code. -#[derive(Copy, Clone, Eq, PartialEq)] -pub struct Span { - pub start: usize, - pub end: usize, -} - -impl Span { - pub fn new(start: usize, end: usize) -> Span { - Span { start, end } - } - - pub fn merge(a: Span, b: Span) -> Span { - Span { - start: a.start.min(b.start), - end: a.end.max(b.end), - } - } - - pub fn at(index: usize) -> Span { - Span { start: index, end: index + 1 } - } - - pub fn pair(&self) -> (usize, usize) { - (self.start, self.end) - } - - pub fn expand(&mut self, other: Span) { - *self = Span::merge(*self, other) - } -} - -impl Display for Span { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "[{}, {}]", self.start, self.end) - } -} - -debug_display!(Span); |
