start parser

This commit is contained in:
nora 2021-10-30 18:48:44 +02:00
parent eb2f67e19f
commit 7c99615560
2 changed files with 88 additions and 21 deletions

View file

@ -7,24 +7,24 @@ mod parse;
pub fn run_program(program: &str) { pub fn run_program(program: &str) {
let lexer = lex::Lexer::lex(program); let lexer = lex::Lexer::lex(program);
let (success, errors) = lexer.partition::<Vec<_>, _>(|result| result.is_ok()); let (success, errors) = lexer.partition::<Vec<_>, _>(|result| result.is_ok());
//
// // terrible, but works
// let tokens = success.into_iter().collect::<Result<_, _>>();
// let _ast = parse::parse(tokens.unwrap());
// terrible, but works if errors.is_empty() {
let tokens = success.into_iter().collect::<Result<_, _>>(); println!(
let _ast = parse::parse(tokens.unwrap()); "{:#?}",
success
// if errors.is_empty() { .into_iter()
// println!( .map(Result::unwrap)
// "{:#?}", .map(|token| token.kind)
// success .collect::<Vec<_>>()
// .into_iter() );
// .map(Result::unwrap) } else {
// .map(|token| token.kind) errors
// .collect::<Vec<_>>() .into_iter()
// ); .map(Result::unwrap_err)
// } else { .for_each(|err| crate::errors::display_error(program, err));
// errors }
// .into_iter()
// .map(Result::unwrap_err)
// .for_each(|err| crate::errors::display_error(program, err));
// }
} }

View file

@ -1,6 +1,7 @@
#![allow(dead_code)] #![allow(dead_code)]
use crate::ast::*; use crate::ast::*;
use crate::errors::{CompilerError, Span};
use crate::lex::{Token, TokenType}; use crate::lex::{Token, TokenType};
use std::iter::Peekable; use std::iter::Peekable;
@ -70,6 +71,58 @@ impl<'code> Parser<'code> {
todo!() todo!()
} }
fn logical_or(&mut self) -> ParseResult<'code, Expr> {
todo!()
}
fn logical_and(&mut self) -> ParseResult<'code, Expr> {
todo!()
}
fn equality(&mut self) -> ParseResult<'code, Expr> {
todo!()
}
fn comparison(&mut self) -> ParseResult<'code, Expr> {
todo!()
}
fn term(&mut self) -> ParseResult<'code, Expr> {
todo!()
}
fn factor(&mut self) -> ParseResult<'code, Expr> {
todo!()
}
fn unary(&mut self) -> ParseResult<'code, Expr> {
todo!()
}
fn primary(&mut self) -> ParseResult<'code, Expr> {
match self.next().ok_or(ParseErr::EOF)?.kind {
TokenType::String(literal) => Ok(Expr::Literal(Literal::String(literal))),
TokenType::Number(literal) => Ok(Expr::Literal(Literal::Number(literal))),
TokenType::False => Ok(Expr::Literal(Literal::Boolean(false))),
TokenType::True => Ok(Expr::Literal(Literal::Boolean(true))),
TokenType::Null => Ok(Expr::Literal(Literal::Null)),
TokenType::BraceO => todo!(),
TokenType::BracketO => todo!(),
TokenType::ParenO => todo!(),
_ => todo!(),
}
}
fn object_literal(&mut self) -> ParseResult<'code, Expr> {
self.expect(TokenType::BraceO)?;
self.expect(TokenType::BraceC)?;
Ok(Expr::Literal(Literal::Object))
}
fn array_literal(&mut self) -> ParseResult<'code, Expr> {
todo!()
}
// helpers // helpers
fn next(&mut self) -> Option<Token<'code>> { fn next(&mut self) -> Option<Token<'code>> {
@ -88,7 +141,7 @@ impl<'code> Parser<'code> {
Err(ParseErr::MismatchedKind { expected: kind }) Err(ParseErr::MismatchedKind { expected: kind })
} }
} else { } else {
Err(ParseErr::UnexpectedEOF { expected: kind }) Err(ParseErr::EOF)
} }
} }
} }
@ -96,5 +149,19 @@ impl<'code> Parser<'code> {
#[derive(Debug)] #[derive(Debug)]
pub enum ParseErr<'code> { pub enum ParseErr<'code> {
MismatchedKind { expected: TokenType<'code> }, MismatchedKind { expected: TokenType<'code> },
UnexpectedEOF { expected: TokenType<'code> }, EOF,
}
impl CompilerError for ParseErr<'_> {
fn span(&self) -> Span {
todo!()
}
fn message(&self) -> String {
todo!()
}
fn note(&self) -> Option<String> {
todo!()
}
} }