//! Token definitions for the Verilog/SystemVerilog lexer. use crate::span::TextRange; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum TokenKind { /// Identifier, including escaped identifiers (`\foo`). Ident, /// System identifier such as `$display`. SystemIdent, /// Reserved keyword (the text field holds which one). Keyword, /// Any numeric literal, including sized/based literals (`8'hFF`) and time /// literals (`10ns`). The text is normalized to contain no whitespace. Number, /// String literal, including the surrounding quotes. String, /// Compiler directive: from a backtick to the end of the (possibly /// continued) line, e.g. `` `timescale 1ns / 1ps `` or `` `ifdef FOO ``. Directive, /// Attribute instance `(* ... *)`, stored verbatim. Attribute, /// Operator or punctuation. Symbol, /// End of file. Eof, } /// A comment that has been detached from the token stream. #[derive(Debug, Clone)] pub struct Comment { /// Full text including the `//` or `/* */` markers. Never contains a /// trailing newline. pub text: String, /// True if at least one blank line separated this comment from whatever /// preceded it. pub blank_before: bool, /// 1-based source line of the opening `//` or `/*`. pub line: u32, /// 1-based source column of the opening `//` or `/*`. The formatter /// dedents the continuation lines of a multi-line comment by this much /// before re-indenting them, which is what keeps re-formatting a fixed /// point. pub col: u32, /// Byte extent in the source text, markers included. Trivia spans live /// here rather than on AST nodes: a node's span is its code extent, and /// the comments attached to it are outside that. pub span: TextRange, } #[derive(Debug, Clone)] pub struct Token { pub kind: TokenKind, pub text: String, /// 1-based source line of the first character. pub line: u32, /// 1-based source column of the first character. pub col: u32, /// Byte extent in the source text. `source[span] == text` for every kind /// except [`TokenKind::Number`], whose text drops the whitespace a sized /// literal may write around its tick (`8 'hFF` → `8'hFF`). pub span: TextRange, /// Comments that appeared before this token (on their own lines, or on /// earlier lines). pub leading: Vec, /// A comment that appeared after this token on the same source line. /// Several such comments are merged into one, joined by a space. pub trailing: Option, /// True if at least one blank line separated this token from the previous /// token or comment. pub blank_before: bool, } impl Token { pub fn is_kw(&self, kw: &str) -> bool { self.kind == TokenKind::Keyword && self.text == kw } pub fn is_sym(&self, sym: &str) -> bool { self.kind == TokenKind::Symbol && self.text == sym } /// True for a macro usage token: a backtick name that is not one of the /// line-oriented compiler directives (those lex as [`TokenKind::Directive`]). /// The text is the whole invocation, argument list included. pub fn is_macro(&self) -> bool { self.kind == TokenKind::Ident && self.text.starts_with('`') } /// True if this macro usage carries an argument list: `` `NAME(...) ``. pub fn has_macro_args(&self) -> bool { self.is_macro() && self.text.contains('(') } } /// 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" | "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" ) }