summaryrefslogtreecommitdiff
path: root/src/syntax/mod.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2019-10-30 22:52:25 +0100
committerLaurenz <laurmaedje@gmail.com>2019-10-30 22:52:25 +0100
commit65ec3764e59353995a4feaa4214aea8c3e59bc3a (patch)
tree66c97b641854770f3551deafd159afffbdc51fb4 /src/syntax/mod.rs
parentb5d8b8f4a5425ec7bcaa50d8394e76cffe4baadc (diff)
Basic node spans ✅
Diffstat (limited to 'src/syntax/mod.rs')
-rw-r--r--src/syntax/mod.rs16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs
index 8a6329de..f508c6cc 100644
--- a/src/syntax/mod.rs
+++ b/src/syntax/mod.rs
@@ -56,12 +56,11 @@ pub enum Token<'s> {
/// A tree representation of source code.
#[derive(Debug, PartialEq)]
pub struct SyntaxTree {
- pub nodes: Vec<Node>,
+ pub nodes: Vec<Spanned<Node>>,
}
impl SyntaxTree {
/// Create an empty syntax tree.
- #[inline]
pub fn new() -> SyntaxTree {
SyntaxTree { nodes: vec![] }
}
@@ -130,6 +129,8 @@ impl Display for Expression {
}
}
+/// Annotates a value with the part of the source code it corresponds to.
+#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Spanned<T> {
pub val: T,
pub span: Span,
@@ -141,6 +142,8 @@ impl<T> Spanned<T> {
}
}
+/// Describes a slice of source code.
+#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Span {
pub start: usize,
pub end: usize,
@@ -154,4 +157,13 @@ impl Span {
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.start = self.start.min(other.start);
+ self.end = self.end.max(other.end);
+ }
}