Files
lucisv/crates/vlog_semantic/src/scan.rs
T
lucioandClaude Fable 5 7cf657a538 Add vlog_semantic and parallel-files (jointly: shared root manifest)
vlog_semantic (new crate): single-file symbol tables and reference
resolution -- the layer that powers go-to-definition, references,
hover and document outline, and later the semantic lint rules.
Two-pass bind-then-resolve (Verilog forward references), 15 symbol
kinds, scope tree, and a three-state Resolution (Resolved /
ExternalCandidate carrying import paths for the future workspace
index / NotFound). scan.rs recovers name spans the AST lacks (95k
corpus spans verified; handshake filed and approved to move
name_span into the parser, which deletes the module). Corpus
baseline: 79.5% in-file resolution, 0.9% unresolved on clean files,
zero panics including tolerant-parse trees.

parallel-files (vfmt): rayon-parallel per-file formatting -- first
dependency, exact-pinned with written justification. 8 MiB worker
stacks discharge the recursion-limit handshake (sabotage-verified);
single-install panic hook discharges the cli-polish handshake;
deterministic input-order output (one stdout/stderr hash across 14
corpus runs at 4 thread counts); --threads flag; hand-rolled
.gitignore subset with 16/16 differential agreement against git
check-ignore. 3.6x wall-clock on the 860-file corpus.

Dev: 2 Opus agents; QA: 2 Sonnet agents; reviews in
docs/features/{vlog-semantic,parallel-files}/README.md.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-08-19 06:39:51 -07:00

194 lines
6.2 KiB
Rust

//! 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<TextRange> {
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<TextRange> {
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<u8> {
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));
}
}