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