Skip to main content

Parser

Struct Parser 

Source
pub struct Parser {
    pub diagnostics: DiagnosticBag,
    /* private fields */
}
Expand description

Top-down operator-precedence (Pratt) parser for CJC source code.

Construct a Parser with Parser::new, then call Parser::parse_program to consume the token stream and produce a Program AST together with any accumulated DiagnosticBag.

§Example

use cjc_lexer::Lexer;
use cjc_parser::Parser;

let (tokens, _) = Lexer::new("fn f() { 1 + 2 }").tokenize();
let parser = Parser::new(tokens);
let (program, diags) = parser.parse_program();
assert!(!diags.has_errors());

Fields§

§diagnostics: DiagnosticBag

Diagnostic bag that collects parse errors and warnings.

Callers can inspect this after parsing completes, but the preferred approach is to use the DiagnosticBag returned by Parser::parse_program.

Implementations§

Source§

impl Parser

Source

pub fn new(tokens: Vec<Token>) -> Self

Create a new parser from a token stream.

The token vector should come from cjc_lexer::Lexer::tokenize and must end with a TokenKind::Eof sentinel (the lexer guarantees this).

§Arguments
  • tokens - Complete token stream produced by the lexer.
§Returns

A Parser ready to be consumed by Parser::parse_program.

Source

pub fn parse_program(self) -> (Program, DiagnosticBag)

Parse the entire token stream into a Program AST.

Consume self and return the resulting program together with all diagnostics accumulated during parsing. The parser uses error recovery (via synchronize) so a Program is always returned, even when errors are present.

§Returns

A tuple of:

§Example
let (tokens, _) = cjc_lexer::Lexer::new("let x = 1;").tokenize();
let (program, diags) = cjc_parser::Parser::new(tokens).parse_program();

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.