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