pub struct Parser<'a> { /* private fields */ }Expand description
Recursive descent Perl parser with error recovery and AST generation. Parser state for a single Perl source input.
Construct with Parser::new and call Parser::parse to obtain an AST.
Non-fatal syntax errors are collected and can be accessed via Parser::errors.
Implementations§
Source§impl<'a> Parser<'a>
impl<'a> Parser<'a>
Sourcepub fn new(input: &'a str) -> Parser<'a>
pub fn new(input: &'a str) -> Parser<'a>
Create a new parser for the provided Perl source.
§Arguments
input- Perl source code to be parsed
§Returns
A configured parser ready to parse the provided source.
§Examples
use perl_parser_core::Parser;
let script = "use strict; my $filter = qr/important/;";
let mut parser = Parser::new(script);
// Parser ready to parse the sourceSourcepub fn new_with_cancellation(
input: &'a str,
cancellation_flag: Arc<Atomic<bool>>,
) -> Parser<'a>
pub fn new_with_cancellation( input: &'a str, cancellation_flag: Arc<Atomic<bool>>, ) -> Parser<'a>
Create a new parser with a cancellation flag for cooperative cancellation.
When the flag is set to true, the parser will return Err(ParseError::Cancelled)
at the next cancellation check point (every 64 statements).
Sourcepub fn new_with_recovery_config(input: &'a str, _config: ()) -> Parser<'a>
pub fn new_with_recovery_config(input: &'a str, _config: ()) -> Parser<'a>
Create a new parser with custom enhanced recovery configuration.
This constructor exists for API compatibility while enhanced recovery configuration is being phased in.
§Arguments
input- Perl source text to tokenize and parse._config- Placeholder recovery configuration parameter.
§Returns
A parser instance initialized for the provided source text.
§Examples
use perl_parser_core::Parser;
let parser = Parser::new_with_recovery_config("my $x = 1;", ());
assert_eq!(parser.errors().len(), 0);Sourcepub fn parse(&mut self) -> Result<Node, ParseError>
pub fn parse(&mut self) -> Result<Node, ParseError>
Parse the source and return the AST for the Parse stage.
§Returns
Ok(Node)- Parsed AST with aProgramroot node.Err(ParseError)- Non-recoverable parsing failure.
§Errors
Returns ParseError for non-recoverable conditions such as recursion limits.
§Examples
use perl_parser_core::Parser;
let mut parser = Parser::new("my $count = 1;");
let ast = parser.parse()?;
assert!(matches!(ast.kind, perl_parser_core::NodeKind::Program { .. }));Sourcepub fn errors(&self) -> &[ParseError]
pub fn errors(&self) -> &[ParseError]
Get all parse errors collected during parsing
When error recovery is enabled, the parser continues after syntax errors and collects them for later retrieval. This is useful for IDE integration where you want to show all errors at once.
§Returns
A slice of all ParseErrors encountered during parsing
§Examples
use perl_parser_core::Parser;
let mut parser = Parser::new("my $x = ; sub foo {");
let _ast = parser.parse(); // Parse with recovery
let errors = parser.errors();
// errors will contain details about syntax errorsSourcepub fn parse_with_recovery(&mut self) -> ParseOutput
pub fn parse_with_recovery(&mut self) -> ParseOutput
Parse with error recovery and return comprehensive output.
This method is preferred for LSP Analyze workflows and always returns
a ParseOutput containing the AST and any collected diagnostics.
§Returns
ParseOutput with the AST and diagnostics collected during parsing.
§Examples
use perl_parser_core::Parser;
let mut parser = Parser::new("my $x = ;");
let output = parser.parse_with_recovery();
assert!(!output.diagnostics.is_empty() || matches!(output.ast.kind, perl_parser_core::NodeKind::Program { .. }));Source§impl<'a> Parser<'a>
impl<'a> Parser<'a>
Sourcepub fn get_errors(&self) -> &[ParseError]
pub fn get_errors(&self) -> &[ParseError]
Get all recorded errors