Skip to content

Commit e2b29ad

Browse files
committed
feat: Use new proc_macro's span apis to get next token with no gap
Fix #86
1 parent adb90c8 commit e2b29ad

File tree

3 files changed

+6
-31
lines changed

3 files changed

+6
-31
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ readme = "README.md"
1111
version = "1.9.6"
1212
authors = ["rust-shell-script <[email protected]>"]
1313
edition = "2018"
14+
rust-version = "1.88"
1415

1516
[workspace]
1617
members = ["macros"]

macros/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ keywords = ["shell", "script", "cli", "process", "pipe"]
88
version = "1.9.6"
99
authors = ["Tao Guo <[email protected]>"]
1010
edition = "2018"
11+
rust-version = "1.88"
1112

1213
[lib]
1314
proc-macro = true
1415

1516
[dependencies]
1617
syn = { version = "2", features = ["full"] }
1718
quote = "1"
18-
proc-macro2 = "1"
19+
proc-macro2 = { version = "1.0.86", features = ["span-locations"] }
1920
proc-macro-error2 = "2"
2021

2122
[dev-dependencies]

macros/src/lexer.rs

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -473,12 +473,10 @@ impl<I: Iterator<Item = TokenTree>> TokenStreamPeekable<I> {
473473
match self.peekable.peek() {
474474
None => None,
475475
Some(item) => {
476-
let (_, cur_end) = Self::span_location(&self.span);
477-
let (new_start, _) = Self::span_location(&item.span());
478-
if new_start > cur_end {
479-
None
480-
} else {
476+
if self.span.end() == item.span().start() {
481477
Some(item)
478+
} else {
479+
None
482480
}
483481
}
484482
}
@@ -487,29 +485,4 @@ impl<I: Iterator<Item = TokenTree>> TokenStreamPeekable<I> {
487485
fn span(&self) -> Span {
488486
self.span
489487
}
490-
491-
// helper function to get (start, end) of Span
492-
fn span_location(span: &Span) -> (usize, usize) {
493-
let mut start = 0;
494-
let mut end = 0;
495-
let mut parse_start = true;
496-
format!("{:?}", span) // output is like this: #0 bytes(95..97)
497-
.chars()
498-
.skip_while(|c| *c != '(')
499-
.skip(1)
500-
.take_while(|c| *c != ')')
501-
.for_each(|c| {
502-
if c == '.' {
503-
parse_start = false;
504-
} else if c.is_ascii_digit() {
505-
let digit = c.to_digit(10).unwrap() as usize;
506-
if parse_start {
507-
start = start * 10 + digit;
508-
} else {
509-
end = end * 10 + digit;
510-
}
511-
}
512-
});
513-
(start, end)
514-
}
515488
}

0 commit comments

Comments
 (0)