pub struct ParserContext { /* private fields */ }Expand description
A reusable parse context that keeps a bumpalo::Bump arena alive between
re-parses, resetting it (O(1)) instead of dropping and reallocating.
This is the preferred entry point for LSP servers or any tool that parses the same document repeatedly. Once the arena has grown to accommodate the largest document seen, subsequent parses reuse the backing memory without any new allocations.
The Rust lifetime system enforces safety: the returned ArenaParseResult
borrows from self, so the borrow checker prevents calling reparse or
reparse_versioned again while the previous result is still alive.
§Example
let mut ctx = php_rs_parser::ParserContext::new();
let result = ctx.reparse("<?php echo 1;");
assert!(result.errors.is_empty());
drop(result); // must be dropped before the next reparse
let result = ctx.reparse("<?php echo 2;");
assert!(result.errors.is_empty());Implementations§
Source§impl ParserContext
impl ParserContext
Sourcepub fn reparse<'a, 'src>(
&'a mut self,
source: &'src str,
) -> ArenaParseResult<'a, 'src>
pub fn reparse<'a, 'src>( &'a mut self, source: &'src str, ) -> ArenaParseResult<'a, 'src>
Reset the arena and parse source using PHP 8.5 (the latest version).
The previous ArenaParseResult must be dropped before calling
this method. The borrow checker enforces this: the returned result
borrows self for the duration of its lifetime, so a second call while
the first result is still live is a compile-time error.
Sourcepub fn reparse_versioned<'a, 'src>(
&'a mut self,
source: &'src str,
version: PhpVersion,
) -> ArenaParseResult<'a, 'src>
pub fn reparse_versioned<'a, 'src>( &'a mut self, source: &'src str, version: PhpVersion, ) -> ArenaParseResult<'a, 'src>
Reset the arena and parse source targeting the given PHP version.
See reparse for lifetime safety notes.
Sourcepub fn reparse_owned(&mut self, source: &str) -> ParseResult
pub fn reparse_owned(&mut self, source: &str) -> ParseResult
Reset the arena and parse source, returning a fully-owned ParseResult.
Unlike reparse, the returned result has no
lifetime parameters and can be stored anywhere. The arena is reused for
the parse but the output is immediately converted to owned types, so
there is no borrow on self after this call returns.
Sourcepub fn reparse_owned_versioned(
&mut self,
source: &str,
version: PhpVersion,
) -> ParseResult
pub fn reparse_owned_versioned( &mut self, source: &str, version: PhpVersion, ) -> ParseResult
Reset the arena and parse source targeting the given PHP version,
returning a fully-owned ParseResult.
See reparse_owned for ownership notes
and reparse_versioned for version
semantics.