Skip to main content

veryl_parser/
token_range.rs

1use crate::resource_table::{PathId, TokenId};
2use crate::veryl_grammar_trait::*;
3use crate::veryl_token::{Token, TokenSource, VerylToken};
4use paste::paste;
5
6#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub struct TokenRange {
8    pub beg: Token,
9    pub end: Token,
10}
11
12impl TokenRange {
13    pub fn new(beg: &VerylToken, end: &VerylToken) -> Self {
14        Self {
15            beg: beg.token,
16            end: end.token,
17        }
18    }
19
20    pub fn from_range(beg: &TokenRange, end: &TokenRange) -> Self {
21        Self {
22            beg: beg.beg,
23            end: end.end,
24        }
25    }
26
27    pub fn include(&self, path: PathId, line: u32, column: u32) -> bool {
28        if self.beg.source == path {
29            if self.beg.line == line {
30                if self.end.line == line {
31                    self.beg.column <= column && column <= self.end.column
32                } else {
33                    self.beg.column <= column
34                }
35            } else if self.end.line == line {
36                column <= self.end.column
37            } else {
38                self.beg.line < line && line < self.end.line
39            }
40        } else {
41            false
42        }
43    }
44
45    pub fn offset(&mut self, value: u32) {
46        self.beg.pos += value;
47        self.end.pos += value;
48    }
49
50    pub fn set_beg(&mut self, value: TokenRange) {
51        self.beg = value.beg;
52    }
53
54    pub fn set_end(&mut self, value: TokenRange) {
55        self.end = value.end;
56    }
57
58    pub fn source(&self) -> TokenSource {
59        self.beg.source
60    }
61}
62
63impl From<&TokenRange> for miette::SourceSpan {
64    fn from(x: &TokenRange) -> Self {
65        let length = (x.end.pos - x.beg.pos + x.end.length) as usize;
66        (x.beg.pos as usize, length).into()
67    }
68}
69
70impl From<TokenRange> for miette::SourceSpan {
71    fn from(x: TokenRange) -> Self {
72        let length = (x.end.pos - x.beg.pos + x.end.length) as usize;
73        (x.beg.pos as usize, length).into()
74    }
75}
76
77impl From<Token> for TokenRange {
78    fn from(value: Token) -> Self {
79        let beg = value;
80        let end = value;
81        TokenRange { beg, end }
82    }
83}
84
85impl From<&Token> for TokenRange {
86    fn from(value: &Token) -> Self {
87        let beg = *value;
88        let end = *value;
89        TokenRange { beg, end }
90    }
91}
92
93impl From<&VerylToken> for TokenRange {
94    fn from(value: &VerylToken) -> Self {
95        let beg = value.token;
96        let end = value.token;
97        TokenRange { beg, end }
98    }
99}
100
101pub trait TokenExt {
102    fn range(&self) -> TokenRange;
103    fn first(&self) -> Token {
104        self.range().beg
105    }
106    fn last(&self) -> Token {
107        self.range().end
108    }
109    fn id(&self) -> TokenId {
110        self.first().id
111    }
112    fn line(&self) -> u32 {
113        self.first().line
114    }
115}
116
117macro_rules! impl_token_ext {
118    ($typename:ty) => {
119        impl TokenExt for $typename {
120            fn range(&self) -> TokenRange {
121                self.into()
122            }
123        }
124    };
125}
126
127macro_rules! impl_token_range {
128    ($typename:ty, $first:ident, $last:ident) => {
129        impl From<&$typename> for TokenRange {
130            fn from(value: &$typename) -> Self {
131                let beg: TokenRange = value.$first.as_ref().into();
132                let end: TokenRange = value.$last.as_ref().into();
133                TokenRange {
134                    beg: beg.beg,
135                    end: end.end,
136                }
137            }
138        }
139        impl_token_ext!($typename);
140    };
141    ($typename:ty, $first:ident) => {
142        impl From<&$typename> for TokenRange {
143            fn from(value: &$typename) -> Self {
144                value.$first.as_ref().into()
145            }
146        }
147        impl_token_ext!($typename);
148    };
149}
150
151macro_rules! impl_token_range_singular {
152    ($typename:ty) => {
153        paste! {
154            impl From<&$typename> for TokenRange {
155                fn from(value: &$typename) -> Self {
156                    let beg = value.[<$typename:snake _token>].token;
157                    let end = beg;
158                    TokenRange { beg, end }
159                }
160            }
161            impl_token_ext!($typename);
162        }
163    };
164}
165
166macro_rules! impl_token_range_enum {
167    ($typename:ty, $( $x:ident ),*) => {
168        paste! {
169            impl From<&$typename> for TokenRange {
170                fn from(value: &$typename) -> Self {
171                    match value {
172                        $(
173                            $typename::[<$x:camel>](x) => x.$x.as_ref().into()
174                        ),*
175                    }
176                }
177            }
178            impl_token_ext!($typename);
179        }
180    };
181}
182
183macro_rules! expression_token_range {
184    ($typename:ty, $beg:ident, $list:ident, $prev:ident) => {
185        impl From<&$typename> for TokenRange {
186            fn from(value: &$typename) -> Self {
187                let beg: TokenRange = value.$beg.as_ref().into();
188                let end = if value.$list.is_empty() {
189                    beg.end
190                } else {
191                    let last = value.$list.last().unwrap();
192                    let end: TokenRange = last.$prev.as_ref().into();
193                    end.end
194                };
195                let beg = beg.beg;
196                TokenRange { beg, end }
197            }
198        }
199        impl_token_ext!($typename);
200    };
201}
202
203macro_rules! impl_token_range_list {
204    ($typename:ty, $item:ty) => {
205        paste! {
206            impl From<&$typename> for TokenRange {
207                fn from(value: &$typename) -> Self {
208                    let mut ret: TokenRange = value.[<$item:snake>].as_ref().into();
209                    if let Some(x) = value.[<$typename:snake _list>].last() {
210                        let end: TokenRange = x.[<$item:snake>].as_ref().into();
211                        ret.end = end.end;
212                    }
213                    if let Some(x) = &value.[<$typename:snake _opt>] {
214                        let end: TokenRange = x.comma.as_ref().into();
215                        ret.end = end.end;
216                    }
217                    ret
218                }
219            }
220            impl_token_ext!($typename);
221        }
222    };
223}
224
225macro_rules! impl_token_range_group {
226    ($typename:ty, $item:ty) => {
227        paste! {
228            impl From<&$typename> for TokenRange {
229                fn from(value: &$typename) -> Self {
230                    let mut ret: TokenRange = match value.[<$typename:snake _group>].as_ref() {
231                        [<$typename Group>]::[<LBrace $typename GroupListRBrace>](x) => {
232                            let beg = x.l_brace.l_brace_token.token;
233                            let end = x.r_brace.r_brace_token.token;
234                            TokenRange { beg, end }
235                        }
236                        [<$typename Group>]::$item(x) => x.[<$item:snake>].as_ref().into(),
237                    };
238                    if let Some(x) = value.[<$typename:snake _list>].first() {
239                        let beg: TokenRange = x.attribute.as_ref().into();
240                        ret.beg = beg.beg;
241                    }
242                    ret
243                }
244            }
245            impl_token_ext!($typename);
246        }
247    };
248    ($typename:ty, $list:ty, $item:ty) => {
249        paste! {
250            impl From<&$typename> for TokenRange {
251                fn from(value: &$typename) -> Self {
252                    let mut ret: TokenRange = match value.[<$typename:snake _group>].as_ref() {
253                        [<$typename Group>]::[<LBrace $list RBrace>](x) => {
254                            let beg = x.l_brace.l_brace_token.token;
255                            let end = x.r_brace.r_brace_token.token;
256                            TokenRange { beg, end }
257                        }
258                        [<$typename Group>]::$item(x) => x.[<$item:snake>].as_ref().into(),
259                    };
260                    if let Some(x) = value.[<$typename:snake _list>].first() {
261                        let beg: TokenRange = x.attribute.as_ref().into();
262                        ret.beg = beg.beg;
263                    }
264                    ret
265                }
266            }
267            impl_token_ext!($typename);
268        }
269    };
270}
271
272// ----------------------------------------------------------------------------
273// VerylToken
274// ----------------------------------------------------------------------------
275
276// Start
277impl_token_range_singular!(Start);
278
279// StringLiteral
280impl_token_range_singular!(StringLiteral);
281
282// Number
283impl_token_range_singular!(Exponent);
284impl_token_range_singular!(FixedPoint);
285impl_token_range_singular!(Based);
286impl_token_range_singular!(BaseLess);
287impl_token_range_singular!(AllBit);
288
289// Operator
290impl_token_range_singular!(AssignmentOperator);
291impl_token_range_singular!(DiamondOperator);
292impl_token_range_singular!(Operator01);
293impl_token_range_singular!(Operator02);
294impl_token_range_singular!(Operator03);
295impl_token_range_singular!(Operator04);
296impl_token_range_singular!(Operator05);
297impl_token_range_singular!(Operator06);
298impl_token_range_singular!(Operator07);
299impl_token_range_singular!(Operator08);
300impl_token_range_singular!(UnaryOperator);
301
302// Symbol
303impl_token_range_singular!(Colon);
304impl_token_range_singular!(ColonColonLAngle);
305impl_token_range_singular!(ColonColon);
306impl_token_range_singular!(Comma);
307impl_token_range_singular!(DotDot);
308impl_token_range_singular!(DotDotEqu);
309impl_token_range_singular!(Dot);
310impl_token_range_singular!(Equ);
311impl_token_range_singular!(HashLBracket);
312impl_token_range_singular!(Hash);
313impl_token_range_singular!(Question);
314impl_token_range_singular!(Quote);
315impl_token_range_singular!(QuoteLBrace);
316impl_token_range_singular!(LAngle);
317impl_token_range_singular!(EmbedLBrace);
318impl_token_range_singular!(EscapedLBrace);
319impl_token_range_singular!(TripleLBrace);
320impl_token_range_singular!(LBrace);
321impl_token_range_singular!(LBracket);
322impl_token_range_singular!(LParen);
323impl_token_range_singular!(LTMinus);
324impl_token_range_singular!(MinusColon);
325impl_token_range_singular!(MinusGT);
326impl_token_range_singular!(PlusColon);
327impl_token_range_singular!(RAngle);
328impl_token_range_singular!(EmbedRBrace);
329impl_token_range_singular!(EscapedRBrace);
330impl_token_range_singular!(TripleRBrace);
331impl_token_range_singular!(RBrace);
332impl_token_range_singular!(RBracket);
333impl_token_range_singular!(RParen);
334impl_token_range_singular!(Semicolon);
335impl_token_range_singular!(Star);
336
337// Keyword
338impl_token_range_singular!(Alias);
339impl_token_range_singular!(AlwaysComb);
340impl_token_range_singular!(AlwaysFf);
341impl_token_range_singular!(As);
342impl_token_range_singular!(Assign);
343impl_token_range_singular!(Bind);
344impl_token_range_singular!(Bit);
345impl_token_range_singular!(BBool);
346impl_token_range_singular!(LBool);
347impl_token_range_singular!(Break);
348impl_token_range_singular!(Case);
349impl_token_range_singular!(Clock);
350impl_token_range_singular!(ClockPosedge);
351impl_token_range_singular!(ClockNegedge);
352impl_token_range_singular!(Connect);
353impl_token_range_singular!(Const);
354impl_token_range_singular!(Converse);
355
356impl From<&Defaul> for TokenRange {
357    fn from(value: &Defaul) -> Self {
358        let beg = value.default_token.token;
359        let end = beg;
360        TokenRange { beg, end }
361    }
362}
363impl_token_ext!(Defaul);
364
365impl_token_range_singular!(Else);
366impl_token_range_singular!(Embed);
367impl_token_range_singular!(Enum);
368impl_token_range_singular!(F32);
369impl_token_range_singular!(F64);
370impl_token_range_singular!(False);
371impl_token_range_singular!(Final);
372impl_token_range_singular!(For);
373impl_token_range_singular!(Function);
374impl_token_range_singular!(I8);
375impl_token_range_singular!(I16);
376impl_token_range_singular!(I32);
377impl_token_range_singular!(I64);
378impl_token_range_singular!(If);
379impl_token_range_singular!(IfReset);
380impl_token_range_singular!(Import);
381impl_token_range_singular!(In);
382impl_token_range_singular!(Include);
383impl_token_range_singular!(Initial);
384impl_token_range_singular!(Inout);
385impl_token_range_singular!(Input);
386impl_token_range_singular!(Inside);
387impl_token_range_singular!(Inst);
388impl_token_range_singular!(Interface);
389impl_token_range_singular!(Let);
390impl_token_range_singular!(Logic);
391impl_token_range_singular!(Lsb);
392impl_token_range_singular!(Modport);
393impl_token_range_singular!(Module);
394impl_token_range_singular!(Msb);
395impl_token_range_singular!(Output);
396impl_token_range_singular!(Outside);
397impl_token_range_singular!(Package);
398impl_token_range_singular!(Param);
399impl_token_range_singular!(Proto);
400impl_token_range_singular!(Pub);
401impl_token_range_singular!(Repeat);
402impl_token_range_singular!(Reset);
403impl_token_range_singular!(ResetAsyncHigh);
404impl_token_range_singular!(ResetAsyncLow);
405impl_token_range_singular!(ResetSyncHigh);
406impl_token_range_singular!(ResetSyncLow);
407impl_token_range_singular!(Return);
408impl_token_range_singular!(Same);
409impl_token_range_singular!(Signed);
410impl_token_range_singular!(Step);
411
412impl From<&Strin> for TokenRange {
413    fn from(value: &Strin) -> Self {
414        let beg = value.string_token.token;
415        let end = beg;
416        TokenRange { beg, end }
417    }
418}
419impl_token_ext!(Strin);
420
421impl_token_range_singular!(Struct);
422impl_token_range_singular!(Switch);
423impl_token_range_singular!(Tri);
424impl_token_range_singular!(True);
425impl_token_range_singular!(Type);
426impl_token_range_singular!(P8);
427impl_token_range_singular!(P16);
428impl_token_range_singular!(P32);
429impl_token_range_singular!(P64);
430impl_token_range_singular!(U8);
431impl_token_range_singular!(U16);
432impl_token_range_singular!(U32);
433impl_token_range_singular!(U64);
434impl_token_range_singular!(Union);
435impl_token_range_singular!(Unsafe);
436impl_token_range_singular!(Var);
437
438// Identifier
439impl_token_range_singular!(DollarIdentifier);
440impl_token_range_singular!(Identifier);
441
442impl_token_range_singular!(Any);
443
444// ----------------------------------------------------------------------------
445// Number
446// ----------------------------------------------------------------------------
447
448impl_token_range_enum!(Number, integral_number, real_number);
449impl_token_range_enum!(IntegralNumber, based, base_less, all_bit);
450impl_token_range_enum!(RealNumber, fixed_point, exponent);
451
452// ----------------------------------------------------------------------------
453// Complex Identifier
454// ----------------------------------------------------------------------------
455
456impl From<&HierarchicalIdentifier> for TokenRange {
457    fn from(value: &HierarchicalIdentifier) -> Self {
458        let mut ret: TokenRange = value.identifier.as_ref().into();
459        if let Some(x) = value.hierarchical_identifier_list.last() {
460            ret.set_end(x.select.as_ref().into());
461        }
462        if let Some(x) = value.hierarchical_identifier_list0.last() {
463            ret.set_end(x.identifier.as_ref().into());
464            if let Some(x) = x.hierarchical_identifier_list0_list.last() {
465                ret.set_end(x.select.as_ref().into());
466            }
467        }
468        ret
469    }
470}
471impl_token_ext!(HierarchicalIdentifier);
472
473impl From<&ScopedIdentifier> for TokenRange {
474    fn from(value: &ScopedIdentifier) -> Self {
475        let mut ret: TokenRange = match value.scoped_identifier_group.as_ref() {
476            ScopedIdentifierGroup::DollarIdentifier(x) => x.dollar_identifier.as_ref().into(),
477            ScopedIdentifierGroup::IdentifierScopedIdentifierOpt(x) => {
478                let mut ret: TokenRange = x.identifier.as_ref().into();
479                if let Some(x) = &x.scoped_identifier_opt {
480                    ret.set_end(x.with_generic_argument.as_ref().into());
481                }
482                ret
483            }
484        };
485        if let Some(x) = value.scoped_identifier_list.last() {
486            ret.set_end(x.identifier.as_ref().into());
487            if let Some(x) = &x.scoped_identifier_opt0 {
488                ret.set_end(x.with_generic_argument.as_ref().into());
489            }
490        }
491        ret
492    }
493}
494impl_token_ext!(ScopedIdentifier);
495
496impl From<&ExpressionIdentifier> for TokenRange {
497    fn from(value: &ExpressionIdentifier) -> Self {
498        let mut ret: TokenRange = value.scoped_identifier.as_ref().into();
499        if let Some(x) = &value.expression_identifier_opt {
500            ret.set_end(x.width.as_ref().into());
501        }
502        if let Some(x) = &value.expression_identifier_list.last() {
503            ret.set_end(x.select.as_ref().into());
504        }
505        if let Some(x) = &value.expression_identifier_list0.last() {
506            ret.set_end(x.identifier.as_ref().into());
507            if let Some(x) = &x.expression_identifier_list0_list.last() {
508                ret.set_end(x.select.as_ref().into());
509            }
510        }
511        ret
512    }
513}
514impl_token_ext!(ExpressionIdentifier);
515
516impl From<&GenericArgIdentifier> for TokenRange {
517    fn from(value: &GenericArgIdentifier) -> Self {
518        let mut ret: TokenRange = value.scoped_identifier.as_ref().into();
519        if let Some(x) = &value.generic_arg_identifier_list.last() {
520            ret.set_end(x.identifier.as_ref().into());
521        }
522        ret
523    }
524}
525impl_token_ext!(GenericArgIdentifier);
526
527// ----------------------------------------------------------------------------
528// Expression
529// ----------------------------------------------------------------------------
530
531impl_token_range!(Expression, if_expression);
532
533impl From<&IfExpression> for TokenRange {
534    fn from(value: &IfExpression) -> Self {
535        let mut ret: TokenRange = value.expression01.as_ref().into();
536        if let Some(x) = value.if_expression_list.first() {
537            ret.set_beg(x.r#if.as_ref().into());
538        }
539        ret
540    }
541}
542impl_token_ext!(IfExpression);
543
544expression_token_range!(Expression01, expression02, expression01_list, expression02);
545
546impl From<&Expression02> for TokenRange {
547    fn from(value: &Expression02) -> Self {
548        let mut ret: TokenRange = value.factor.as_ref().into();
549        if let Some(ref x) = value.expression02_opt {
550            ret.set_end(x.casting_type.as_ref().into());
551        };
552        if let Some(x) = value.expression02_list.first() {
553            ret.set_beg(match x.expression02_op.as_ref() {
554                Expression02Op::UnaryOperator(x) => x.unary_operator.as_ref().into(),
555                Expression02Op::Operator06(x) => x.operator06.as_ref().into(),
556                Expression02Op::Operator05(x) => x.operator05.as_ref().into(),
557                Expression02Op::Operator03(x) => x.operator03.as_ref().into(),
558                Expression02Op::Operator04(x) => x.operator04.as_ref().into(),
559            });
560        }
561        ret
562    }
563}
564impl_token_ext!(Expression02);
565
566impl From<&Factor> for TokenRange {
567    fn from(value: &Factor) -> Self {
568        match value {
569            Factor::Number(x) => x.number.as_ref().into(),
570            Factor::BooleanLiteral(x) => x.boolean_literal.as_ref().into(),
571            Factor::IdentifierFactor(x) => {
572                x.identifier_factor.expression_identifier.as_ref().into()
573            }
574            Factor::LParenExpressionRParen(x) => {
575                let beg = x.l_paren.l_paren_token.token;
576                let end = x.r_paren.r_paren_token.token;
577                TokenRange { beg, end }
578            }
579            Factor::LBraceConcatenationListRBrace(x) => {
580                let beg = x.l_brace.l_brace_token.token;
581                let end = x.r_brace.r_brace_token.token;
582                TokenRange { beg, end }
583            }
584            Factor::QuoteLBraceArrayLiteralListRBrace(x) => {
585                let beg = x.quote_l_brace.quote_l_brace_token.token;
586                let end = x.r_brace.r_brace_token.token;
587                TokenRange { beg, end }
588            }
589            Factor::CaseExpression(x) => x.case_expression.as_ref().into(),
590            Factor::SwitchExpression(x) => x.switch_expression.as_ref().into(),
591            Factor::StringLiteral(x) => x.string_literal.as_ref().into(),
592            Factor::FactorGroup(x) => match x.factor_group.as_ref() {
593                FactorGroup::Msb(x) => x.msb.as_ref().into(),
594                FactorGroup::Lsb(x) => x.lsb.as_ref().into(),
595            },
596            Factor::InsideExpression(x) => x.inside_expression.as_ref().into(),
597            Factor::OutsideExpression(x) => x.outside_expression.as_ref().into(),
598            Factor::TypeExpression(x) => x.type_expression.as_ref().into(),
599            Factor::FactorTypeFactor(x) => x.factor_type_factor.as_ref().into(),
600        }
601    }
602}
603impl_token_ext!(Factor);
604
605impl_token_range_enum!(BooleanLiteral, r#true, r#false);
606
607impl From<&IdentifierFactor> for TokenRange {
608    fn from(value: &IdentifierFactor) -> Self {
609        let mut ret: TokenRange = value.expression_identifier.as_ref().into();
610        if let Some(x) = &value.identifier_factor_opt {
611            ret.set_end(match x.identifier_factor_opt_group.as_ref() {
612                IdentifierFactorOptGroup::FunctionCall(x) => x.function_call.as_ref().into(),
613                IdentifierFactorOptGroup::StructConstructor(x) => {
614                    x.struct_constructor.as_ref().into()
615                }
616            });
617        }
618        ret
619    }
620}
621impl_token_ext!(IdentifierFactor);
622
623impl From<&FactorTypeFactor> for TokenRange {
624    fn from(value: &FactorTypeFactor) -> Self {
625        let beg: TokenRange = if let Some(x) = value.factor_type_factor_list.first() {
626            x.type_modifier.as_ref().into()
627        } else {
628            value.factor_type.as_ref().into()
629        };
630        let end: TokenRange = value.factor_type.as_ref().into();
631        TokenRange {
632            beg: beg.beg,
633            end: end.end,
634        }
635    }
636}
637impl_token_ext!(FactorTypeFactor);
638
639impl_token_range!(FunctionCall, l_paren, r_paren);
640impl_token_range_list!(ArgumentList, ArgumentItem);
641
642impl From<&ArgumentItem> for TokenRange {
643    fn from(value: &ArgumentItem) -> Self {
644        let mut ret: TokenRange = value.argument_expression.as_ref().into();
645        if let Some(x) = &value.argument_item_opt {
646            ret.set_end(x.expression.as_ref().into());
647        }
648        ret
649    }
650}
651impl_token_ext!(ArgumentItem);
652
653impl_token_range!(ArgumentExpression, expression);
654impl_token_range!(StructConstructor, quote_l_brace, r_brace);
655impl_token_range_list!(StructConstructorList, StructConstructorItem);
656impl_token_range!(StructConstructorItem, identifier, expression);
657impl_token_range_list!(ConcatenationList, ConcatenationItem);
658
659impl From<&ConcatenationItem> for TokenRange {
660    fn from(value: &ConcatenationItem) -> Self {
661        let mut ret: TokenRange = value.expression.as_ref().into();
662        if let Some(x) = &value.concatenation_item_opt {
663            ret.set_end(x.expression.as_ref().into());
664        }
665        ret
666    }
667}
668impl_token_ext!(ConcatenationItem);
669
670impl_token_range_list!(ArrayLiteralList, ArrayLiteralItem);
671
672impl From<&ArrayLiteralItem> for TokenRange {
673    fn from(value: &ArrayLiteralItem) -> Self {
674        match value.array_literal_item_group.as_ref() {
675            ArrayLiteralItemGroup::ExpressionArrayLiteralItemOpt(x) => {
676                let mut ret: TokenRange = x.expression.as_ref().into();
677                if let Some(x) = &x.array_literal_item_opt {
678                    ret.set_end(x.expression.as_ref().into());
679                }
680                ret
681            }
682            ArrayLiteralItemGroup::DefaulColonExpression(x) => {
683                let beg: TokenRange = x.defaul.as_ref().into();
684                let end: TokenRange = x.expression.as_ref().into();
685                TokenRange {
686                    beg: beg.beg,
687                    end: end.end,
688                }
689            }
690        }
691    }
692}
693impl_token_ext!(ArrayLiteralItem);
694
695impl_token_range!(CaseExpression, case, r_brace);
696impl_token_range!(SwitchExpression, switch, r_brace);
697impl_token_range!(TypeExpression, r#type, r_paren);
698impl_token_range!(InsideExpression, inside, r_brace);
699impl_token_range!(OutsideExpression, outside, r_brace);
700impl_token_range_list!(RangeList, RangeItem);
701impl_token_range!(RangeItem, range);
702
703// ----------------------------------------------------------------------------
704// Select / Width / Array / Range
705// ----------------------------------------------------------------------------
706
707impl_token_range!(Select, l_bracket, r_bracket);
708impl_token_range_enum!(SelectOperator, colon, plus_colon, minus_colon, step);
709impl_token_range!(Width, l_angle, r_angle);
710impl_token_range!(Array, l_bracket, r_bracket);
711
712impl From<&Range> for TokenRange {
713    fn from(value: &Range) -> Self {
714        let mut ret: TokenRange = value.expression.as_ref().into();
715        if let Some(x) = &value.range_opt {
716            ret.set_end(x.expression.as_ref().into());
717        }
718        ret
719    }
720}
721impl_token_ext!(Range);
722
723impl_token_range_enum!(RangeOperator, dot_dot, dot_dot_equ);
724
725// ----------------------------------------------------------------------------
726// ScalarType / ArrayType / CastingType
727// ----------------------------------------------------------------------------
728
729impl_token_range_enum!(
730    FixedType, p8, p16, p32, p64, u8, u16, u32, u64, i32, i8, i16, i64, f32, f64, b_bool, l_bool,
731    strin
732);
733impl_token_range_enum!(
734    VariableType,
735    clock,
736    clock_posedge,
737    clock_negedge,
738    reset,
739    reset_async_high,
740    reset_async_low,
741    reset_sync_high,
742    reset_sync_low,
743    logic,
744    bit
745);
746impl_token_range!(UserDefinedType, scoped_identifier);
747impl_token_range_enum!(TypeModifier, tri, signed, defaul);
748
749impl From<&FactorType> for TokenRange {
750    fn from(value: &FactorType) -> Self {
751        match value.factor_type_group.as_ref() {
752            FactorTypeGroup::VariableTypeFactorTypeOpt(x) => {
753                let mut ret: TokenRange = x.variable_type.as_ref().into();
754                if let Some(ref x) = x.factor_type_opt {
755                    ret.set_end(x.width.as_ref().into());
756                }
757                ret
758            }
759            FactorTypeGroup::FixedType(x) => x.fixed_type.as_ref().into(),
760        }
761    }
762}
763impl_token_ext!(FactorType);
764
765impl From<&ScalarType> for TokenRange {
766    fn from(value: &ScalarType) -> Self {
767        let mut ret: TokenRange = match &*value.scalar_type_group {
768            ScalarTypeGroup::UserDefinedTypeScalarTypeOpt(x) => {
769                let mut ret: TokenRange = x.user_defined_type.as_ref().into();
770                if let Some(x) = &x.scalar_type_opt {
771                    ret.set_end(x.width.as_ref().into());
772                }
773                ret
774            }
775            ScalarTypeGroup::FactorType(x) => x.factor_type.as_ref().into(),
776        };
777
778        if let Some(x) = value.scalar_type_list.first() {
779            ret.set_beg(x.type_modifier.as_ref().into());
780        }
781
782        ret
783    }
784}
785impl_token_ext!(ScalarType);
786
787impl From<&ArrayType> for TokenRange {
788    fn from(value: &ArrayType) -> Self {
789        let mut ret: TokenRange = value.scalar_type.as_ref().into();
790        if let Some(x) = &value.array_type_opt {
791            ret.set_end(x.array.as_ref().into());
792        }
793        ret
794    }
795}
796impl_token_ext!(ArrayType);
797
798impl_token_range_enum!(
799    CastingType,
800    p8,
801    p16,
802    p32,
803    p64,
804    u8,
805    u16,
806    u32,
807    u64,
808    i8,
809    i16,
810    i32,
811    i64,
812    f32,
813    f64,
814    b_bool,
815    l_bool,
816    clock,
817    clock_posedge,
818    clock_negedge,
819    reset,
820    reset_async_high,
821    reset_async_low,
822    reset_sync_high,
823    reset_sync_low,
824    user_defined_type,
825    based,
826    base_less
827);
828
829// ----------------------------------------------------------------------------
830// ClockDomain
831// ----------------------------------------------------------------------------
832
833impl_token_range!(ClockDomain, quote, identifier);
834
835// ----------------------------------------------------------------------------
836// Statement
837// ----------------------------------------------------------------------------
838
839impl_token_range!(StatementBlock, l_brace, r_brace);
840
841impl From<&StatementBlockGroup> for TokenRange {
842    fn from(value: &StatementBlockGroup) -> Self {
843        let mut ret: TokenRange = match value.statement_block_group_group.as_ref() {
844            StatementBlockGroupGroup::BlockLBraceStatementBlockGroupGroupListRBrace(x) => {
845                let beg = x.block.block_token.token;
846                let end = x.r_brace.r_brace_token.token;
847                TokenRange { beg, end }
848            }
849            StatementBlockGroupGroup::StatementBlockItem(x) => {
850                x.statement_block_item.as_ref().into()
851            }
852        };
853        if let Some(x) = value.statement_block_group_list.first() {
854            let beg: TokenRange = x.attribute.as_ref().into();
855            ret.beg = beg.beg;
856        }
857        ret
858    }
859}
860impl_token_ext!(StatementBlockGroup);
861
862impl_token_range_enum!(
863    StatementBlockItem,
864    var_declaration,
865    let_statement,
866    statement,
867    const_declaration,
868    concatenation_assignment
869);
870impl_token_range_enum!(
871    Statement,
872    identifier_statement,
873    if_statement,
874    if_reset_statement,
875    return_statement,
876    break_statement,
877    for_statement,
878    case_statement,
879    switch_statement
880);
881impl_token_range!(LetStatement, r#let, semicolon);
882impl_token_range!(ConcatenationAssignment, l_brace, semicolon);
883impl_token_range!(IdentifierStatement, expression_identifier, semicolon);
884
885impl From<&Assignment> for TokenRange {
886    fn from(value: &Assignment) -> Self {
887        let mut ret: TokenRange = match value.assignment_group.as_ref() {
888            AssignmentGroup::Equ(x) => x.equ.as_ref().into(),
889            AssignmentGroup::AssignmentOperator(x) => x.assignment_operator.as_ref().into(),
890            AssignmentGroup::DiamondOperator(x) => x.diamond_operator.as_ref().into(),
891        };
892        ret.set_end(value.expression.as_ref().into());
893        ret
894    }
895}
896impl_token_ext!(Assignment);
897
898impl From<&IfStatement> for TokenRange {
899    fn from(value: &IfStatement) -> Self {
900        let mut ret: TokenRange = value.r#if.as_ref().into();
901        ret.set_end(value.statement_block.as_ref().into());
902        if let Some(x) = value.if_statement_list.last() {
903            ret.set_end(x.statement_block.as_ref().into());
904        }
905        if let Some(x) = &value.if_statement_opt {
906            ret.set_end(x.statement_block.as_ref().into());
907        }
908        ret
909    }
910}
911impl_token_ext!(IfStatement);
912
913impl From<&IfStatementList> for TokenRange {
914    fn from(value: &IfStatementList) -> Self {
915        let mut ret: TokenRange = value.r#else.as_ref().into();
916        ret.set_end(value.statement_block.as_ref().into());
917        ret
918    }
919}
920impl_token_ext!(IfStatementList);
921
922impl From<&IfResetStatement> for TokenRange {
923    fn from(value: &IfResetStatement) -> Self {
924        let mut ret: TokenRange = value.if_reset.as_ref().into();
925        ret.set_end(value.statement_block.as_ref().into());
926        if let Some(x) = value.if_reset_statement_list.last() {
927            ret.set_end(x.statement_block.as_ref().into());
928        }
929        if let Some(x) = &value.if_reset_statement_opt {
930            ret.set_end(x.statement_block.as_ref().into());
931        }
932        ret
933    }
934}
935impl_token_ext!(IfResetStatement);
936
937impl From<&IfResetStatementList> for TokenRange {
938    fn from(value: &IfResetStatementList) -> Self {
939        let mut ret: TokenRange = value.r#else.as_ref().into();
940        ret.set_end(value.statement_block.as_ref().into());
941        ret
942    }
943}
944impl_token_ext!(IfResetStatementList);
945
946impl_token_range!(ReturnStatement, r#return, semicolon);
947impl_token_range!(BreakStatement, r#break, semicolon);
948impl_token_range!(ForStatement, r#for, statement_block);
949impl_token_range!(CaseStatement, r#case, r_brace);
950
951impl From<&CaseItem> for TokenRange {
952    fn from(value: &CaseItem) -> Self {
953        let mut ret: TokenRange = match value.case_item_group.as_ref() {
954            CaseItemGroup::CaseCondition(x) => x.case_condition.as_ref().into(),
955            CaseItemGroup::Defaul(x) => x.defaul.as_ref().into(),
956        };
957        match value.case_item_group0.as_ref() {
958            CaseItemGroup0::Statement(x) => {
959                ret.set_end(x.statement.as_ref().into());
960            }
961            CaseItemGroup0::StatementBlock(x) => {
962                ret.set_end(x.statement_block.as_ref().into());
963            }
964        }
965        ret
966    }
967}
968impl_token_ext!(CaseItem);
969
970impl From<&CaseCondition> for TokenRange {
971    fn from(value: &CaseCondition) -> Self {
972        let mut ret: TokenRange = value.range_item.as_ref().into();
973        if let Some(x) = value.case_condition_list.last() {
974            ret.set_end(x.range_item.as_ref().into());
975        }
976        ret
977    }
978}
979impl_token_ext!(CaseCondition);
980
981impl_token_range!(SwitchStatement, switch, r_brace);
982
983impl From<&SwitchItem> for TokenRange {
984    fn from(value: &SwitchItem) -> Self {
985        let mut ret: TokenRange = match value.switch_item_group.as_ref() {
986            SwitchItemGroup::SwitchCondition(x) => x.switch_condition.as_ref().into(),
987            SwitchItemGroup::Defaul(x) => x.defaul.as_ref().into(),
988        };
989        match value.switch_item_group0.as_ref() {
990            SwitchItemGroup0::Statement(x) => {
991                ret.set_end(x.statement.as_ref().into());
992            }
993            SwitchItemGroup0::StatementBlock(x) => {
994                ret.set_end(x.statement_block.as_ref().into());
995            }
996        }
997        ret
998    }
999}
1000impl_token_ext!(SwitchItem);
1001
1002impl From<&SwitchCondition> for TokenRange {
1003    fn from(value: &SwitchCondition) -> Self {
1004        let mut ret: TokenRange = value.expression.as_ref().into();
1005        if let Some(x) = value.switch_condition_list.last() {
1006            ret.set_end(x.expression.as_ref().into());
1007        }
1008        ret
1009    }
1010}
1011impl_token_ext!(SwitchCondition);
1012
1013// ----------------------------------------------------------------------------
1014// Attribute
1015// ----------------------------------------------------------------------------
1016
1017impl_token_range!(Attribute, hash_l_bracket, r_bracket);
1018impl_token_range_list!(AttributeList, AttributeItem);
1019impl_token_range_enum!(AttributeItem, identifier, string_literal);
1020
1021// ----------------------------------------------------------------------------
1022// Declaration
1023// ----------------------------------------------------------------------------
1024
1025impl_token_range!(LetDeclaration, r#let, semicolon);
1026impl_token_range!(VarDeclaration, var, semicolon);
1027impl_token_range!(ConstDeclaration, r#const, semicolon);
1028
1029impl From<&ConstDeclarationGroup> for TokenRange {
1030    fn from(value: &ConstDeclarationGroup) -> Self {
1031        match value {
1032            ConstDeclarationGroup::Type(x) => x.r#type.as_ref().into(),
1033            ConstDeclarationGroup::ArrayType(x) => x.array_type.as_ref().into(),
1034        }
1035    }
1036}
1037impl_token_ext!(ConstDeclarationGroup);
1038
1039impl_token_range!(TypeDefDeclaration, r#type, semicolon);
1040impl_token_range!(AlwaysFfDeclaration, always_ff, statement_block);
1041impl_token_range!(AlwaysFfEventList, l_paren, r_paren);
1042impl_token_range!(AlwaysFfClock, hierarchical_identifier);
1043impl_token_range!(AlwaysFfReset, hierarchical_identifier);
1044impl_token_range!(AlwaysCombDeclaration, always_comb, statement_block);
1045impl_token_range!(AssignDeclaration, assign, semicolon);
1046
1047impl From<&AssignDestination> for TokenRange {
1048    fn from(value: &AssignDestination) -> Self {
1049        match value {
1050            AssignDestination::HierarchicalIdentifier(x) => {
1051                x.hierarchical_identifier.as_ref().into()
1052            }
1053            AssignDestination::LBraceAssignConcatenationListRBrace(x) => {
1054                let beg = x.l_brace.l_brace_token.token;
1055                let end = x.r_brace.r_brace_token.token;
1056                TokenRange { beg, end }
1057            }
1058        }
1059    }
1060}
1061impl_token_ext!(AssignDestination);
1062
1063impl_token_range_list!(AssignConcatenationList, AssignConcatenationItem);
1064impl_token_range!(AssignConcatenationItem, hierarchical_identifier);
1065impl_token_range!(ConnectDeclaration, connect, semicolon);
1066impl_token_range!(ModportDeclaration, modport, r_brace);
1067impl_token_range_list!(ModportList, ModportGroup);
1068impl_token_range_group!(ModportGroup, ModportList, ModportItem);
1069impl_token_range!(ModportItem, identifier, direction);
1070
1071impl From<&ModportDefault> for TokenRange {
1072    fn from(value: &ModportDefault) -> Self {
1073        match value {
1074            ModportDefault::Input(x) => x.input.as_ref().into(),
1075            ModportDefault::Output(x) => x.output.as_ref().into(),
1076            ModportDefault::SameLParenModportDefaultListRParen(x) => {
1077                let beg = x.same.same_token.token;
1078                let end = x.r_paren.r_paren_token.token;
1079                TokenRange { beg, end }
1080            }
1081            ModportDefault::ConverseLParenModportDefaultListRParen(x) => {
1082                let beg = x.converse.converse_token.token;
1083                let end = x.r_paren.r_paren_token.token;
1084                TokenRange { beg, end }
1085            }
1086        }
1087    }
1088}
1089impl_token_ext!(ModportDefault);
1090
1091impl_token_range!(EnumDeclaration, r#enum, r_brace);
1092impl_token_range_list!(EnumList, EnumGroup);
1093impl_token_range_group!(EnumGroup, EnumList, EnumItem);
1094
1095impl From<&EnumItem> for TokenRange {
1096    fn from(value: &EnumItem) -> Self {
1097        let mut ret: TokenRange = value.identifier.as_ref().into();
1098        if let Some(x) = &value.enum_item_opt {
1099            ret.set_end(x.expression.as_ref().into());
1100        }
1101        ret
1102    }
1103}
1104impl_token_ext!(EnumItem);
1105
1106impl_token_range_enum!(StructUnion, r#struct, union);
1107impl_token_range!(StructUnionDeclaration, struct_union, r_brace);
1108impl_token_range_list!(StructUnionList, StructUnionGroup);
1109impl_token_range_group!(StructUnionGroup, StructUnionList, StructUnionItem);
1110impl_token_range!(StructUnionItem, identifier, scalar_type);
1111impl_token_range!(InitialDeclaration, initial, statement_block);
1112impl_token_range!(FinalDeclaration, r#final, statement_block);
1113
1114// ----------------------------------------------------------------------------
1115// InstDeclaration
1116// ----------------------------------------------------------------------------
1117
1118impl_token_range!(InstDeclaration, inst, semicolon);
1119impl_token_range!(BindDeclaration, bind, semicolon);
1120
1121impl From<&ComponentInstantiation> for TokenRange {
1122    fn from(value: &ComponentInstantiation) -> Self {
1123        let mut ret: TokenRange = value.identifier.as_ref().into();
1124        if let Some(x) = &value.component_instantiation_opt2 {
1125            ret.set_end(x.inst_port.as_ref().into());
1126        } else if let Some(x) = &value.component_instantiation_opt1 {
1127            ret.set_end(x.inst_parameter.as_ref().into());
1128        } else if let Some(x) = &value.component_instantiation_opt0 {
1129            ret.set_end(x.array.as_ref().into());
1130        } else {
1131            ret.set_end(value.scoped_identifier.as_ref().into());
1132        }
1133        ret
1134    }
1135}
1136
1137impl_token_range!(InstParameter, hash, r_paren);
1138impl_token_range_list!(InstParameterList, InstParameterGroup);
1139impl_token_range_group!(InstParameterGroup, InstParameterList, InstParameterItem);
1140
1141impl From<&InstParameterItem> for TokenRange {
1142    fn from(value: &InstParameterItem) -> Self {
1143        let mut ret: TokenRange = value.identifier.as_ref().into();
1144        if let Some(x) = &value.inst_parameter_item_opt {
1145            ret.set_end(x.expression.as_ref().into());
1146        }
1147        ret
1148    }
1149}
1150impl_token_ext!(InstParameterItem);
1151
1152impl_token_range!(InstPort, l_paren, r_paren);
1153impl_token_range_list!(InstPortList, InstPortGroup);
1154impl_token_range_group!(InstPortGroup, InstPortList, InstPortItem);
1155
1156impl From<&InstPortItem> for TokenRange {
1157    fn from(value: &InstPortItem) -> Self {
1158        let mut ret: TokenRange = value.identifier.as_ref().into();
1159        if let Some(x) = &value.inst_port_item_opt {
1160            ret.set_end(x.expression.as_ref().into());
1161        }
1162        ret
1163    }
1164}
1165impl_token_ext!(InstPortItem);
1166
1167// ----------------------------------------------------------------------------
1168// WithParameter
1169// ----------------------------------------------------------------------------
1170
1171impl_token_range!(WithParameter, hash, r_paren);
1172impl_token_range_list!(WithParameterList, WithParameterGroup);
1173impl_token_range_group!(WithParameterGroup, WithParameterList, WithParameterItem);
1174
1175impl From<&WithParameterItem> for TokenRange {
1176    fn from(value: &WithParameterItem) -> Self {
1177        let mut ret: TokenRange = match value.with_parameter_item_group.as_ref() {
1178            WithParameterItemGroup::Param(x) => x.param.as_ref().into(),
1179            WithParameterItemGroup::Const(x) => x.r#const.as_ref().into(),
1180        };
1181
1182        if let Some(x) = &value.with_parameter_item_opt {
1183            ret.set_end(x.expression.as_ref().into());
1184        } else {
1185            ret.set_end(value.with_parameter_item_group0.as_ref().into());
1186        }
1187
1188        ret
1189    }
1190}
1191impl_token_ext!(WithParameterItem);
1192impl_token_range_enum!(WithParameterItemGroup0, array_type, r#type);
1193
1194// ----------------------------------------------------------------------------
1195// WithGenericParameter
1196// ----------------------------------------------------------------------------
1197
1198impl From<&GenericBound> for TokenRange {
1199    fn from(value: &GenericBound) -> Self {
1200        match value {
1201            GenericBound::Type(x) => x.r#type.as_ref().into(),
1202            GenericBound::InstScopedIdentifier(x) => {
1203                let mut ret: TokenRange = x.inst.as_ref().into();
1204                ret.set_end(x.scoped_identifier.as_ref().into());
1205                ret
1206            }
1207            GenericBound::GenericProtoBound(x) => x.generic_proto_bound.as_ref().into(),
1208        }
1209    }
1210}
1211impl_token_ext!(GenericBound);
1212
1213impl_token_range!(WithGenericParameter, colon_colon_l_angle, r_angle);
1214impl_token_range_list!(WithGenericParameterList, WithGenericParameterItem);
1215
1216impl From<&WithGenericParameterItem> for TokenRange {
1217    fn from(value: &WithGenericParameterItem) -> Self {
1218        let mut ret: TokenRange = value.identifier.as_ref().into();
1219        ret.set_end(value.generic_bound.as_ref().into());
1220        if let Some(x) = &value.with_generic_parameter_item_opt {
1221            ret.set_end(x.with_generic_argument_item.as_ref().into());
1222        }
1223        ret
1224    }
1225}
1226impl_token_ext!(WithGenericParameterItem);
1227
1228impl_token_range_enum!(GenericProtoBound, scoped_identifier, fixed_type);
1229
1230// ----------------------------------------------------------------------------
1231// WithGenericArgument
1232// ----------------------------------------------------------------------------
1233
1234impl_token_range!(WithGenericArgument, colon_colon_l_angle, r_angle);
1235impl_token_range_list!(WithGenericArgumentList, WithGenericArgumentItem);
1236impl_token_range_enum!(
1237    WithGenericArgumentItem,
1238    generic_arg_identifier,
1239    fixed_type,
1240    number,
1241    boolean_literal
1242);
1243
1244// ----------------------------------------------------------------------------
1245// PortDeclaration
1246// ----------------------------------------------------------------------------
1247
1248impl_token_range!(PortDeclaration, l_paren, r_paren);
1249impl_token_range_list!(PortDeclarationList, PortDeclarationGroup);
1250impl_token_range_group!(
1251    PortDeclarationGroup,
1252    PortDeclarationList,
1253    PortDeclarationItem
1254);
1255
1256impl From<&PortDeclarationItem> for TokenRange {
1257    fn from(value: &PortDeclarationItem) -> Self {
1258        let mut ret: TokenRange = value.identifier.as_ref().into();
1259        match value.port_declaration_item_group.as_ref() {
1260            PortDeclarationItemGroup::PortTypeConcrete(x) => {
1261                ret.set_end(x.port_type_concrete.as_ref().into());
1262            }
1263            PortDeclarationItemGroup::PortTypeAbstract(x) => {
1264                ret.set_end(x.port_type_abstract.as_ref().into());
1265            }
1266        }
1267        ret
1268    }
1269}
1270impl_token_ext!(PortDeclarationItem);
1271
1272impl From<&PortDeclarationItemGroup> for TokenRange {
1273    fn from(value: &PortDeclarationItemGroup) -> Self {
1274        match value {
1275            PortDeclarationItemGroup::PortTypeConcrete(x) => x.port_type_concrete.as_ref().into(),
1276            PortDeclarationItemGroup::PortTypeAbstract(x) => x.port_type_abstract.as_ref().into(),
1277        }
1278    }
1279}
1280impl_token_ext!(PortDeclarationItemGroup);
1281
1282impl From<&PortTypeConcrete> for TokenRange {
1283    fn from(value: &PortTypeConcrete) -> Self {
1284        let mut ret: TokenRange = value.direction.as_ref().into();
1285        ret.set_end(value.array_type.as_ref().into());
1286        if let Some(x) = &value.port_type_concrete_opt0 {
1287            ret.set_end(x.port_default_value.as_ref().into());
1288        }
1289        ret
1290    }
1291}
1292impl_token_ext!(PortTypeConcrete);
1293
1294impl_token_range!(PortDefaultValue, expression);
1295
1296impl From<&PortTypeAbstract> for TokenRange {
1297    fn from(value: &PortTypeAbstract) -> Self {
1298        let mut ret: TokenRange = value.interface.as_ref().into();
1299        if let Some(x) = &value.port_type_abstract_opt {
1300            ret.set_beg(x.clock_domain.as_ref().into());
1301        }
1302        if let Some(x) = &value.port_type_abstract_opt0 {
1303            ret.set_beg(x.identifier.as_ref().into());
1304        }
1305        if let Some(x) = &value.port_type_abstract_opt1 {
1306            ret.set_beg(x.array.as_ref().into());
1307        }
1308        ret
1309    }
1310}
1311impl_token_ext!(PortTypeAbstract);
1312
1313impl_token_range_enum!(Direction, input, output, inout, modport, import);
1314
1315// ----------------------------------------------------------------------------
1316// Function
1317// ----------------------------------------------------------------------------
1318
1319impl_token_range!(FunctionDeclaration, function, statement_block);
1320
1321// ----------------------------------------------------------------------------
1322// Import
1323// ----------------------------------------------------------------------------
1324
1325impl_token_range!(ImportDeclaration, import, semicolon);
1326
1327// ----------------------------------------------------------------------------
1328// Unsafe
1329// ----------------------------------------------------------------------------
1330
1331impl_token_range!(UnsafeBlock, r#unsafe, r_brace);
1332
1333// ----------------------------------------------------------------------------
1334// Module/Interface
1335// ----------------------------------------------------------------------------
1336
1337impl_token_range!(ModuleDeclaration, module, r_brace);
1338impl_token_range_group!(ModuleGroup, ModuleItem);
1339impl_token_range!(ModuleItem, generate_item);
1340impl_token_range!(InterfaceDeclaration, interface, r_brace);
1341impl_token_range_group!(InterfaceGroup, InterfaceItem);
1342impl_token_range_enum!(InterfaceItem, generate_item, modport_declaration);
1343
1344impl From<&GenerateIfDeclaration> for TokenRange {
1345    fn from(value: &GenerateIfDeclaration) -> Self {
1346        let mut ret: TokenRange = value.r#if.as_ref().into();
1347        ret.set_end(value.generate_named_block.as_ref().into());
1348        if let Some(x) = value.generate_if_declaration_list.last() {
1349            ret.set_end(x.generate_optional_named_block.as_ref().into());
1350        }
1351        if let Some(x) = &value.generate_if_declaration_opt {
1352            ret.set_end(x.generate_optional_named_block.as_ref().into());
1353        }
1354        ret
1355    }
1356}
1357impl_token_ext!(GenerateIfDeclaration);
1358
1359impl_token_range!(
1360    GenerateIfDeclarationList,
1361    r#else,
1362    generate_optional_named_block
1363);
1364impl_token_range!(GenerateForDeclaration, r#for, generate_named_block);
1365impl_token_range!(GenerateBlockDeclaration, generate_named_block);
1366impl_token_range!(GenerateNamedBlock, colon, r_brace);
1367
1368impl From<&GenerateOptionalNamedBlock> for TokenRange {
1369    fn from(value: &GenerateOptionalNamedBlock) -> Self {
1370        let beg = value.l_brace.l_brace_token.token;
1371        let end = value.r_brace.r_brace_token.token;
1372        let mut ret = TokenRange { beg, end };
1373        if let Some(x) = &value.generate_optional_named_block_opt {
1374            ret.set_beg(x.colon.as_ref().into());
1375        }
1376        ret
1377    }
1378}
1379impl_token_ext!(GenerateOptionalNamedBlock);
1380
1381impl_token_range_group!(GenerateGroup, GenerateItem);
1382impl_token_range_enum!(
1383    GenerateItem,
1384    let_declaration,
1385    var_declaration,
1386    inst_declaration,
1387    bind_declaration,
1388    const_declaration,
1389    always_ff_declaration,
1390    always_comb_declaration,
1391    assign_declaration,
1392    connect_declaration,
1393    function_declaration,
1394    generate_if_declaration,
1395    generate_for_declaration,
1396    generate_block_declaration,
1397    type_def_declaration,
1398    enum_declaration,
1399    struct_union_declaration,
1400    import_declaration,
1401    alias_declaration,
1402    initial_declaration,
1403    final_declaration,
1404    unsafe_block,
1405    embed_declaration
1406);
1407
1408// ----------------------------------------------------------------------------
1409// Package
1410// ----------------------------------------------------------------------------
1411
1412impl_token_range!(PackageDeclaration, package, r_brace);
1413impl_token_range_group!(PackageGroup, PackageItem);
1414impl_token_range_enum!(
1415    PackageItem,
1416    const_declaration,
1417    type_def_declaration,
1418    enum_declaration,
1419    struct_union_declaration,
1420    function_declaration,
1421    import_declaration,
1422    alias_declaration,
1423    embed_declaration
1424);
1425
1426// ----------------------------------------------------------------------------
1427// Alias
1428// ----------------------------------------------------------------------------
1429
1430impl_token_range!(AliasDeclaration, alias, semicolon);
1431
1432// ----------------------------------------------------------------------------
1433// Proto
1434// ----------------------------------------------------------------------------
1435
1436impl From<&ProtoDeclaration> for TokenRange {
1437    fn from(value: &ProtoDeclaration) -> Self {
1438        let beg: TokenRange = value.proto.as_ref().into();
1439        let end: TokenRange = match &*value.proto_declaration_group {
1440            ProtoDeclarationGroup::ProtoModuleDeclaration(x) => {
1441                x.proto_module_declaration.as_ref().into()
1442            }
1443            ProtoDeclarationGroup::ProtoInterfaceDeclaration(x) => {
1444                x.proto_interface_declaration.as_ref().into()
1445            }
1446            ProtoDeclarationGroup::ProtoPackageDeclaration(x) => {
1447                x.proto_package_declaration.as_ref().into()
1448            }
1449        };
1450        TokenRange {
1451            beg: beg.beg,
1452            end: end.end,
1453        }
1454    }
1455}
1456impl_token_range!(ProtoModuleDeclaration, module, semicolon);
1457impl_token_range!(ProtoInterfaceDeclaration, interface, r_brace);
1458impl_token_range_enum!(
1459    ProtoInterfaceItem,
1460    var_declaration,
1461    proto_const_declaration,
1462    proto_function_declaration,
1463    proto_type_def_declaration,
1464    proto_alias_declaration,
1465    modport_declaration,
1466    import_declaration
1467);
1468impl_token_range!(ProtoPackageDeclaration, package, r_brace);
1469impl_token_range_enum!(
1470    ProtoPacakgeItem,
1471    proto_const_declaration,
1472    proto_type_def_declaration,
1473    enum_declaration,
1474    struct_union_declaration,
1475    proto_function_declaration,
1476    proto_alias_declaration,
1477    import_declaration
1478);
1479impl_token_range!(ProtoConstDeclaration, r#const, semicolon);
1480
1481impl From<&ProtoConstDeclarationGroup> for TokenRange {
1482    fn from(value: &ProtoConstDeclarationGroup) -> Self {
1483        match value {
1484            ProtoConstDeclarationGroup::Type(x) => x.r#type.as_ref().into(),
1485            ProtoConstDeclarationGroup::ArrayType(x) => x.array_type.as_ref().into(),
1486        }
1487    }
1488}
1489impl_token_ext!(ProtoConstDeclarationGroup);
1490
1491impl_token_range!(ProtoTypeDefDeclaration, r#type, semicolon);
1492impl_token_range!(ProtoFunctionDeclaration, function, semicolon);
1493impl_token_range!(ProtoAliasDeclaration, alias, semicolon);
1494
1495// ----------------------------------------------------------------------------
1496// Embed
1497// ----------------------------------------------------------------------------
1498
1499impl_token_range!(EmbedDeclaration, embed, embed_content);
1500impl_token_range!(EmbedContent, triple_l_brace, triple_r_brace);
1501impl_token_range!(EmbedScopedIdentifier, escaped_l_brace, escaped_r_brace);
1502
1503impl From<&EmbedItem> for TokenRange {
1504    fn from(value: &EmbedItem) -> Self {
1505        match value {
1506            EmbedItem::EmbedLBraceEmbedItemListEmbedRBrace(x) => {
1507                let beg: TokenRange = x.embed_l_brace.as_ref().into();
1508                let end: TokenRange = x.embed_r_brace.as_ref().into();
1509                TokenRange {
1510                    beg: beg.beg,
1511                    end: end.end,
1512                }
1513            }
1514            EmbedItem::EmbedScopedIdentifier(x) => x.embed_scoped_identifier.as_ref().into(),
1515            EmbedItem::Any(x) => x.any.as_ref().into(),
1516        }
1517    }
1518}
1519
1520// ----------------------------------------------------------------------------
1521// Include
1522// ----------------------------------------------------------------------------
1523
1524impl_token_range!(IncludeDeclaration, include, semicolon);
1525
1526// ----------------------------------------------------------------------------
1527// Description
1528// ----------------------------------------------------------------------------
1529
1530impl From<&DescriptionItem> for TokenRange {
1531    fn from(value: &DescriptionItem) -> Self {
1532        match value {
1533            DescriptionItem::DescriptionItemOptPublicDescriptionItem(x) => {
1534                let mut ret: TokenRange = x.public_description_item.as_ref().into();
1535                if let Some(x) = &x.description_item_opt {
1536                    ret.set_beg(x.r#pub.as_ref().into());
1537                }
1538                ret
1539            }
1540            DescriptionItem::ImportDeclaration(x) => x.import_declaration.as_ref().into(),
1541            DescriptionItem::BindDeclaration(x) => x.bind_declaration.as_ref().into(),
1542            DescriptionItem::EmbedDeclaration(x) => x.embed_declaration.as_ref().into(),
1543            DescriptionItem::IncludeDeclaration(x) => x.include_declaration.as_ref().into(),
1544        }
1545    }
1546}
1547impl_token_ext!(DescriptionItem);
1548
1549impl_token_range_group!(DescriptionGroup, DescriptionItem);
1550impl_token_range_enum!(
1551    PublicDescriptionItem,
1552    module_declaration,
1553    interface_declaration,
1554    package_declaration,
1555    alias_declaration,
1556    proto_declaration,
1557    function_declaration
1558);
1559
1560// ----------------------------------------------------------------------------
1561// SourceCode
1562// ----------------------------------------------------------------------------
1563
1564impl From<&Veryl> for TokenRange {
1565    fn from(value: &Veryl) -> Self {
1566        let mut ret: TokenRange = value.start.as_ref().into();
1567        if let Some(x) = value.veryl_list.last() {
1568            ret.set_end(x.description_group.as_ref().into());
1569        }
1570        ret
1571    }
1572}
1573impl_token_ext!(Veryl);