ripex 0.2.0

Multi-language structural parsing and fact extraction.
Documentation
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
use crate::arena::Arena;
use crate::diagnostics::{DiagnosticCode, ParseError};
use crate::js::ast::*;
use crate::js::config::ParserOptions;
use crate::js::lexer::{Comment, Token, TokenKind};
use crate::span::Span;

use super::declarations;
use super::expressions;
use super::modules;
use super::patterns;
use super::statements;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Context {
    TopLevel,
    InFunction,
    InLoop,
    InSwitch,
    InClass,
    InConstructor,
    InModule,
    InAsync,
    InGenerator,
    InArrow,
}

impl Context {
    pub fn has_await(&self) -> bool {
        matches!(self, Context::InAsync | Context::InArrow)
    }

    pub fn has_yield(&self) -> bool {
        matches!(self, Context::InGenerator)
    }

    pub fn has_return(&self) -> bool {
        matches!(
            self,
            Context::InFunction
                | Context::InArrow
                | Context::InConstructor
                | Context::InAsync
                | Context::InGenerator
        )
    }
}

pub struct Parser<'a> {
    pub tokens: Vec<Token>,
    pub pos: usize,
    pub ast: Arena<Expr>,
    pub errors: Vec<ParseError>,
    pub comments: Vec<Comment>,
    pub ctx: Vec<Context>,
    pub options: &'a ParserOptions,
    pub in_async: bool,
    pub in_generator: bool,
    pub depth: u32,
}

impl<'a> Parser<'a> {
    pub fn new(tokens: Vec<Token>, options: &'a ParserOptions) -> Self {
        let mut ctx = Vec::new();
        ctx.push(if options.is_module() {
            Context::InModule
        } else {
            Context::TopLevel
        });
        Parser {
            tokens,
            pos: 0,
            ast: Arena::new(),
            errors: Vec::new(),
            comments: Vec::new(),
            ctx,
            options,
            in_async: false,
            in_generator: false,
            depth: 0,
        }
    }

    pub fn try_new(
        tokens: Vec<Token>,
        options: &'a ParserOptions,
    ) -> Result<Self, Vec<ParseError>> {
        Ok(Self::new(tokens, options))
    }

    pub fn peek(&self) -> TokenKind {
        if self.pos >= self.tokens.len() {
            return TokenKind::Eof;
        }
        self.tokens[self.pos].kind
    }

    pub fn peek_ahead(&self, n: usize) -> TokenKind {
        let idx = self.pos + n;
        if idx >= self.tokens.len() {
            return TokenKind::Eof;
        }
        self.tokens[idx].kind
    }

    pub fn advance(&mut self) -> Token {
        let tok = if self.pos < self.tokens.len() {
            self.tokens[self.pos].clone()
        } else {
            return Token::new(TokenKind::Eof, crate::span::Span::ZERO);
        };
        if !tok.leading_comments.is_empty() && self.options.capture_comments {
            self.comments.extend(tok.leading_comments.iter().cloned());
        }
        self.pos += 1;
        tok
    }

    pub fn expect(&mut self, kind: TokenKind) -> Result<Token, ParseError> {
        if self.peek() == kind {
            Ok(self.advance())
        } else {
            let tok = self.current_token().clone();
            let err = self.error(DiagnosticCode::UnexpectedToken, &tok);
            self.advance();
            Err(err)
        }
    }

    pub fn expect_ident(&mut self) -> String {
        if self.peek() == TokenKind::Ident {
            let token = self.advance();
            token.value
        } else {
            let tok = self.current_token().clone();
            let err = self.error(DiagnosticCode::UnexpectedToken, &tok);
            self.errors.push(err);
            self.advance();
            String::new()
        }
    }

    pub fn expect_advance(&mut self) -> Token {
        self.advance()
    }

    pub fn is_eof(&self) -> bool {
        self.pos >= self.tokens.len() || self.tokens[self.pos].kind == TokenKind::Eof
    }

    pub fn current_pos(&self) -> usize {
        self.pos
    }

    pub fn current_token(&self) -> &Token {
        if self.pos >= self.tokens.len() {
            static EOF: Token = Token {
                kind: TokenKind::Eof,
                span: Span::ZERO,
                value: String::new(),
                leading_comments: Vec::new(),
                has_line_break: false,
            };
            return &EOF;
        }
        &self.tokens[self.pos]
    }

    pub fn previous_token(&self) -> Option<&Token> {
        if self.pos == 0 {
            return None;
        }
        Some(&self.tokens[self.pos - 1])
    }

    pub fn token_at(&self, i: usize) -> &Token {
        if i < self.tokens.len() {
            &self.tokens[i]
        } else if let Some(last) = self.tokens.last() {
            last
        } else {
            static EOF_TOKEN: std::sync::LazyLock<Token> =
                std::sync::LazyLock::new(|| Token::new(TokenKind::Eof, Span::ZERO));
            &EOF_TOKEN
        }
    }

    pub fn span_since(&self, start: usize) -> Span {
        if self.tokens.is_empty() {
            return Span::ZERO;
        }
        let start_idx = start.min(self.tokens.len() - 1);
        let start_pos = self.tokens[start_idx].span.start;
        let end_pos = if self.pos > 0 {
            let end_idx = (self.pos - 1).min(self.tokens.len() - 1);
            self.tokens[end_idx].span.end
        } else {
            start_pos
        };
        Span::new(start_pos, end_pos)
    }

    pub fn span_between(&self, start: usize, end: usize) -> Span {
        if self.tokens.is_empty() {
            return Span::ZERO;
        }
        let start_idx = start.min(self.tokens.len() - 1);
        let start_pos = self.tokens[start_idx].span.start;
        let end_pos = if end > 0 {
            let end_idx = (end - 1).min(self.tokens.len() - 1);
            self.tokens[end_idx].span.end
        } else {
            start_pos
        };
        Span::new(start_pos, end_pos)
    }

    pub fn error(&mut self, code: DiagnosticCode, t: &Token) -> ParseError {
        ParseError::new(code, t.span)
    }

    pub fn error_at(&mut self, code: DiagnosticCode, span: Span) -> ParseError {
        ParseError::new(code, span)
    }

    pub fn error_msg(
        &mut self,
        code: DiagnosticCode,
        span: Span,
        msg: impl Into<String>,
    ) -> ParseError {
        ParseError::with_message(code, span, msg)
    }

    pub fn expr_span(&self, id: ExprRef) -> Span {
        use super::expressions::HasSpan;
        self.ast[id].span()
    }

    // ---- Recursion guard ----

    pub fn bump_recursion(&mut self) -> Result<(), ParseError> {
        self.depth += 1;
        if self.depth > crate::limits::MAX_RECURSION {
            let err = ParseError::new(
                DiagnosticCode::MaxRecursionExceeded,
                self.current_token().span,
            );
            if !self
                .errors
                .iter()
                .any(|existing| existing.code == DiagnosticCode::MaxRecursionExceeded)
            {
                self.errors.push(err.clone());
            }
            self.depth -= 1;
            return Err(err);
        }
        Ok(())
    }

    pub fn pop_recursion(&mut self) {
        self.depth = self.depth.saturating_sub(1);
    }

    // ---- Context helpers ----

    pub fn push_ctx(&mut self, ctx: Context) {
        self.ctx.push(ctx);
    }

    pub fn pop_ctx(&mut self) {
        self.ctx.pop();
    }

    pub fn in_async_ctx(&self) -> bool {
        for context in self.ctx.iter().rev() {
            match context {
                Context::InAsync | Context::InArrow | Context::InModule => return true,
                Context::InFunction | Context::InConstructor => return false,
                _ => {}
            }
        }
        false
    }

    pub fn in_generator_ctx(&self) -> bool {
        self.ctx.iter().any(|c| c.has_yield())
    }

    pub fn in_function_ctx(&self) -> bool {
        self.ctx.iter().any(|c| c.has_return())
    }

    pub fn in_loop_ctx(&self) -> bool {
        self.ctx.iter().any(|c| matches!(c, Context::InLoop))
    }

    pub fn in_switch_ctx(&self) -> bool {
        self.ctx.iter().any(|c| matches!(c, Context::InSwitch))
    }

    // ---- Entry points ----

    pub fn parse_program(&mut self) -> Program {
        if self.options.is_module() {
            Program::Module(self.parse_module())
        } else {
            let start = self.current_pos();
            let body = self.parse_script();
            Program::Script(Script {
                span: self.span_since(start),
                body,
            })
        }
    }

    pub fn parse_module(&mut self) -> Module {
        let start = self.current_pos();
        let mut body = Vec::new();
        while !self.is_eof() {
            if self.peek() == TokenKind::Hashbang {
                self.advance();
                continue;
            }
            match self.peek() {
                TokenKind::Import => {
                    body.push(ModuleItem::Import(self.parse_import()));
                }
                TokenKind::Export => {
                    body.push(ModuleItem::Export(self.parse_export()));
                }
                _ => {
                    if let Some(stmt) = self.parse_stmt() {
                        body.push(ModuleItem::Stmt(stmt));
                    } else {
                        break;
                    }
                }
            }
        }
        Module {
            span: self.span_since(start),
            body,
        }
    }

    pub fn parse_script(&mut self) -> Vec<Stmt> {
        let mut stmts = Vec::new();
        while !self.is_eof() {
            if self.peek() == TokenKind::Hashbang {
                self.advance();
                continue;
            }
            if let Some(stmt) = self.parse_stmt() {
                stmts.push(stmt);
            } else {
                break;
            }
        }
        stmts
    }

    // ---- Expression forwarding ----

    pub fn alloc_expr(&mut self, expr: Expr) -> ExprRef {
        self.ast.alloc(expr)
    }

    pub fn parse_expr(&mut self) -> ExprRef {
        if self.bump_recursion().is_err() {
            return self.ast.alloc(Expr::Ident(Ident {
                name: String::new(),
                span: Span::ZERO,
                optional: false,
            }));
        }
        let result = expressions::parse_expr(self, 0);
        self.pop_recursion();
        result
    }

    pub fn parse_assignment_expr(&mut self) -> ExprRef {
        if self.bump_recursion().is_err() {
            return self.ast.alloc(Expr::Ident(Ident {
                name: String::new(),
                span: Span::ZERO,
                optional: false,
            }));
        }
        let result = expressions::parse_assign_expr(self);
        self.pop_recursion();
        result
    }

    pub fn parse_cond_expr(&mut self) -> ExprRef {
        if self.bump_recursion().is_err() {
            return self.ast.alloc(Expr::Ident(Ident {
                name: String::new(),
                span: Span::ZERO,
                optional: false,
            }));
        }
        let result = expressions::parse_cond_expr(self);
        self.pop_recursion();
        result
    }

    // ---- Statement forwarding ----

    pub fn parse_stmt(&mut self) -> Option<Stmt> {
        if self.bump_recursion().is_err() {
            return Some(Stmt::Empty(EmptyStmt { span: Span::ZERO }));
        }
        let result = statements::parse_stmt(self);
        self.pop_recursion();
        result
    }

    pub fn parse_block(&mut self) -> BlockStmt {
        if self.bump_recursion().is_err() {
            return BlockStmt {
                span: Span::ZERO,
                stmts: Vec::new(),
            };
        }
        let result = statements::parse_block(self);
        self.pop_recursion();
        result
    }

    // ---- Declaration forwarding ----

    pub fn parse_decl(&mut self) -> Option<Decl> {
        declarations::parse_decl(self)
    }

    pub fn parse_var_stmt(&mut self) -> Stmt {
        declarations::parse_var_stmt(self)
    }

    pub fn parse_fn_decl(&mut self) -> FnDecl {
        declarations::parse_fn_decl(self)
    }

    pub fn parse_class_decl(&mut self) -> ClassDecl {
        declarations::parse_class_decl(self)
    }

    pub fn parse_fn_expr(&mut self) -> FnExpr {
        declarations::parse_fn_expr(self)
    }

    pub fn parse_class_expr(&mut self) -> ClassExpr {
        declarations::parse_class_expr(self)
    }

    // ---- Pattern forwarding ----

    pub fn parse_pat(&mut self) -> Pat {
        patterns::parse_pat(self)
    }

    pub fn parse_binding_pat(&mut self) -> Pat {
        patterns::parse_binding_pat(self)
    }

    pub fn parse_assignment_pat(&mut self) -> Pat {
        patterns::parse_assignment_pat(self)
    }

    // ---- Module forwarding ----

    pub fn parse_import(&mut self) -> ImportDecl {
        modules::parse_import(self)
    }

    pub fn parse_export(&mut self) -> ExportDecl {
        modules::parse_export(self)
    }
}