Skip to main content

JSParserImpl

Struct JSParserImpl 

Source
pub struct JSParserImpl<'gc, 'ast, 'ctx, 'a> { /* private fields */ }
Expand description

The JS parser.

Four lifetime parameters:

  • 'gc: the borrow-of-lock lifetime. gc.alloc(n) returns &'gc Node<'gc> because GCLock::alloc<'s>(&'s self, ..) makes 's = 'gc. This is also the node child-ref lifetime (i.e. child refs inside built nodes are &'gc Node<'gc>).
  • 'ast: the Context’s own arena lifetime (first type param of GCLock<'ast, 'ctx>). Kept separate so the borrow 'gc doesn’t accidentally constrain when the Context was created.
  • 'ctx: the &mut Context borrow lifetime (second type param). Kept separate for the same reason.
  • 'a: the lexer’s borrow lifetimes (&'a mut SourceErrorManager and &'a AtomTable).

In practice 'ast, 'ctx, and 'a all unify to the enclosing call frame, so callers see no extra friction.

Port of lib/Parser/JSParserImpl.h/.cpp.

Implementations§

Source§

impl<'gc, 'ast, 'ctx, 'a> JSParserImpl<'gc, 'ast, 'ctx, 'a>

Source

pub fn pre_parse_buffer( gc: &'gc GCLock<'ast, 'ctx>, lexer: JSLexer<'a>, strict: bool, ) -> Option<JSParserImpl<'gc, 'ast, 'ctx, 'a>>

Port of JSParserImpl::preParseBuffer (JSParserImpl.cpp:7534-7546). The C++ PreParser wrapper holds an AllocationScope (cpp:7523) that reclaims the whole pass AST when the returned shared_ptr dies; here the scope is opened around parse() and dropped before returning — tighter, and sound because the Program result is discarded and JSParserImpl holds no node references. The pass output is the side-table + parser flags only.

Source

pub fn parse_lazy_function( &mut self, kind: NodeKind, param_yield: bool, param_await: bool, start: SMLoc, ) -> Option<&'gc Node<'gc>>

On-demand parse of a single deferred function body. Called when a previously lazy-stubbed function is first executed: the parser is seeked back to start and the function is re-parsed eagerly so its real body (instead of the lazy stub) is produced.

Port of JSParserImpl::parseLazyFunction (JSParserImpl.cpp:7548-7600). kind selects which eager entry point to drive; param_yield/ param_await restore the grammar context the function was originally parsed in. Returns the re-parsed function node (the FunctionExpression, FunctionDeclaration, or ArrowFunctionExpression), or — for accessors and class methods — the value function extracted from the wrapping Property/MethodDefinition node (cpp:7572,7591).

Source§

impl<'gc, 'ast, 'ctx, 'a> JSParserImpl<'gc, 'ast, 'ctx, 'a>

Source

pub fn new(gc: &'gc GCLock<'ast, 'ctx>, lexer: JSLexer<'a>) -> Self

Construct the parser and lex the first token (C++ ctor does tok_ = lexer_.advance()).

Source

pub fn new_with_pass( gc: &'gc GCLock<'ast, 'ctx>, lexer: JSLexer<'a>, pass: ParserPass, ) -> Self

Construct the parser in a specific pass. Port of the C++ JSParserImpl(Context&, bufferId, ParserPass) ctor (JSParserImpl.cpp:39).

Source

pub fn get_use_static_builtin(&self) -> bool

True if the parser detected use static builtin.

Source

pub fn take_pre_parsed(&mut self) -> PreParsedBufferInfo

Move the pre-parsed side-table out of the parser (leaving an empty one in its place). Called after a PreParse run to hand the table to the caller before a subsequent LazyParse.

Source

pub fn set_pre_parsed(&mut self, t: PreParsedBufferInfo)

Install a pre-parsed side-table produced by a prior PreParse run. Called before a LazyParse so the parser can skip already-indexed function bodies.

Source

pub fn set_strict_mode(&mut self, strict: bool)

Set the strict-mode flag on the underlying lexer. Mirrors the C++ JSParser::setStrictMode (JSParser.h:66-68) that HBC.cpp:158 calls immediately before parseLazyFunction to propagate the function’s recorded strict mode into the re-parse.

Source

pub fn parse(&mut self) -> Option<&'gc Node<'gc>>

Parse the whole program. Entry point for the parser (and, on the C++ side, of the PreParse pass too — JSParserImpl::preParseBuffer, JSParserImpl.cpp:7539, calls this same parse(); parseLazyFunction is a separate entry with no such gate and is unaffected). Port of JSParserImpl::parse (JSParserImpl.cpp:164-172):

Optional<ESTree::ProgramNode *> JSParserImpl::parse() {
  PerfSection parsing("Parsing JavaScript");
  tok_ = lexer_.advance();
  auto res = parseProgram();
  if (!res)
    return None;
  if (lexer_.getSourceMgr().getErrorCount() != 0)
    return None;
  return res.getValue();
}

tok_ = lexer_.advance() is done by the lexer’s own construction (see Self::new’s doc); the tail gate is ported here: even when parseProgram recovers and returns a tree, a nonzero error count (e.g. a strict-mode octal literal) discards it.

Auto Trait Implementations§

§

impl<'gc, 'ast, 'ctx, 'a> !RefUnwindSafe for JSParserImpl<'gc, 'ast, 'ctx, 'a>

§

impl<'gc, 'ast, 'ctx, 'a> !Send for JSParserImpl<'gc, 'ast, 'ctx, 'a>

§

impl<'gc, 'ast, 'ctx, 'a> !Sync for JSParserImpl<'gc, 'ast, 'ctx, 'a>

§

impl<'gc, 'ast, 'ctx, 'a> !UnwindSafe for JSParserImpl<'gc, 'ast, 'ctx, 'a>

§

impl<'gc, 'ast, 'ctx, 'a> Freeze for JSParserImpl<'gc, 'ast, 'ctx, 'a>

§

impl<'gc, 'ast, 'ctx, 'a> Unpin for JSParserImpl<'gc, 'ast, 'ctx, 'a>

§

impl<'gc, 'ast, 'ctx, 'a> UnsafeUnpin for JSParserImpl<'gc, 'ast, 'ctx, 'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.