lib_ruby_parser/
current_arg_stack.rs

1use std::cell::RefCell;
2use std::rc::Rc;
3// Stack that holds names of current arguments,
4// i.e. while parsing
5//   def m1(a = (def m2(b = def m3(c = 1); end); end)); end
6//                                   ^
7// stack is [:a, :b, :c]
8//
9// Emulates `p->cur_arg` in MRI's parse.y
10//
11//
12#[derive(Debug, Clone, Default)]
13pub(crate) struct CurrentArgStack {
14    stack: Rc<RefCell<Vec<Option<String>>>>,
15}
16
17impl CurrentArgStack {
18    pub(crate) fn new() -> Self {
19        Self {
20            stack: Rc::new(RefCell::new(vec![])),
21        }
22    }
23
24    pub(crate) fn is_empty(&self) -> bool {
25        self.stack.borrow().is_empty()
26    }
27
28    pub(crate) fn push(&self, value: Option<String>) {
29        self.stack.borrow_mut().push(value)
30    }
31
32    pub(crate) fn set(&self, value: Option<String>) {
33        self.pop();
34        self.push(value)
35    }
36
37    pub(crate) fn pop(&self) {
38        self.stack.borrow_mut().pop();
39    }
40
41    pub(crate) fn top(&self) -> Option<String> {
42        match self.stack.borrow().last() {
43            Some(Some(value)) => Some(value.clone()),
44            _ => None,
45        }
46    }
47}