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>becauseGCLock::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: theContext’s own arena lifetime (first type param ofGCLock<'ast, 'ctx>). Kept separate so the borrow'gcdoesn’t accidentally constrain when theContextwas created.'ctx: the&mut Contextborrow lifetime (second type param). Kept separate for the same reason.'a: the lexer’s borrow lifetimes (&'a mut SourceErrorManagerand&'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>
impl<'gc, 'ast, 'ctx, 'a> JSParserImpl<'gc, 'ast, 'ctx, 'a>
Sourcepub fn pre_parse_buffer(
gc: &'gc GCLock<'ast, 'ctx>,
lexer: JSLexer<'a>,
strict: bool,
) -> Option<JSParserImpl<'gc, 'ast, 'ctx, 'a>>
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.
Sourcepub fn parse_lazy_function(
&mut self,
kind: NodeKind,
param_yield: bool,
param_await: bool,
start: SMLoc,
) -> Option<&'gc Node<'gc>>
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>
impl<'gc, 'ast, 'ctx, 'a> JSParserImpl<'gc, 'ast, 'ctx, 'a>
Sourcepub fn new(gc: &'gc GCLock<'ast, 'ctx>, lexer: JSLexer<'a>) -> Self
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()).
Sourcepub fn new_with_pass(
gc: &'gc GCLock<'ast, 'ctx>,
lexer: JSLexer<'a>,
pass: ParserPass,
) -> Self
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).
Sourcepub fn get_use_static_builtin(&self) -> bool
pub fn get_use_static_builtin(&self) -> bool
True if the parser detected use static builtin.
Sourcepub fn take_pre_parsed(&mut self) -> PreParsedBufferInfo
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.
Sourcepub fn set_pre_parsed(&mut self, t: PreParsedBufferInfo)
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.
Sourcepub fn set_strict_mode(&mut self, strict: bool)
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.
Sourcepub fn parse(&mut self) -> Option<&'gc Node<'gc>>
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.