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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use crate::ns::*;

/// Context used to control the parsing of an expression.
#[derive(Clone)]
pub struct ParserExpressionContext {
    pub min_precedence: OperatorPrecedence,
    pub allow_in: bool,
    pub allow_assignment: bool,
}

impl Default for ParserExpressionContext {
    fn default() -> Self {
        Self {
            min_precedence: OperatorPrecedence::List,
            allow_in: true,
            allow_assignment: true,
        }
    }
}

#[derive(Clone)]
pub enum ParserDirectiveContext {
    Default,
    TopLevel,
    PackageBlock,
    ClassBlock {
        name: String,
    },
    InterfaceBlock,
    EnumBlock,
    ConstructorBlock {
        super_statement_found: Rc<Cell<bool>>,
    },
    WithControl {
        super_statement_found: Option<Rc<Cell<bool>>>,
        to_be_labeled: Option<String>,
        control_context: ParserControlFlowContext,
        labels: HashMap<String, ParserControlFlowContext>,
    },
}

impl ParserDirectiveContext {
    pub fn may_contain_super_statement(&self) -> bool {
        matches!(self, Self::ConstructorBlock { .. }) || matches!(self, Self::WithControl { .. })
    }

    pub fn super_statement_found(&self) -> bool {
        match self {
            Self::ConstructorBlock { super_statement_found } => super_statement_found.get(),
            Self::WithControl { super_statement_found, .. } => super_statement_found.as_ref().or(Some(&Rc::new(Cell::new(false)))).unwrap().get(),
            _ => false,
        }
    }

    pub fn set_super_statement_found(&self, value: bool) {
        match self {
            Self::ConstructorBlock { super_statement_found } => { super_statement_found.set(value) },
            Self::WithControl { super_statement_found, .. } => {
                if let Some(found) = super_statement_found.as_ref() {
                    found.set(value);
                }
            },
            _ => {},
        }
    }

    pub fn function_name_is_constructor(&self, name: &(String, Location)) -> bool {
        if let ParserDirectiveContext::ClassBlock { name: ref name_1 } = self {
            &name.0 == name_1
        } else {
            false
        }
    }

    pub fn is_top_level_or_package(&self) -> bool {
        matches!(self, ParserDirectiveContext::TopLevel) || matches!(self, ParserDirectiveContext::PackageBlock)
    }

    pub fn is_type_block(&self) -> bool {
        match self {
            Self::ClassBlock { .. } |
            Self::InterfaceBlock |
            Self::EnumBlock => true,
            _ => false,
        }
    }

    pub fn clone_control(&self) -> Self {
        match self {
            Self::WithControl { .. } => self.clone(),
            _ => Self::Default,
        }
    }

    pub fn override_control_context(&self, label_only: bool, mut context: ParserControlFlowContext) -> Self {
        let mut prev_context = None;
        let mut label = None;
        let mut super_statement_found: Option<Rc<Cell<bool>>> = None;
        let mut labels = match self {
            Self::WithControl { control_context, labels, to_be_labeled: label1, super_statement_found: super_found_1 } => {
                prev_context = Some(control_context.clone());
                label = label1.clone();
                super_statement_found = super_found_1.clone();
                labels.clone()
            },
            _ => HashMap::new(),
        };
        if let Some(label) = label.clone() {
            labels.insert(label, context.clone());
        }
        if label_only {
            context = prev_context.unwrap_or(ParserControlFlowContext {
                breakable: false,
                iteration: false,
            });
        }
        Self::WithControl { control_context: context, labels, to_be_labeled: None, super_statement_found }
    }

    pub fn put_label(&self, label: String) -> Self {
        match self {
            Self::WithControl { control_context, labels, to_be_labeled: _, super_statement_found } => Self::WithControl {
                to_be_labeled: Some(label),
                control_context: control_context.clone(),
                labels: labels.clone(),
                super_statement_found: super_statement_found.clone(),
            },
            _ => Self::WithControl {
                to_be_labeled: Some(label),
                control_context: ParserControlFlowContext {
                    breakable: false,
                    iteration: false,
                },
                labels: HashMap::new(),
                super_statement_found: match self {
                    Self::ConstructorBlock { super_statement_found } => Some(super_statement_found.clone()),
                    _ => None,
                },
            },
        }
    }

    pub fn is_label_defined(&self, label: String) -> bool {
        self.resolve_label(label).is_some()
    }

    pub fn resolve_label(&self, label: String) -> Option<ParserControlFlowContext> {
        if let Self::WithControl { labels, .. } = &self { labels.get(&label).map(|c| c.clone()) } else { None }
    }

    pub fn is_break_allowed(&self, label: Option<String>) -> bool {
        if let Some(label) = label {
            let context = self.resolve_label(label);
            if let Some(context) = context { context.breakable } else { false }
        } else {
            if let Self::WithControl { control_context, .. } = &self { control_context.breakable } else { false }
        }
    }

    pub fn is_continue_allowed(&self, label: Option<String>) -> bool {
        if let Some(label) = label {
            let context = self.resolve_label(label);
            if let Some(context) = context { context.iteration } else { false }
        } else {
            if let Self::WithControl { control_context, .. } = &self { control_context.iteration } else { false }
        }
    }
}

#[derive(Clone)]
pub struct ParserControlFlowContext {
    pub breakable: bool,
    pub iteration: bool,
}