Skip to content

Commit

Permalink
♻️ simplify parser
Browse files Browse the repository at this point in the history
  • Loading branch information
KPMGE committed May 22, 2024
1 parent 4f82119 commit 6033b4a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn debug_lexer(mut lexer: Lexer) {
println!();
}

fn debug_parser<L: Iterator<Item = Token>>(mut parser: Parser<L>) {
fn debug_parser(mut parser: Parser) {
let program = parser.parse_program();

println!("Parsed program: ");
Expand Down
30 changes: 10 additions & 20 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
ast::{AstNode, BlockStatement, Expression, Statement},
token::Token,
ast::{AstNode, BlockStatement, Expression, Statement}, lexer::Lexer, token::Token
};

#[derive(Debug, PartialEq, PartialOrd)]
Expand All @@ -15,18 +14,15 @@ enum Precedence {
}

#[derive(Debug)]
pub struct Parser<L: Iterator<Item = Token>> {
pub struct Parser<'p> {
pub errors: Vec<String>,
lexer: L,
lexer: Lexer<'p>,
current_token: Option<Token>,
next_token: Option<Token>,
}

impl<L> Parser<L>
where
L: Iterator<Item = Token>,
{
pub fn new(lexer: L) -> Self {
impl<'p> Parser<'p> {
pub fn new(lexer: Lexer<'p>) -> Self {
let mut p = Parser {
lexer,
current_token: None,
Expand Down Expand Up @@ -151,7 +147,7 @@ where
let is_next_token_precedence_higher = precedence < self.next_token.clone()?.precedence();

while !self.expect_current_token(Token::Semicolon) && is_next_token_precedence_higher {
if let Some(infix_parse_fn) = self.next_token.clone()?.get_infix_parse_fn() {
if let Some(infix_parse_fn) = self.next_token.clone()?.infix_parse_fn() {
self.advance_tokens();
return infix_parse_fn(self, left_expression);
}
Expand Down Expand Up @@ -463,8 +459,8 @@ where
}
}

type InfixParseFnOption<L> = Option<fn(&mut Parser<L>, Expression) -> Option<Expression>>;
type PrefixParseFnOption<L> = Option<fn(&mut Parser<L>) -> Option<Expression>>;
type InfixParserFn<'p> = fn(&mut Parser<'p>, Expression) -> Option<Expression>;
type PrefixParseFn<'p> = fn(&mut Parser<'p>) -> Option<Expression>;

impl Token {
fn precedence(&self) -> Precedence {
Expand All @@ -478,10 +474,7 @@ impl Token {
}
}

fn prefix_parse_fn<L>(&self) -> PrefixParseFnOption<L>
where
L: Iterator<Item = Token>,
{
fn prefix_parse_fn<'p>(&self) -> Option<PrefixParseFn<'p>> {
match self {
Token::String(_) => Some(Parser::parse_string),
Token::Identifier(_) => Some(Parser::parse_identifier),
Expand All @@ -497,10 +490,7 @@ impl Token {
}
}

fn get_infix_parse_fn<L>(&self) -> InfixParseFnOption<L>
where
L: Iterator<Item = Token>,
{
fn infix_parse_fn<'p>(&self) -> Option<InfixParserFn<'p>> {
match self {
Token::Plus => Some(Parser::parse_infix_expression),
Token::Minus => Some(Parser::parse_infix_expression),
Expand Down

0 comments on commit 6033b4a

Please sign in to comment.