Refactoring M5: keyword + project-wide identifier completion; v0.3.0
Five completion tiers in one response: module skeletons (M4, unchanged gate), scope-visible symbols with real kinds, workspace-index names and `define macros (backtick-aware, never inventing punctuation), Verilog/SV keywords (single-source KEYWORDS list factored from the lexer's table, lexing unchanged), and token-harvested project words — comments and strings contribute nothing and get no completions. Candidate tables build with the index over the M1 scope ladder (94 ms / 860 files, ~1.4 MiB); a keystroke answers in ~300 µs, budget-tested. Version 0.3.0 across the workspace and extension, CHANGELOG covers the M1-M5 refactoring layer. Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
78ba0b8ba6
commit
d8e37086df
18 files changed
+2281
-320
No files matched your search
@@ -16,6 +16,7 @@ pub use lexer::{tokenize, LexError};
|
||||
pub use line_index::LineIndex;
|
||||
pub use parser::{parse, parse_tolerant, ParseDiagnostic, ParseError};
|
||||
pub use span::TextRange;
|
||||
pub use token::KEYWORDS;
|
||||
|
||||
/// Convenience: lex and parse in one step.
|
||||
pub fn parse_source(source: &str) -> Result<ast::SourceFile, Error> {
|
||||
|
||||
+233
-196
@@ -110,200 +110,237 @@ impl Token {
|
||||
}
|
||||
}
|
||||
|
||||
/// Reserved words we treat as keywords. This covers Verilog-2001 plus the
|
||||
/// SystemVerilog subset the parser understands. Words not listed here lex as
|
||||
/// plain identifiers.
|
||||
pub fn is_keyword(s: &str) -> bool {
|
||||
matches!(
|
||||
s,
|
||||
"module"
|
||||
| "macromodule"
|
||||
| "endmodule"
|
||||
| "interface"
|
||||
| "endinterface"
|
||||
| "package"
|
||||
| "endpackage"
|
||||
| "program"
|
||||
| "endprogram"
|
||||
| "modport"
|
||||
| "clocking"
|
||||
| "endclocking"
|
||||
| "input"
|
||||
| "output"
|
||||
| "inout"
|
||||
| "ref"
|
||||
| "wire"
|
||||
| "tri"
|
||||
| "tri0"
|
||||
| "tri1"
|
||||
| "wand"
|
||||
| "wor"
|
||||
| "trireg"
|
||||
| "supply0"
|
||||
| "supply1"
|
||||
| "uwire"
|
||||
| "reg"
|
||||
| "logic"
|
||||
| "bit"
|
||||
| "byte"
|
||||
| "shortint"
|
||||
| "int"
|
||||
| "longint"
|
||||
| "integer"
|
||||
| "time"
|
||||
| "realtime"
|
||||
| "real"
|
||||
| "shortreal"
|
||||
| "string"
|
||||
| "chandle"
|
||||
| "event"
|
||||
| "var"
|
||||
| "void"
|
||||
| "signed"
|
||||
| "unsigned"
|
||||
| "packed"
|
||||
| "parameter"
|
||||
| "localparam"
|
||||
| "defparam"
|
||||
| "specparam"
|
||||
| "type"
|
||||
| "typedef"
|
||||
| "enum"
|
||||
| "struct"
|
||||
| "union"
|
||||
| "assign"
|
||||
| "alias"
|
||||
| "always"
|
||||
| "always_comb"
|
||||
| "always_ff"
|
||||
| "always_latch"
|
||||
| "initial"
|
||||
| "final"
|
||||
| "begin"
|
||||
| "end"
|
||||
| "if"
|
||||
| "else"
|
||||
| "case"
|
||||
| "casex"
|
||||
| "casez"
|
||||
// `randcase` is reserved (IEEE 1800 Table B.1) and opens a
|
||||
// statement of its own; `let` is reserved too and opens an item.
|
||||
// Both are here rather than matched by text so a declaration can
|
||||
// never accidentally be named after one.
|
||||
| "randcase"
|
||||
| "let"
|
||||
| "endcase"
|
||||
| "default"
|
||||
| "unique"
|
||||
| "unique0"
|
||||
| "priority"
|
||||
| "for"
|
||||
| "while"
|
||||
| "repeat"
|
||||
| "forever"
|
||||
| "do"
|
||||
| "foreach"
|
||||
| "break"
|
||||
| "continue"
|
||||
| "return"
|
||||
| "fork"
|
||||
| "join"
|
||||
| "join_any"
|
||||
| "join_none"
|
||||
| "disable"
|
||||
| "wait"
|
||||
| "posedge"
|
||||
| "negedge"
|
||||
| "edge"
|
||||
| "or"
|
||||
| "and"
|
||||
| "nand"
|
||||
| "nor"
|
||||
| "xor"
|
||||
| "xnor"
|
||||
| "not"
|
||||
| "buf"
|
||||
| "bufif0"
|
||||
| "bufif1"
|
||||
| "notif0"
|
||||
| "notif1"
|
||||
| "pullup"
|
||||
| "pulldown"
|
||||
| "generate"
|
||||
| "endgenerate"
|
||||
| "genvar"
|
||||
// Class-related reserved words. `new`, `this`, `super` and `null`
|
||||
// are deliberately *not* here: they stand in expression position
|
||||
// and already parse as ordinary names, so promoting them to
|
||||
// keywords would break `x = new(...)` and `super.f()` for nothing.
|
||||
| "class"
|
||||
| "endclass"
|
||||
| "extends"
|
||||
| "implements"
|
||||
| "virtual"
|
||||
| "pure"
|
||||
| "extern"
|
||||
| "rand"
|
||||
| "randc"
|
||||
| "local"
|
||||
| "protected"
|
||||
| "constraint"
|
||||
| "covergroup"
|
||||
| "endgroup"
|
||||
| "function"
|
||||
| "endfunction"
|
||||
| "task"
|
||||
| "endtask"
|
||||
| "automatic"
|
||||
| "static"
|
||||
| "const"
|
||||
| "import"
|
||||
| "export"
|
||||
| "timeunit"
|
||||
| "timeprecision"
|
||||
| "assert"
|
||||
| "assume"
|
||||
| "cover"
|
||||
| "property"
|
||||
| "endproperty"
|
||||
| "sequence"
|
||||
| "endsequence"
|
||||
| "inside"
|
||||
| "with"
|
||||
| "iff"
|
||||
| "release"
|
||||
| "force"
|
||||
| "deassign"
|
||||
| "primitive"
|
||||
| "endprimitive"
|
||||
| "specify"
|
||||
| "endspecify"
|
||||
| "table"
|
||||
| "endtable"
|
||||
| "small"
|
||||
| "medium"
|
||||
| "large"
|
||||
| "scalared"
|
||||
| "vectored"
|
||||
| "highz0"
|
||||
| "highz1"
|
||||
| "strong0"
|
||||
| "strong1"
|
||||
| "pull0"
|
||||
| "pull1"
|
||||
| "weak0"
|
||||
| "weak1"
|
||||
| "cmos"
|
||||
| "rcmos"
|
||||
| "nmos"
|
||||
| "pmos"
|
||||
| "rnmos"
|
||||
| "rpmos"
|
||||
| "tran"
|
||||
| "rtran"
|
||||
| "tranif0"
|
||||
| "tranif1"
|
||||
| "rtranif0"
|
||||
| "rtranif1"
|
||||
)
|
||||
/// Every reserved word the lexer knows, in the order they are written
|
||||
/// below. Verilog-2001 plus the SystemVerilog subset the parser understands;
|
||||
/// words not listed here lex as plain identifiers.
|
||||
///
|
||||
/// [`is_keyword`] and this slice are generated from the same list, so a word
|
||||
/// can never be one without being the other — which is what lets a completion
|
||||
/// list offer exactly what the lexer reserves.
|
||||
macro_rules! keywords {
|
||||
($($word:literal),* $(,)?) => {
|
||||
pub const KEYWORDS: &[&str] = &[$($word),*];
|
||||
|
||||
/// Whether `s` is one of [`KEYWORDS`].
|
||||
pub fn is_keyword(s: &str) -> bool {
|
||||
matches!(s, $($word)|*)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
keywords![
|
||||
"module",
|
||||
"macromodule",
|
||||
"endmodule",
|
||||
"interface",
|
||||
"endinterface",
|
||||
"package",
|
||||
"endpackage",
|
||||
"program",
|
||||
"endprogram",
|
||||
"modport",
|
||||
"clocking",
|
||||
"endclocking",
|
||||
"input",
|
||||
"output",
|
||||
"inout",
|
||||
"ref",
|
||||
"wire",
|
||||
"tri",
|
||||
"tri0",
|
||||
"tri1",
|
||||
"wand",
|
||||
"wor",
|
||||
"trireg",
|
||||
"supply0",
|
||||
"supply1",
|
||||
"uwire",
|
||||
"reg",
|
||||
"logic",
|
||||
"bit",
|
||||
"byte",
|
||||
"shortint",
|
||||
"int",
|
||||
"longint",
|
||||
"integer",
|
||||
"time",
|
||||
"realtime",
|
||||
"real",
|
||||
"shortreal",
|
||||
"string",
|
||||
"chandle",
|
||||
"event",
|
||||
"var",
|
||||
"void",
|
||||
"signed",
|
||||
"unsigned",
|
||||
"packed",
|
||||
"parameter",
|
||||
"localparam",
|
||||
"defparam",
|
||||
"specparam",
|
||||
"type",
|
||||
"typedef",
|
||||
"enum",
|
||||
"struct",
|
||||
"union",
|
||||
"assign",
|
||||
"alias",
|
||||
"always",
|
||||
"always_comb",
|
||||
"always_ff",
|
||||
"always_latch",
|
||||
"initial",
|
||||
"final",
|
||||
"begin",
|
||||
"end",
|
||||
"if",
|
||||
"else",
|
||||
"case",
|
||||
"casex",
|
||||
"casez",
|
||||
// `randcase` is reserved (IEEE 1800 Table B.1) and opens a
|
||||
// statement of its own; `let` is reserved too and opens an item.
|
||||
// Both are here rather than matched by text so a declaration can
|
||||
// never accidentally be named after one.
|
||||
"randcase",
|
||||
"let",
|
||||
"endcase",
|
||||
"default",
|
||||
"unique",
|
||||
"unique0",
|
||||
"priority",
|
||||
"for",
|
||||
"while",
|
||||
"repeat",
|
||||
"forever",
|
||||
"do",
|
||||
"foreach",
|
||||
"break",
|
||||
"continue",
|
||||
"return",
|
||||
"fork",
|
||||
"join",
|
||||
"join_any",
|
||||
"join_none",
|
||||
"disable",
|
||||
"wait",
|
||||
"posedge",
|
||||
"negedge",
|
||||
"edge",
|
||||
"or",
|
||||
"and",
|
||||
"nand",
|
||||
"nor",
|
||||
"xor",
|
||||
"xnor",
|
||||
"not",
|
||||
"buf",
|
||||
"bufif0",
|
||||
"bufif1",
|
||||
"notif0",
|
||||
"notif1",
|
||||
"pullup",
|
||||
"pulldown",
|
||||
"generate",
|
||||
"endgenerate",
|
||||
"genvar",
|
||||
// Class-related reserved words. `new`, `this`, `super` and `null`
|
||||
// are deliberately *not* here: they stand in expression position
|
||||
// and already parse as ordinary names, so promoting them to
|
||||
// keywords would break `x = new(...)` and `super.f()` for nothing.
|
||||
"class",
|
||||
"endclass",
|
||||
"extends",
|
||||
"implements",
|
||||
"virtual",
|
||||
"pure",
|
||||
"extern",
|
||||
"rand",
|
||||
"randc",
|
||||
"local",
|
||||
"protected",
|
||||
"constraint",
|
||||
"covergroup",
|
||||
"endgroup",
|
||||
"function",
|
||||
"endfunction",
|
||||
"task",
|
||||
"endtask",
|
||||
"automatic",
|
||||
"static",
|
||||
"const",
|
||||
"import",
|
||||
"export",
|
||||
"timeunit",
|
||||
"timeprecision",
|
||||
"assert",
|
||||
"assume",
|
||||
"cover",
|
||||
"property",
|
||||
"endproperty",
|
||||
"sequence",
|
||||
"endsequence",
|
||||
"inside",
|
||||
"with",
|
||||
"iff",
|
||||
"release",
|
||||
"force",
|
||||
"deassign",
|
||||
"primitive",
|
||||
"endprimitive",
|
||||
"specify",
|
||||
"endspecify",
|
||||
"table",
|
||||
"endtable",
|
||||
"small",
|
||||
"medium",
|
||||
"large",
|
||||
"scalared",
|
||||
"vectored",
|
||||
"highz0",
|
||||
"highz1",
|
||||
"strong0",
|
||||
"strong1",
|
||||
"pull0",
|
||||
"pull1",
|
||||
"weak0",
|
||||
"weak1",
|
||||
"cmos",
|
||||
"rcmos",
|
||||
"nmos",
|
||||
"pmos",
|
||||
"rnmos",
|
||||
"rpmos",
|
||||
"tran",
|
||||
"rtran",
|
||||
"tranif0",
|
||||
"tranif1",
|
||||
"rtranif0",
|
||||
"rtranif1",
|
||||
];
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn the_slice_and_the_predicate_are_the_same_list() {
|
||||
// They are generated from one `keywords!` invocation, so this can only
|
||||
// fail if the macro is edited into disagreeing with itself — which is
|
||||
// exactly the drift the macro exists to prevent.
|
||||
assert!(KEYWORDS.iter().all(|word| is_keyword(word)));
|
||||
assert!(!is_keyword("clk"), "an ordinary name is not reserved");
|
||||
assert!(!is_keyword("new"), "deliberately not a keyword, see the list");
|
||||
assert!(!KEYWORDS.contains(&"clk"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_keyword_is_listed_twice() {
|
||||
let mut sorted = KEYWORDS.to_vec();
|
||||
sorted.sort_unstable();
|
||||
let mut unique = sorted.clone();
|
||||
unique.dedup();
|
||||
assert_eq!(sorted, unique, "a duplicate entry would double a completion list");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user