summaryrefslogtreecommitdiff
path: root/src/syntax/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/syntax/mod.rs')
-rw-r--r--src/syntax/mod.rs45
1 files changed, 38 insertions, 7 deletions
diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs
index f9b24e68..286bb2c7 100644
--- a/src/syntax/mod.rs
+++ b/src/syntax/mod.rs
@@ -116,8 +116,15 @@ impl FuncArgs {
}
}
-/// An argument or return value.
+/// One argument passed to a function.
#[derive(Debug, Clone, PartialEq)]
+pub enum FuncArg {
+ Positional(Spanned<Expression>),
+ Keyword(Spanned<(Spanned<String>, Spanned<Expression>)>),
+}
+
+/// An argument or return value.
+#[derive(Clone, PartialEq)]
pub enum Expression {
Ident(String),
Str(String),
@@ -145,8 +152,10 @@ impl Display for Expression {
}
}
+debug_display!(Expression);
+
/// Annotates a value with the part of the source code it corresponds to.
-#[derive(Debug, Copy, Clone, Eq, PartialEq)]
+#[derive(Copy, Clone, Eq, PartialEq)]
pub struct Spanned<T> {
pub val: T,
pub span: Span,
@@ -157,8 +166,8 @@ impl<T> Spanned<T> {
Spanned { val, span }
}
- pub fn value(&self) -> &T {
- &self.val
+ pub fn value(self) -> T {
+ self.val
}
pub fn span_map<F, U>(self, f: F) -> Spanned<U> where F: FnOnce(T) -> U {
@@ -166,8 +175,16 @@ impl<T> Spanned<T> {
}
}
+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(Debug, Copy, Clone, Eq, PartialEq)]
+#[derive(Copy, Clone, Eq, PartialEq)]
pub struct Span {
pub start: usize,
pub end: usize,
@@ -178,6 +195,13 @@ impl 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 }
}
@@ -187,7 +211,14 @@ impl Span {
}
pub fn expand(&mut self, other: Span) {
- self.start = self.start.min(other.start);
- self.end = self.end.max(other.end);
+ *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);