Skip to main content

oxc_parser/embedding/
borrowed.rs

1//! Short immutable admission over the original complete parser observation.
2
3use oxc_ast::ast::{Comment, Expression};
4use oxc_diagnostics::Diagnostics;
5use oxc_span::{GetSpan, SourceType, Span};
6
7use super::EmbeddingObservation;
8use crate::ParseOptions;
9
10/// A root authenticated by its original embedding owner, without moving it.
11/// The root remains borrowed for the owner's lifetime, not the arena lifetime.
12/// No caller AST, source window or replacement admission status can mint this.
13pub struct AdmittedBorrowedExpression<'o, 'a> {
14    owner: &'o EmbeddingObservation<'a>,
15    root: &'o Expression<'a>,
16}
17
18impl<'a> EmbeddingObservation<'a> {
19    /// Borrow only the original successfully admitted Expr root. Existing local
20    /// holes and other goals retain their observations without exposing a proof.
21    /// Selection checks the stored fixed wrapper in constant size; this never
22    /// consumes the owner, allocates, parses or walks expression descendants.
23    #[must_use]
24    pub fn admitted_expression(&self) -> Option<AdmittedBorrowedExpression<'_, 'a>> {
25        let root = self.expression()?;
26        Some(AdmittedBorrowedExpression { owner: self, root })
27    }
28}
29
30impl<'o, 'a> AdmittedBorrowedExpression<'o, 'a> {
31    #[must_use]
32    pub const fn original(&self) -> &'o EmbeddingObservation<'a> {
33        self.owner
34    }
35    #[must_use]
36    pub const fn expression(&self) -> &'o Expression<'a> {
37        self.root
38    }
39    #[must_use]
40    pub const fn content(&self) -> &'a str {
41        self.owner.content
42    }
43    #[must_use]
44    pub const fn source_type(&self) -> SourceType {
45        self.owner.source_type
46    }
47    #[must_use]
48    pub const fn options(&self) -> ParseOptions {
49        self.owner.options
50    }
51    #[must_use]
52    pub const fn parser_content_span(&self) -> Span {
53        self.owner.content_span
54    }
55    #[must_use]
56    pub fn parser_container_span(&self) -> Span {
57        self.root.span()
58    }
59    #[must_use]
60    pub fn comments(&self) -> &'o [Comment] {
61        self.owner.comments()
62    }
63    #[must_use]
64    pub fn diagnostics(&self) -> &'o Diagnostics {
65        self.owner.diagnostics()
66    }
67    /// Borrow the stored stock-lexer fact without another source or AST scan.
68    /// False does not certify semantic or classic-runtime early-error validity.
69    #[must_use]
70    pub const fn has_legacy_literals(&self) -> bool {
71        self.owner.parsed.has_legacy_literals
72    }
73}