//! Recovering the byte range of a name the AST kept only as a `String`. //! //! Most declarations in `vlog_parser`'s AST store their name as a plain //! `String` and give a span only for the whole construct (`module m ... `, //! `input logic [W-1:0] d`). A language server needs the range of the *name*, //! so this scans the construct's own source text for it. See the README's //! Handshakes section: once the parser carries `name_span` on those nodes, //! this module goes away. use vlog_parser::span::TextRange; /// A cursor over one source window that hands out the ranges of successive /// identifiers, skipping comments, strings and macro invocations. pub(crate) struct NameScanner<'a> { src: &'a [u8], pos: usize, end: usize, } impl<'a> NameScanner<'a> { /// A scanner over `window` of `src`. pub(crate) fn new(src: &'a str, window: TextRange) -> Self { let end = (window.end as usize).min(src.len()); let pos = (window.start as usize).min(end); NameScanner { src: src.as_bytes(), pos, end } } /// The range of the next whole-word `name`, or `None` if the window holds /// no more. The cursor moves past what it returns, so repeated calls walk /// a list (`genvar i, j;`) left to right. pub(crate) fn find(&mut self, name: &str) -> Option { while let Some(word) = self.next_word() { if &self.src[word.as_range()] == name.as_bytes() { return Some(word); } } None } /// The range of the next identifier of any spelling. fn next_word(&mut self) -> Option { while self.pos < self.end { let b = self.src[self.pos]; match b { b'/' if self.peek(1) == Some(b'/') => self.skip_line(), b'/' if self.peek(1) == Some(b'*') => self.skip_block_comment(), b'"' => self.skip_string(), // A macro invocation is one opaque token: `` `WIDTH `` must // not answer a search for the identifier `WIDTH`. b'`' => { self.pos += 1; self.skip_word(); } _ if is_ident_start(b) => { let start = self.pos; self.skip_word(); return Some(TextRange::new(start as u32, self.pos as u32)); } _ => self.pos += 1, } } None } fn peek(&self, n: usize) -> Option { self.src.get(self.pos + n).copied() } fn skip_word(&mut self) { // An escaped identifier (`\a+b `) runs to the next whitespace. if self.src.get(self.pos) == Some(&b'\\') { self.pos += 1; while self.pos < self.end && !self.src[self.pos].is_ascii_whitespace() { self.pos += 1; } return; } while self.pos < self.end && is_ident_continue(self.src[self.pos]) { self.pos += 1; } } fn skip_line(&mut self) { while self.pos < self.end && self.src[self.pos] != b'\n' { self.pos += 1; } } fn skip_block_comment(&mut self) { self.pos += 2; while self.pos < self.end { if self.src[self.pos] == b'*' && self.peek(1) == Some(b'/') { self.pos += 2; return; } self.pos += 1; } } fn skip_string(&mut self) { self.pos += 1; while self.pos < self.end { match self.src[self.pos] { b'\\' => self.pos += 2, b'"' => { self.pos += 1; return; } _ => self.pos += 1, } } } } fn is_ident_start(b: u8) -> bool { b.is_ascii_alphabetic() || b == b'_' || b == b'$' || b == b'\\' } fn is_ident_continue(b: u8) -> bool { b.is_ascii_alphanumeric() || b == b'_' || b == b'$' } /// The range of `name` inside `window`, or an empty range at the window start /// when the scan comes up empty (a name the source spells differently — an /// escaped identifier, or text a macro produced). pub(crate) fn name_span(src: &str, window: TextRange, name: &str) -> TextRange { NameScanner::new(src, window).find(name).unwrap_or(TextRange::empty(window.start)) } /// The range of `name` written *after* the first `kw` in `window`. Used where /// the construct starts with a keyword and the name follows it, possibly /// past qualifiers (`function automatic int f`). pub(crate) fn name_after_kw(src: &str, window: TextRange, kw: &str, name: &str) -> TextRange { let mut scanner = NameScanner::new(src, window); if scanner.find(kw).is_none() { return TextRange::empty(window.start); } scanner.find(name).unwrap_or(TextRange::empty(window.start)) } #[cfg(test)] mod tests { use super::*; fn whole(src: &str) -> TextRange { TextRange::new(0, src.len() as u32) } #[test] fn finds_a_name_after_its_keyword() { let src = "module top (input a);"; let span = name_after_kw(src, whole(src), "module", "top"); assert_eq!(&src[span.as_range()], "top"); assert_eq!(span.start, 7); } #[test] fn skips_comments_and_strings() { let src = "/* d */ \"d\" // d\n d;"; let span = name_span(src, whole(src), "d"); assert_eq!(span.start, 18); } #[test] fn matches_whole_words_only() { let src = "input wire [W-1:0] w;"; let span = name_span(src, whole(src), "w"); assert_eq!(&src[span.as_range()], "w"); assert_eq!(span.start, 19); } #[test] fn a_macro_invocation_is_one_opaque_token() { let src = "`WIDTH WIDTH"; let span = name_span(src, whole(src), "WIDTH"); assert_eq!(span.start, 7); } #[test] fn a_name_that_is_not_there_gives_an_empty_range() { let src = "module m;"; let span = name_span(src, whole(src), "nope"); assert!(span.is_empty()); } #[test] fn successive_finds_walk_a_list() { let src = "genvar i, j;"; let mut scanner = NameScanner::new(src, whole(src)); assert_eq!(scanner.find("i").map(|s| s.start), Some(7)); assert_eq!(scanner.find("j").map(|s| s.start), Some(10)); } }