Struct syntect::parsing::ParseState

source ·
pub struct ParseState { /* private fields */ }
Expand description

Keeps the current parser state (the internal syntax interpreter stack) between lines of parsing.

If you are parsing an entire file you create one of these at the start and use it all the way to the end.

Caching

One reason this is exposed is that since it implements Clone you can actually cache these (probably along with a HighlightState) and only re-start parsing from the point of a change. See the docs for HighlightState for more in-depth discussion of caching.

This state doesn’t keep track of the current scope stack and parsing only returns changes to this stack so if you want to construct scope stacks you’ll need to keep track of that as well. Note that HighlightState contains exactly this as a public field that you can use.

Note: Caching is for advanced users who have tons of time to maximize performance or want to do so eventually. It is not recommended that you try caching the first time you implement highlighting.

Implementations§

source§

impl ParseState

source

pub fn new(syntax: &SyntaxReference) -> ParseState

Creates a state from a syntax definition, keeping its own reference-counted point to the main context of the syntax

source

pub fn parse_line( &mut self, line: &str, syntax_set: &SyntaxSet ) -> Result<Vec<(usize, ScopeStackOp)>, ParsingError>

Parses a single line of the file. Because of the way regex engines work you unfortunately have to pass in a single line contiguous in memory. This can be bad for really long lines. Sublime Text avoids this by just not highlighting lines that are too long (thousands of characters).

For efficiency reasons this returns only the changes to the current scope at each point in the line. You can use ScopeStack::apply on each operation in succession to get the stack for a given point. Look at the code in highlighter.rs for an example of doing this for highlighting purposes.

The returned vector is in order both by index to apply at (the usize) and also by order to apply them at a given index (e.g popping old scopes before pushing new scopes).

The SyntaxSet has to be the one that contained the syntax that was used to construct this ParseState, or an extended version of it. Otherwise the parsing would return the wrong result or even panic. The reason for this is that contexts within the SyntaxSet are referenced via indexes.

Trait Implementations§

source§

impl Clone for ParseState

source§

fn clone(&self) -> ParseState

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for ParseState

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq<ParseState> for ParseState

source§

fn eq(&self, other: &ParseState) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for ParseState

source§

impl StructuralEq for ParseState

source§

impl StructuralPartialEq for ParseState

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.