mirror of
https://github.com/Noratrieb/dilaria.git
synced 2026-01-14 17:35:03 +01:00
fix parser test
This commit is contained in:
parent
bc218efe0e
commit
92b40b17ed
2 changed files with 64 additions and 27 deletions
|
|
@ -3,7 +3,7 @@
|
||||||
//! It's a handwritten recursive descent parser. It has an internal peekable iterator from where
|
//! It's a handwritten recursive descent parser. It has an internal peekable iterator from where
|
||||||
//! it gets its next tokens. Only a lookahead of one is required.
|
//! it gets its next tokens. Only a lookahead of one is required.
|
||||||
|
|
||||||
#[cfg(test_ignore_lol)]
|
#[cfg(test)]
|
||||||
mod test;
|
mod test;
|
||||||
|
|
||||||
use crate::ast::*;
|
use crate::ast::*;
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,20 @@
|
||||||
|
//! Test for the parser
|
||||||
|
//!
|
||||||
|
//! These tests are horrible and break all the time. Never do it like this again.
|
||||||
|
//! That said it's too late to fix it.
|
||||||
|
|
||||||
use crate::errors::Span;
|
use crate::errors::Span;
|
||||||
use crate::parse::Parser;
|
use crate::parse::Parser;
|
||||||
|
use crate::RtAlloc;
|
||||||
use bumpalo::Bump;
|
use bumpalo::Bump;
|
||||||
use prelude::*;
|
use prelude::*;
|
||||||
|
|
||||||
mod prelude {
|
mod prelude {
|
||||||
pub(super) use super::{parser, test_literal_bin_op, test_number_literal, token};
|
pub(super) use super::{parser, rt, test_literal_bin_op, test_number_literal, token};
|
||||||
pub(super) use crate::ast::{Expr, Stmt};
|
pub(super) use crate::ast::{Expr, Stmt};
|
||||||
pub(super) use crate::lex::TokenKind::*;
|
pub(super) use crate::lex::TokenKind::*;
|
||||||
pub type Token = crate::lex::Token<'static>;
|
pub type Token = crate::lex::Token;
|
||||||
pub type TokenType = crate::lex::TokenKind<'static>;
|
pub type TokenType = crate::lex::TokenKind;
|
||||||
pub(super) use bumpalo::Bump;
|
pub(super) use bumpalo::Bump;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -19,10 +25,12 @@ fn token(kind: TokenType) -> Token {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parser<'ast>(
|
fn rt() -> RtAlloc {
|
||||||
tokens: std::vec::Vec<Token>,
|
// SAFETY: this is just a test what could go wrong
|
||||||
alloc: &'ast Bump,
|
unsafe { RtAlloc::new() }
|
||||||
) -> Parser<'static, 'ast, std::vec::IntoIter<Token>>
|
}
|
||||||
|
|
||||||
|
fn parser(tokens: std::vec::Vec<Token>, alloc: &Bump) -> Parser<std::vec::IntoIter<Token>>
|
||||||
where {
|
where {
|
||||||
Parser {
|
Parser {
|
||||||
tokens: tokens.into_iter().peekable(),
|
tokens: tokens.into_iter().peekable(),
|
||||||
|
|
@ -51,6 +59,7 @@ fn test_number_literal<F: FnOnce(Vec<Token>, &Bump) -> Expr>(parser: F) {
|
||||||
|
|
||||||
mod assignment {
|
mod assignment {
|
||||||
use super::prelude::*;
|
use super::prelude::*;
|
||||||
|
use crate::parse::test::rt;
|
||||||
use bumpalo::Bump;
|
use bumpalo::Bump;
|
||||||
|
|
||||||
fn parse_assignment(tokens: Vec<Token>, alloc: &Bump) -> Stmt {
|
fn parse_assignment(tokens: Vec<Token>, alloc: &Bump) -> Stmt {
|
||||||
|
|
@ -60,7 +69,10 @@ mod assignment {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn simple() {
|
fn simple() {
|
||||||
let tokens = [Ident("hugo"), Equal, Number(10.0), Semi].map(token).into();
|
let mut rt = rt();
|
||||||
|
let tokens = [Ident(rt.intern_string("hugo")), Equal, Number(10.0), Semi]
|
||||||
|
.map(token)
|
||||||
|
.into();
|
||||||
|
|
||||||
let alloc = Bump::new();
|
let alloc = Bump::new();
|
||||||
let ast = parse_assignment(tokens, &alloc);
|
let ast = parse_assignment(tokens, &alloc);
|
||||||
|
|
@ -69,10 +81,11 @@ mod assignment {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn call_expr() {
|
fn call_expr() {
|
||||||
|
let mut rt = rt();
|
||||||
let tokens = [
|
let tokens = [
|
||||||
Ident("hugo"),
|
Ident(rt.intern_string("hugo")),
|
||||||
Dot,
|
Dot,
|
||||||
Ident("age"),
|
Ident(rt.intern_string("age")),
|
||||||
Equal,
|
Equal,
|
||||||
Number(2021.0),
|
Number(2021.0),
|
||||||
Minus,
|
Minus,
|
||||||
|
|
@ -98,7 +111,15 @@ mod r#fn {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn empty() {
|
fn empty() {
|
||||||
let tokens = [Fn, Ident("empty"), ParenO, ParenC, BraceO, BraceC]
|
let mut rt = rt();
|
||||||
|
let tokens = [
|
||||||
|
Fn,
|
||||||
|
Ident(rt.intern_string("empty")),
|
||||||
|
ParenO,
|
||||||
|
ParenC,
|
||||||
|
BraceO,
|
||||||
|
BraceC,
|
||||||
|
]
|
||||||
.map(token)
|
.map(token)
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
|
|
@ -109,13 +130,14 @@ mod r#fn {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn params_body() {
|
fn params_body() {
|
||||||
|
let mut rt = rt();
|
||||||
let tokens = [
|
let tokens = [
|
||||||
Fn,
|
Fn,
|
||||||
Ident("empty"),
|
Ident(rt.intern_string("empty")),
|
||||||
ParenO,
|
ParenO,
|
||||||
Ident("a"),
|
Ident(rt.intern_string("a")),
|
||||||
Comma,
|
Comma,
|
||||||
Ident("b"),
|
Ident(rt.intern_string("b")),
|
||||||
ParenC,
|
ParenC,
|
||||||
BraceO,
|
BraceO,
|
||||||
Number(10.0),
|
Number(10.0),
|
||||||
|
|
@ -550,7 +572,14 @@ mod call {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn field_simple() {
|
fn field_simple() {
|
||||||
let tokens = [Ident("hugo"), Dot, Ident("name")].map(token).into();
|
let mut rt = rt();
|
||||||
|
let tokens = [
|
||||||
|
Ident(rt.intern_string("hugo")),
|
||||||
|
Dot,
|
||||||
|
Ident(rt.intern_string("name")),
|
||||||
|
]
|
||||||
|
.map(token)
|
||||||
|
.into();
|
||||||
let alloc = Bump::new();
|
let alloc = Bump::new();
|
||||||
let ast = parse_call(tokens, &alloc);
|
let ast = parse_call(tokens, &alloc);
|
||||||
insta::assert_debug_snapshot!(ast);
|
insta::assert_debug_snapshot!(ast);
|
||||||
|
|
@ -558,7 +587,10 @@ mod call {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn simple() {
|
fn simple() {
|
||||||
let tokens = [Ident("print"), ParenO, ParenC].map(token).into();
|
let mut rt = rt();
|
||||||
|
let tokens = [Ident(rt.intern_string("print")), ParenO, ParenC]
|
||||||
|
.map(token)
|
||||||
|
.into();
|
||||||
let alloc = Bump::new();
|
let alloc = Bump::new();
|
||||||
let ast = parse_call(tokens, &alloc);
|
let ast = parse_call(tokens, &alloc);
|
||||||
insta::assert_debug_snapshot!(ast);
|
insta::assert_debug_snapshot!(ast);
|
||||||
|
|
@ -566,8 +598,9 @@ mod call {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn fn_args() {
|
fn fn_args() {
|
||||||
|
let mut rt = rt();
|
||||||
let tokens = [
|
let tokens = [
|
||||||
Ident("print"),
|
Ident(rt.intern_string("print")),
|
||||||
ParenO,
|
ParenO,
|
||||||
Number(10.0),
|
Number(10.0),
|
||||||
Comma,
|
Comma,
|
||||||
|
|
@ -584,12 +617,13 @@ mod call {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn nested() {
|
fn nested() {
|
||||||
|
let mut rt = rt();
|
||||||
let tokens = [
|
let tokens = [
|
||||||
Ident("hugo"),
|
Ident(rt.intern_string("hugo")),
|
||||||
Dot,
|
Dot,
|
||||||
Ident("name"),
|
Ident(rt.intern_string("name")),
|
||||||
Dot,
|
Dot,
|
||||||
Ident("print"),
|
Ident(rt.intern_string("print")),
|
||||||
ParenO,
|
ParenO,
|
||||||
ParenC,
|
ParenC,
|
||||||
]
|
]
|
||||||
|
|
@ -602,9 +636,10 @@ mod call {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn with_exprs() {
|
fn with_exprs() {
|
||||||
|
let mut rt = rt();
|
||||||
// print((10 + 5).abs())
|
// print((10 + 5).abs())
|
||||||
let tokens = [
|
let tokens = [
|
||||||
Ident("print"),
|
Ident(rt.intern_string("print")),
|
||||||
ParenO,
|
ParenO,
|
||||||
ParenO,
|
ParenO,
|
||||||
Number(10.0),
|
Number(10.0),
|
||||||
|
|
@ -612,7 +647,7 @@ mod call {
|
||||||
Number(5.0),
|
Number(5.0),
|
||||||
ParenC,
|
ParenC,
|
||||||
Dot,
|
Dot,
|
||||||
Ident("abs"),
|
Ident(rt.intern_string("abs")),
|
||||||
ParenO,
|
ParenO,
|
||||||
ParenC,
|
ParenC,
|
||||||
ParenC,
|
ParenC,
|
||||||
|
|
@ -635,7 +670,8 @@ mod primary {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn ident_test() {
|
fn ident_test() {
|
||||||
let tokens = [Ident("tokens")].map(token).into();
|
let mut rt = rt();
|
||||||
|
let tokens = [Ident(rt.intern_string("tokens"))].map(token).into();
|
||||||
let alloc = Bump::new();
|
let alloc = Bump::new();
|
||||||
let ast = parse_primary(tokens, &alloc);
|
let ast = parse_primary(tokens, &alloc);
|
||||||
insta::assert_debug_snapshot!(ast);
|
insta::assert_debug_snapshot!(ast);
|
||||||
|
|
@ -651,7 +687,8 @@ mod primary {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn number() {
|
fn number() {
|
||||||
let tokens = [String("uwu".to_string())].map(token).into();
|
let mut rt = rt();
|
||||||
|
let tokens = [String(rt.intern_string("uwu"))].map(token).into();
|
||||||
let alloc = Bump::new();
|
let alloc = Bump::new();
|
||||||
let ast = parse_primary(tokens, &alloc);
|
let ast = parse_primary(tokens, &alloc);
|
||||||
insta::assert_debug_snapshot!(ast);
|
insta::assert_debug_snapshot!(ast);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue