lib_ruby_parser_ast/nodes/
inner_node.rs

1use crate::Bytes;
2use crate::Loc;
3use crate::Node;
4
5pub trait InnerNode: std::fmt::Debug {
6    fn expression(&self) -> &Loc;
7    fn str_type(&self) -> &'static str;
8    fn inspected_children(&self, indent: usize) -> Vec<String>;
9
10    fn inspect(&self, indent: usize) -> String {
11        let indented = "  ".repeat(indent);
12        let mut sexp = format!("{}s(:{}", indented, self.str_type());
13
14        for child in self.inspected_children(indent) {
15            sexp.push_str(&child);
16        }
17
18        sexp.push(')');
19
20        sexp
21    }
22
23    fn print_with_locs(&self);
24}
25
26pub(crate) struct InspectVec {
27    indent: usize,
28    strings: Vec<String>,
29}
30
31impl InspectVec {
32    pub(crate) fn new(indent: usize) -> Self {
33        Self {
34            indent,
35            strings: vec![],
36        }
37    }
38
39    pub(crate) fn push_str(&mut self, string: &str) {
40        self.strings.push(format!(", {:?}", string));
41    }
42
43    pub(crate) fn push_raw_str(&mut self, string: &str) {
44        self.strings.push(format!(", {}", string));
45    }
46
47    pub(crate) fn push_maybe_str(&mut self, string: &Option<String>) {
48        if let Some(string) = string.as_ref() {
49            self.strings.push(format!(", {:?}", string));
50        }
51    }
52
53    pub(crate) fn push_maybe_str_or_nil(&mut self, string: &Option<String>) {
54        if let Some(string) = string.as_ref() {
55            self.push_str(string)
56        } else {
57            self.push_nil()
58        }
59    }
60
61    pub(crate) fn push_nil(&mut self) {
62        self.strings.push(", nil".to_string());
63    }
64
65    pub(crate) fn push_u8(&mut self, n: &u8) {
66        self.strings.push(format!(", {}", n))
67    }
68
69    pub(crate) fn push_node(&mut self, node: &Node) {
70        self.strings
71            .push(format!(",\n{}", node.inspect(self.indent + 1)))
72    }
73
74    pub(crate) fn push_maybe_node(&mut self, node: &Option<Box<Node>>) {
75        if let Some(node) = node.as_ref() {
76            self.push_node(node)
77        }
78    }
79
80    pub(crate) fn push_regex_options(&mut self, node: &Option<Box<Node>>) {
81        if let Some(node) = node.as_ref() {
82            self.push_node(node)
83        } else {
84            self.strings.push(format!(
85                ",\n{}{}",
86                "  ".repeat(self.indent + 1),
87                "s(:regopt)"
88            ))
89        }
90    }
91
92    pub(crate) fn push_maybe_node_or_nil(&mut self, node: &Option<Box<Node>>) {
93        if let Some(node) = node.as_ref() {
94            self.push_node(node)
95        } else {
96            self.push_nil()
97        }
98    }
99
100    pub(crate) fn push_nodes(&mut self, nodes: &[Node]) {
101        for node in nodes.iter() {
102            self.push_node(node)
103        }
104    }
105
106    pub(crate) fn push_chars(&mut self, chars: &Option<String>) {
107        if let Some(chars) = chars.as_ref() {
108            for c in chars.as_str().chars() {
109                self.push_str(&format!("{}", c));
110            }
111        }
112    }
113
114    pub(crate) fn push_string_value(&mut self, bytes: &Bytes) {
115        self.push_str(&bytes.to_string_lossy())
116    }
117
118    pub(crate) fn strings(&mut self) -> Vec<String> {
119        std::mem::take(&mut self.strings)
120    }
121}