lib_ruby_parser_ast/nodes/types/
def.rs

1// This file is autogenerated by codegen/node_file.liquid
2
3use crate::nodes::InnerNode;
4use crate::nodes::InspectVec;
5use crate::Loc;
6use crate::Node;
7
8/// Represents method definition using `def` keyword (not on a singleton, see `Defs` node).
9#[derive(Debug, Clone, PartialEq)]
10#[repr(C)]
11pub struct Def {
12    /// Name of the method, `String("foo")` for `def foo; end`
13    pub name: String,
14
15    /// Arguments of a method, `None` if there's no arguments.
16    ///
17    /// All information about parentheses around arguments is stored in this node.
18    pub args: Option<Box<Node>>,
19
20    /// Body of a method, `None` if there's no body.
21    pub body: Option<Box<Node>>,
22
23    /// Location of the `def` keyword.
24    ///
25    /// ```text
26    /// def foo; end
27    /// ~~~
28    /// ```
29    pub keyword_l: Loc,
30
31    /// Location of the method name.
32    ///
33    /// ```text
34    /// def foo; end
35    ///     ~~~
36    /// ```
37    pub name_l: Loc,
38
39    /// Location of the `end` keyword.
40    ///
41    /// ```text
42    /// def foo; end
43    ///          ~~~
44    /// ```
45    ///
46    /// `None` for endless method definition
47    pub end_l: Option<Loc>,
48
49    /// Location of the `=` operator for endless method definition
50    ///
51    /// ```text
52    /// def m() = 1
53    ///         ~
54    /// ```
55    ///
56    /// `None` for regular method definition
57    pub assignment_l: Option<Loc>,
58
59    /// Location of the full expression
60    ///
61    /// ```text
62    /// def m(a); foo; end
63    /// ~~~~~~~~~~~~~~~~~~
64    /// ```
65    pub expression_l: Loc,
66
67}
68
69impl InnerNode for Def {
70    fn expression(&self) -> &Loc {
71        &self.expression_l
72    }
73
74    fn inspected_children(&self, indent: usize) -> Vec<String> {
75        let mut result = InspectVec::new(indent);
76        result.push_str(&self.name);
77        result.push_maybe_node_or_nil(&self.args);
78        result.push_maybe_node_or_nil(&self.body);
79        
80        result.strings()
81    }
82
83    fn str_type(&self) -> &'static str {
84        "def"
85    }
86
87    fn print_with_locs(&self) {
88        println!("{}", self.inspect(0));
89        if let Some(node) = self.args.as_ref() { node.inner_ref().print_with_locs() }
90        if let Some(node) = self.body.as_ref() { node.inner_ref().print_with_locs() }
91        self.keyword_l.print("keyword");
92        self.name_l.print("name");
93        if let Some(loc) = self.end_l.as_ref() { loc.print("end") }
94        if let Some(loc) = self.assignment_l.as_ref() { loc.print("assignment") }
95        self.expression_l.print("expression");
96        
97    }
98}