summaryrefslogtreecommitdiffstats
path: root/third_party/rust/jsparagus-parser/src/simulator.rs
blob: 0e060fd5c14a09b85426af92f5819600f5e91a33 (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//! Simulates parser execution, for a single token of input, without incurring
//! any side effects.
//!
//! This is basically a copy of the parser.rs source code with calls to
//! generated_parser::reduce, and stack bookkeeping, omitted.

use crate::parser::Parser;
use arrayvec::ArrayVec;
use ast::SourceLocation;
use generated_parser::{
    noop_actions, ParseError, ParserTrait, Result, StackValue, TermValue, TerminalId, Token, TABLES,
};

/// The Simulator is used to check whether we can shift one token, either to
/// check what might be accepted, or to check whether we can End parsing now.
/// This is used by the REPL to verify whether or not we can end the input.
pub struct Simulator<'alloc, 'parser> {
    /// Define the top of the immutable stack.
    sp: usize,
    /// Immutable state stack coming from the forked parser.
    state_stack: &'parser [usize],
    /// Immuatable term stack coming from the forked parser.
    node_stack: &'parser [TermValue<StackValue<'alloc>>],
    /// Mutable state stack used by the simulator on top of the immutable
    /// parser's state stack.
    ///
    /// Uses a fixed-size array as the number of lookahead is bounded to a lower
    /// value, panics otherwise.
    sim_state_stack: ArrayVec<usize, 4>,
    /// Mutable term stack used by the simulator on top of the immutable
    /// parser's term stack.
    ///
    /// Uses a fixed-size array as the number of lookahead is bounded to a lower
    /// value, panics otherwise.
    sim_node_stack: ArrayVec<TermValue<()>, 4>,
    /// Mutable term stack used by the simulator for replaying terms when
    /// reducing non-terminals are replaying lookahead terminals.
    ///
    /// Uses a fixed-size array as the number of lookahead is bounded to a lower
    /// value, panics otherwise.
    replay_stack: ArrayVec<TermValue<()>, 4>,
}

impl<'alloc, 'parser> ParserTrait<'alloc, ()> for Simulator<'alloc, 'parser> {
    fn shift(&mut self, tv: TermValue<()>) -> Result<'alloc, bool> {
        // Shift the new terminal/nonterminal and its associated value.
        let mut state = self.state();
        assert!(state < TABLES.shift_count);
        let mut tv = tv;
        loop {
            let term_index: usize = tv.term.into();
            assert!(term_index < TABLES.shift_width);
            let index = state * TABLES.shift_width + term_index;
            let goto = TABLES.shift_table[index];
            if goto < 0 {
                // Error handling is in charge of shifting an ErrorSymbol from the
                // current state.
                self.try_error_handling(tv)?;
                tv = self.replay_stack.pop().unwrap();
                continue;
            }
            state = goto as usize;
            self.sim_state_stack.push(state);
            self.sim_node_stack.push(tv);
            // Execute any actions, such as reduce actions.
            if state >= TABLES.shift_count {
                assert!(state < TABLES.action_count + TABLES.shift_count);
                if noop_actions(self, state)? {
                    return Ok(true);
                }
                state = self.state();
            }
            assert!(state < TABLES.shift_count);
            if let Some(tv_temp) = self.replay_stack.pop() {
                tv = tv_temp;
            } else {
                break;
            }
        }
        Ok(false)
    }
    fn shift_replayed(&mut self, state: usize) {
        let tv = self.replay_stack.pop().unwrap();
        self.sim_state_stack.push(state);
        self.sim_node_stack.push(tv);
    }
    fn unshift(&mut self) {
        let tv = self.pop();
        self.replay(tv)
    }
    fn pop(&mut self) -> TermValue<()> {
        if let Some(s) = self.sim_node_stack.pop() {
            self.sim_state_stack.pop();
            return s;
        }
        let t = self.node_stack[self.sp - 1].term;
        self.sp -= 1;
        TermValue { term: t, value: () }
    }
    fn replay(&mut self, tv: TermValue<()>) {
        self.replay_stack.push(tv)
    }
    fn epsilon(&mut self, state: usize) {
        if self.sim_state_stack.is_empty() {
            self.sim_state_stack.push(self.state_stack[self.sp]);
            self.sim_node_stack.push(TermValue {
                term: self.node_stack[self.sp - 1].term,
                value: (),
            });
            self.sp -= 1;
        }
        *self.sim_state_stack.last_mut().unwrap() = state;
    }
    fn top_state(&self) -> usize {
        self.state()
    }
    fn check_not_on_new_line(&mut self, _peek: usize) -> Result<'alloc, bool> {
        Ok(true)
    }
}

impl<'alloc, 'parser> Simulator<'alloc, 'parser> {
    pub fn new(
        state_stack: &'parser [usize],
        node_stack: &'parser [TermValue<StackValue<'alloc>>],
    ) -> Simulator<'alloc, 'parser> {
        let sp = state_stack.len() - 1;
        assert_eq!(state_stack.len(), node_stack.len() + 1);
        Simulator {
            sp,
            state_stack,
            node_stack,
            sim_state_stack: ArrayVec::new(),
            sim_node_stack: ArrayVec::new(),
            replay_stack: ArrayVec::new(),
        }
    }

    fn state(&self) -> usize {
        if let Some(res) = self.sim_state_stack.last() {
            *res
        } else {
            self.state_stack[self.sp]
        }
    }

    pub fn write_token(&mut self, t: TerminalId) -> Result<'alloc, ()> {
        // Shift the token with the associated StackValue.
        let accept = self.shift(TermValue {
            term: t.into(),
            value: (),
        })?;
        // JavaScript grammar accepts empty inputs, therefore we can never
        // accept any program before receiving a TerminalId::End.
        assert!(!accept);
        Ok(())
    }

    pub fn close(&mut self, _position: usize) -> Result<'alloc, ()> {
        // Shift the End terminal with the associated StackValue.
        let accept = self.shift(TermValue {
            term: TerminalId::End.into(),
            value: (),
        })?;
        // Adding a TerminalId::End would either lead to a parse error, or to
        // accepting the current input. In which case we return matching node
        // value.
        assert!(accept);

        // We can either reduce a Script/Module, or a Script/Module followed by
        // an <End> terminal.
        assert!(self.sp + self.sim_node_stack.len() >= 1);
        Ok(())
    }

    // Simulate the action of Parser::try_error_handling.
    fn try_error_handling(&mut self, t: TermValue<()>) -> Result<'alloc, bool> {
        if t.term.is_terminal() {
            let term = t.term.to_terminal();
            let bogus_loc = SourceLocation::new(0, 0);
            let token = &Token::basic_token(term, bogus_loc);

            // Error tokens might them-self cause more errors to be reported.
            // This happens due to the fact that the ErrorToken can be replayed,
            // and while the ErrorToken might be in the lookahead rules, it
            // might not be in the shifted terms coming after the reduced
            // nonterminal.
            if term == TerminalId::ErrorToken {
                return Err(Parser::parse_error(token).into());
            }

            // Otherwise, check if the current rule accept an Automatic
            // Semi-Colon insertion (ASI).
            let state = self.state();
            assert!(state < TABLES.shift_count);
            let error_code = TABLES.error_codes[state];
            if let Some(error_code) = error_code {
                Parser::recover(token, error_code)?;
                self.replay(t);
                self.replay(TermValue {
                    term: TerminalId::ErrorToken.into(),
                    value: (),
                });
                return Ok(false);
            }
            return Err(Parser::parse_error(token).into());
        }
        // On error, don't attempt error handling again.
        Err(ParseError::ParserCannotUnpackToken.into())
    }
}