Introduce InnerSpan abstraction
This should be used when trying to get at subsets of a larger span, especially when the larger span is not available in the code attempting to work with those subsets (especially common in the fmt_macros crate). This is usually a good replacement for (BytePos, BytePos) and (usize, usize) tuples. This commit also removes from_inner_byte_pos, since it took usize arguments, which is error prone.
This commit is contained in:
parent
a859440092
commit
b1c357e0c3
6 changed files with 82 additions and 75 deletions
|
@ -1,5 +1,6 @@
|
|||
pub mod printf {
|
||||
use super::strcursor::StrCursor as Cur;
|
||||
use syntax_pos::InnerSpan;
|
||||
|
||||
/// Represents a single `printf`-style substitution.
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
|
@ -18,7 +19,7 @@ pub mod printf {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn position(&self) -> Option<(usize, usize)> {
|
||||
pub fn position(&self) -> Option<InnerSpan> {
|
||||
match *self {
|
||||
Substitution::Format(ref fmt) => Some(fmt.position),
|
||||
_ => None,
|
||||
|
@ -28,7 +29,7 @@ pub mod printf {
|
|||
pub fn set_position(&mut self, start: usize, end: usize) {
|
||||
match self {
|
||||
Substitution::Format(ref mut fmt) => {
|
||||
fmt.position = (start, end);
|
||||
fmt.position = InnerSpan::new(start, end);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
@ -65,7 +66,7 @@ pub mod printf {
|
|||
/// Type of parameter being converted.
|
||||
pub type_: &'a str,
|
||||
/// Byte offset for the start and end of this formatting directive.
|
||||
pub position: (usize, usize),
|
||||
pub position: InnerSpan,
|
||||
}
|
||||
|
||||
impl Format<'_> {
|
||||
|
@ -282,9 +283,9 @@ pub mod printf {
|
|||
let (mut sub, tail) = parse_next_substitution(self.s)?;
|
||||
self.s = tail;
|
||||
match sub {
|
||||
Substitution::Format(_) => if let Some((start, end)) = sub.position() {
|
||||
sub.set_position(start + self.pos, end + self.pos);
|
||||
self.pos += end;
|
||||
Substitution::Format(_) => if let Some(inner_span) = sub.position() {
|
||||
sub.set_position(inner_span.start + self.pos, inner_span.end + self.pos);
|
||||
self.pos += inner_span.end;
|
||||
}
|
||||
Substitution::Escape => self.pos += 2,
|
||||
}
|
||||
|
@ -373,7 +374,7 @@ pub mod printf {
|
|||
precision: None,
|
||||
length: None,
|
||||
type_: at.slice_between(next).unwrap(),
|
||||
position: (start.at, next.at),
|
||||
position: InnerSpan::new(start.at, next.at),
|
||||
}),
|
||||
next.slice_after()
|
||||
));
|
||||
|
@ -560,7 +561,7 @@ pub mod printf {
|
|||
drop(next);
|
||||
|
||||
end = at;
|
||||
let position = (start.at, end.at);
|
||||
let position = InnerSpan::new(start.at, end.at);
|
||||
|
||||
let f = Format {
|
||||
span: start.slice_between(end).unwrap(),
|
||||
|
@ -650,7 +651,7 @@ pub mod printf {
|
|||
precision: $prec,
|
||||
length: $len,
|
||||
type_: $type_,
|
||||
position: $pos,
|
||||
position: syntax_pos::InnerSpan::new($pos.0, $pos.1),
|
||||
}),
|
||||
"!"
|
||||
))
|
||||
|
@ -761,6 +762,7 @@ pub mod printf {
|
|||
|
||||
pub mod shell {
|
||||
use super::strcursor::StrCursor as Cur;
|
||||
use syntax_pos::InnerSpan;
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub enum Substitution<'a> {
|
||||
|
@ -778,11 +780,11 @@ pub mod shell {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn position(&self) -> Option<(usize, usize)> {
|
||||
pub fn position(&self) -> Option<InnerSpan> {
|
||||
match self {
|
||||
Substitution::Ordinal(_, pos) |
|
||||
Substitution::Name(_, pos) |
|
||||
Substitution::Escape(pos) => Some(*pos),
|
||||
Substitution::Escape(pos) => Some(InnerSpan::new(pos.0, pos.1)),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -823,7 +825,7 @@ pub mod shell {
|
|||
match parse_next_substitution(self.s) {
|
||||
Some((mut sub, tail)) => {
|
||||
self.s = tail;
|
||||
if let Some((start, end)) = sub.position() {
|
||||
if let Some(InnerSpan { start, end }) = sub.position() {
|
||||
sub.set_position(start + self.pos, end + self.pos);
|
||||
self.pos += end;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue