172 lines
6.0 KiB
Rust
172 lines
6.0 KiB
Rust
//! The four cheap gates answer the same thing over a parse the caller already
|
|
//! has as they do over the text they would parse themselves.
|
|
//!
|
|
//! Take 5 §P1b (`docs/features/perf5-p1/README.md`) added `*_in` forms of
|
|
//! `port_action_modules`, `imported_packages`, `extract_site` and
|
|
//! `inline_site` so a server can spend the parse it already made. The text
|
|
//! forms stayed and became wrappers, and this file is the proof that the two
|
|
//! agree — over clean buffers, over buffers that do not parse, over CRLF, over
|
|
//! text with a non-BMP character in front of the cursor, and at every byte of
|
|
//! a small buffer rather than at a cursor chosen to pass.
|
|
|
|
use vlog_parser::parse_source_tolerant;
|
|
use vlog_refactor::{
|
|
extract_site, extract_site_in, imported_packages, imported_packages_in, inline_site,
|
|
inline_site_in, port_action_modules, port_action_modules_in,
|
|
};
|
|
|
|
/// A module that instantiates another, imports two packages, and has items
|
|
/// worth extracting — so every one of the four gates has something to find
|
|
/// somewhere in it.
|
|
const CLEAN: &str = "\
|
|
module m (
|
|
input logic clk_i,
|
|
output logic q_o
|
|
);
|
|
import a_pkg::*;
|
|
import b_pkg::*;
|
|
logic w;
|
|
sub u_sub (.a(clk_i), .b(w));
|
|
assign q_o = w;
|
|
endmodule
|
|
";
|
|
|
|
/// The same text with a construct the parser cannot recover into an item, so
|
|
/// the tolerant parse reports diagnostics — the case three of the four gates
|
|
/// refuse over.
|
|
const BROKEN: &str = "\
|
|
module m;
|
|
sub u_sub (.a(1));
|
|
logic ;;; w
|
|
endmodule
|
|
";
|
|
|
|
/// A Verilog-2001 header, which is the one shape `convert to ANSI` answers at
|
|
/// and the one `port_action_modules` finds without an instantiation.
|
|
const NON_ANSI: &str = "\
|
|
module old (a, b);
|
|
input a;
|
|
output b;
|
|
assign b = a;
|
|
endmodule
|
|
";
|
|
|
|
/// A non-BMP character in a comment before everything else: the bytes before
|
|
/// every cursor differ from the UTF-16 units, which is the shape a client's
|
|
/// position conversion produces.
|
|
const ASTRAL: &str = "\
|
|
// \u{1F600} a comment
|
|
module m;
|
|
sub u_sub (.a(1));
|
|
endmodule
|
|
";
|
|
|
|
fn crlf(text: &str) -> String {
|
|
text.replace('\n', "\r\n")
|
|
}
|
|
|
|
/// Every gate, both ways, at one offset — asserting the two agree.
|
|
fn agree_at(text: &str, offset: u32) {
|
|
let (tree, diagnostics) = parse_source_tolerant(text);
|
|
assert_eq!(
|
|
port_action_modules(text, offset),
|
|
port_action_modules_in(&tree, offset),
|
|
"port_action_modules at {offset}"
|
|
);
|
|
assert_eq!(
|
|
imported_packages(text, offset),
|
|
imported_packages_in(&tree, &diagnostics, text, offset),
|
|
"imported_packages at {offset}"
|
|
);
|
|
assert_eq!(
|
|
inline_site(text, offset),
|
|
inline_site_in(&tree, &diagnostics, text, offset),
|
|
"inline_site at {offset}"
|
|
);
|
|
// Extraction answers for a range: the empty one at the cursor (which is
|
|
// what an editor sends on a cursor move) and one covering the rest of the
|
|
// buffer.
|
|
let end = text.len() as u32;
|
|
assert_eq!(
|
|
extract_site(text, offset, offset),
|
|
extract_site_in(&tree, &diagnostics, text, offset, offset),
|
|
"extract_site at the cursor {offset}"
|
|
);
|
|
assert_eq!(
|
|
extract_site(text, offset, end),
|
|
extract_site_in(&tree, &diagnostics, text, offset, end),
|
|
"extract_site over {offset}..{end}"
|
|
);
|
|
}
|
|
|
|
/// Every byte offset of a buffer, and one past its end.
|
|
fn agree_everywhere(text: &str) {
|
|
for offset in 0..=text.len() as u32 {
|
|
agree_at(text, offset);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn the_parsed_gates_answer_a_clean_buffer_exactly_as_the_text_gates_do() {
|
|
agree_everywhere(CLEAN);
|
|
}
|
|
|
|
#[test]
|
|
fn the_parsed_gates_answer_a_buffer_that_does_not_parse_exactly_as_the_text_gates_do() {
|
|
agree_everywhere(BROKEN);
|
|
// And the three that refuse over diagnostics really do refuse here, so
|
|
// this case is not agreeing by both answering about a clean buffer.
|
|
let (tree, diagnostics) = parse_source_tolerant(BROKEN);
|
|
assert!(!diagnostics.is_empty(), "the fixture must not parse cleanly");
|
|
let cursor = BROKEN.find("u_sub").expect("the instance") as u32;
|
|
assert!(imported_packages_in(&tree, &diagnostics, BROKEN, cursor).is_empty());
|
|
assert!(inline_site_in(&tree, &diagnostics, BROKEN, cursor).is_none());
|
|
assert!(extract_site_in(&tree, &diagnostics, BROKEN, cursor, cursor).is_none());
|
|
// The port gate has no diagnostics check and never had one: it still finds
|
|
// the instantiation under the cursor.
|
|
assert_eq!(port_action_modules_in(&tree, cursor), vec!["sub".to_string()]);
|
|
}
|
|
|
|
#[test]
|
|
fn the_parsed_gates_agree_over_crlf_line_endings() {
|
|
agree_everywhere(&crlf(CLEAN));
|
|
agree_everywhere(&crlf(BROKEN));
|
|
}
|
|
|
|
#[test]
|
|
fn the_parsed_gates_agree_with_a_non_bmp_character_before_the_cursor() {
|
|
agree_everywhere(ASTRAL);
|
|
}
|
|
|
|
#[test]
|
|
fn the_parsed_gates_agree_on_a_non_ansi_header() {
|
|
agree_everywhere(NON_ANSI);
|
|
// Again, not vacuously: the header cursor really does name its module.
|
|
let (tree, _) = parse_source_tolerant(NON_ANSI);
|
|
let cursor = NON_ANSI.find("(a, b)").expect("the header") as u32;
|
|
assert_eq!(port_action_modules_in(&tree, cursor), vec!["old".to_string()]);
|
|
}
|
|
|
|
#[test]
|
|
fn the_parsed_gates_agree_on_an_empty_buffer() {
|
|
agree_everywhere("");
|
|
agree_everywhere("\n\n");
|
|
}
|
|
|
|
#[test]
|
|
fn the_parsed_gates_find_what_the_fixture_was_written_to_offer() {
|
|
// A guard on the fixture itself: if `CLEAN` stopped offering anything,
|
|
// every assertion above would still pass and prove nothing.
|
|
let (tree, diagnostics) = parse_source_tolerant(CLEAN);
|
|
let import = CLEAN.find("import a_pkg").expect("the import run") as u32;
|
|
assert_eq!(
|
|
imported_packages_in(&tree, &diagnostics, CLEAN, import),
|
|
vec!["a_pkg".to_string(), "b_pkg".to_string()]
|
|
);
|
|
let instance = CLEAN.find("u_sub").expect("the instance") as u32;
|
|
assert_eq!(port_action_modules_in(&tree, instance), vec!["sub".to_string()]);
|
|
assert!(inline_site_in(&tree, &diagnostics, CLEAN, instance).is_some());
|
|
let assign = CLEAN.find("assign q_o").expect("the assign") as u32;
|
|
assert!(extract_site_in(&tree, &diagnostics, CLEAN, assign, assign).is_some());
|
|
}
|