moore_svlog_syntax/
token.rs

1// Copyright (c) 2016-2021 Fabian Schuiki
2
3//! Defines all tokens that may result from performing lexical analysis on a
4//! SystemVerilog source file. This module is inspired heavily by the tokens
5//! used in the Rust compiler.
6
7pub use self::DelimToken::*;
8pub use self::Lit::*;
9pub use self::Token::*;
10use moore_common::name::Name;
11use std::fmt::{Display, Formatter, Result};
12
13/// A primary token emitted by the lexer.
14#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
15pub enum Token {
16    // Symbols
17    Comma,
18    Period,
19    Colon,
20    Semicolon,
21    At,
22    Hashtag,
23    DoubleHashtag,
24    Namespace,
25    Ternary,
26    AddColon,
27    SubColon,
28    Apostrophe,
29    Dollar,
30
31    Operator(Op),
32
33    /// An opening delimiter
34    OpenDelim(DelimToken),
35    /// A closing delimiter
36    CloseDelim(DelimToken),
37
38    /// A literal
39    Literal(Lit),
40    /// A system task or function identifier, e.g. "$display"
41    SysIdent(Name),
42    /// A compiler directive, e.g. "`timescale"
43    CompDir(Name),
44    /// An identifier
45    Ident(Name),
46    /// An escaped identifier
47    EscIdent(Name),
48    /// An unsigned number
49    // UnsignedNumber(Name),
50    /// A keyword
51    Keyword(Kw),
52
53    // The end of the input file
54    Eof,
55}
56
57impl Token {
58    pub fn as_str(self) -> &'static str {
59        match self {
60            // Symbols
61            Comma => ",",
62            Period => ".",
63            Colon => ":",
64            Semicolon => ";",
65            At => "@",
66            Hashtag => "#",
67            DoubleHashtag => "##",
68            Namespace => "::",
69            Ternary => "?",
70            AddColon => "+:",
71            SubColon => "-:",
72            Apostrophe => "'",
73            Dollar => "$",
74
75            Operator(op) => op.as_str(),
76
77            // Opening and closing delimiters
78            OpenDelim(Paren) => "(",
79            OpenDelim(Brack) => "[",
80            OpenDelim(Brace) => "{",
81            OpenDelim(Bgend) => "begin",
82            CloseDelim(Paren) => ")",
83            CloseDelim(Brack) => "]",
84            CloseDelim(Brace) => "}",
85            CloseDelim(Bgend) => "end",
86
87            Keyword(kw) => kw.as_str(),
88
89            Literal(_) => "literal",
90            SysIdent(_) => "system identifier",
91            CompDir(_) => "compiler directive",
92            Ident(_) => "identifier",
93            EscIdent(_) => "escaped identifier",
94
95            Eof => "end of file",
96        }
97    }
98}
99
100impl Display for Token {
101    fn fmt(&self, f: &mut Formatter) -> Result {
102        write!(f, "{}", self.as_str())
103    }
104}
105
106/// A delimiter token such as parentheses or brackets.
107#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
108pub enum DelimToken {
109    /// A round paranthesis `(` or `)`
110    Paren,
111    /// A square bracket `[` or `]`
112    Brack,
113    /// A curly brace `{` or `}`
114    Brace,
115    /// A `begin` or `end`
116    Bgend,
117}
118
119/// Abstract literals such as strings.
120#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
121pub enum Lit {
122    Str(Name),
123    BasedInteger(Option<Name>, bool, char, Name),
124    /// One of `'0`, `'1`, `'x`, and `'z`.
125    UnbasedUnsized(char),
126    /// A number given as integer and optional fractional part.
127    Number(Name, Option<Name>),
128    /// A time literal given as integer part, fractional part, and unit.
129    Time(Name, Option<Name>, TimeUnit),
130}
131
132/// The unit of a time literal.
133#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
134pub enum TimeUnit {
135    Second,
136    MilliSecond,
137    MicroSecond,
138    NanoSecond,
139    PicoSecond,
140    FemtoSecond,
141}
142
143/// Operator symbols.
144#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
145pub enum Op {
146    // Assignment
147    Assign,
148    AssignAdd,
149    AssignSub,
150    AssignMul,
151    AssignDiv,
152    AssignMod,
153    AssignBitAnd,
154    AssignBitOr,
155    AssignBitXor,
156    AssignLogicShL,
157    AssignLogicShR,
158    AssignArithShL,
159    AssignArithShR,
160
161    // Arithmetic
162    Add,
163    Sub,
164    Mul,
165    Div,
166    Mod,
167    Pow,
168    Inc,
169    Dec,
170
171    // Equality
172    LogicEq,
173    LogicNeq,
174    CaseEq,
175    CaseNeq,
176    WildcardEq,
177    WildcardNeq,
178
179    // Relational
180    Lt,
181    Leq,
182    Gt,
183    Geq,
184
185    // Logic
186    LogicNot,
187    LogicAnd,
188    LogicOr,
189    LogicImpl,
190    LogicEquiv,
191
192    // Bitwise
193    BitNot,
194    BitAnd,
195    BitNand,
196    BitOr,
197    BitNor,
198    BitXor,
199    BitXnor,
200    BitNxor,
201
202    // Shift
203    LogicShL,
204    LogicShR,
205    ArithShL,
206    ArithShR,
207
208    // Sequence
209    SeqImplOl,
210    SeqImplNol,
211    SeqFollowOl,
212    SeqFollowNol,
213}
214
215impl Op {
216    pub fn as_str(self) -> &'static str {
217        match self {
218            // Assignment
219            Op::Assign => "=",
220            Op::AssignAdd => "+=",
221            Op::AssignSub => "-=",
222            Op::AssignMul => "*=",
223            Op::AssignDiv => "/=",
224            Op::AssignMod => "%=",
225            Op::AssignBitAnd => "&=",
226            Op::AssignBitOr => "|=",
227            Op::AssignBitXor => "^=",
228            Op::AssignLogicShL => "<<=",
229            Op::AssignLogicShR => ">>=",
230            Op::AssignArithShL => "<<<=",
231            Op::AssignArithShR => ">>>=",
232
233            // Arithmetic
234            Op::Add => "+",
235            Op::Sub => "-",
236            Op::Mul => "*",
237            Op::Div => "/",
238            Op::Mod => "%",
239            Op::Pow => "**",
240            Op::Inc => "++",
241            Op::Dec => "--",
242
243            // Equality
244            Op::LogicEq => "==",
245            Op::LogicNeq => "!=",
246            Op::CaseEq => "===",
247            Op::CaseNeq => "!==",
248            Op::WildcardEq => "==?",
249            Op::WildcardNeq => "!=?",
250
251            // Relational
252            Op::Lt => "<",
253            Op::Leq => "<=",
254            Op::Gt => ">",
255            Op::Geq => ">=",
256
257            // Logic
258            Op::LogicNot => "!",
259            Op::LogicAnd => "&&",
260            Op::LogicOr => "||",
261            Op::LogicImpl => "->",
262            Op::LogicEquiv => "<->",
263
264            // Bitwise
265            Op::BitNot => "~",
266            Op::BitAnd => "&",
267            Op::BitNand => "~&",
268            Op::BitOr => "|",
269            Op::BitNor => "~|",
270            Op::BitXor => "^",
271            Op::BitXnor => "^~",
272            Op::BitNxor => "~^",
273
274            // Shift
275            Op::LogicShL => "<<",
276            Op::LogicShR => ">>",
277            Op::ArithShL => "<<<",
278            Op::ArithShR => ">>>",
279
280            // Sequence
281            Op::SeqImplOl => "|->",
282            Op::SeqImplNol => "|=>",
283            Op::SeqFollowOl => "#-#",
284            Op::SeqFollowNol => "#=#",
285        }
286    }
287
288    pub fn get_precedence(self) -> Precedence {
289        match self {
290            // Assignment
291            Op::Assign
292            | Op::AssignAdd
293            | Op::AssignSub
294            | Op::AssignMul
295            | Op::AssignDiv
296            | Op::AssignMod
297            | Op::AssignBitAnd
298            | Op::AssignBitOr
299            | Op::AssignBitXor
300            | Op::AssignLogicShL
301            | Op::AssignLogicShR
302            | Op::AssignArithShL
303            | Op::AssignArithShR => Precedence::Assignment,
304
305            // Arithmetic
306            Op::Add | Op::Sub => Precedence::Add,
307            Op::Mul | Op::Div | Op::Mod => Precedence::Mul,
308            Op::Pow => Precedence::Pow,
309            Op::Inc | Op::Dec => Precedence::Unary,
310
311            // Equality
312            Op::LogicEq
313            | Op::LogicNeq
314            | Op::CaseEq
315            | Op::CaseNeq
316            | Op::WildcardEq
317            | Op::WildcardNeq => Precedence::Equality,
318
319            // Relational
320            Op::Lt | Op::Leq | Op::Gt | Op::Geq => Precedence::Relational,
321
322            // Logic
323            Op::LogicNot => Precedence::Unary,
324            Op::LogicAnd => Precedence::LogicAnd,
325            Op::LogicOr => Precedence::LogicOr,
326            Op::LogicImpl | Op::LogicEquiv => Precedence::Implication,
327
328            // Bitwise
329            Op::BitNot => Precedence::Unary,
330            Op::BitAnd => Precedence::BitAnd,
331            Op::BitNand => Precedence::Unary,
332            Op::BitOr => Precedence::BitOr,
333            Op::BitNor => Precedence::Unary,
334            Op::BitXor | Op::BitXnor | Op::BitNxor => Precedence::BitXor,
335
336            // Shift
337            Op::LogicShL | Op::LogicShR | Op::ArithShL | Op::ArithShR => Precedence::Shift,
338
339            // Sequence
340            Op::SeqImplOl | Op::SeqImplNol | Op::SeqFollowOl | Op::SeqFollowNol => Precedence::Max,
341        }
342    }
343}
344
345impl Display for Op {
346    fn fmt(&self, f: &mut Formatter) -> Result {
347        write!(f, "{}", self.as_str())
348    }
349}
350
351/// Expression precedence. Note that a few kinds of expression are
352/// right-associative rather than the default left-associative.
353#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
354pub enum Precedence {
355    Min,
356    MinTypMax,
357    Concatenation, // no associativity
358    Assignment,    // no associativity
359    Implication,   // right-associative
360    Ternary,       // right-associative
361    LogicOr,
362    LogicAnd,
363    BitOr,
364    BitXor,
365    BitAnd,
366    Equality,
367    Relational,
368    Shift,
369    Add,
370    Mul,
371    Pow,
372    Unary,
373    Postfix,
374    Scope,
375    Max,
376}
377
378macro_rules! declare_keywords {(
379    $( ($konst: ident, $string: expr) )*
380) => {
381    #[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Debug, Hash)]
382    pub enum Kw {
383        $($konst,)*
384    }
385
386    impl Kw {
387        pub fn as_str(self) -> &'static str {
388            match self {
389                $(Kw::$konst => $string,)*
390            }
391        }
392    }
393
394    impl Display for Kw {
395        fn fmt(&self, f: &mut Formatter) -> Result {
396            write!(f, "{}", self.as_str())
397        }
398    }
399
400    pub fn find_keyword<S: AsRef<str>>(name: S) -> Option<Kw> {
401        use std::collections::HashMap;
402        use once_cell::sync::Lazy;
403        static TBL: Lazy<HashMap<&'static str, Kw>> = Lazy::new(|| {
404            let mut tbl = HashMap::new();
405            $(tbl.insert($string, Kw::$konst);)*
406            tbl
407        });
408        TBL.get(name.as_ref()).map(|kw| *kw)
409    }
410}}
411
412declare_keywords! {
413    // Keywords as per IEEE 1800-2009 Table B.1
414    (AcceptOn,             "accept_on")
415    (Alias,                "alias")
416    (Always,               "always")
417    (AlwaysComb,           "always_comb")
418    (AlwaysFf,             "always_ff")
419    (AlwaysLatch,          "always_latch")
420    (And,                  "and")
421    (Assert,               "assert")
422    (Assign,               "assign")
423    (Assume,               "assume")
424    (Automatic,            "automatic")
425    (Before,               "before")
426    (Begin,                "begin")
427    (Bind,                 "bind")
428    (Bins,                 "bins")
429    (Binsof,               "binsof")
430    (Bit,                  "bit")
431    (Break,                "break")
432    (Buf,                  "buf")
433    (Bufif0,               "bufif0")
434    (Bufif1,               "bufif1")
435    (Byte,                 "byte")
436    (Case,                 "case")
437    (Casex,                "casex")
438    (Casez,                "casez")
439    (Cell,                 "cell")
440    (Chandle,              "chandle")
441    (Checker,              "checker")
442    (Class,                "class")
443    (Clocking,             "clocking")
444    (Cmos,                 "cmos")
445    (Config,               "config")
446    (Const,                "const")
447    (Constraint,           "constraint")
448    (Context,              "context")
449    (Continue,             "continue")
450    (Cover,                "cover")
451    (Covergroup,           "covergroup")
452    (Coverpoint,           "coverpoint")
453    (Cross,                "cross")
454    (Deassign,             "deassign")
455    (Default,              "default")
456    (Defparam,             "defparam")
457    (Design,               "design")
458    (Disable,              "disable")
459    (Dist,                 "dist")
460    (Do,                   "do")
461    (Edge,                 "edge")
462    (Else,                 "else")
463    (End,                  "end")
464    (Endcase,              "endcase")
465    (Endchecker,           "endchecker")
466    (Endclass,             "endclass")
467    (Endclocking,          "endclocking")
468    (Endconfig,            "endconfig")
469    (Endfunction,          "endfunction")
470    (Endgenerate,          "endgenerate")
471    (Endgroup,             "endgroup")
472    (Endinterface,         "endinterface")
473    (Endmodule,            "endmodule")
474    (Endpackage,           "endpackage")
475    (Endprimitive,         "endprimitive")
476    (Endprogram,           "endprogram")
477    (Endproperty,          "endproperty")
478    (Endsequence,          "endsequence")
479    (Endspecify,           "endspecify")
480    (Endtable,             "endtable")
481    (Endtask,              "endtask")
482    (Enum,                 "enum")
483    (Event,                "event")
484    (Eventually,           "eventually")
485    (Expect,               "expect")
486    (Export,               "export")
487    (Extends,              "extends")
488    (Extern,               "extern")
489    (Final,                "final")
490    (FirstMatch,           "first_match")
491    (For,                  "for")
492    (Force,                "force")
493    (Foreach,              "foreach")
494    (Forever,              "forever")
495    (Fork,                 "fork")
496    (Forkjoin,             "forkjoin")
497    (Function,             "function")
498    (Generate,             "generate")
499    (Genvar,               "genvar")
500    (Global,               "global")
501    (Highz0,               "highz0")
502    (Highz1,               "highz1")
503    (If,                   "if")
504    (Iff,                  "iff")
505    (Ifnone,               "ifnone")
506    (IgnoreBins,           "ignore_bins")
507    (IllegalBins,          "illegal_bins")
508    (Implements,           "implements")
509    (Implies,              "implies")
510    (Import,               "import")
511    (Incdir,               "incdir")
512    (Include,              "include")
513    (Initial,              "initial")
514    (Inout,                "inout")
515    (Input,                "input")
516    (Inside,               "inside")
517    (Instance,             "instance")
518    (Int,                  "int")
519    (Integer,              "integer")
520    (Interconnect,         "interconnect")
521    (Interface,            "interface")
522    (Intersect,            "intersect")
523    (Join,                 "join")
524    (JoinAny,              "join_any")
525    (JoinNone,             "join_none")
526    (Large,                "large")
527    (Let,                  "let")
528    (Liblist,              "liblist")
529    (Library,              "library")
530    (Local,                "local")
531    (Localparam,           "localparam")
532    (Logic,                "logic")
533    (Longint,              "longint")
534    (Macromodule,          "macromodule")
535    (Matches,              "matches")
536    (Medium,               "medium")
537    (Modport,              "modport")
538    (Module,               "module")
539    (Nand,                 "nand")
540    (Negedge,              "negedge")
541    (Nettype,              "nettype")
542    (New,                  "new")
543    (Nexttime,             "nexttime")
544    (Nmos,                 "nmos")
545    (Nor,                  "nor")
546    (Noshowcancelled,      "noshowcancelled")
547    (Not,                  "not")
548    (Notif0,               "notif0")
549    (Notif1,               "notif1")
550    (Null,                 "null")
551    (Or,                   "or")
552    (Output,               "output")
553    (Package,              "package")
554    (Packed,               "packed")
555    (Parameter,            "parameter")
556    (Pmos,                 "pmos")
557    (Posedge,              "posedge")
558    (Primitive,            "primitive")
559    (Priority,             "priority")
560    (Program,              "program")
561    (Property,             "property")
562    (Protected,            "protected")
563    (Pull0,                "pull0")
564    (Pull1,                "pull1")
565    (Pulldown,             "pulldown")
566    (Pullup,               "pullup")
567    (PulsestyleOndetect,   "pulsestyle_ondetect")
568    (PulsestyleOnevent,    "pulsestyle_onevent")
569    (Pure,                 "pure")
570    (Rand,                 "rand")
571    (Randc,                "randc")
572    (Randcase,             "randcase")
573    (Randsequence,         "randsequence")
574    (Rcmos,                "rcmos")
575    (Real,                 "real")
576    (Realtime,             "realtime")
577    (Ref,                  "ref")
578    (Reg,                  "reg")
579    (RejectOn,             "reject_on")
580    (Release,              "release")
581    (Repeat,               "repeat")
582    (Restrict,             "restrict")
583    (Return,               "return")
584    (Rnmos,                "rnmos")
585    (Rpmos,                "rpmos")
586    (Rtran,                "rtran")
587    (Rtranif0,             "rtranif0")
588    (Rtranif1,             "rtranif1")
589    (SAlways,              "s_always")
590    (SEventually,          "s_eventually")
591    (SNexttime,            "s_nexttime")
592    (SUntil,               "s_until")
593    (SUntilWith,           "s_until_with")
594    (Scalared,             "scalared")
595    (Sequence,             "sequence")
596    (Shortint,             "shortint")
597    (Shortreal,            "shortreal")
598    (Showcancelled,        "showcancelled")
599    (Signed,               "signed")
600    (Small,                "small")
601    (Soft,                 "soft")
602    (Solve,                "solve")
603    (Specify,              "specify")
604    (Specparam,            "specparam")
605    (Static,               "static")
606    (String,               "string")
607    (Strong,               "strong")
608    (Strong0,              "strong0")
609    (Strong1,              "strong1")
610    (Struct,               "struct")
611    (Super,                "super")
612    (Supply0,              "supply0")
613    (Supply1,              "supply1")
614    (SyncAcceptOn,         "sync_accept_on")
615    (SyncRejectOn,         "sync_reject_on")
616    (Table,                "table")
617    (Tagged,               "tagged")
618    (Task,                 "task")
619    (This,                 "this")
620    (Throughout,           "throughout")
621    (Time,                 "time")
622    (Timeprecision,        "timeprecision")
623    (Timeunit,             "timeunit")
624    (Tran,                 "tran")
625    (Tranif0,              "tranif0")
626    (Tranif1,              "tranif1")
627    (Tri,                  "tri")
628    (Tri0,                 "tri0")
629    (Tri1,                 "tri1")
630    (Triand,               "triand")
631    (Trior,                "trior")
632    (Trireg,               "trireg")
633    (Type,                 "type")
634    (Typedef,              "typedef")
635    (Union,                "union")
636    (Unique,               "unique")
637    (Unique0,              "unique0")
638    (Unsigned,             "unsigned")
639    (Until,                "until")
640    (UntilWith,            "until_with")
641    (Untyped,              "untyped")
642    (Use,                  "use")
643    (Uwire,                "uwire")
644    (Var,                  "var")
645    (Vectored,             "vectored")
646    (Virtual,              "virtual")
647    (Void,                 "void")
648    (Wait,                 "wait")
649    (WaitOrder,            "wait_order")
650    (Wand,                 "wand")
651    (Weak,                 "weak")
652    (Weak0,                "weak0")
653    (Weak1,                "weak1")
654    (While,                "while")
655    (Wildcard,             "wildcard")
656    (Wire,                 "wire")
657    (With,                 "with")
658    (Within,               "within")
659    (Wor,                  "wor")
660    (Xnor,                 "xnor")
661    (Xor,                  "xor")
662}