pub struct Parse<T> { /* private fields */ }Expand description
The result of parsing: a syntax tree and a collection of errors.
This type is designed to be stored in Salsa databases as it contains
the thread-safe GreenNode instead of the non-thread-safe SyntaxNode.
Implementations§
Source§impl Parse<Makefile>
impl Parse<Makefile>
Sourcepub fn apply_edit(&self, old_text: &str, edit: &TextEdit) -> (Self, String)
pub fn apply_edit(&self, old_text: &str, edit: &TextEdit) -> (Self, String)
Apply an incremental text edit and reparse only the affected region.
This is more efficient than a full reparse for large files, as it reuses the green tree nodes for unaffected top-level items.
§Arguments
old_text- The full text before the editedit- The edit to apply
§Returns
A new Parse<Makefile> with the edit applied, and the new full text.
§Example
use makefile_lossless::{Makefile, Parse, TextEdit, TextRange};
let old_text = "VAR1 = old\nVAR2 = value\n";
let parse = Parse::<Makefile>::parse_makefile(old_text);
// Change "old" to "new" in VAR1
let edit = TextEdit::new(
TextRange::new(7.into(), 10.into()),
"new".to_string(),
);
let (new_parse, new_text) = parse.apply_edit(old_text, &edit);
assert_eq!(new_text, "VAR1 = new\nVAR2 = value\n");
let makefile: Makefile = new_parse.tree();
let vars: Vec<_> = makefile.variable_definitions().collect();
assert_eq!(vars.len(), 2);
assert_eq!(vars[0].raw_value(), Some("new".to_string()));
assert_eq!(vars[1].raw_value(), Some("value".to_string()));Source§impl<T> Parse<T>
impl<T> Parse<T>
Sourcepub fn new(
green: GreenNode,
errors: Vec<ErrorInfo>,
positioned_errors: Vec<PositionedParseError>,
) -> Self
pub fn new( green: GreenNode, errors: Vec<ErrorInfo>, positioned_errors: Vec<PositionedParseError>, ) -> Self
Create a new Parse result from a GreenNode, errors, and positioned errors
Sourcepub fn positioned_errors(&self) -> &[PositionedParseError]
pub fn positioned_errors(&self) -> &[PositionedParseError]
Get parse errors with position information
Sourcepub fn to_result(self) -> Result<T, Error>
pub fn to_result(self) -> Result<T, Error>
Convert to a Result, returning the tree if there are no errors
Sourcepub fn tree(&self) -> T
pub fn tree(&self) -> T
Get the parsed syntax tree
Returns the tree even if there are parse errors. Use errors(),
positioned_errors(), or ok() to check for errors separately if needed.
This allows for error-resilient tooling that can work with partial/invalid input.
Sourcepub fn syntax_node(&self) -> SyntaxNode<Lang>
pub fn syntax_node(&self) -> SyntaxNode<Lang>
Get the syntax node