Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sam0x17 committed Mar 29, 2024
1 parent 22328e1 commit d5f834a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ pub trait Parsable:
let text = s.source_text();
if stream.remaining().starts_with(text) {
let mut value = value;
value.set_span(stream.consume(text.len())?);
*value.span_mut() = stream.consume(text.len())?;
return Ok(value);
}
let prefix = common_prefix(text, stream.remaining());
Expand All @@ -372,8 +372,6 @@ pub trait Parsable:
Err(Error::expected(span, expected))
}

fn set_span(&mut self, span: impl Into<Span>);

fn unparse(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.span().source_text())
}
Expand Down
19 changes: 19 additions & 0 deletions src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,32 @@ pub struct LineCol {
/// A trait for types that have a [`Span`].
pub trait Spanned {
/// Returns the underlying [`Span`] of self.
///
/// If the type has multiple [`Span`]s, this method should return the primary [`Span`],
/// i.e. by joining all of the [`Span`]s together, rather than storing a permanent primary
/// [`Span`] on the type directly.
fn span(&self) -> Span;

/// Provides mutable access to the underlying [`Span`] of self.
///
/// Use with caution. With great power comes great responsibility. In particular, changing
/// the [`Span`] of a type that has already been parsed or for types that are part of a larger
/// structure can lead to unexpected behavior.
///
/// One guarantee implementers must provide is that the [`Span`] returned by this method is
/// always the same as the one returned by [`span`], even if the type is mutated in between
/// calls to the two methods.
fn span_mut(&mut self) -> &mut Span;
}

impl Spanned for Span {
fn span(&self) -> Span {
self.clone()
}

fn span_mut(&mut self) -> &mut Span {
self
}
}

/// A trait for types that have multiple [`Span`]s.
Expand Down

0 comments on commit d5f834a

Please sign in to comment.