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: DiagnosticBagDiagnostic 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
impl Parser
Sourcepub fn new(tokens: Vec<Token>) -> Self
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.
Sourcepub fn parse_program(self) -> (Program, DiagnosticBag)
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:
Program- The top-level AST containing all parsed declarations.DiagnosticBag- Parse errors and warnings (check withDiagnosticBag::has_errors).
§Example
let (tokens, _) = cjc_lexer::Lexer::new("let x = 1;").tokenize();
let (program, diags) = cjc_parser::Parser::new(tokens).parse_program();