Skip to main content

Cursor

Struct Cursor 

Source
pub struct Cursor<'a> {
    pub source: &'a str,
    pub pos: usize,
    pub line: usize,
    pub column: usize,
    pub base_offset: usize,
}
Expand description

源代码解析游标

Fields§

§source: &'a str

源代码字符串

§pos: usize

当前字节偏移位置

§line: usize

当前行号

§column: usize

当前列号

§base_offset: usize

基础偏移量

Implementations§

Source§

impl<'a> Cursor<'a>

Source

pub fn new(source: &'a str) -> Self

创建一个新的游标

Source

pub fn with_position(source: &'a str, pos: Position) -> Self

从指定位置创建游标

Source

pub fn with_sliced_source(source: &'a str, pos: Position) -> Self

从切片源创建游标

Source

pub fn is_eof(&self) -> bool

判断是否已到达文件末尾

Source

pub fn peek(&self) -> char

查看当前字符(不消费)

Source

pub fn peek_n(&self, n: usize) -> char

查看第 n 个字符(不消费)

Source

pub fn peek_str(&self, s: &str) -> bool

判断当前位置是否以指定字符串开头

Source

pub fn consume(&mut self) -> char

消费当前字符

Source

pub fn consume_n(&mut self, n: usize)

消费 n 个字符

Source

pub fn consume_str(&mut self, s: &str) -> bool

如果匹配则消费字符串

Source

pub fn consume_while<F>(&mut self, f: F) -> String
where F: Fn(char) -> bool,

消费满足条件的字符

Source

pub fn skip_whitespace(&mut self)

跳过空白字符

Source

pub fn consume_whitespace(&mut self) -> String

消费并返回空白字符

Source

pub fn skip_spaces(&mut self)

跳过空格和制表符

Source

pub fn expect(&mut self, expected: char) -> Result<()>

期望消费指定字符

Source

pub fn expect_str(&mut self, expected: &str) -> Result<()>

期望消费指定字符串

Source

pub fn position(&self) -> Position

获取当前位置

Source

pub fn current_str(&self, start: usize) -> &str

获取从起始位置到当前位置的字符串

Source

pub fn span_at_current(&self) -> Span

获取当前字符的位置范围

Source

pub fn span_from(&self, start: Position) -> Span

获取从指定起始位置到当前位置的范围

Source

pub fn consume_ident(&mut self) -> Result<String>

消费一个标识符

Source

pub fn consume_string(&mut self) -> Result<String>

消费一个字符串字面量

Source

pub fn span_at_pos(&self, pos: Position) -> Span

在指定位置创建一个零宽度的 Span

Auto Trait Implementations§

§

impl<'a> Freeze for Cursor<'a>

§

impl<'a> RefUnwindSafe for Cursor<'a>

§

impl<'a> Send for Cursor<'a>

§

impl<'a> Sync for Cursor<'a>

§

impl<'a> Unpin for Cursor<'a>

§

impl<'a> UnsafeUnpin for Cursor<'a>

§

impl<'a> UnwindSafe for Cursor<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.