Skip to main content

Parser

Struct Parser 

Source
pub struct Parser<'a> { /* private fields */ }
Expand description

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>

Source

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

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).

Source

pub fn from_tokens(tokens: Vec<Token>, source: &'a str) -> Parser<'a>

Create a parser from pre-lexed tokens, skipping the lexer pass.

This constructor is the integration point for the incremental parsing pipeline: when cached tokens are available for an unchanged region of source, they can be fed directly into the parser without re-lexing.

§Arguments
  • tokens — Pre-lexed Token values produced by a prior TokenStream pass. Trivia tokens (whitespace, comments) should already be filtered out, as TokenStream::from_vec does not apply trivia skipping. An Eof token does not need to be included; the stream synthesises one when the buffer is exhausted.
  • source — The original Perl source text. This is still required for heredoc content collection which operates directly on byte offsets in the source rather than on the token stream.
§Returns

A configured parser that will consume tokens in order without invoking the lexer. The resulting AST is structurally identical to one produced by Parser::new with the same source, provided the token list is complete and accurate.

§Context-sensitive token disambiguation

The standard parser uses relex_as_term to re-lex ambiguous tokens (e.g. / as division vs. regex) in context-sensitive positions. When using pre-lexed tokens the kind is fixed from the original lex pass, so the original parse context must have been correct. In practice this means from_tokens is safe to use when the token stream comes from a previous successful parse of the same source.

§Examples
use perl_parser_core::{Parser, Token, TokenKind, TokenStream};

let source = "my $x = 42;";

// Collect pre-lexed tokens (normally cached from a prior parse)
let mut stream = TokenStream::new(source);
let mut tokens = Vec::new();
loop {
    match stream.next() {
        Ok(t) if t.kind == TokenKind::Eof => break,
        Ok(t) => tokens.push(t),
        Err(_) => break,
    }
}

let mut parser = Parser::from_tokens(tokens, source);
let ast = parser.parse()?;
assert!(matches!(ast.kind, perl_parser_core::NodeKind::Program { .. }));
Source

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);
Source

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 a Program root 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 { .. }));
Source

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

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>

Source

pub fn get_errors(&self) -> &[ParseError]

Get all recorded errors

Auto Trait Implementations§

§

impl<'a> Freeze for Parser<'a>

§

impl<'a> RefUnwindSafe for Parser<'a>

§

impl<'a> Send for Parser<'a>

§

impl<'a> Sync for Parser<'a>

§

impl<'a> Unpin for Parser<'a>

§

impl<'a> UnsafeUnpin for Parser<'a>

§

impl<'a> UnwindSafe for Parser<'a>

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more