From 4df0118aa4f7f78b935755649ce2fb4ec988e26b Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Wed, 24 Apr 2024 21:09:55 +0200 Subject: [PATCH] lang: first test and stuff --- Cargo.lock | 7 ++++ crates/lang/Cargo.toml | 1 + crates/lang/src/parser/grammar.rs | 22 ++++++++++- .../lang/src/parser/grammar/expression/lit.rs | 39 +++++++++++++++++++ testfiles/test.owo | 5 +-- 5 files changed, 69 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d759c0b..0252665 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -504,6 +504,12 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "indoc" +version = "2.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b248f5224d1d606005e02c97f5aa4e88eeb230488bcc03bc9ca4d7991399f2b5" + [[package]] name = "ir" version = "0.1.0" @@ -538,6 +544,7 @@ dependencies = [ "ego-tree", "enumset", "indexmap", + "indoc", "logos", "petgraph", "rowan", diff --git a/crates/lang/Cargo.toml b/crates/lang/Cargo.toml index fba2bcf..c64720c 100644 --- a/crates/lang/Cargo.toml +++ b/crates/lang/Cargo.toml @@ -15,6 +15,7 @@ ego-tree = "0.6.2" rowan = "0.15.15" drop_bomb = "0.1.5" enumset = "1.1.3" +indoc = "2" [lints] workspace = true diff --git a/crates/lang/src/parser/grammar.rs b/crates/lang/src/parser/grammar.rs index 75ed999..afcee25 100644 --- a/crates/lang/src/parser/grammar.rs +++ b/crates/lang/src/parser/grammar.rs @@ -1,6 +1,13 @@ +use std::fmt::Debug; + use crate::parser::syntax_kind::SyntaxKind::*; -use super::Parser; +use super::{ + input::Input, + output::Output, + syntax_kind::{self, lex}, + Parser, +}; mod expression; @@ -12,3 +19,16 @@ pub fn source_file(p: &mut Parser) { root.complete(p, ROOT); } + +fn check_parser(input: &str, parser_fn: fn(&mut Parser), output: &str) { + let toks = lex(input); + let mut parser = Parser::new(Input::new(&toks)); + + parser_fn(&mut parser); + + let p_out = dbg!(parser.finish()); + let o = Output::from_parser_output(toks, p_out); + + let s = format!("{o:?}"); + assert_eq!(&s, output); +} diff --git a/crates/lang/src/parser/grammar/expression/lit.rs b/crates/lang/src/parser/grammar/expression/lit.rs index afc1408..4917c88 100644 --- a/crates/lang/src/parser/grammar/expression/lit.rs +++ b/crates/lang/src/parser/grammar/expression/lit.rs @@ -1,6 +1,8 @@ use enumset::enum_set; +use indoc::indoc; use crate::parser::{ + grammar::check_parser, syntax_kind::{SyntaxKind::*, TokenSet}, CompletedMarker, Parser, }; @@ -18,3 +20,40 @@ pub fn literal(p: &mut Parser) -> Option { Some(lit.complete(p, LITERAL)) } + +#[test] +fn test_parse_lst_lit() { + check_parser( + "42", + |p| { + literal(p); + }, + indoc! {r#" + LITERAL { + INT_NUM "42"; + } + "#}, + ); + check_parser( + "3.14", + |p| { + literal(p); + }, + indoc! {r#" + LITERAL { + FLOAT_NUM "3.14"; + } + "#}, + ); + check_parser( + r#""Meow""#, + |p| { + literal(p); + }, + indoc! {r#" + LITERAL { + STRING "\"Meow\""; + } + "#}, + ); +} diff --git a/testfiles/test.owo b/testfiles/test.owo index 4a3beda..d81cc07 100644 --- a/testfiles/test.owo +++ b/testfiles/test.owo @@ -1,4 +1 @@ -mat 2x2 [ - 1 2, 2; - 3, 4 -] +42