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 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 new() -> Self
pub fn new() -> Self
Create a new context with an empty arena and the default ParseOptions.
Sourcepub fn with_options(options: ParseOptions) -> Self
pub fn with_options(options: ParseOptions) -> Self
Create a new context with an empty arena that parses with options.
Sourcepub fn options(&self) -> &ParseOptions
pub fn options(&self) -> &ParseOptions
The options used by every reparse.
Sourcepub fn set_options(&mut self, options: ParseOptions)
pub fn set_options(&mut self, options: ParseOptions)
Replace the options used by later reparses, keeping the arena.
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.
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_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.