rascal_parser/parser.rs
1use crate::ast::Expression;
2use crate::scanner::token::Token;
3
4/// The parser takes an iterator of [Token]s from the scanner, and uses the grammar of the language
5/// to convert this stream of tokens into a semantically valid Abstract Syntax Tree (AST).
6///
7/// Whereas the scanner ensures incoming syntax is correct, e.g. the characters are arranged into
8/// chunks of valid tokens, the parser ensures the order of these tokens is meaningful and correct.
9pub struct Parser {
10 tokens: Vec<Token>,
11}
12impl From<Vec<Token>> for Parser {
13 fn from(tokens: Vec<Token>) -> Self {
14 Parser { tokens }
15 }
16}
17impl Parser {
18 pub fn parse(&mut self) -> Result<Expression, Vec<ParserError>> {
19 todo!()
20 }
21 /// Returns the next value in the token list, or the EOF token when the end of the iterator is
22 /// reached.
23 pub fn next_or_eof(&mut self) -> Token {
24 self.tokens.pop().unwrap_or(Token::Eof)
25 }
26 pub fn peek(&self) -> &Token {
27 self.tokens.last().unwrap_or(&Token::Eof)
28 }
29}
30
31#[derive(Debug, Clone)]
32pub enum ParserError {
33 UnmatchedBrace,
34 UnexpectedToken(Token),
35}