mirror of
https://gitee.com/openharmony/third_party_rust_proc-macro2
synced 2024-11-23 07:19:41 +00:00
Deduplicate implementations of LineColumn
This commit is contained in:
parent
9924b79370
commit
a4be9825bc
@ -1,3 +1,5 @@
|
||||
#[cfg(span_locations)]
|
||||
use crate::location::LineColumn;
|
||||
use crate::parse::{self, Cursor};
|
||||
use crate::rcvec::{RcVec, RcVecBuilder, RcVecIntoIter, RcVecMut};
|
||||
use crate::{Delimiter, Spacing, TokenTree};
|
||||
@ -332,12 +334,6 @@ impl Debug for SourceFile {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(crate) struct LineColumn {
|
||||
pub line: usize,
|
||||
pub column: usize,
|
||||
}
|
||||
|
||||
#[cfg(span_locations)]
|
||||
thread_local! {
|
||||
static SOURCE_MAP: RefCell<SourceMap> = RefCell::new(SourceMap {
|
||||
|
43
src/lib.rs
43
src/lib.rs
@ -139,6 +139,9 @@ use crate::fallback as imp;
|
||||
#[cfg(wrap_proc_macro)]
|
||||
mod imp;
|
||||
|
||||
#[cfg(span_locations)]
|
||||
mod location;
|
||||
|
||||
use crate::marker::Marker;
|
||||
use core::cmp::Ordering;
|
||||
use core::fmt::{self, Debug, Display};
|
||||
@ -150,6 +153,9 @@ use std::error::Error;
|
||||
#[cfg(procmacro2_semver_exempt)]
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[cfg(span_locations)]
|
||||
pub use crate::location::LineColumn;
|
||||
|
||||
/// An abstract stream of tokens, or more concretely a sequence of token trees.
|
||||
///
|
||||
/// This type provides interfaces for iterating over token trees and for
|
||||
@ -356,37 +362,6 @@ impl Debug for SourceFile {
|
||||
}
|
||||
}
|
||||
|
||||
/// A line-column pair representing the start or end of a `Span`.
|
||||
///
|
||||
/// This type is semver exempt and not exposed by default.
|
||||
#[cfg(span_locations)]
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
pub struct LineColumn {
|
||||
/// The 1-indexed line in the source file on which the span starts or ends
|
||||
/// (inclusive).
|
||||
pub line: usize,
|
||||
/// The 0-indexed column (in UTF-8 characters) in the source file on which
|
||||
/// the span starts or ends (inclusive).
|
||||
pub column: usize,
|
||||
}
|
||||
|
||||
#[cfg(span_locations)]
|
||||
impl Ord for LineColumn {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
self.line
|
||||
.cmp(&other.line)
|
||||
.then(self.column.cmp(&other.column))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(span_locations)]
|
||||
impl PartialOrd for LineColumn {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
/// A region of source code, along with macro expansion information.
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Span {
|
||||
@ -492,8 +467,7 @@ impl Span {
|
||||
#[cfg(span_locations)]
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
|
||||
pub fn start(&self) -> LineColumn {
|
||||
let imp::LineColumn { line, column } = self.inner.start();
|
||||
LineColumn { line, column }
|
||||
self.inner.start()
|
||||
}
|
||||
|
||||
/// Get the ending line/column in the source file for this span.
|
||||
@ -508,8 +482,7 @@ impl Span {
|
||||
#[cfg(span_locations)]
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
|
||||
pub fn end(&self) -> LineColumn {
|
||||
let imp::LineColumn { line, column } = self.inner.end();
|
||||
LineColumn { line, column }
|
||||
self.inner.end()
|
||||
}
|
||||
|
||||
/// Creates an empty span pointing to directly before this span.
|
||||
|
29
src/location.rs
Normal file
29
src/location.rs
Normal file
@ -0,0 +1,29 @@
|
||||
use core::cmp::Ordering;
|
||||
|
||||
/// A line-column pair representing the start or end of a `Span`.
|
||||
///
|
||||
/// This type is semver exempt and not exposed by default.
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "span-locations")))]
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
pub struct LineColumn {
|
||||
/// The 1-indexed line in the source file on which the span starts or ends
|
||||
/// (inclusive).
|
||||
pub line: usize,
|
||||
/// The 0-indexed column (in UTF-8 characters) in the source file on which
|
||||
/// the span starts or ends (inclusive).
|
||||
pub column: usize,
|
||||
}
|
||||
|
||||
impl Ord for LineColumn {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
self.line
|
||||
.cmp(&other.line)
|
||||
.then(self.column.cmp(&other.column))
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for LineColumn {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
use crate::detection::inside_proc_macro;
|
||||
#[cfg(span_locations)]
|
||||
use crate::location::LineColumn;
|
||||
use crate::{fallback, Delimiter, Punct, Spacing, TokenTree};
|
||||
use core::fmt::{self, Debug, Display};
|
||||
use core::iter::FromIterator;
|
||||
@ -389,12 +391,6 @@ impl Debug for SourceFile {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(super_unstable, feature = "span-locations"))]
|
||||
pub(crate) struct LineColumn {
|
||||
pub line: usize,
|
||||
pub column: usize,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub(crate) enum Span {
|
||||
Compiler(proc_macro::Span),
|
||||
@ -471,7 +467,7 @@ impl Span {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(super_unstable, feature = "span-locations"))]
|
||||
#[cfg(span_locations)]
|
||||
pub fn start(&self) -> LineColumn {
|
||||
match self {
|
||||
#[cfg(proc_macro_span)]
|
||||
@ -481,14 +477,11 @@ impl Span {
|
||||
}
|
||||
#[cfg(not(proc_macro_span))]
|
||||
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
|
||||
Span::Fallback(s) => {
|
||||
let fallback::LineColumn { line, column } = s.start();
|
||||
LineColumn { line, column }
|
||||
}
|
||||
Span::Fallback(s) => s.start(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(super_unstable, feature = "span-locations"))]
|
||||
#[cfg(span_locations)]
|
||||
pub fn end(&self) -> LineColumn {
|
||||
match self {
|
||||
#[cfg(proc_macro_span)]
|
||||
@ -498,10 +491,7 @@ impl Span {
|
||||
}
|
||||
#[cfg(not(proc_macro_span))]
|
||||
Span::Compiler(_) => LineColumn { line: 0, column: 0 },
|
||||
Span::Fallback(s) => {
|
||||
let fallback::LineColumn { line, column } = s.end();
|
||||
LineColumn { line, column }
|
||||
}
|
||||
Span::Fallback(s) => s.end(),
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user