Skip to main content

ParserContext

Struct ParserContext 

Source
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 ParseResult 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

Source

pub fn new() -> Self

Create a new context with an empty arena.

Source

pub fn reparse<'a, 'src>( &'a mut self, source: &'src str, ) -> ParseResult<'a, 'src>

Reset the arena and parse source using PHP 8.5 (the latest version).

The previous ParseResult 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.

Source

pub fn reparse_versioned<'a, 'src>( &'a mut self, source: &'src str, version: PhpVersion, ) -> ParseResult<'a, 'src>

Reset the arena and parse source targeting the given PHP version.

See reparse for lifetime safety notes.

Trait Implementations§

Source§

impl Default for ParserContext

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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.