From b01e707dba7810c3d28c82a84dec9064cc01d3c8 Mon Sep 17 00:00:00 2001 From: csmoe Date: Mon, 31 Dec 2018 21:30:37 +0800 Subject: doc parser input --- crates/ra_syntax/src/parser_impl/input.rs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'crates/ra_syntax/src/parser_impl') diff --git a/crates/ra_syntax/src/parser_impl/input.rs b/crates/ra_syntax/src/parser_impl/input.rs index ac6d900d8..083a7aa15 100644 --- a/crates/ra_syntax/src/parser_impl/input.rs +++ b/crates/ra_syntax/src/parser_impl/input.rs @@ -4,11 +4,26 @@ use std::ops::{Add, AddAssign}; pub(crate) struct ParserInput<'t> { text: &'t str, + /// start position of each token(expect whitespace and comment) + /// ```non-rust + /// struct Foo; + /// ^------^--- + /// | | ^- + /// 0 7 10 + /// ``` + /// (token, start_offset): `[(struct, 0), (Foo, 7), (;, 10)]` start_offsets: Vec, - tokens: Vec, // non-whitespace tokens + /// non-whitespace/comment tokens + /// ```non-rust + /// struct Foo {} + /// ^^^^^^ ^^^ ^^ + /// ``` + /// tokens: `[struct, Foo, {, }]` + tokens: Vec, } impl<'t> ParserInput<'t> { + /// Generate input from tokens(expect comment and whitespace). pub fn new(text: &'t str, raw_tokens: &'t [Token]) -> ParserInput<'t> { let mut tokens = Vec::new(); let mut start_offsets = Vec::new(); @@ -28,6 +43,7 @@ impl<'t> ParserInput<'t> { } } + /// Get the syntax kind of token at given input position. pub fn kind(&self, pos: InputPosition) -> SyntaxKind { let idx = pos.0 as usize; if !(idx < self.tokens.len()) { @@ -36,7 +52,8 @@ impl<'t> ParserInput<'t> { self.tokens[idx].kind } - pub fn len(&self, pos: InputPosition) -> TextUnit { + /// Get the length of a token at given input position. + pub fn token_len(&self, pos: InputPosition) -> TextUnit { let idx = pos.0 as usize; if !(idx < self.tokens.len()) { return 0.into(); @@ -44,7 +61,8 @@ impl<'t> ParserInput<'t> { self.tokens[idx].len } - pub fn start(&self, pos: InputPosition) -> TextUnit { + /// Get the start position of a taken at given input position. + pub fn token_start_at(&self, pos: InputPosition) -> TextUnit { let idx = pos.0 as usize; if !(idx < self.tokens.len()) { return 0.into(); @@ -52,7 +70,8 @@ impl<'t> ParserInput<'t> { self.start_offsets[idx] } - pub fn text(&self, pos: InputPosition) -> &'t str { + /// Get the raw text of a toen at given input position. + pub fn token_text(&self, pos: InputPosition) -> &'t str { let idx = pos.0 as usize; if !(idx < self.tokens.len()) { return ""; -- cgit v1.2.3