Skip to main content

glua_parser/syntax/node/lua/
stat.rs

1use crate::{
2    LuaAstToken, LuaGeneralToken, LuaLocalAttribute, LuaSyntaxNode, LuaTokenKind,
3    kind::LuaSyntaxKind,
4    syntax::{
5        LuaCommentOwner,
6        node::LuaNameToken,
7        traits::{LuaAstChildren, LuaAstNode, LuaAstTokenChildren},
8    },
9};
10
11use super::{
12    LuaBlock, LuaLocalName,
13    expr::{LuaCallExpr, LuaClosureExpr, LuaExpr, LuaVarExpr},
14};
15
16#[derive(Debug, Clone, PartialEq, Eq, Hash)]
17pub enum LuaStat {
18    LocalStat(LuaLocalStat),
19    AssignStat(LuaAssignStat),
20    CallExprStat(LuaCallExprStat),
21    FuncStat(LuaFuncStat),
22    LocalFuncStat(LuaLocalFuncStat),
23    IfStat(LuaIfStat),
24    WhileStat(LuaWhileStat),
25    DoStat(LuaDoStat),
26    ForStat(LuaForStat),
27    ForRangeStat(LuaForRangeStat),
28    RepeatStat(LuaRepeatStat),
29    BreakStat(LuaBreakStat),
30    ReturnStat(LuaReturnStat),
31    GotoStat(LuaGotoStat),
32    LabelStat(LuaLabelStat),
33    EmptyStat(LuaEmptyStat),
34    GlobalStat(LuaGlobalStat),
35}
36
37impl LuaAstNode for LuaStat {
38    fn syntax(&self) -> &LuaSyntaxNode {
39        match self {
40            LuaStat::LocalStat(node) => node.syntax(),
41            LuaStat::AssignStat(node) => node.syntax(),
42            LuaStat::CallExprStat(node) => node.syntax(),
43            LuaStat::FuncStat(node) => node.syntax(),
44            LuaStat::LocalFuncStat(node) => node.syntax(),
45            LuaStat::IfStat(node) => node.syntax(),
46            LuaStat::WhileStat(node) => node.syntax(),
47            LuaStat::DoStat(node) => node.syntax(),
48            LuaStat::ForStat(node) => node.syntax(),
49            LuaStat::ForRangeStat(node) => node.syntax(),
50            LuaStat::RepeatStat(node) => node.syntax(),
51            LuaStat::BreakStat(node) => node.syntax(),
52            LuaStat::ReturnStat(node) => node.syntax(),
53            LuaStat::GotoStat(node) => node.syntax(),
54            LuaStat::LabelStat(node) => node.syntax(),
55            LuaStat::EmptyStat(node) => node.syntax(),
56            LuaStat::GlobalStat(node) => node.syntax(),
57        }
58    }
59
60    fn can_cast(kind: LuaSyntaxKind) -> bool
61    where
62        Self: Sized,
63    {
64        matches!(
65            kind,
66            LuaSyntaxKind::LocalStat
67                | LuaSyntaxKind::AssignStat
68                | LuaSyntaxKind::CallExprStat
69                | LuaSyntaxKind::FuncStat
70                | LuaSyntaxKind::LocalFuncStat
71                | LuaSyntaxKind::IfStat
72                | LuaSyntaxKind::WhileStat
73                | LuaSyntaxKind::DoStat
74                | LuaSyntaxKind::ForStat
75                | LuaSyntaxKind::ForRangeStat
76                | LuaSyntaxKind::RepeatStat
77                | LuaSyntaxKind::BreakStat
78                | LuaSyntaxKind::ReturnStat
79                | LuaSyntaxKind::GotoStat
80                | LuaSyntaxKind::LabelStat
81                | LuaSyntaxKind::EmptyStat
82                | LuaSyntaxKind::GlobalStat
83        )
84    }
85
86    fn cast(syntax: LuaSyntaxNode) -> Option<Self>
87    where
88        Self: Sized,
89    {
90        match syntax.kind().into() {
91            LuaSyntaxKind::LocalStat => Some(LuaStat::LocalStat(LuaLocalStat::cast(syntax)?)),
92            LuaSyntaxKind::AssignStat => Some(LuaStat::AssignStat(LuaAssignStat::cast(syntax)?)),
93            LuaSyntaxKind::CallExprStat => {
94                Some(LuaStat::CallExprStat(LuaCallExprStat::cast(syntax)?))
95            }
96            LuaSyntaxKind::FuncStat => Some(LuaStat::FuncStat(LuaFuncStat::cast(syntax)?)),
97            LuaSyntaxKind::LocalFuncStat => {
98                Some(LuaStat::LocalFuncStat(LuaLocalFuncStat::cast(syntax)?))
99            }
100            LuaSyntaxKind::IfStat => Some(LuaStat::IfStat(LuaIfStat::cast(syntax)?)),
101            LuaSyntaxKind::WhileStat => Some(LuaStat::WhileStat(LuaWhileStat::cast(syntax)?)),
102            LuaSyntaxKind::DoStat => Some(LuaStat::DoStat(LuaDoStat::cast(syntax)?)),
103            LuaSyntaxKind::ForStat => Some(LuaStat::ForStat(LuaForStat::cast(syntax)?)),
104            LuaSyntaxKind::ForRangeStat => {
105                Some(LuaStat::ForRangeStat(LuaForRangeStat::cast(syntax)?))
106            }
107            LuaSyntaxKind::RepeatStat => Some(LuaStat::RepeatStat(LuaRepeatStat::cast(syntax)?)),
108            LuaSyntaxKind::BreakStat => Some(LuaStat::BreakStat(LuaBreakStat::cast(syntax)?)),
109            LuaSyntaxKind::ReturnStat => Some(LuaStat::ReturnStat(LuaReturnStat::cast(syntax)?)),
110            LuaSyntaxKind::GotoStat => Some(LuaStat::GotoStat(LuaGotoStat::cast(syntax)?)),
111            LuaSyntaxKind::LabelStat => Some(LuaStat::LabelStat(LuaLabelStat::cast(syntax)?)),
112            LuaSyntaxKind::EmptyStat => Some(LuaStat::EmptyStat(LuaEmptyStat::cast(syntax)?)),
113            LuaSyntaxKind::GlobalStat => Some(LuaStat::GlobalStat(LuaGlobalStat::cast(syntax)?)),
114            _ => None,
115        }
116    }
117}
118
119impl LuaCommentOwner for LuaStat {}
120
121impl LuaStat {
122    pub fn get_parent_block(&self) -> Option<LuaBlock> {
123        LuaBlock::cast(self.syntax().parent()?)
124    }
125}
126
127#[derive(Debug, Clone, PartialEq, Eq, Hash)]
128pub enum LuaLoopStat {
129    WhileStat(LuaWhileStat),
130    RepeatStat(LuaRepeatStat),
131    ForStat(LuaForStat),
132    ForRangeStat(LuaForRangeStat),
133}
134
135impl LuaAstNode for LuaLoopStat {
136    fn syntax(&self) -> &LuaSyntaxNode {
137        match self {
138            LuaLoopStat::WhileStat(node) => node.syntax(),
139            LuaLoopStat::RepeatStat(node) => node.syntax(),
140            LuaLoopStat::ForStat(node) => node.syntax(),
141            LuaLoopStat::ForRangeStat(node) => node.syntax(),
142        }
143    }
144
145    fn can_cast(kind: LuaSyntaxKind) -> bool
146    where
147        Self: Sized,
148    {
149        matches!(
150            kind,
151            LuaSyntaxKind::WhileStat
152                | LuaSyntaxKind::RepeatStat
153                | LuaSyntaxKind::ForStat
154                | LuaSyntaxKind::ForRangeStat
155        )
156    }
157
158    fn cast(syntax: LuaSyntaxNode) -> Option<Self>
159    where
160        Self: Sized,
161    {
162        if let Some(node) = LuaWhileStat::cast(syntax.clone()) {
163            Some(LuaLoopStat::WhileStat(node))
164        } else if let Some(node) = LuaRepeatStat::cast(syntax.clone()) {
165            Some(LuaLoopStat::RepeatStat(node))
166        } else if let Some(node) = LuaForStat::cast(syntax.clone()) {
167            Some(LuaLoopStat::ForStat(node))
168        } else {
169            LuaForRangeStat::cast(syntax.clone()).map(LuaLoopStat::ForRangeStat)
170        }
171    }
172}
173
174impl LuaCommentOwner for LuaLoopStat {}
175
176impl LuaLoopStat {
177    pub fn get_parent_block(&self) -> Option<LuaBlock> {
178        LuaBlock::cast(self.syntax().parent()?)
179    }
180}
181
182#[derive(Debug, Clone, PartialEq, Eq, Hash)]
183pub struct LuaLocalStat {
184    syntax: LuaSyntaxNode,
185}
186
187impl LuaAstNode for LuaLocalStat {
188    fn syntax(&self) -> &LuaSyntaxNode {
189        &self.syntax
190    }
191
192    fn can_cast(kind: LuaSyntaxKind) -> bool
193    where
194        Self: Sized,
195    {
196        kind == LuaSyntaxKind::LocalStat
197    }
198
199    fn cast(syntax: LuaSyntaxNode) -> Option<Self>
200    where
201        Self: Sized,
202    {
203        if syntax.kind() == LuaSyntaxKind::LocalStat.into() {
204            Some(Self { syntax })
205        } else {
206            None
207        }
208    }
209}
210
211impl LuaCommentOwner for LuaLocalStat {}
212
213impl LuaLocalStat {
214    pub fn get_local_name_list(&self) -> LuaAstChildren<LuaLocalName> {
215        self.children()
216    }
217
218    pub fn get_value_exprs(&self) -> LuaAstChildren<LuaExpr> {
219        self.children()
220    }
221
222    /// 仅从`AST`分析, 并不是绝对准确的, 因为允许最后一个表达式返回多个值
223    pub fn get_local_name_by_value(&self, value: LuaExpr) -> Option<LuaLocalName> {
224        let local_names = self.get_local_name_list();
225        let value_exprs = self.get_value_exprs().collect::<Vec<_>>();
226
227        for (i, local_name) in local_names.enumerate() {
228            if let Some(value_expr) = value_exprs.get(i)
229                && value_expr.syntax() == value.syntax()
230            {
231                return Some(local_name);
232            }
233        }
234        None
235    }
236
237    pub fn get_attrib(&self) -> Option<LuaLocalAttribute> {
238        self.child()
239    }
240}
241
242#[derive(Debug, Clone, PartialEq, Eq, Hash)]
243pub struct LuaAssignStat {
244    syntax: LuaSyntaxNode,
245}
246
247impl LuaAstNode for LuaAssignStat {
248    fn syntax(&self) -> &LuaSyntaxNode {
249        &self.syntax
250    }
251
252    fn can_cast(kind: LuaSyntaxKind) -> bool
253    where
254        Self: Sized,
255    {
256        kind == LuaSyntaxKind::AssignStat
257    }
258
259    fn cast(syntax: LuaSyntaxNode) -> Option<Self>
260    where
261        Self: Sized,
262    {
263        if syntax.kind() == LuaSyntaxKind::AssignStat.into() {
264            Some(Self { syntax })
265        } else {
266            None
267        }
268    }
269}
270
271impl LuaCommentOwner for LuaAssignStat {}
272
273impl LuaAssignStat {
274    pub fn get_var_and_expr_list(&self) -> (Vec<LuaVarExpr>, Vec<LuaExpr>) {
275        let mut vars = Vec::new();
276        let mut exprs = Vec::new();
277        let mut meet_assign = false;
278        for child in self.syntax.children_with_tokens() {
279            if child.kind().to_token().is_assign_op() {
280                meet_assign = true;
281            }
282
283            if let Some(node) = child.into_node() {
284                if meet_assign {
285                    if let Some(var) = LuaExpr::cast(node) {
286                        exprs.push(var);
287                    }
288                } else if let Some(var) = LuaVarExpr::cast(node) {
289                    vars.push(var);
290                }
291            }
292        }
293
294        (vars, exprs)
295    }
296
297    pub fn get_assign_op(&self) -> Option<LuaGeneralToken> {
298        for child in self.syntax.children_with_tokens() {
299            if let Some(token) = child.into_token()
300                && token.kind().to_token().is_assign_op()
301            {
302                return LuaGeneralToken::cast(token);
303            }
304        }
305        None
306    }
307}
308
309#[derive(Debug, Clone, PartialEq, Eq, Hash)]
310pub struct LuaCallExprStat {
311    syntax: LuaSyntaxNode,
312}
313
314impl LuaAstNode for LuaCallExprStat {
315    fn syntax(&self) -> &LuaSyntaxNode {
316        &self.syntax
317    }
318
319    fn can_cast(kind: LuaSyntaxKind) -> bool
320    where
321        Self: Sized,
322    {
323        kind == LuaSyntaxKind::CallExprStat
324    }
325
326    fn cast(syntax: LuaSyntaxNode) -> Option<Self>
327    where
328        Self: Sized,
329    {
330        if syntax.kind() == LuaSyntaxKind::CallExprStat.into() {
331            Some(Self { syntax })
332        } else {
333            None
334        }
335    }
336}
337
338impl LuaCommentOwner for LuaCallExprStat {}
339
340impl LuaCallExprStat {
341    pub fn get_call_expr(&self) -> Option<LuaCallExpr> {
342        self.child()
343    }
344}
345
346#[derive(Debug, Clone, PartialEq, Eq, Hash)]
347pub struct LuaFuncStat {
348    syntax: LuaSyntaxNode,
349}
350
351impl LuaAstNode for LuaFuncStat {
352    fn syntax(&self) -> &LuaSyntaxNode {
353        &self.syntax
354    }
355
356    fn can_cast(kind: LuaSyntaxKind) -> bool
357    where
358        Self: Sized,
359    {
360        kind == LuaSyntaxKind::FuncStat
361    }
362
363    fn cast(syntax: LuaSyntaxNode) -> Option<Self>
364    where
365        Self: Sized,
366    {
367        if syntax.kind() == LuaSyntaxKind::FuncStat.into() {
368            Some(Self { syntax })
369        } else {
370            None
371        }
372    }
373}
374
375impl LuaCommentOwner for LuaFuncStat {}
376
377impl LuaFuncStat {
378    pub fn get_func_name(&self) -> Option<LuaVarExpr> {
379        self.child()
380    }
381
382    pub fn get_closure(&self) -> Option<LuaClosureExpr> {
383        self.child()
384    }
385
386    pub fn is_global(&self) -> bool {
387        self.token_by_kind(LuaTokenKind::TkGlobal).is_some()
388    }
389}
390
391#[derive(Debug, Clone, PartialEq, Eq, Hash)]
392pub struct LuaLocalFuncStat {
393    syntax: LuaSyntaxNode,
394}
395
396impl LuaAstNode for LuaLocalFuncStat {
397    fn syntax(&self) -> &LuaSyntaxNode {
398        &self.syntax
399    }
400
401    fn can_cast(kind: LuaSyntaxKind) -> bool
402    where
403        Self: Sized,
404    {
405        kind == LuaSyntaxKind::LocalFuncStat
406    }
407
408    fn cast(syntax: LuaSyntaxNode) -> Option<Self>
409    where
410        Self: Sized,
411    {
412        if syntax.kind() == LuaSyntaxKind::LocalFuncStat.into() {
413            Some(Self { syntax })
414        } else {
415            None
416        }
417    }
418}
419
420impl LuaCommentOwner for LuaLocalFuncStat {}
421
422impl LuaLocalFuncStat {
423    pub fn get_local_name(&self) -> Option<LuaLocalName> {
424        self.child()
425    }
426
427    pub fn get_closure(&self) -> Option<LuaClosureExpr> {
428        self.child()
429    }
430}
431
432#[derive(Debug, Clone, PartialEq, Eq, Hash)]
433pub struct LuaIfStat {
434    syntax: LuaSyntaxNode,
435}
436
437impl LuaAstNode for LuaIfStat {
438    fn syntax(&self) -> &LuaSyntaxNode {
439        &self.syntax
440    }
441
442    fn can_cast(kind: LuaSyntaxKind) -> bool
443    where
444        Self: Sized,
445    {
446        kind == LuaSyntaxKind::IfStat
447    }
448
449    fn cast(syntax: LuaSyntaxNode) -> Option<Self>
450    where
451        Self: Sized,
452    {
453        if syntax.kind() == LuaSyntaxKind::IfStat.into() {
454            Some(Self { syntax })
455        } else {
456            None
457        }
458    }
459}
460
461impl LuaCommentOwner for LuaIfStat {}
462
463impl LuaIfStat {
464    pub fn get_condition_expr(&self) -> Option<LuaExpr> {
465        self.child()
466    }
467
468    pub fn get_block(&self) -> Option<LuaBlock> {
469        self.child()
470    }
471
472    pub fn get_else_if_clause_list(&self) -> LuaAstChildren<LuaElseIfClauseStat> {
473        self.children()
474    }
475
476    pub fn get_else_clause(&self) -> Option<LuaElseClauseStat> {
477        self.child()
478    }
479
480    pub fn get_all_clause(&self) -> LuaAstChildren<LuaIfClauseStat> {
481        self.children()
482    }
483}
484
485#[derive(Debug, Clone, PartialEq, Eq, Hash)]
486pub struct LuaElseIfClauseStat {
487    syntax: LuaSyntaxNode,
488}
489
490impl LuaAstNode for LuaElseIfClauseStat {
491    fn syntax(&self) -> &LuaSyntaxNode {
492        &self.syntax
493    }
494
495    fn can_cast(kind: LuaSyntaxKind) -> bool
496    where
497        Self: Sized,
498    {
499        kind == LuaSyntaxKind::ElseIfClauseStat
500    }
501
502    fn cast(syntax: LuaSyntaxNode) -> Option<Self>
503    where
504        Self: Sized,
505    {
506        if syntax.kind() == LuaSyntaxKind::ElseIfClauseStat.into() {
507            Some(Self { syntax })
508        } else {
509            None
510        }
511    }
512}
513
514impl LuaCommentOwner for LuaElseIfClauseStat {}
515
516impl LuaElseIfClauseStat {
517    pub fn get_condition_expr(&self) -> Option<LuaExpr> {
518        self.child()
519    }
520
521    pub fn get_block(&self) -> Option<LuaBlock> {
522        self.child()
523    }
524}
525
526#[derive(Debug, Clone, PartialEq, Eq, Hash)]
527pub struct LuaElseClauseStat {
528    syntax: LuaSyntaxNode,
529}
530
531impl LuaAstNode for LuaElseClauseStat {
532    fn syntax(&self) -> &LuaSyntaxNode {
533        &self.syntax
534    }
535
536    fn can_cast(kind: LuaSyntaxKind) -> bool
537    where
538        Self: Sized,
539    {
540        kind == LuaSyntaxKind::ElseClauseStat
541    }
542
543    fn cast(syntax: LuaSyntaxNode) -> Option<Self>
544    where
545        Self: Sized,
546    {
547        if syntax.kind() == LuaSyntaxKind::ElseClauseStat.into() {
548            Some(Self { syntax })
549        } else {
550            None
551        }
552    }
553}
554
555impl LuaCommentOwner for LuaElseClauseStat {}
556
557impl LuaElseClauseStat {
558    pub fn get_block(&self) -> Option<LuaBlock> {
559        self.child()
560    }
561}
562
563#[derive(Debug, Clone, PartialEq, Eq, Hash)]
564pub enum LuaIfClauseStat {
565    ElseIf(LuaElseIfClauseStat),
566    Else(LuaElseClauseStat),
567}
568
569impl LuaAstNode for LuaIfClauseStat {
570    fn syntax(&self) -> &LuaSyntaxNode {
571        match self {
572            LuaIfClauseStat::ElseIf(node) => node.syntax(),
573            LuaIfClauseStat::Else(node) => node.syntax(),
574        }
575    }
576
577    fn can_cast(kind: LuaSyntaxKind) -> bool
578    where
579        Self: Sized,
580    {
581        LuaElseIfClauseStat::can_cast(kind) || LuaElseClauseStat::can_cast(kind)
582    }
583
584    fn cast(syntax: LuaSyntaxNode) -> Option<Self>
585    where
586        Self: Sized,
587    {
588        if LuaElseIfClauseStat::can_cast(syntax.kind().into()) {
589            Some(LuaIfClauseStat::ElseIf(LuaElseIfClauseStat::cast(syntax)?))
590        } else if LuaElseClauseStat::can_cast(syntax.kind().into()) {
591            Some(LuaIfClauseStat::Else(LuaElseClauseStat::cast(syntax)?))
592        } else {
593            None
594        }
595    }
596}
597
598impl LuaCommentOwner for LuaIfClauseStat {}
599
600impl LuaIfClauseStat {
601    pub fn get_parent_if_stat(&self) -> Option<LuaIfStat> {
602        LuaIfStat::cast(self.syntax().parent()?)
603    }
604
605    pub fn get_block(&self) -> Option<LuaBlock> {
606        match self {
607            LuaIfClauseStat::ElseIf(node) => node.get_block(),
608            LuaIfClauseStat::Else(node) => node.get_block(),
609        }
610    }
611
612    pub fn get_condition_expr(&self) -> Option<LuaExpr> {
613        match self {
614            LuaIfClauseStat::ElseIf(node) => node.get_condition_expr(),
615            LuaIfClauseStat::Else(_) => None,
616        }
617    }
618}
619
620#[derive(Debug, Clone, PartialEq, Eq, Hash)]
621pub struct LuaWhileStat {
622    syntax: LuaSyntaxNode,
623}
624
625impl LuaAstNode for LuaWhileStat {
626    fn syntax(&self) -> &LuaSyntaxNode {
627        &self.syntax
628    }
629
630    fn can_cast(kind: LuaSyntaxKind) -> bool
631    where
632        Self: Sized,
633    {
634        kind == LuaSyntaxKind::WhileStat
635    }
636
637    fn cast(syntax: LuaSyntaxNode) -> Option<Self>
638    where
639        Self: Sized,
640    {
641        if syntax.kind() == LuaSyntaxKind::WhileStat.into() {
642            Some(Self { syntax })
643        } else {
644            None
645        }
646    }
647}
648
649impl LuaCommentOwner for LuaWhileStat {}
650
651impl LuaWhileStat {
652    pub fn get_condition_expr(&self) -> Option<LuaExpr> {
653        self.child()
654    }
655
656    pub fn get_block(&self) -> Option<LuaBlock> {
657        self.child()
658    }
659}
660
661#[derive(Debug, Clone, PartialEq, Eq, Hash)]
662pub struct LuaDoStat {
663    syntax: LuaSyntaxNode,
664}
665
666impl LuaAstNode for LuaDoStat {
667    fn syntax(&self) -> &LuaSyntaxNode {
668        &self.syntax
669    }
670
671    fn can_cast(kind: LuaSyntaxKind) -> bool
672    where
673        Self: Sized,
674    {
675        kind == LuaSyntaxKind::DoStat
676    }
677
678    fn cast(syntax: LuaSyntaxNode) -> Option<Self>
679    where
680        Self: Sized,
681    {
682        if syntax.kind() == LuaSyntaxKind::DoStat.into() {
683            Some(Self { syntax })
684        } else {
685            None
686        }
687    }
688}
689
690impl LuaCommentOwner for LuaDoStat {}
691
692impl LuaDoStat {
693    pub fn get_block(&self) -> Option<LuaBlock> {
694        self.child()
695    }
696}
697
698#[derive(Debug, Clone, PartialEq, Eq, Hash)]
699pub struct LuaForStat {
700    syntax: LuaSyntaxNode,
701}
702
703impl LuaAstNode for LuaForStat {
704    fn syntax(&self) -> &LuaSyntaxNode {
705        &self.syntax
706    }
707
708    fn can_cast(kind: LuaSyntaxKind) -> bool
709    where
710        Self: Sized,
711    {
712        kind == LuaSyntaxKind::ForStat
713    }
714
715    fn cast(syntax: LuaSyntaxNode) -> Option<Self>
716    where
717        Self: Sized,
718    {
719        if syntax.kind() == LuaSyntaxKind::ForStat.into() {
720            Some(Self { syntax })
721        } else {
722            None
723        }
724    }
725}
726
727impl LuaCommentOwner for LuaForStat {}
728
729impl LuaForStat {
730    pub fn get_var_name(&self) -> Option<LuaNameToken> {
731        self.token()
732    }
733
734    pub fn get_iter_expr(&self) -> LuaAstChildren<LuaExpr> {
735        self.children()
736    }
737
738    pub fn get_block(&self) -> Option<LuaBlock> {
739        self.child()
740    }
741}
742
743#[derive(Debug, Clone, PartialEq, Eq, Hash)]
744pub struct LuaForRangeStat {
745    syntax: LuaSyntaxNode,
746}
747
748impl LuaAstNode for LuaForRangeStat {
749    fn syntax(&self) -> &LuaSyntaxNode {
750        &self.syntax
751    }
752
753    fn can_cast(kind: LuaSyntaxKind) -> bool
754    where
755        Self: Sized,
756    {
757        kind == LuaSyntaxKind::ForRangeStat
758    }
759
760    fn cast(syntax: LuaSyntaxNode) -> Option<Self>
761    where
762        Self: Sized,
763    {
764        if syntax.kind() == LuaSyntaxKind::ForRangeStat.into() {
765            Some(Self { syntax })
766        } else {
767            None
768        }
769    }
770}
771
772impl LuaCommentOwner for LuaForRangeStat {}
773
774impl LuaForRangeStat {
775    pub fn get_var_name_list(&self) -> LuaAstTokenChildren<LuaNameToken> {
776        self.tokens()
777    }
778
779    pub fn get_expr_list(&self) -> LuaAstChildren<LuaExpr> {
780        self.children()
781    }
782
783    pub fn get_block(&self) -> Option<LuaBlock> {
784        self.child()
785    }
786}
787
788#[derive(Debug, Clone, PartialEq, Eq, Hash)]
789pub struct LuaRepeatStat {
790    syntax: LuaSyntaxNode,
791}
792
793impl LuaAstNode for LuaRepeatStat {
794    fn syntax(&self) -> &LuaSyntaxNode {
795        &self.syntax
796    }
797
798    fn can_cast(kind: LuaSyntaxKind) -> bool
799    where
800        Self: Sized,
801    {
802        kind == LuaSyntaxKind::RepeatStat
803    }
804
805    fn cast(syntax: LuaSyntaxNode) -> Option<Self>
806    where
807        Self: Sized,
808    {
809        if syntax.kind() == LuaSyntaxKind::RepeatStat.into() {
810            Some(Self { syntax })
811        } else {
812            None
813        }
814    }
815}
816
817impl LuaCommentOwner for LuaRepeatStat {}
818
819impl LuaRepeatStat {
820    pub fn get_block(&self) -> Option<LuaBlock> {
821        self.child()
822    }
823
824    pub fn get_condition_expr(&self) -> Option<LuaExpr> {
825        self.child()
826    }
827}
828
829#[derive(Debug, Clone, PartialEq, Eq, Hash)]
830pub struct LuaBreakStat {
831    syntax: LuaSyntaxNode,
832}
833
834impl LuaAstNode for LuaBreakStat {
835    fn syntax(&self) -> &LuaSyntaxNode {
836        &self.syntax
837    }
838
839    fn can_cast(kind: LuaSyntaxKind) -> bool
840    where
841        Self: Sized,
842    {
843        kind == LuaSyntaxKind::BreakStat
844    }
845
846    fn cast(syntax: LuaSyntaxNode) -> Option<Self>
847    where
848        Self: Sized,
849    {
850        if syntax.kind() == LuaSyntaxKind::BreakStat.into() {
851            Some(Self { syntax })
852        } else {
853            None
854        }
855    }
856}
857
858impl LuaCommentOwner for LuaBreakStat {}
859
860#[derive(Debug, Clone, PartialEq, Eq, Hash)]
861pub struct LuaReturnStat {
862    syntax: LuaSyntaxNode,
863}
864
865impl LuaAstNode for LuaReturnStat {
866    fn syntax(&self) -> &LuaSyntaxNode {
867        &self.syntax
868    }
869
870    fn can_cast(kind: LuaSyntaxKind) -> bool
871    where
872        Self: Sized,
873    {
874        kind == LuaSyntaxKind::ReturnStat
875    }
876
877    fn cast(syntax: LuaSyntaxNode) -> Option<Self>
878    where
879        Self: Sized,
880    {
881        if syntax.kind() == LuaSyntaxKind::ReturnStat.into() {
882            Some(Self { syntax })
883        } else {
884            None
885        }
886    }
887}
888
889impl LuaCommentOwner for LuaReturnStat {}
890
891impl LuaReturnStat {
892    pub fn get_expr_list(&self) -> LuaAstChildren<LuaExpr> {
893        self.children()
894    }
895}
896
897#[derive(Debug, Clone, PartialEq, Eq, Hash)]
898pub struct LuaGotoStat {
899    syntax: LuaSyntaxNode,
900}
901
902impl LuaAstNode for LuaGotoStat {
903    fn syntax(&self) -> &LuaSyntaxNode {
904        &self.syntax
905    }
906
907    fn can_cast(kind: LuaSyntaxKind) -> bool
908    where
909        Self: Sized,
910    {
911        kind == LuaSyntaxKind::GotoStat
912    }
913
914    fn cast(syntax: LuaSyntaxNode) -> Option<Self>
915    where
916        Self: Sized,
917    {
918        if syntax.kind() == LuaSyntaxKind::GotoStat.into() {
919            Some(Self { syntax })
920        } else {
921            None
922        }
923    }
924}
925
926impl LuaCommentOwner for LuaGotoStat {}
927
928impl LuaGotoStat {
929    pub fn get_label_name_token(&self) -> Option<LuaNameToken> {
930        self.token()
931    }
932}
933
934#[derive(Debug, Clone, PartialEq, Eq, Hash)]
935pub struct LuaLabelStat {
936    syntax: LuaSyntaxNode,
937}
938
939impl LuaAstNode for LuaLabelStat {
940    fn syntax(&self) -> &LuaSyntaxNode {
941        &self.syntax
942    }
943
944    fn can_cast(kind: LuaSyntaxKind) -> bool
945    where
946        Self: Sized,
947    {
948        kind == LuaSyntaxKind::LabelStat
949    }
950
951    fn cast(syntax: LuaSyntaxNode) -> Option<Self>
952    where
953        Self: Sized,
954    {
955        if syntax.kind() == LuaSyntaxKind::LabelStat.into() {
956            Some(Self { syntax })
957        } else {
958            None
959        }
960    }
961}
962
963impl LuaCommentOwner for LuaLabelStat {}
964
965impl LuaLabelStat {
966    pub fn get_label_name_token(&self) -> Option<LuaNameToken> {
967        self.token()
968    }
969}
970
971#[derive(Debug, Clone, PartialEq, Eq, Hash)]
972pub struct LuaEmptyStat {
973    syntax: LuaSyntaxNode,
974}
975
976impl LuaAstNode for LuaEmptyStat {
977    fn syntax(&self) -> &LuaSyntaxNode {
978        &self.syntax
979    }
980
981    fn can_cast(kind: LuaSyntaxKind) -> bool
982    where
983        Self: Sized,
984    {
985        kind == LuaSyntaxKind::EmptyStat
986    }
987
988    fn cast(syntax: LuaSyntaxNode) -> Option<Self>
989    where
990        Self: Sized,
991    {
992        if syntax.kind() == LuaSyntaxKind::EmptyStat.into() {
993            Some(Self { syntax })
994        } else {
995            None
996        }
997    }
998}
999
1000impl LuaCommentOwner for LuaEmptyStat {}
1001
1002#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1003pub struct LuaGlobalStat {
1004    syntax: LuaSyntaxNode,
1005}
1006
1007impl LuaAstNode for LuaGlobalStat {
1008    fn syntax(&self) -> &LuaSyntaxNode {
1009        &self.syntax
1010    }
1011
1012    fn can_cast(kind: LuaSyntaxKind) -> bool
1013    where
1014        Self: Sized,
1015    {
1016        kind == LuaSyntaxKind::GlobalStat
1017    }
1018
1019    fn cast(syntax: LuaSyntaxNode) -> Option<Self>
1020    where
1021        Self: Sized,
1022    {
1023        if syntax.kind() == LuaSyntaxKind::GlobalStat.into() {
1024            Some(Self { syntax })
1025        } else {
1026            None
1027        }
1028    }
1029}
1030
1031impl LuaCommentOwner for LuaGlobalStat {}
1032
1033impl LuaGlobalStat {
1034    pub fn get_local_name_list(&self) -> LuaAstChildren<LuaLocalName> {
1035        self.children()
1036    }
1037
1038    pub fn get_attrib(&self) -> Option<LuaLocalAttribute> {
1039        self.child()
1040    }
1041
1042    pub fn is_any_global(&self) -> bool {
1043        self.token_by_kind(LuaTokenKind::TkMul).is_some()
1044    }
1045}